Skip to content

Commit

Permalink
feat(hono-base): app.on supports multiple paths (honojs#1923)
Browse files Browse the repository at this point in the history
* feat(hono-base): `app.on` supports multiple paths

* denoify
  • Loading branch information
yusukebe authored and nakasyou committed Jan 13, 2024
1 parent f850f88 commit 3b55e52
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
14 changes: 8 additions & 6 deletions deno_dist/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ class Hono<
})

// Implementation of app.on(method, path, ...handlers[])
this.on = (method: string | string[], path: string, ...handlers: H[]) => {
this.on = (method: string | string[], path: string | string[], ...handlers: H[]) => {
if (!method) return this
this.#path = path
for (const m of [method].flat()) {
handlers.map((handler) => {
this.addRoute(m.toUpperCase(), this.#path, handler)
})
for (const p of [path].flat()) {
this.#path = p
for (const m of [method].flat()) {
handlers.map((handler) => {
this.addRoute(m.toUpperCase(), this.#path, handler)
})
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this as any
Expand Down
7 changes: 7 additions & 0 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,13 @@ export interface OnHandlerInterface<
S & ToSchema<string, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,
BasePath
>

// app.on(method | method[], path[], ...handlers[])
<I extends Input = {}, R extends HandlerResponse<any> = any>(
methods: string | string[],
paths: string[],
...handlers: H<E, any, I, R>[]
): Hono<E, S & ToSchema<string, string, I['in'], MergeTypedResponseData<R>>, BasePath>
}

type ExtractKey<S> = S extends Record<infer Key, unknown>
Expand Down
14 changes: 8 additions & 6 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ class Hono<
})

// Implementation of app.on(method, path, ...handlers[])
this.on = (method: string | string[], path: string, ...handlers: H[]) => {
this.on = (method: string | string[], path: string | string[], ...handlers: H[]) => {
if (!method) return this
this.#path = path
for (const m of [method].flat()) {
handlers.map((handler) => {
this.addRoute(m.toUpperCase(), this.#path, handler)
})
for (const p of [path].flat()) {
this.#path = p
for (const m of [method].flat()) {
handlers.map((handler) => {
this.addRoute(m.toUpperCase(), this.#path, handler)
})
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this as any
Expand Down
25 changes: 24 additions & 1 deletion src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,30 @@ describe('Multiple methods with `app.on`', () => {
})
})

describe('Multiple paths with one handler', () => {
const app = new Hono()

const paths = ['/hello', '/ja/hello', '/en/hello']
app.on('GET', paths, (c) => {
return c.json({
path: c.req.path,
routePath: c.req.routePath,
})
})

it('Should handle multiple paths', async () => {
paths.map(async (path) => {
const res = await app.request(path)
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toEqual({
path,
routePath: path,
})
})
})
})

describe('Multiple handler', () => {
describe('handler + handler', () => {
const app = new Hono()
Expand Down Expand Up @@ -1710,7 +1734,6 @@ describe('Multiple handler', () => {
expect(res.ok).toBe(true)
expect(await res.text()).toBe('type: car, url: good-car')
})

it('Should return a correct param - GET /foo/food/good-food', async () => {
const res = await app.request('/foo/food/good-food')
expect(res.ok).toBe(true)
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,13 @@ export interface OnHandlerInterface<
S & ToSchema<string, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,
BasePath
>

// app.on(method | method[], path[], ...handlers[])
<I extends Input = {}, R extends HandlerResponse<any> = any>(
methods: string | string[],
paths: string[],
...handlers: H<E, any, I, R>[]
): Hono<E, S & ToSchema<string, string, I['in'], MergeTypedResponseData<R>>, BasePath>
}

type ExtractKey<S> = S extends Record<infer Key, unknown>
Expand Down

0 comments on commit 3b55e52

Please sign in to comment.