Skip to content
Myr

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

5 files+32 added−32 removed

modified:deno_dist/request.ts+4 added-4 removed
4 unmodified lines
5 InputToDataByTarget,6 ParamKeys,7 ParamKeyToRecord,8 RemoveQuestion,9 UndefinedIfHavingQuestion,9 ValidationTargets,10 RouterRoute,11} from './types.ts'12import { parseBody } from './utils/body.ts'
61 unmodified lines
74 * 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 lines
modified:deno_dist/types.ts+2 added-6 removed
1620 unmodified lines
16211622export 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 lines
1726export 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 lines
1772 : never17731774export type RemoveQuestion<T> = T extends `${infer R}?` ? R : T17751778export type UndefinedIfHavingQuestion<T> = T extends `${infer _}?` ? string | undefined : string17791776////////////////////////////////////////1777////// //////1778////// Utilities //////1779////// //////
23 unmodified lines
modified:src/request.ts+4 added-4 removed
4 unmodified lines
5 InputToDataByTarget,6 ParamKeys,7 ParamKeyToRecord,8 RemoveQuestion,9 UndefinedIfHavingQuestion,9 ValidationTargets,10 RouterRoute,11} from './types'12import { parseBody } from './utils/body'
61 unmodified lines
74 * 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 lines
modified:src/types.test.ts+20 added-12 removed
16 unmodified lines
17 ParamKeys,18 ParamKeyToRecord,19 RemoveQuestion,20 ToSchema,21 UndefinedIfHavingQuestion,21} from './types'22import type { Expect, Equal } from './utils/types'23import { validator } from './validator'24
405 unmodified lines
430 type Expected = { [K in '/animal/type']: string }431 type verify = Expect<Equal<Expected, Actual>>432 })433 })434435 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})455456describe('For HonoRequest', () => {457 type Input = {
23 unmodified lines
481 test('RemoveQuestion', () => {482 type Actual = RemoveQuestion<'/animal/type?'>483 type verify = Expect<Equal<'/animal/type', Actual>>484 })466467 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})486487describe('AddParam', () => {488 it('Should add params to input correctly', () => {
893 unmodified lines
modified:src/types.ts+2 added-6 removed
1620 unmodified lines
16211622export 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 lines
1726export 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 lines
1772 : never17731774export type RemoveQuestion<T> = T extends `${infer R}?` ? R : T17751778export type UndefinedIfHavingQuestion<T> = T extends `${infer _}?` ? string | undefined : string17791776////////////////////////////////////////1777////// //////1778////// Utilities //////1779////// //////
23 unmodified lines