refactor: update webhooks signature api guide

This commit is contained in:
moonrailgun 2024-10-31 22:19:50 +08:00
parent a8a47ed94d
commit 266b08f2da
4 changed files with 97 additions and 40 deletions

View File

@ -3,41 +3,56 @@ import React from 'react';
import { useTranslation } from '@i18next-toolkit/react'; import { useTranslation } from '@i18next-toolkit/react';
import { CodeExample } from '../CodeExample'; import { CodeExample } from '../CodeExample';
export const FeedApiGuide: React.FC<{ channelId: string }> = React.memo( interface FeedApiGuideProps {
(props) => { channelId: string;
const { t } = useTranslation(); webhookSignature?: string;
}
export const FeedApiGuide: React.FC<FeedApiGuideProps> = React.memo((props) => {
const { t } = useTranslation();
return ( return (
<Card className="w-full overflow-hidden"> <Card className="w-full overflow-hidden">
<CardHeader> <CardHeader>
<div>{t('You can send a message to this channel with:')}</div> <div>{t('You can send a message to this channel with:')}</div>
</CardHeader> </CardHeader>
<CardContent className="flex w-full flex-col gap-5 overflow-hidden"> <CardContent className="flex w-full flex-col gap-5 overflow-hidden">
<CodeExample <CodeExample
example={{ example={{
curl: { curl: {
label: 'curl', label: 'curl',
code: generateCurlCode(props.channelId), code: generateCurlCode(props.channelId, props.webhookSignature),
}, },
fetch: { fetch: {
label: 'fetch', label: 'fetch',
code: generateFetchCode(props.channelId), 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> <div>{t('Integrate with third party with webhook')}</div>
</CardContent> </CardContent>
</Card> </Card>
); );
} });
);
FeedApiGuide.displayName = 'FeedApiGuide'; FeedApiGuide.displayName = 'FeedApiGuide';
function generateCurlCode(channelId: string) { function generateCurlCode(channelId: string, webhookSignature?: string) {
const code = `curl -X POST ${window.location.origin}/open/feed/${channelId}/send \\ 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" \\ -H "Content-Type: application/json" \\
-d '{ -d '{
"eventName": "test name", "eventName": "test name",
@ -46,12 +61,27 @@ function generateCurlCode(channelId: string) {
"source": "custom", "source": "custom",
"important": false "important": false
}'`; }'`;
return code;
} }
function generateFetchCode(channelId: string) { function generateFetchCode(channelId: string, webhookSignature?: string) {
const code = `fetch('${window.location.origin}/open/feed/${channelId}/send', { 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', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -64,6 +94,4 @@ function generateFetchCode(channelId: string) {
important: false, important: false,
}) })
})`; })`;
return code;
} }

View File

@ -8,6 +8,7 @@ import { FaStripe } from 'react-icons/fa6';
export const FeedIntegration: React.FC<{ export const FeedIntegration: React.FC<{
feedId: string; feedId: string;
webhookSignature: string;
}> = React.memo((props) => { }> = React.memo((props) => {
const { t } = useTranslation(); const { t } = useTranslation();
@ -92,7 +93,7 @@ export const FeedIntegration: React.FC<{
<CodeBlock <CodeBlock
code={`POST ${window.location.origin}/open/feed/${props.feedId}/send code={`POST ${window.location.origin}/open/feed/${props.feedId}/send
${props.webhookSignature ? `\nHeader:\nX-Webhook-Signature: ${props.webhookSignature}\n` : ''}
Body Body
{ {
eventName: "", eventName: "",

View File

@ -114,7 +114,12 @@ function PageComponent() {
{info?.id && ( {info?.id && (
<DialogWrapper <DialogWrapper
title={t('Integration')} title={t('Integration')}
content={<FeedIntegration feedId={info.id} />} content={
<FeedIntegration
feedId={info.id}
webhookSignature={info.webhookSignature}
/>
}
> >
<Button variant="default" size="icon" Icon={LuWebhook} /> <Button variant="default" size="icon" Icon={LuWebhook} />
</DialogWrapper> </DialogWrapper>
@ -194,7 +199,12 @@ function PageComponent() {
)} )}
renderEmpty={() => ( renderEmpty={() => (
<div className="w-full overflow-hidden p-4"> <div className="w-full overflow-hidden p-4">
{!isInitialLoading && <FeedApiGuide channelId={channelId} />} {!isInitialLoading && (
<FeedApiGuide
channelId={channelId}
webhookSignature={info?.webhookSignature}
/>
)}
</div> </div>
)} )}
/> />

View File

@ -325,9 +325,27 @@ export const feedRouter = router({
) )
) )
.output(FeedEventModelSchema) .output(FeedEventModelSchema)
.mutation(async ({ input }) => { .mutation(async ({ input, ctx }) => {
const { channelId, ...data } = input; 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({ const event = await prisma.feedEvent.create({
data: { data: {
...data, ...data,