Skip to content
Myr

fix(type): Fix reflecting basePath when no path in handler (#1325)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(type): Reflects BasePath in HandlerInterface if without path * test(path): add path param tests for routing without path and with basePath * did denonify * fix(type): Handler type * test(path): add test for chained route

authored by sizumita · committed by GitHub · parent d8ed83f · Browse files · 3 files+97 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+97 added−40 removed

  • tests:+27 added−0 removed
  • other:+70 added−40 removed

3 files+97 added−40 removed

modified:deno_dist/types.ts+35 added-20 removed
70 unmodified lines
71> {72 //// app.get(...handlers[])7374 // app.get(handler, handler)75 <I extends Input = {}, O = {}>(76 ...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I, O>]75 <76 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,77 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>8283 // app.get(handler x 3)80 <P extends string, O = {}, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2>(81 ...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I2, O>, H<E, ExtractKey<S>, I3, O>]82 ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I3['in'], O>>, BasePath>84 <85 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,86 O = {},87 I extends Input = {},88 I2 extends Input = I,89 I3 extends Input = I & I290 >(91 ...handlers: [92 Handler<E, P, I, O>, Handler<E, P, I2, O>, Handler<E, P, I3, O>]93 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3['in'], O>>, BasePath>9495 // app.get(handler x 4)96 <86 P extends string,97 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,98 O = {},99 I extends Input = {},100 I2 extends Input = I,101 I3 extends Input = I & I2,102 I4 extends Input = I2 & I3103 >(104 ...handlers: [94 H<E, ExtractKey<S>, I, O>,95 H<E, ExtractKey<S>, I2, O>,96 H<E, ExtractKey<S>, I3, O>,97 H<E, ExtractKey<S>, I4, O>105 Handler<E, P, I, O>,106 Handler<E, P, I2, O>,107 Handler<E, P, I3, O>,108 Handler<E, P, I4, O>109 ]99 ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I4['in'], O>>, BasePath>110 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4['in'], O>>, BasePath>111112 // app.get(handler x 5)113 <103 P extends string,114 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,115 O = {},116 I extends Input = {},117 I2 extends Input = I,118 I3 extends Input = I & I2,119 I4 extends Input = I2 & I3,120 I5 extends Input = I3 & I4121 >(122 ...handlers: [112 H<E, ExtractKey<S>, I, O>,113 H<E, ExtractKey<S>, I2, O>,114 H<E, ExtractKey<S>, I3, O>,115 H<E, ExtractKey<S>, I4, O>,116 H<E, ExtractKey<S>, I5, O>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 ]118 ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I5['in'], O>>, BasePath>129 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I5['in'], O>>, BasePath>130131 // app.get(...handlers[])121 <I extends Input = {}, O = {}>(...handlers: Handler<E, ExtractKey<S>, I, O>[]): Hono<132 <133 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,134 I extends Input = {},135 O = {}136 >(...handlers: Handler<E, P, I, O>[]): Hono<137 E,123 RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I['in'], O>>,138 RemoveBlankRecord<S | Schema<M, P, I['in'], O>>,139 BasePath140 >141142 //// app.get(path, ...handlers[])
316 unmodified lines
modified:src/types.test.ts+27 added
143 unmodified lines
144 }145 type verify = Expect<Equal<Expected, Actual>>146 })147 })148149 describe('Without path', () => {150 const app = new Hono<Env>().basePath('/foo/:foo')151152 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)158 }159 )160 type Actual = typeof route161 type Expected = Hono<Env, Schema<'get', '/foo/:foo', {}, {}>, '/foo/:foo'>162 type verify = Expect<Equal<Expected, Actual>>163 })164165 it('Chained', () => {166 const route = app.post('/books/:id').get((c) => {167 const id = c.req.param('id')168 return c.text(id)169 })170 type Actual = typeof route171 type Expected = Hono<Env, Schema<'get', '/foo/:foo/books/:id', {}, {}> | Schema<'post', '/foo/:foo/books/:id', {}, {}>, '/foo/:foo'>172 type verify = Expect<Equal<Expected, Actual>>173 })174 })175})176177describe('OnHandlerInterface', () => {178 const app = new Hono()
291 unmodified lines
modified:src/types.ts+35 added-20 removed
70 unmodified lines
71> {72 //// app.get(...handlers[])7374 // app.get(handler, handler)75 <I extends Input = {}, O = {}>(76 ...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I, O>]75 <76 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,77 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>8283 // app.get(handler x 3)80 <P extends string, O = {}, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2>(81 ...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I2, O>, H<E, ExtractKey<S>, I3, O>]82 ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I3['in'], O>>, BasePath>84 <85 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,86 O = {},87 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>]93 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3['in'], O>>, BasePath>9495 // app.get(handler x 4)96 <86 P extends string,97 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,98 O = {},99 I extends Input = {},100 I2 extends Input = I,101 I3 extends Input = I & I2,102 I4 extends Input = I2 & I3103 >(104 ...handlers: [94 H<E, ExtractKey<S>, I, O>,95 H<E, ExtractKey<S>, I2, O>,96 H<E, ExtractKey<S>, I3, O>,97 H<E, ExtractKey<S>, I4, O>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 ]99 ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I4['in'], O>>, BasePath>110 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4['in'], O>>, BasePath>111112 // app.get(handler x 5)113 <103 P extends string,114 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,115 O = {},116 I extends Input = {},117 I2 extends Input = I,118 I3 extends Input = I & I2,119 I4 extends Input = I2 & I3,120 I5 extends Input = I3 & I4121 >(122 ...handlers: [112 H<E, ExtractKey<S>, I, O>,113 H<E, ExtractKey<S>, I2, O>,114 H<E, ExtractKey<S>, I3, O>,115 H<E, ExtractKey<S>, I4, O>,116 H<E, ExtractKey<S>, I5, O>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 ]118 ): Hono<E, RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I5['in'], O>>, BasePath>129 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I5['in'], O>>, BasePath>130131 // app.get(...handlers[])121 <I extends Input = {}, O = {}>(...handlers: Handler<E, ExtractKey<S>, I, O>[]): Hono<132 <133 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,134 I extends Input = {},135 O = {}136 >(...handlers: Handler<E, P, I, O>[]): Hono<137 E,123 RemoveBlankRecord<S | Schema<M, ExtractKey<S>, I['in'], O>>,138 RemoveBlankRecord<S | Schema<M, P, I['in'], O>>,139 BasePath140 >141142 //// app.get(path, ...handlers[])
316 unmodified lines