0
0
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:
Yudai Nakata 2023-02-11 16:16:56 +09:00 committed by GitHub
parent bd90ec5d2b
commit da7087c754
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 34 deletions

View File

@ -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 })

View File

@ -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: () => {} })

View File

@ -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)

View File

@ -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)