mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 11:51:01 +01:00
feat(adapter)!: simplify HandleInterface and save size (#881)
This commit is contained in:
parent
bd90ec5d2b
commit
da7087c754
@ -31,7 +31,7 @@ describe('Adapter for Cloudflare Pages', () => {
|
||||
app.get('/foo', (c) => {
|
||||
return c.text('/api/foo')
|
||||
})
|
||||
const handler = handle('/api', app)
|
||||
const handler = handle(app, '/api')
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const res = await handler({ request })
|
||||
|
@ -9,23 +9,14 @@ type EventContext = {
|
||||
}
|
||||
|
||||
interface HandleInterface {
|
||||
<E extends Env>(app: Hono<E>): (eventContext: EventContext) => Response | Promise<Response>
|
||||
<E extends Env>(path: string, app: Hono<E>): (
|
||||
<E extends Env>(app: Hono<E>, path?: string): (
|
||||
eventContext: EventContext
|
||||
) => Response | Promise<Response>
|
||||
}
|
||||
|
||||
export const handle: HandleInterface = (arg1: string | Hono, arg2?: Hono) => {
|
||||
if (typeof arg1 === 'string') {
|
||||
const app = new Hono()
|
||||
app.route(arg1, arg2)
|
||||
return (eventContext) => {
|
||||
const { request, env, waitUntil } = eventContext
|
||||
return app.fetch(request, env, { waitUntil, passThroughOnException: () => {} })
|
||||
}
|
||||
}
|
||||
return (eventContext) => {
|
||||
const { request, env, waitUntil } = eventContext
|
||||
return arg1.fetch(request, env, { waitUntil, passThroughOnException: () => {} })
|
||||
}
|
||||
}
|
||||
export const handle: HandleInterface =
|
||||
<E extends Env>(subApp: Hono<E>, path: string = '/') =>
|
||||
({ request, env, waitUntil }) =>
|
||||
new Hono()
|
||||
.route(path, subApp)
|
||||
.fetch(request, env, { waitUntil, passThroughOnException: () => {} })
|
||||
|
@ -19,7 +19,7 @@ describe('Adapter for Next.js', () => {
|
||||
app.get('/foo', (c) => {
|
||||
return c.text('/api/foo')
|
||||
})
|
||||
const handler = handle('/api', app)
|
||||
const handler = handle(app, '/api')
|
||||
const req = new Request('http://localhost/api/foo')
|
||||
const res = await handler(req)
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -3,21 +3,10 @@ import { Hono } from '../../hono'
|
||||
import type { Env } from '../../types'
|
||||
|
||||
interface HandleInterface {
|
||||
<E extends Env, R extends Request, R2 extends Response>(app: Hono<E>): (req: R) => Promise<R2>
|
||||
<E extends Env, R extends Request, R2 extends Response>(path: string, app: Hono<E>): (
|
||||
req: R
|
||||
) => Promise<R2>
|
||||
<E extends Env>(subApp: Hono<E>, path?: string): (req: Request) => Promise<Response>
|
||||
}
|
||||
|
||||
export const handle: HandleInterface = (arg1: string | Hono, arg2?: Hono) => {
|
||||
if (typeof arg1 === 'string') {
|
||||
const app = new Hono()
|
||||
app.route(arg1, arg2)
|
||||
return async (req) => {
|
||||
return app.fetch(req)
|
||||
}
|
||||
}
|
||||
return async (req) => {
|
||||
return arg1.fetch(req)
|
||||
}
|
||||
}
|
||||
export const handle: HandleInterface =
|
||||
<E extends Env>(subApp: Hono<E>, path: string = '/') =>
|
||||
async (req) =>
|
||||
new Hono().route(path, subApp).fetch(req)
|
||||
|
Loading…
Reference in New Issue
Block a user