Skip to content

Commit 99dca50

Browse files
committed
feat: add @mx-space/webhook dependency and update revalidate time
Signed-off-by: Innei <[email protected]>
1 parent 28e71b4 commit 99dca50

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@iconify/tailwind": "0.1.4",
8989
"@innei/eslint-config-react-ts": "0.12.0",
9090
"@innei/prettier": "0.12.0",
91+
"@mx-space/webhook": "0.2.2",
9192
"@next/bundle-analyzer": "14.0.4",
9293
"@tailwindcss/typography": "0.5.10",
9394
"@types/js-cookie": "3.0.6",

pnpm-lock.yaml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/api/webhook/route.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { revalidateTag } from 'next/cache'
2+
import type { RequestWithJSONBody } from '@mx-space/webhook'
3+
import type { NextRequest } from 'next/server'
4+
5+
import {
6+
BusinessEvents,
7+
InvalidSignatureError,
8+
readDataFromRequest,
9+
} from '@mx-space/webhook'
10+
11+
import { NextServerResponse } from '~/lib/edge-function.server'
12+
13+
// export const runtime = 'edge'
14+
export const POST = async (nextreq: NextRequest) => {
15+
const secret = process.env.WEBHOOK_SECRET
16+
const res = new NextServerResponse()
17+
if (!secret) return res.status(500).send('WEBHOOK_SECRET is not set')
18+
const req: RequestWithJSONBody = nextreq.clone() as any
19+
const headers = {} as Record<string, string>
20+
for (const [key, value] of req.headers.entries()) {
21+
headers[key] = value
22+
}
23+
24+
try {
25+
const { type } = await readDataFromRequest({
26+
req: {
27+
...req,
28+
headers: headers as any,
29+
body: await req.json(),
30+
} as any,
31+
secret,
32+
})
33+
34+
switch (type) {
35+
case 'health-check': {
36+
return res.status(200).send('OK')
37+
}
38+
case BusinessEvents.NOTE_CREATE:
39+
case BusinessEvents.NOTE_UPDATE: {
40+
revalidateTag('note')
41+
return res.status(200).send('OK')
42+
}
43+
}
44+
} catch (err) {
45+
if (err instanceof InvalidSignatureError) {
46+
return res.status(400).send(err.message)
47+
} else {
48+
console.error(err)
49+
return res
50+
.status(500)
51+
.send('An error occurred while processing the request')
52+
}
53+
}
54+
}

src/app/notes/page.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import { apiClient } from '~/lib/request'
55

66
import Redirect from './redirect'
77

8-
export const revalidate = 60
9-
108
export default async function Page() {
119
const data = await fetch(apiClient.note.proxy.latest.toString(true), {
1210
next: {
13-
revalidate: 30,
11+
revalidate: 60 * 60,
12+
tags: ['note'],
1413
},
1514
})
1615
.then((res) => res.json() as Promise<NoteWrappedPayload>)

src/components/widgets/comment/CommentBox/ActionBar.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ const SubmitButton = () => {
182182
const commentDto: CommentDto = { text, author, mail, avatar, source, url }
183183

184184
if (isLogged) {
185-
delete commentDto.source
186185
delete commentDto.avatar
187186
}
188187

@@ -200,6 +199,7 @@ const SubmitButton = () => {
200199
.post<CommentModel>({
201200
data: {
202201
text,
202+
source,
203203
},
204204
})
205205
.then(wrappedCompletedCallback)
@@ -218,7 +218,7 @@ const SubmitButton = () => {
218218
return apiClient.comment.proxy.master
219219
.comment(refId)
220220
.post<CommentModel>({
221-
data: { text },
221+
data: { text, source },
222222
})
223223
.then(async (res) => {
224224
if (syncToRecently)
@@ -253,8 +253,8 @@ const SubmitButton = () => {
253253
const toastCopy = isLogged
254254
? '发表成功啦~'
255255
: isReply
256-
? '感谢你的回复!'
257-
: '感谢你的评论!'
256+
? '感谢你的回复!'
257+
: '感谢你的评论!'
258258

259259
const commentListQueryKey = buildQueryKey(originalRefId)
260260

0 commit comments

Comments
 (0)