work with routes() helper
This commit is contained in:
parent
8268fd13ae
commit
84b001e651
|
|
@ -48,8 +48,8 @@ export function routes(def: Record<string, Handler>): Hono {
|
||||||
|
|
||||||
for (const key in def) {
|
for (const key in def) {
|
||||||
const parts = key.split(" ") // GET /path
|
const parts = key.split(" ") // GET /path
|
||||||
const method = parts[0] || "GET"
|
const method = (!parts[0] || parts[0].startsWith("/")) ? "GET" : parts[0]
|
||||||
const path = parts[1] || "/"
|
const path = (parts[0]?.startsWith("/") ? parts[0] : parts[1]) || "/"
|
||||||
|
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
app.on(method, path, async c => toResponse(await def[key](c)))
|
app.on(method, path, async c => toResponse(await def[key](c)))
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,18 @@ try {
|
||||||
}
|
}
|
||||||
|
|
||||||
const handler = mod.default
|
const handler = mod.default
|
||||||
if (typeof handler !== "function") {
|
let app: Hono
|
||||||
|
|
||||||
|
if (typeof handler === "function") {
|
||||||
|
app = new Hono
|
||||||
|
app.all("*", async c => toResponse(await handler(c)))
|
||||||
|
} else if (handler && typeof handler === "object" && handler.routes && handler.router) {
|
||||||
|
app = handler
|
||||||
|
} else {
|
||||||
webappLog(appName, `no default export`)
|
webappLog(appName, `no default export`)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = new Hono()
|
|
||||||
app.all("*", async c => toResponse(await handler(c)))
|
|
||||||
const port = Number(process.env.PORT || 4000)
|
const port = Number(process.env.PORT || 4000)
|
||||||
Bun.serve({ port, fetch: app.fetch })
|
Bun.serve({ port, fetch: app.fetch })
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user