fix(types): fix handler types (#1328)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent fa75c26 · Browse files · 2 files·+57 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+57 added−34 removed
- tests:+49 added−12 removed
- other:+8 added−22 removed
Provenance
- Commit
b1f3858- Tree
15caec0- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
fa75c26
2 files changed+57 added−34 removed
modified:src/types.test.ts+49 added-12 removed
48 unmodified lines49 describe('no path pattern', () => {50 const app = new Hono<Env>()51 const middleware: MiddlewareHandler<52 Env,53- never,53+ '/',54 {55 in: { json: Payload }56 out: { json: Payload }57 }3 unmodified lines61 test('Context', () => {62 const route = app.get(middleware, (c) => {63 type Expected = Context<64 Env,65- never,65+ '/',66 {67 in: { json: Payload }68 out: { json: Payload }69 }9 unmodified lines79 return c.jsonT({80 message: 'Hello!',81 })82 })83+ type Actual = ExtractSchema<typeof route>84+ type Expected = {85+ '/': {86+ $get: {87+ input: {88+ json: Payload89+ }90+ output: {91+ message: string92+ }93+ }94+ }95+ }96+ type verify = Expect<Equal<Expected, Actual>>97 })98 })99 100 describe('path pattern', () => {62 unmodified lines163 describe('Without path', () => {164 const app = new Hono<Env>().basePath('/foo/:foo')165 166 it('With basePath and path params', () => {153- const route = app.get(154- async (c) => {155- const foo = c.req.param('foo')156- expect(typeof foo).toBe('string')157- return c.text(foo)167+ const route = app.get(async (c) => {168+ const foo = c.req.param('foo')169+ expect(typeof foo).toBe('string')170+ return c.text(foo)171+ })172+ type Actual = ExtractSchema<typeof route>173+174+ type Expected = {175+ '/foo/:foo': {176+ $get: {177+ input: {178+ param: {179+ foo: string180+ }181+ }182+ output: {}183+ }184 }159- )160- type Actual = typeof route161- type Expected = Hono<Env, Schema<'get', '/foo/:foo', {}, {}>, '/foo/:foo'>185+ }186 type verify = Expect<Equal<Expected, Actual>>187 })188 189 it('Chained', () => {190 const route = app.post('/books/:id').get((c) => {191 const id = c.req.param('id')192 return c.text(id)193 })170- type Actual = typeof route171- type Expected = Hono<Env, Schema<'get', '/foo/:foo/books/:id', {}, {}> | Schema<'post', '/foo/:foo/books/:id', {}, {}>, '/foo/:foo'>194+ type Actual = ExtractSchema<typeof route>195+ type Expected = {196+ '/foo/:foo/books/:id': {197+ $get: {198+ input: {199+ param: {200+ id: string201+ } & {202+ foo: string203+ }204+ }205+ output: {}206+ }207+ }208+ }209 type verify = Expect<Equal<Expected, Actual>>210 })211 })212 })294 unmodified linesmodified:src/types.ts+8 added-22 removed
76 unmodified lines77 I extends Input = {},78 O = {}79 >(80 ...handlers: [H<E, P, I, O>, H<E, P, I, O>]81- ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I['in'], O>>, BasePath>81+ ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I['in'], O>>, BasePath>82 83 // app.get(handler x 3)84 <85 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,1 unmodified line87 I extends Input = {},88 I2 extends Input = I,89 I3 extends Input = I & I290 >(91- ...handlers: [92- H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]91+ ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]92 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3['in'], O>>, BasePath>93 94 // app.get(handler x 4)95 <3 unmodified lines99 I2 extends Input = I,100 I3 extends Input = I & I2,101 I4 extends Input = I2 & I3102 >(104- ...handlers: [105- H<E, P, I, O>,106- H<E, P, I2, O>,107- H<E, P, I3, O>,108- H<E, P, I4, O>109- ]103+ ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]104 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4['in'], O>>, BasePath>105 106 // app.get(handler x 5)107 <4 unmodified lines112 I3 extends Input = I & I2,113 I4 extends Input = I2 & I3,114 I5 extends Input = I3 & I4115 >(122- ...handlers: [123- H<E, P, I, O>,124- H<E, P, I2, O>,125- H<E, P, I3, O>,126- H<E, P, I4, O>,127- H<E, P, I5, O>128- ]116+ ...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>]117 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I5['in'], O>>, BasePath>118 119 // app.get(...handlers[])120 <121 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,122 I extends Input = {},123 O = {}136- >(...handlers: Handler<E, P, I, O>[]): Hono<137- E,138- RemoveBlankRecord<S | Schema<M, P, I['in'], O>>,139- BasePath140- >124+ >(125+ ...handlers: Handler<E, P, I, O>[]126+ ): Hono<E, RemoveBlankRecord<Schema<M, P, I['in'], O>>, BasePath>127 128 //// app.get(path, ...handlers[])129 130 // app.get(path, handler)296 unmodified lines427 ////// Utilities //////428 ////// //////429 ////////////////////////////////////////430 445-export type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never431+export type ExtractSchema<T> = T extends Hono<infer _, infer S, any> ? S : never432 433 ////////////////////////////////////////434 ////// //////435 ////// FetchEvent //////9 unmodified lines