feat: 后端初始化

This commit is contained in:
2026-07-28 10:54:47 +08:00
parent 88f5371ed8
commit dfe0d6bd95
24 changed files with 22326 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
export type JsonBody = Record<string, unknown> | unknown[]
export const json = (body: JsonBody, init: ResponseInit = {}) =>
Response.json(body, {
...init,
headers: {
'Content-Type': 'application/json',
...(init.headers || {})
}
})
export const ok = (data: unknown = null) => json({ success: true, data })
export const fail = (message: string, status = 400, code = 'BAD_REQUEST') =>
json({ success: false, error: { code, message } }, { status })
export const methodNotAllowed = (allowedMethods: string[]) =>
json(
{ success: false, error: { code: 'METHOD_NOT_ALLOWED', message: 'Method not allowed' } },
{
status: 405,
headers: {
Allow: allowedMethods.join(', ')
}
}
)
export const handleOptions = () =>
new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS'
}
})