implement bookmark delete
This commit is contained in:
@@ -1,22 +1,46 @@
|
||||
import { HttpError } from "./error.ts"
|
||||
|
||||
type HttpMethod = "GET" | "POST" | "DELETE" | "PUT" | "OPTIONS" | "PATCH"
|
||||
|
||||
const ALLOWED_ORIGINS = ["http://localhost:5173"]
|
||||
|
||||
function httpHandler<Route extends string>(
|
||||
handler: (request: Bun.BunRequest<Route>) => Promise<Response>,
|
||||
): (request: Bun.BunRequest<Route>) => Promise<Response> {
|
||||
return async (request) => {
|
||||
let response: Response
|
||||
try {
|
||||
const response = await handler(request)
|
||||
return response
|
||||
response = await handler(request)
|
||||
} catch (error) {
|
||||
if (error instanceof HttpError) {
|
||||
if (error.message) {
|
||||
return Response.json({ message: error.message }, { status: error.status })
|
||||
response = Response.json({ message: error.message }, { status: error.status })
|
||||
} else {
|
||||
response = new Response(undefined, { status: error.status })
|
||||
}
|
||||
return new Response(undefined, { status: error.status })
|
||||
} else {
|
||||
console.error(error)
|
||||
response = new Response(undefined, { status: 500 })
|
||||
}
|
||||
return new Response(undefined, { status: 500 })
|
||||
}
|
||||
for (const origin of ALLOWED_ORIGINS) {
|
||||
response.headers.set("Access-Control-Allow-Origin", origin)
|
||||
}
|
||||
response.headers.set("Access-Control-Allow-Credentials", "true")
|
||||
return response
|
||||
}
|
||||
}
|
||||
|
||||
export { httpHandler }
|
||||
function preflightHandler<Route extends string>({ allowedMethods }: { allowedMethods: HttpMethod[] }) {
|
||||
return async (request: Bun.BunRequest<Route>) =>
|
||||
new Response(undefined, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": ALLOWED_ORIGINS,
|
||||
"Access-Control-Allow-Methods": allowedMethods.join(", "),
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export { httpHandler, preflightHandler }
|
||||
|
Reference in New Issue
Block a user