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,8 +3,11 @@ import React from 'react';
import { useTranslation } from '@i18next-toolkit/react';
import { CodeExample } from '../CodeExample';
export const FeedApiGuide: React.FC<{ channelId: string }> = React.memo(
(props) => {
interface FeedApiGuideProps {
channelId: string;
webhookSignature?: string;
}
export const FeedApiGuide: React.FC<FeedApiGuideProps> = React.memo((props) => {
const { t } = useTranslation();
return (
@ -17,11 +20,11 @@ export const FeedApiGuide: React.FC<{ channelId: string }> = React.memo(
example={{
curl: {
label: 'curl',
code: generateCurlCode(props.channelId),
code: generateCurlCode(props.channelId, props.webhookSignature),
},
fetch: {
label: 'fetch',
code: generateFetchCode(props.channelId),
code: generateFetchCode(props.channelId, props.webhookSignature),
},
}}
/>
@ -32,12 +35,24 @@ export const FeedApiGuide: React.FC<{ channelId: string }> = React.memo(
</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;
}

View File

@ -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: "",

View File

@ -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>
)}
/>

View File

@ -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,