refactor: update webhooks signature api guide
This commit is contained in:
parent
a8a47ed94d
commit
266b08f2da
@ -3,41 +3,56 @@ import React from 'react';
|
||||
import { useTranslation } from '@i18next-toolkit/react';
|
||||
import { CodeExample } from '../CodeExample';
|
||||
|
||||
export const FeedApiGuide: React.FC<{ channelId: string }> = React.memo(
|
||||
(props) => {
|
||||
const { t } = useTranslation();
|
||||
interface FeedApiGuideProps {
|
||||
channelId: string;
|
||||
webhookSignature?: string;
|
||||
}
|
||||
export const FeedApiGuide: React.FC<FeedApiGuideProps> = React.memo((props) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<Card className="w-full overflow-hidden">
|
||||
<CardHeader>
|
||||
<div>{t('You can send a message to this channel with:')}</div>
|
||||
</CardHeader>
|
||||
<CardContent className="flex w-full flex-col gap-5 overflow-hidden">
|
||||
<CodeExample
|
||||
example={{
|
||||
curl: {
|
||||
label: 'curl',
|
||||
code: generateCurlCode(props.channelId),
|
||||
},
|
||||
fetch: {
|
||||
label: 'fetch',
|
||||
code: generateFetchCode(props.channelId),
|
||||
},
|
||||
}}
|
||||
/>
|
||||
return (
|
||||
<Card className="w-full overflow-hidden">
|
||||
<CardHeader>
|
||||
<div>{t('You can send a message to this channel with:')}</div>
|
||||
</CardHeader>
|
||||
<CardContent className="flex w-full flex-col gap-5 overflow-hidden">
|
||||
<CodeExample
|
||||
example={{
|
||||
curl: {
|
||||
label: 'curl',
|
||||
code: generateCurlCode(props.channelId, props.webhookSignature),
|
||||
},
|
||||
fetch: {
|
||||
label: 'fetch',
|
||||
code: generateFetchCode(props.channelId, props.webhookSignature),
|
||||
},
|
||||
}}
|
||||
/>
|
||||
|
||||
<div className="pl-2 font-bold">{t('OR')}</div>
|
||||
<div className="pl-2 font-bold">{t('OR')}</div>
|
||||
|
||||
<div>{t('Integrate with third party with webhook')}</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
);
|
||||
<div>{t('Integrate with third party with webhook')}</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
});
|
||||
FeedApiGuide.displayName = 'FeedApiGuide';
|
||||
|
||||
function generateCurlCode(channelId: string) {
|
||||
const code = `curl -X POST ${window.location.origin}/open/feed/${channelId}/send \\
|
||||
function generateCurlCode(channelId: string, webhookSignature?: string) {
|
||||
if (webhookSignature) {
|
||||
return `curl -X POST ${window.location.origin}/open/feed/${channelId}/send \\
|
||||
-H "Content-Type: application/json" \\
|
||||
-H "X-Webhook-Signature: ${webhookSignature}" \\
|
||||
-d '{
|
||||
"eventName": "test name",
|
||||
"eventContent": "test content",
|
||||
"tags": ["test"],
|
||||
"source": "custom",
|
||||
"important": false
|
||||
}'`;
|
||||
}
|
||||
|
||||
return `curl -X POST ${window.location.origin}/open/feed/${channelId}/send \\
|
||||
-H "Content-Type: application/json" \\
|
||||
-d '{
|
||||
"eventName": "test name",
|
||||
@ -46,12 +61,27 @@ function generateCurlCode(channelId: string) {
|
||||
"source": "custom",
|
||||
"important": false
|
||||
}'`;
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
function generateFetchCode(channelId: string) {
|
||||
const code = `fetch('${window.location.origin}/open/feed/${channelId}/send', {
|
||||
function generateFetchCode(channelId: string, webhookSignature?: string) {
|
||||
if (webhookSignature) {
|
||||
return `fetch('${window.location.origin}/open/feed/${channelId}/send', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Webhook-Signature': '${webhookSignature}'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
eventName: 'test name',
|
||||
eventContent: 'test content',
|
||||
tags: ['test'],
|
||||
source: 'custom',
|
||||
important: false,
|
||||
})
|
||||
})`;
|
||||
}
|
||||
|
||||
return `fetch('${window.location.origin}/open/feed/${channelId}/send', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
@ -64,6 +94,4 @@ function generateFetchCode(channelId: string) {
|
||||
important: false,
|
||||
})
|
||||
})`;
|
||||
|
||||
return code;
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import { FaStripe } from 'react-icons/fa6';
|
||||
|
||||
export const FeedIntegration: React.FC<{
|
||||
feedId: string;
|
||||
webhookSignature: string;
|
||||
}> = React.memo((props) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
@ -92,7 +93,7 @@ export const FeedIntegration: React.FC<{
|
||||
|
||||
<CodeBlock
|
||||
code={`POST ${window.location.origin}/open/feed/${props.feedId}/send
|
||||
|
||||
${props.webhookSignature ? `\nHeader:\nX-Webhook-Signature: ${props.webhookSignature}\n` : ''}
|
||||
Body
|
||||
{
|
||||
eventName: "",
|
||||
|
@ -114,7 +114,12 @@ function PageComponent() {
|
||||
{info?.id && (
|
||||
<DialogWrapper
|
||||
title={t('Integration')}
|
||||
content={<FeedIntegration feedId={info.id} />}
|
||||
content={
|
||||
<FeedIntegration
|
||||
feedId={info.id}
|
||||
webhookSignature={info.webhookSignature}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<Button variant="default" size="icon" Icon={LuWebhook} />
|
||||
</DialogWrapper>
|
||||
@ -194,7 +199,12 @@ function PageComponent() {
|
||||
)}
|
||||
renderEmpty={() => (
|
||||
<div className="w-full overflow-hidden p-4">
|
||||
{!isInitialLoading && <FeedApiGuide channelId={channelId} />}
|
||||
{!isInitialLoading && (
|
||||
<FeedApiGuide
|
||||
channelId={channelId}
|
||||
webhookSignature={info?.webhookSignature}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
|
@ -325,9 +325,27 @@ export const feedRouter = router({
|
||||
)
|
||||
)
|
||||
.output(FeedEventModelSchema)
|
||||
.mutation(async ({ input }) => {
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { channelId, ...data } = input;
|
||||
|
||||
const channel = await prisma.feedChannel.findUnique({
|
||||
where: {
|
||||
id: channelId,
|
||||
},
|
||||
});
|
||||
if (channel?.webhookSignature) {
|
||||
const signature = ctx.req.headers['x-webhook-signature'];
|
||||
if (!signature) {
|
||||
throw new Error(
|
||||
'This channel configured with webhook signature, but no signature found'
|
||||
);
|
||||
}
|
||||
|
||||
if (channel.webhookSignature !== signature) {
|
||||
throw new Error('Invalid webhook signature');
|
||||
}
|
||||
}
|
||||
|
||||
const event = await prisma.feedEvent.create({
|
||||
data: {
|
||||
...data,
|
||||
|
Loading…
Reference in New Issue
Block a user