refactor: update webhooks signature api guide
This commit is contained in:
parent
a8a47ed94d
commit
266b08f2da
@ -3,8 +3,11 @@ 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;
|
||||||
|
webhookSignature?: string;
|
||||||
|
}
|
||||||
|
export const FeedApiGuide: React.FC<FeedApiGuideProps> = React.memo((props) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -17,11 +20,11 @@ export const FeedApiGuide: React.FC<{ channelId: string }> = React.memo(
|
|||||||
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),
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -32,12 +35,24 @@ export const FeedApiGuide: React.FC<{ channelId: string }> = React.memo(
|
|||||||
</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;
|
|
||||||
}
|
}
|
||||||
|
@ -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: "",
|
||||||
|
@ -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>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user