Skip to content
Myr

fix(types): `MergeSchemaPath` infer inputs not only params (#2154)

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

* fix(types): `MergeSchemaPath` infer inputs not only params * denoify * format

authored by Yusuke Wada · committed by GitHub · parent 97dd369 · Browse files · 3 files+80 added−10 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+80 added−10 removed

  • tests:+62 added−4 removed
  • other:+18 added−6 removed

3 files+80 added−10 removed

modified:deno_dist/types.ts+9 added-3 removed
1625 unmodified lines
1626 : Path extends `${infer Start}:${infer Param}/${infer Rest}`1627 ? { [K in Param | keyof ExtractParams<`/${Rest}`>]: string }1628 : Path extends `${infer Start}:${infer Param}`1629 ? { [K in Param]: string }1630 : {}1630 : never16311632type FlattenIfIntersect<T> = T extends infer O ? { [K in keyof O]: O[K] } : never16331634export type MergeSchemaPath<OrigSchema extends Schema, SubPath extends string> = {1635 [P in keyof OrigSchema as MergePath<SubPath, P & string>]: {1636 [M in keyof OrigSchema[P]]: OrigSchema[P][M] extends {1637 input: infer Input1638 output: infer Output1639 }1640 ? {1639 input: Input extends { param: infer Params }1640 ? { param: Params & ExtractParams<SubPath> }1641 input: Input extends { param: infer _ }1642 ? ExtractParams<SubPath> extends never1643 ? Input1644 : FlattenIfIntersect<Input & { param: ExtractParams<SubPath> }>1645 : RemoveBlankRecord<ExtractParams<SubPath>> extends never1646 ? Input1647 : Input & { param: ExtractParams<SubPath> }1648 output: Output
3 unmodified lines
1652}16531654export type AddParam<I, P extends string> = ParamKeys<P> extends never1655 ? I1656 : I extends { param: infer _ }1657 ? I1658 : I & { param: UnionToIntersection<ParamKeyToRecord<ParamKeys<P>>> }16591660type AddDollar<T extends string> = `$${Lowercase<T>}`1661
116 unmodified lines
modified:src/types.test.ts+62 added-4 removed
4 unmodified lines
5import { createMiddleware } from './helper'6import { Hono } from './hono'7import { poweredBy } from './middleware/powered-by'8import type {9 AddParam,10 Env,11 ExtractSchema,12 Handler,13 InputToDataByTarget,
461 unmodified lines
475 })476 })477})478478describe('merge path', () => {479 test('MergePath', () => {479describe('AddParam', () => {480 it('Should add params to input correctly', () => {481 type Actual = AddParam<482 {483 param: {484 id: string485 }486 } & {487 query: {488 page: string489 }490 },491 '/:id'492 >493 type Expected = {494 query: {495 page: string496 }497 } & {498 param: {499 id: string500 }501 }502 type verify = Expect<Equal<Expected, Actual>>503 })504})505506describe('ToSchema', () => {507 it('Should convert parameters to schema correctly', () => {508 type Actual = ToSchema<'get', '/:id', { param: { id: string }; query: { page: string } }, {}>509 type Expected = {510 '/:id': {511 $get: {512 input: {513 param: {514 id: string515 }516 query: {517 page: string518 }519 }520 output: {}521 }522 }523 }524 type verify = Expect<Equal<Expected, Actual>>525 })526})527528describe('MergePath', () => {529 it('Should merge paths correctly', () => {530 type path1 = MergePath<'/api', '/book'>531 type verify1 = Expect<Equal<'/api/book', path1>>532 type path2 = MergePath<'/api/', '/book'>533 type verify2 = Expect<Equal<'/api/book', path2>>
1 unmodified line
535 type verify3 = Expect<Equal<'/api/', path3>>536 type path4 = MergePath<'/api', '/'>537 type verify4 = Expect<Equal<'/api', path4>>538 })539})540490 test('MergeSchemaPath', () => {541describe('MergeSchemaPath', () => {542 it('Should merge schema and sub path correctly', () => {543 type Sub = ToSchema<544 'post',545 '/posts',546 {
41 unmodified lines
588589 type verify = Expect<Equal<Expected, Actual>>590 })591540 test('MergeSchemePath - with params and the subpath does not have params', () => {592 it('Should merge schema which has params and sub path does not have params', () => {593 type Actual = MergeSchemaPath<594 {595 '/': {596 $get: {597 input: {598 param: {599 id: string600 }601 query: {602 page: string603 }604 }605 output: {}606 }607 }
6 unmodified lines
614 input: {615 param: {616 id: string617 }618 query: {619 page: string620 }621 }622 output: {}623 }624 }
524 unmodified lines
modified:src/types.ts+9 added-3 removed
1625 unmodified lines
1626 : Path extends `${infer Start}:${infer Param}/${infer Rest}`1627 ? { [K in Param | keyof ExtractParams<`/${Rest}`>]: string }1628 : Path extends `${infer Start}:${infer Param}`1629 ? { [K in Param]: string }1630 : {}1630 : never16311632type FlattenIfIntersect<T> = T extends infer O ? { [K in keyof O]: O[K] } : never16331634export type MergeSchemaPath<OrigSchema extends Schema, SubPath extends string> = {1635 [P in keyof OrigSchema as MergePath<SubPath, P & string>]: {1636 [M in keyof OrigSchema[P]]: OrigSchema[P][M] extends {1637 input: infer Input1638 output: infer Output1639 }1640 ? {1639 input: Input extends { param: infer Params }1640 ? { param: Params & ExtractParams<SubPath> }1641 input: Input extends { param: infer _ }1642 ? ExtractParams<SubPath> extends never1643 ? Input1644 : FlattenIfIntersect<Input & { param: ExtractParams<SubPath> }>1645 : RemoveBlankRecord<ExtractParams<SubPath>> extends never1646 ? Input1647 : Input & { param: ExtractParams<SubPath> }1648 output: Output
3 unmodified lines
1652}16531654export type AddParam<I, P extends string> = ParamKeys<P> extends never1655 ? I1656 : I extends { param: infer _ }1657 ? I1658 : I & { param: UnionToIntersection<ParamKeyToRecord<ParamKeys<P>>> }16591660type AddDollar<T extends string> = `$${Lowercase<T>}`1661
116 unmodified lines