fix(request): infer params in a path includes one or more optional parameter (#2576)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(request): infer params in a path includes one or more optional parameter * denoify
authored by Yusuke Wada · committed by GitHub · parent 06fe3ec · Browse files · 5 files·+32 added−32 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+32 added−32 removed
- tests:+20 added−12 removed
- other:+12 added−20 removed
Provenance
- Commit
f298549- Tree
fb542e6- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
06fe3ec
5 files changed+32 added−32 removed
modified:deno_dist/request.ts+4 added-4 removed
4 unmodified lines5 InputToDataByTarget,6 ParamKeys,7 ParamKeyToRecord,8 RemoveQuestion,9- UndefinedIfHavingQuestion,9 ValidationTargets,10 RouterRoute,11 } from './types.ts'12 import { parseBody } from './utils/body.ts'61 unmodified lines74 * const { id, comment_id } = c.req.param()75 * ```76 * @see https://hono.dev/api/routing#path-parameter77 */79- param<P2 extends string = P>(80- key: RemoveQuestion<ParamKeys<P2>>81- ): UndefinedIfHavingQuestion<ParamKeys<P2>>78+ param<P2 extends ParamKeys<P> = ParamKeys<P>>(key: P2 extends `${infer _}?` ? never : P2): string79+ param<P2 extends RemoveQuestion<ParamKeys<P>> = RemoveQuestion<ParamKeys<P>>>(80+ key: P281+ ): string | undefined82 param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>83 param(key?: string): unknown {84 return key ? this.getDecodedParam(key) : this.getAllDecodedParams()85 }262 unmodified linesmodified:deno_dist/types.ts+2 added-6 removed
1620 unmodified lines1621 1622 export type Schema = {1623 [Path: string]: {1624 [Method: `$${Lowercase<string>}`]: {1625- input: Partial<ValidationTargets> & {1626- param?: Record<string, string>1627- }1625+ input: Partial<ValidationTargets>1626 output: any1627 }1628 }1629 }96 unmodified lines1726 export type ValidationTargets = {1727 json: any1728 form: Record<string, string | File>1729 query: Record<string, string | string[]>1732- param: Record<string, string>1730+ param: Record<string, string> | Record<string, string | undefined>1731 header: Record<string, string>1732 cookie: Record<string, string>1733 }1734 37 unmodified lines1772 : never1773 1774 export type RemoveQuestion<T> = T extends `${infer R}?` ? R : T1775 1778-export type UndefinedIfHavingQuestion<T> = T extends `${infer _}?` ? string | undefined : string1779-1776 ////////////////////////////////////////1777 ////// //////1778 ////// Utilities //////1779 ////// //////23 unmodified linesmodified:src/request.ts+4 added-4 removed
4 unmodified lines5 InputToDataByTarget,6 ParamKeys,7 ParamKeyToRecord,8 RemoveQuestion,9- UndefinedIfHavingQuestion,9 ValidationTargets,10 RouterRoute,11 } from './types'12 import { parseBody } from './utils/body'61 unmodified lines74 * const { id, comment_id } = c.req.param()75 * ```76 * @see https://hono.dev/api/routing#path-parameter77 */79- param<P2 extends string = P>(80- key: RemoveQuestion<ParamKeys<P2>>81- ): UndefinedIfHavingQuestion<ParamKeys<P2>>78+ param<P2 extends ParamKeys<P> = ParamKeys<P>>(key: P2 extends `${infer _}?` ? never : P2): string79+ param<P2 extends RemoveQuestion<ParamKeys<P>> = RemoveQuestion<ParamKeys<P>>>(80+ key: P281+ ): string | undefined82 param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>83 param(key?: string): unknown {84 return key ? this.getDecodedParam(key) : this.getAllDecodedParams()85 }262 unmodified linesmodified:src/types.test.ts+20 added-12 removed
16 unmodified lines17 ParamKeys,18 ParamKeyToRecord,19 RemoveQuestion,20 ToSchema,21- UndefinedIfHavingQuestion,21 } from './types'22 import type { Expect, Equal } from './utils/types'23 import { validator } from './validator'24 405 unmodified lines430 type Expected = { [K in '/animal/type']: string }431 type verify = Expect<Equal<Expected, Actual>>432 })433 })434+435+ describe('Path parameters in app', () => {436+ test('Optional parameters - /api/:a/:b?', () => {437+ const app = new Hono()438+ const routes = app.get('/api/:a/:b?', (c) => {439+ const a = c.req.param('a')440+ const b = c.req.param('b')441+ expectTypeOf(a).toEqualTypeOf<string>()442+ expectTypeOf(b).toEqualTypeOf<string | undefined>()443+ return c.json({ a, b })444+ })445+ type T = ExtractSchema<typeof routes>446+ type Output = T['/api/:a/:b?']['$get']['output']447+ type Expected = {448+ a: string449+ b: string | undefined450+ }451+ type verify = Expect<Equal<Expected, Output>>452+ })453+ })454 })455 456 describe('For HonoRequest', () => {457 type Input = {23 unmodified lines481 test('RemoveQuestion', () => {482 type Actual = RemoveQuestion<'/animal/type?'>483 type verify = Expect<Equal<'/animal/type', Actual>>484 })466-467- describe('UndefinedIfHavingQuestion', () => {468- test('With ?', () => {469- type Actual = UndefinedIfHavingQuestion<'/animal/type?'>470- type verify = Expect<Equal<string | undefined, Actual>>471- })472- test('Without ?', () => {473- type Actual = UndefinedIfHavingQuestion<'/animal/type'>474- type verify = Expect<Equal<string, Actual>>475- })476- })485 })486 487 describe('AddParam', () => {488 it('Should add params to input correctly', () => {893 unmodified linesmodified:src/types.ts+2 added-6 removed
1620 unmodified lines1621 1622 export type Schema = {1623 [Path: string]: {1624 [Method: `$${Lowercase<string>}`]: {1625- input: Partial<ValidationTargets> & {1626- param?: Record<string, string>1627- }1625+ input: Partial<ValidationTargets>1626 output: any1627 }1628 }1629 }96 unmodified lines1726 export type ValidationTargets = {1727 json: any1728 form: Record<string, string | File>1729 query: Record<string, string | string[]>1732- param: Record<string, string>1730+ param: Record<string, string> | Record<string, string | undefined>1731 header: Record<string, string>1732 cookie: Record<string, string>1733 }1734 37 unmodified lines1772 : never1773 1774 export type RemoveQuestion<T> = T extends `${infer R}?` ? R : T1775 1778-export type UndefinedIfHavingQuestion<T> = T extends `${infer _}?` ? string | undefined : string1779-1776 ////////////////////////////////////////1777 ////// //////1778 ////// Utilities //////1779 ////// //////23 unmodified lines