feat: 后端初始化
This commit is contained in:
@@ -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'
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user