fix(types): `app.route` with multiple endpoints returns types correctly (#919)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(types): `app.route` with multiple endpoints returns types correctly * fix `OnHandlerInterface` patterns too. * denoify * fix type mismatch of Env * remove unnecessary assert
authored by Yusuke Wada · committed by GitHub · parent 14ed9f7 · Browse files · 5 files·+82 added−42 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+82 added−42 removed
- tests:+38 added−2 removed
- other:+44 added−40 removed
Provenance
- Commit
b3868d1- Tree
ed9cccc- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
14ed9f7
5 files changed+82 added−42 removed
modified:deno_dist/hono.ts+6 added-4 removed
81 unmodified lines82 if (typeof handler !== 'string') {83 this.addRoute(method, this.path, handler)84 }85 })86+ // eslint-disable-next-line @typescript-eslint/no-explicit-any86- return this87+ return this as any88 }89 })90 91 // Implementation of app.on(method, path, ...handlers[])4 unmodified lines96 handlers.map((handler) => {97 this.addRoute(m.toUpperCase(), this.path, handler)98 })99 }100+ // eslint-disable-next-line @typescript-eslint/no-explicit-any99- return this101+ return this as any102 }103 104 // Implementation of app.use(...handlers[]) or app.get(path, ...handlers[])105 this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => {13 unmodified lines119 120 private notFoundHandler: NotFoundHandler = notFoundHandler121 private errorHandler: ErrorHandler = errorHandler122 121- route<SubPath extends string, SubSchema>(123+ route<SubPath extends string, SubEnv extends Env, SubSchema>(124 path: SubPath,123- app?: Hono<E, SubSchema>125+ app?: Hono<SubEnv, SubSchema>126 ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>> {127 this._tempPath = path128 if (app) {129 app.routes.map((r) => {146 unmodified linesmodified:deno_dist/types.ts+16 added-16 removed
61 unmodified lines62 63 // app.get(handler, handler)64 <I = {}, O = {}>(...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I, O>]): Hono<65 E,66- S & Schema<M, ExtractKey<S>, I, O>66+ RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I, O>>67 >68 69 // app.get(handler x 3)70 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(71 ...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I2, O>, H<E, ExtractKey<S>, I3, O>]72- ): Hono<E, S & Schema<M, ExtractKey<S>, I3, O>>72+ ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I3, O>>>73 74 // app.get(handler x 4)75 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(76 ...handlers: [1 unmodified line78 H<E, ExtractKey<S>, I2, O>,79 H<E, ExtractKey<S>, I3, O>,80 H<E, ExtractKey<S>, I4, O>81 ]82- ): Hono<E, S & Schema<M, ExtractKey<S>, I4, O>>82+ ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I4, O>>>83 84 // app.get(handler x 5)85 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3, I5 = I3 & I4>(86 ...handlers: [2 unmodified lines89 H<E, ExtractKey<S>, I3, O>,90 H<E, ExtractKey<S>, I4, O>,91 H<E, ExtractKey<S>, I5, O>92 ]93- ): Hono<E, S & Schema<M, ExtractKey<S>, I5, O>>93+ ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I5, O>>>94 95 // app.get(...handlers[])96 <I = {}, O = {}>(...handlers: Handler<E, ExtractKey<S>, I, O>[]): Hono<97 E,98- S & Schema<M, ExtractKey<S>, I, O>98+ RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I, O>>99 >100 101 //// app.get(path, ...handlers[])102 103 // app.get(path, handler, handler)104 <P extends string, O = {}, I = {}>(path: P, ...handlers: [H<E, P, I, O>, H<E, P, I, O>]): Hono<105 E,106- S & Schema<M, P, I, O>106+ RemoveBlankRecord<S | Schema<M, P, I, O>>107 >108 109 // app.get(path, handler x3)110 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(111 path: P,112 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]113- ): Hono<E, S & Schema<M, P, I3, O>>113+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>114 115 // app.get(path, handler x4)116 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(117 path: P,118 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]119- ): Hono<E, S & Schema<M, P, I4, O>>119+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>120 121 // app.get(path, handler x5)122 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3, I5 = I3 & I4>(123 path: P,124 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>, H<E, P, I5, O>]125- ): Hono<E, S & Schema<M, P, I5, O>>125+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I5, O>>>126 127 // app.get(path, ...handlers[])128 <P extends string, I = {}, O = {}>(path: P, ...handlers: H<E, P, I, O>[]): Hono<129 E,130- S & Schema<M, P, I, O>130+ RemoveBlankRecord<S | Schema<M, P, I, O>>131 >132 }133 134 ////////////////////////////////////////20 unmodified lines155 <M extends string, P extends string, O = {}, I = {}>(156 method: M,157 path: P,158 ...handlers: [H<E, P, I, O>, H<E, P, I, O>]159- ): Hono<E, S & Schema<M, P, I, O>>159+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>160 161 // app.get(method, path, handler x3)162 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(163 method: M,164 path: P,165 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]166- ): Hono<E, S & Schema<M, P, I3, O>>166+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>167 168 // app.get(method, path, handler x4)169 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(170 method: M,171 path: P,172 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]173- ): Hono<E, S & Schema<M, P, I4, O>>173+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>174 175 // app.get(method, path, handler x5)176 <177 M extends string,7 unmodified lines185 >(186 method: M,187 path: P,188 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>, H<E, P, I5, O>]189- ): Hono<E, S & Schema<M, P, I5, O>>189+ ): Hono<E, S | Schema<M, P, I5, O>>190 191 <M extends string, P extends string, O extends {} = {}, I = {}>(192 method: M,193 path: P,194 ...handlers: H<E, P, I, O>[]195- ): Hono<E, S & Schema<M, P, I, O>>195+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>196 197 // app.on(method[], path, ...handler)198 <P extends string, O extends {} = {}, I = {}>(199 methods: string[],200 path: P,201 ...handlers: H<E, P, I, O>[]202- ): Hono<E, S & Schema<string, P, I, O>>202+ ): Hono<E, RemoveBlankRecord<S | Schema<string, P, I, O>>>203 }204 205 type ExtractKey<S> = S extends Record<infer Key, unknown>206 ? Key extends string114 unmodified linesmodified:src/client/client.test.ts+38 added-2 removed
1+/* eslint-disable @typescript-eslint/no-explicit-any */2 /* eslint-disable @typescript-eslint/no-unused-vars */3 /* eslint-disable @typescript-eslint/ban-ts-comment */4 import FormData from 'form-data'5 import { rest } from 'msw'6 import { setupServer } from 'msw/node'7 import _fetch, { Request as NodeFetchRequest } from 'node-fetch'8 import { Hono } from '../hono'8-import type { Equal, Expect } from '../utils/types'9+import type { Expect } from '../utils/types'10+import type { Equal, NotEqual } from '../utils/types'11 import { validator } from '../validator'12 import { hc } from './client'13 import type { InferRequestType, InferResponseType } from './types'14 263 unmodified lines278 describe('Merge path with `app.route()`', () => {279 const server = setupServer(280 rest.get('http://localhost/api/search', async (req, res, ctx) => {281 return res(280- ctx.status(200),282 ctx.json({283 ok: true,284 })285 )286+ }),287+ rest.get('http://localhost/api/foo', async (req, res, ctx) => {288+ return res(289+ ctx.json({290+ ok: true,291+ })292+ )293+ }),294+ rest.post('http://localhost/api/bar', async (req, res, ctx) => {295+ return res(296+ ctx.json({297+ ok: true,298+ })299+ )300 })301 )302 303 beforeAll(() => server.listen())15 unmodified lines319 const data = await res.json()320 type verify = Expect<Equal<boolean, typeof data.ok>>321 expect(data.ok).toBe(true)322 })323+324+ describe('Multiple endpoints', () => {325+ const api = new Hono()326+ .get('/foo', (c) => c.jsonT({ foo: '' }))327+ .post('/bar', (c) => c.jsonT({ bar: 0 }))328+ const app = new Hono().route('/api', api)329+ type AppType = typeof app330+ const client = hc<typeof app>('http://localhost')331+332+ it('Should return correct types - GET /api/foo', async () => {333+ const res = await client.api.foo.$get()334+ const data = await res.json()335+ type verify = Expect<Equal<string, typeof data.foo>>336+ })337+338+ it('Should return correct types - POST /api/bar', async () => {339+ const res = await client.api.bar.$post()340+ const data = await res.json()341+ type verify = Expect<Equal<number, typeof data.bar>>342+ })343+ })344 })345 346 describe('Use custom fetch method', () => {347 it('Should call the custom fetch method when provided', async () => {24 unmodified linesmodified:src/hono.ts+6 added-4 removed
81 unmodified lines82 if (typeof handler !== 'string') {83 this.addRoute(method, this.path, handler)84 }85 })86+ // eslint-disable-next-line @typescript-eslint/no-explicit-any86- return this87+ return this as any88 }89 })90 91 // Implementation of app.on(method, path, ...handlers[])4 unmodified lines96 handlers.map((handler) => {97 this.addRoute(m.toUpperCase(), this.path, handler)98 })99 }100+ // eslint-disable-next-line @typescript-eslint/no-explicit-any99- return this101+ return this as any102 }103 104 // Implementation of app.use(...handlers[]) or app.get(path, ...handlers[])105 this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => {13 unmodified lines119 120 private notFoundHandler: NotFoundHandler = notFoundHandler121 private errorHandler: ErrorHandler = errorHandler122 121- route<SubPath extends string, SubSchema>(123+ route<SubPath extends string, SubEnv extends Env, SubSchema>(124 path: SubPath,123- app?: Hono<E, SubSchema>125+ app?: Hono<SubEnv, SubSchema>126 ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>> {127 this._tempPath = path128 if (app) {129 app.routes.map((r) => {146 unmodified linesmodified:src/types.ts+16 added-16 removed
61 unmodified lines62 63 // app.get(handler, handler)64 <I = {}, O = {}>(...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I, O>]): Hono<65 E,66- S & Schema<M, ExtractKey<S>, I, O>66+ RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I, O>>67 >68 69 // app.get(handler x 3)70 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(71 ...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I2, O>, H<E, ExtractKey<S>, I3, O>]72- ): Hono<E, S & Schema<M, ExtractKey<S>, I3, O>>72+ ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I3, O>>>73 74 // app.get(handler x 4)75 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(76 ...handlers: [1 unmodified line78 H<E, ExtractKey<S>, I2, O>,79 H<E, ExtractKey<S>, I3, O>,80 H<E, ExtractKey<S>, I4, O>81 ]82- ): Hono<E, S & Schema<M, ExtractKey<S>, I4, O>>82+ ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I4, O>>>83 84 // app.get(handler x 5)85 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3, I5 = I3 & I4>(86 ...handlers: [2 unmodified lines89 H<E, ExtractKey<S>, I3, O>,90 H<E, ExtractKey<S>, I4, O>,91 H<E, ExtractKey<S>, I5, O>92 ]93- ): Hono<E, S & Schema<M, ExtractKey<S>, I5, O>>93+ ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I5, O>>>94 95 // app.get(...handlers[])96 <I = {}, O = {}>(...handlers: Handler<E, ExtractKey<S>, I, O>[]): Hono<97 E,98- S & Schema<M, ExtractKey<S>, I, O>98+ RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I, O>>99 >100 101 //// app.get(path, ...handlers[])102 103 // app.get(path, handler, handler)104 <P extends string, O = {}, I = {}>(path: P, ...handlers: [H<E, P, I, O>, H<E, P, I, O>]): Hono<105 E,106- S & Schema<M, P, I, O>106+ RemoveBlankRecord<S | Schema<M, P, I, O>>107 >108 109 // app.get(path, handler x3)110 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(111 path: P,112 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]113- ): Hono<E, S & Schema<M, P, I3, O>>113+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>114 115 // app.get(path, handler x4)116 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(117 path: P,118 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]119- ): Hono<E, S & Schema<M, P, I4, O>>119+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>120 121 // app.get(path, handler x5)122 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3, I5 = I3 & I4>(123 path: P,124 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>, H<E, P, I5, O>]125- ): Hono<E, S & Schema<M, P, I5, O>>125+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I5, O>>>126 127 // app.get(path, ...handlers[])128 <P extends string, I = {}, O = {}>(path: P, ...handlers: H<E, P, I, O>[]): Hono<129 E,130- S & Schema<M, P, I, O>130+ RemoveBlankRecord<S | Schema<M, P, I, O>>131 >132 }133 134 ////////////////////////////////////////20 unmodified lines155 <M extends string, P extends string, O = {}, I = {}>(156 method: M,157 path: P,158 ...handlers: [H<E, P, I, O>, H<E, P, I, O>]159- ): Hono<E, S & Schema<M, P, I, O>>159+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>160 161 // app.get(method, path, handler x3)162 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(163 method: M,164 path: P,165 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]166- ): Hono<E, S & Schema<M, P, I3, O>>166+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>167 168 // app.get(method, path, handler x4)169 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(170 method: M,171 path: P,172 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]173- ): Hono<E, S & Schema<M, P, I4, O>>173+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>174 175 // app.get(method, path, handler x5)176 <177 M extends string,7 unmodified lines185 >(186 method: M,187 path: P,188 ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>, H<E, P, I5, O>]189- ): Hono<E, S & Schema<M, P, I5, O>>189+ ): Hono<E, S | Schema<M, P, I5, O>>190 191 <M extends string, P extends string, O extends {} = {}, I = {}>(192 method: M,193 path: P,194 ...handlers: H<E, P, I, O>[]195- ): Hono<E, S & Schema<M, P, I, O>>195+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>196 197 // app.on(method[], path, ...handler)198 <P extends string, O extends {} = {}, I = {}>(199 methods: string[],200 path: P,201 ...handlers: H<E, P, I, O>[]202- ): Hono<E, S & Schema<string, P, I, O>>202+ ): Hono<E, RemoveBlankRecord<S | Schema<string, P, I, O>>>203 }204 205 type ExtractKey<S> = S extends Record<infer Key, unknown>206 ? Key extends string114 unmodified lines