fix(types): remove type-errors for routes (#1371)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(types): remove type-errors for routes * denoify * fixed the client and the presets * rename
authored by Yusuke Wada · committed by GitHub · parent f860435 · Browse files · 14 files·+182 added−164 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
14 files+182 added−164 removed
- tests:+4 added−4 removed
- other:+178 added−160 removed
Provenance
- Commit
6034dd7- Tree
164642b- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
f860435
14 files changed+182 added−164 removed
modified:deno_dist/client/types.ts+4 added-15 removed
12234567891011121314155678921101112131438 unmodified lines535455566857585960615 unmodified lines6768697082717273747513 unmodified linesimport type { Hono } from '../hono.ts'import type { ValidationTargets } from '../types.ts'import type { Schema } from '../types.ts'import type { RemoveBlankRecord } from '../utils/types.ts'
type MethodName = `$${string}`
type Endpoint = Record<MethodName, Data>
type Data = { input: Partial<ValidationTargets> & { param?: Record<string, string> } output: {}}
export type ClientRequestOptions = { headers?: Record<string, string> fetch?: typeof fetch}
type ClientRequest<S extends Data> = {type ClientRequest<S extends Schema> = { [M in keyof S]: S[M] extends { input: infer R; output: infer O } ? RemoveBlankRecord<R> extends never ? (args?: {}, options?: ClientRequestOptions) => Promise<ClientResponse<O>> : (38 unmodified lines : never
type PathToChain< Path extends string, E extends Endpoint, E extends Schema, Original extends string = ''> = Path extends `/${infer P}` ? PathToChain<P, E, Path> : Path extends `${infer P}/${infer R}`5 unmodified lines }
// eslint-disable-next-line @typescript-eslint/no-explicit-anyexport type Client<T> = T extends Hono<any, infer S, any> ? S extends Record<infer K, Endpoint> ? S extends Record<infer K, Schema> ? K extends string ? PathToChain<K, S> : never : never13 unmodified linesmodified:deno_dist/preset/quick.ts+6 added-6 removed
123455678910117891011121314153 unmodified linesimport { HonoBase } from '../hono-base.ts'import { LinearRouter } from '../router/linear-router/index.ts'import { SmartRouter } from '../router/smart-router/index.ts'import { TrieRouter } from '../router/trie-router/index.ts'import type { Env } from '../types.ts'import type { Env, Schema } from '../types.ts'
export class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase< E, S, BasePath> {export class Hono< E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends HonoBase<E, S, BasePath> { constructor(init: Partial<Pick<Hono, 'getPath'> & { strict: boolean }> = {}) { super(init) this.router = new SmartRouter({ routers: [new LinearRouter(), new TrieRouter()],3 unmodified linesmodified:deno_dist/preset/tiny.ts+6 added-6 removed
1233456789567891011121314import { HonoBase } from '../hono-base.ts'import { PatternRouter } from '../router/pattern-router/index.ts'import type { Env } from '../types.ts'import type { Env, Schema } from '../types.ts'
export class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase< E, S, BasePath> {export class Hono< E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends HonoBase<E, S, BasePath> { constructor(init: Partial<Pick<Hono, 'getPath'> & { strict: boolean }> = {}) { super(init) this.router = new PatternRouter() }}modified:deno_dist/hono-base.ts+21 added-10 removed
17 unmodified lines18 TypedResponse,19 MergePath,20 MergeSchemaPath,21 FetchEventLike,22+ Schema,23 } from './types.ts'24 import type { RemoveBlankRecord } from './utils/types.ts'25 import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url.ts'26 5 unmodified lines32 handler: H33 }34 35 function defineDynamicClass(): {35- new <E extends Env = Env, S = {}, BasePath extends string = '/'>(): {36+ new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {37 [M in Methods]: HandlerInterface<E, M, S, BasePath>38 } & {39 on: OnHandlerInterface<E, S, BasePath>40 } & {15 unmodified lines56 const message = 'Internal Server Error'57 return c.text(message, 500)58 }59 59-class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends defineDynamicClass()<60- E,61- S,62- BasePath63-> {60+class Hono<61+ E extends Env = Env,62+ S extends Schema = {},63+ BasePath extends string = '/'64+> extends defineDynamicClass()<E, S, BasePath> {65 /*66 This class is like an abstract class and does not have a router.67 To use it, inherit the class and implement router in the constructor.68 */69 unmodified lines138 139 private notFoundHandler: NotFoundHandler = notFoundHandler140 private errorHandler: ErrorHandler = errorHandler141 141- route<SubPath extends string, SubEnv extends Env, SubSchema, SubBasePath extends string>(142+ route<143+ SubPath extends string,144+ SubEnv extends Env,145+ SubSchema extends Schema,146+ SubBasePath extends string147+ >(148 path: SubPath,149 app: Hono<SubEnv, SubSchema, SubBasePath>144- ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>, BasePath>150+ ): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath>151 /** @description152 * Use `basePath` instead of `route` when passing **one** argument, such as `app.route('/api')`.153 * The use of `route` with **one** argument has been removed in v4.154 * However, you can still use `route` with **two** arguments, like `app.route('/api', subApp)`."155 */156 route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>151- route<SubPath extends string, SubEnv extends Env, SubSchema, SubBasePath extends string>(157+ route<158+ SubPath extends string,159+ SubEnv extends Env,160+ SubSchema extends Schema,161+ SubBasePath extends string162+ >(163 path: SubPath,164 app?: Hono<SubEnv, SubSchema, SubBasePath>154- ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>, BasePath> {165+ ): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath> {166 const subApp = this.basePath(path)167 168 if (!app) {169 // eslint-disable-next-line @typescript-eslint/no-explicit-any230 unmodified linesmodified:deno_dist/hono.ts+6 added-6 removed
123455678910117891011121314155 unmodified linesimport { HonoBase } from './hono-base.ts'import { RegExpRouter } from './router/reg-exp-router/index.ts'import { SmartRouter } from './router/smart-router/index.ts'import { TrieRouter } from './router/trie-router/index.ts'import type { Env } from './types.ts'import type { Env, Schema } from './types.ts'
export class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase< E, S, BasePath> {export class Hono< E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends HonoBase<E, S, BasePath> { constructor(init: Partial<Pick<Hono, 'router' | 'getPath'> & { strict: boolean }> = {}) { super(init) this.router = init.router ??5 unmodified linesmodified:deno_dist/types.ts+45 added-36 removed
64 unmodified lines65 66 export interface HandlerInterface<67 E extends Env = Env,68 M extends string = any,69- S = {},69+ S extends Schema = {},70 BasePath extends string = '/'71 > {72 //// app.get(...handlers[])73 3 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, P, I['in'], O>>, BasePath>81+ ): Hono<E, S & ToSchema<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>,2 unmodified lines88 I2 extends Input = I,89 I3 extends Input = I & I290 >(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>92+ ): Hono<E, S & ToSchema<M, P, I3['in'], O>, BasePath>93 94 // app.get(handler x 4)95 <96 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,3 unmodified lines100 I3 extends Input = I & I2,101 I4 extends Input = I & I2 & I3102 >(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>104+ ): Hono<E, S & ToSchema<M, P, I4['in'], O>, BasePath>105 106 // app.get(handler x 5)107 <108 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,4 unmodified lines113 I4 extends Input = I2 & I3,114 I5 extends Input = I & I2 & I3 & I4115 >(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>117+ ): Hono<E, S & ToSchema<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 = {}124 >(125 ...handlers: Handler<E, P, I, O>[]126- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I['in'], O>>, BasePath>126+ ): Hono<E, S & ToSchema<M, P, I['in'], O>, BasePath>127 128 //// app.get(path, ...handlers[])129 130 // app.get(path, handler)131 <P extends string, O = {}, I extends Input = {}>(132 path: P,133 handler: H<E, MergePath<BasePath, P>, I, O>134- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>134+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>135 136 // app.get(path, handler, handler)137 <P extends string, O = {}, I extends Input = {}>(138 path: P,139 ...handlers: [H<E, MergePath<BasePath, P>, I, O>, H<E, MergePath<BasePath, P>, I, O>]140- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>140+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>141 142 // app.get(path, handler x3)143 <P extends string, O = {}, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2>(144 path: P,1 unmodified line146 H<E, MergePath<BasePath, P>, I, O>,147 H<E, MergePath<BasePath, P>, I2, O>,148 H<E, MergePath<BasePath, P>, I3, O>149 ]150- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I3['in'], O>>, BasePath>150+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], O>, BasePath>151 152 // app.get(path, handler x4)153 <154 P extends string,9 unmodified lines164 H<E, MergePath<BasePath, P>, I2, O>,165 H<E, MergePath<BasePath, P>, I3, O>,166 H<E, MergePath<BasePath, P>, I4, O>167 ]168- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I4['in'], O>>, BasePath>168+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], O>, BasePath>169 170 // app.get(path, handler x5)171 <172 P extends string,11 unmodified lines184 H<E, MergePath<BasePath, P>, I3, O>,185 H<E, MergePath<BasePath, P>, I4, O>,186 H<E, MergePath<BasePath, P>, I5, O>187 ]188- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I5['in'], O>>, BasePath>188+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], O>, BasePath>189 190 // app.get(path, ...handlers[])191 <P extends string, I extends Input = {}, O = {}>(192 path: P,193 ...handlers: H<E, MergePath<BasePath, P>, I, O>[]194- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>194+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>195 }196 197 ////////////////////////////////////////198 ////// //////2 unmodified lines201 ////////////////////////////////////////202 203 export interface MiddlewareHandlerInterface<204 E extends Env = Env,205- S = {},205+ S extends Schema = {},206 BasePath extends string = '/'207 > {208 //// app.get(...handlers[])209 (...handlers: MiddlewareHandler<E, MergePath<BasePath, ExtractKey<S>>>[]): Hono<E, S, BasePath>10 unmodified lines220 ////// OnHandlerInterface //////221 ////// //////222 ////////////////////////////////////////223 224-export interface OnHandlerInterface<E extends Env = Env, S = {}, BasePath extends string = '/'> {224+export interface OnHandlerInterface<225+ E extends Env = Env,226+ S extends Schema = {},227+ BasePath extends string = '/'228+> {229 // app.on(method, path, handler, handler)230 <M extends string, P extends string, O = {}, I extends Input = {}>(231 method: M,232 path: P,233 ...handlers: [H<E, MergePath<BasePath, P>, I, O>, H<E, MergePath<BasePath, P>, I, O>]230- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>234+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>235 236 // app.get(method, path, handler x3)237 <238 M extends string,9 unmodified lines248 H<E, MergePath<BasePath, P>, I, O>,249 H<E, MergePath<BasePath, P>, I2, O>,250 H<E, MergePath<BasePath, P>, I3, O>251 ]248- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I3['in'], O>>, BasePath>252+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], O>, BasePath>253 254 // app.get(method, path, handler x4)255 <256 M extends string,11 unmodified lines268 H<E, MergePath<BasePath, P>, I2, O>,269 H<E, MergePath<BasePath, P>, I3, O>,270 H<E, MergePath<BasePath, P>, I4, O>271 ]268- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I4['in'], O>>, BasePath>272+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], O>, BasePath>273 274 // app.get(method, path, handler x5)275 <276 M extends string,13 unmodified lines290 H<E, MergePath<BasePath, P>, I3, O>,291 H<E, MergePath<BasePath, P>, I4, O>,292 H<E, MergePath<BasePath, P>, I5, O>293 ]290- ): Hono<E, S | Schema<M, MergePath<BasePath, P>, I5['in'], O>, BasePath>294+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], O>, BasePath>295 296 <M extends string, P extends string, O extends {} = {}, I extends Input = {}>(297 method: M,298 path: P,299 ...handlers: H<E, MergePath<BasePath, P>, I, O>[]296- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>300+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>301 302 // app.on(method[], path, ...handler)303 <P extends string, O extends {} = {}, I extends Input = {}>(304 methods: string[],305 path: P,306 ...handlers: H<E, MergePath<BasePath, P>, I, O>[]303- ): Hono<E, RemoveBlankRecord<S | Schema<string, MergePath<BasePath, P>, I['in'], O>>, BasePath>307+ ): Hono<E, S & ToSchema<string, MergePath<BasePath, P>, I['in'], O>, BasePath>308 }309 310 type ExtractKey<S> = S extends Record<infer Key, unknown>311 ? Key extends string2 unmodified lines314 : string315 316 ////////////////////////////////////////317 ////// //////314-////// Schema //////318+////// ToSchema //////319 ////// //////320 ////////////////////////////////////////321 318-export type Schema<M extends string, P extends string, I extends Input['in'], O> = {319- [K in P]: AddDollar<{320- [K2 in M]: {322+export type ToSchema<M extends string, P extends string, I extends Input['in'], O> = {323+ [K in P]: {324+ [K2 in M as AddDollar<string & K2>]: {325 input: unknown extends I ? AddParam<{}, P> : AddParam<I, P>326 output: unknown extends O ? {} : O327 }324- }>328+ }329 }330 331+export type Schema = {332+ [Path: string]: {333+ [Method: `$${Lowercase<string>}`]: {334+ input: Partial<ValidationTargets> & {335+ param?: Record<string, string>336+ }337+ output: {}338+ }339+ }340+}341+342+export type MergeSchemaPath<OrigSchema, SubPath extends string> = {343+ [K in keyof OrigSchema as `${SubPath}${K & string}`]: OrigSchema[K]344+}345+346 export type AddParam<I, P extends string> = ParamKeys<P> extends never347 ? I348 : I & { param: UnionToIntersection<ParamKeyToRecord<ParamKeys<P>>> }349 331-export type AddDollar<T> = T extends Record<infer K, infer R>332- ? K extends string333- ? { [MethodName in `$${Lowercase<K>}`]: R }334- : never335- : never350+type AddDollar<T extends string> = `$${Lowercase<T>}`351 337-export type MergeSchemaPath<S, P extends string> = S extends Record<infer Key, infer T>338- ? Key extends string339- ? Record<MergePath<P, Key>, T>340- : never341- : never342-352 export type MergePath<A extends string, B extends string> = A extends ''353 ? B354 : A extends '/'355 ? B102 unmodified linesmodified:src/adapter/aws-lambda/handler.ts+2 added-2 removed
1 // @denoify-ignore2 import crypto from 'crypto'3 import type { Hono } from '../../hono'4-import type { Env } from '../../types'4+import type { Env, Schema } from '../../types'5 6 import { encodeBase64 } from '../../utils/encode'7 import type { ApiGatewayRequestContext, LambdaFunctionUrlRequestContext } from './custom-context'8 48 unmodified lines57 58 /**59 * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)60 */61-export const handle = <E extends Env = Env, S = {}, BasePath extends string = '/'>(61+export const handle = <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(62 app: Hono<E, S, BasePath>63 ) => {64 return async (65 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent104 unmodified linesmodified:src/client/types.ts+4 added-15 removed
1 import type { Hono } from '../hono'2-import type { ValidationTargets } from '../types'2+import type { Schema } from '../types'3 import type { RemoveBlankRecord } from '../utils/types'4 5-type MethodName = `$${string}`6-7-type Endpoint = Record<MethodName, Data>8-9-type Data = {10- input: Partial<ValidationTargets> & {11- param?: Record<string, string>12- }13- output: {}14-}15-5 export type ClientRequestOptions = {6 headers?: Record<string, string>7 fetch?: typeof fetch8 }9 21-type ClientRequest<S extends Data> = {10+type ClientRequest<S extends Schema> = {11 [M in keyof S]: S[M] extends { input: infer R; output: infer O }12 ? RemoveBlankRecord<R> extends never13 ? (args?: {}, options?: ClientRequestOptions) => Promise<ClientResponse<O>>14 : (38 unmodified lines53 : never54 55 type PathToChain<56 Path extends string,68- E extends Endpoint,57+ E extends Schema,58 Original extends string = ''59 > = Path extends `/${infer P}`60 ? PathToChain<P, E, Path>61 : Path extends `${infer P}/${infer R}`5 unmodified lines67 }68 69 // eslint-disable-next-line @typescript-eslint/no-explicit-any70 export type Client<T> = T extends Hono<any, infer S, any>82- ? S extends Record<infer K, Endpoint>71+ ? S extends Record<infer K, Schema>72 ? K extends string73 ? PathToChain<K, S>74 : never75 : never13 unmodified linesmodified:src/preset/quick.ts+6 added-6 removed
1 import { HonoBase } from '../hono-base'2 import { LinearRouter } from '../router/linear-router'3 import { SmartRouter } from '../router/smart-router'4 import { TrieRouter } from '../router/trie-router'5-import type { Env } from '../types'5+import type { Env, Schema } from '../types'6 7-export class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase<8- E,9- S,10- BasePath11-> {7+export class Hono<8+ E extends Env = Env,9+ S extends Schema = {},10+ BasePath extends string = '/'11+> extends HonoBase<E, S, BasePath> {12 constructor(init: Partial<Pick<Hono, 'getPath'> & { strict: boolean }> = {}) {13 super(init)14 this.router = new SmartRouter({15 routers: [new LinearRouter(), new TrieRouter()],3 unmodified linesmodified:src/preset/tiny.ts+6 added-6 removed
1 import { HonoBase } from '../hono-base'2 import { PatternRouter } from '../router/pattern-router'3-import type { Env } from '../types'3+import type { Env, Schema } from '../types'4 5-export class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase<6- E,7- S,8- BasePath9-> {5+export class Hono<6+ E extends Env = Env,7+ S extends Schema = {},8+ BasePath extends string = '/'9+> extends HonoBase<E, S, BasePath> {10 constructor(init: Partial<Pick<Hono, 'getPath'> & { strict: boolean }> = {}) {11 super(init)12 this.router = new PatternRouter()13 }1 unmodified linemodified:src/hono-base.ts+21 added-10 removed
17 unmodified lines18 TypedResponse,19 MergePath,20 MergeSchemaPath,21 FetchEventLike,22+ Schema,23 } from './types'24 import type { RemoveBlankRecord } from './utils/types'25 import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/url'26 5 unmodified lines32 handler: H33 }34 35 function defineDynamicClass(): {35- new <E extends Env = Env, S = {}, BasePath extends string = '/'>(): {36+ new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {37 [M in Methods]: HandlerInterface<E, M, S, BasePath>38 } & {39 on: OnHandlerInterface<E, S, BasePath>40 } & {15 unmodified lines56 const message = 'Internal Server Error'57 return c.text(message, 500)58 }59 59-class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends defineDynamicClass()<60- E,61- S,62- BasePath63-> {60+class Hono<61+ E extends Env = Env,62+ S extends Schema = {},63+ BasePath extends string = '/'64+> extends defineDynamicClass()<E, S, BasePath> {65 /*66 This class is like an abstract class and does not have a router.67 To use it, inherit the class and implement router in the constructor.68 */69 unmodified lines138 139 private notFoundHandler: NotFoundHandler = notFoundHandler140 private errorHandler: ErrorHandler = errorHandler141 141- route<SubPath extends string, SubEnv extends Env, SubSchema, SubBasePath extends string>(142+ route<143+ SubPath extends string,144+ SubEnv extends Env,145+ SubSchema extends Schema,146+ SubBasePath extends string147+ >(148 path: SubPath,149 app: Hono<SubEnv, SubSchema, SubBasePath>144- ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>, BasePath>150+ ): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath>151 /** @description152 * Use `basePath` instead of `route` when passing **one** argument, such as `app.route('/api')`.153 * The use of `route` with **one** argument has been removed in v4.154 * However, you can still use `route` with **two** arguments, like `app.route('/api', subApp)`."155 */156 route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>151- route<SubPath extends string, SubEnv extends Env, SubSchema, SubBasePath extends string>(157+ route<158+ SubPath extends string,159+ SubEnv extends Env,160+ SubSchema extends Schema,161+ SubBasePath extends string162+ >(163 path: SubPath,164 app?: Hono<SubEnv, SubSchema, SubBasePath>154- ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>, BasePath> {165+ ): Hono<E, MergeSchemaPath<SubSchema, SubPath> & S, BasePath> {166 const subApp = this.basePath(path)167 168 if (!app) {169 // eslint-disable-next-line @typescript-eslint/no-explicit-any230 unmodified linesmodified:src/hono.ts+6 added-6 removed
1 import { HonoBase } from './hono-base'2 import { RegExpRouter } from './router/reg-exp-router'3 import { SmartRouter } from './router/smart-router'4 import { TrieRouter } from './router/trie-router'5-import type { Env } from './types'5+import type { Env, Schema } from './types'6 7-export class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends HonoBase<8- E,9- S,10- BasePath11-> {7+export class Hono<8+ E extends Env = Env,9+ S extends Schema = {},10+ BasePath extends string = '/'11+> extends HonoBase<E, S, BasePath> {12 constructor(init: Partial<Pick<Hono, 'router' | 'getPath'> & { strict: boolean }> = {}) {13 super(init)14 this.router =15 init.router ??5 unmodified linesmodified:src/types.test.ts+4 added-4 removed
12 unmodified lines13 MiddlewareHandler,14 ParamKeys,15 ParamKeyToRecord,16 RemoveQuestion,17- Schema,17+ ToSchema,18 UndefinedIfHavingQuestion,19 } from './types'20 import type { Expect, Equal } from './utils/types'21 241 unmodified lines263 describe('Schema', () => {264 test('Schema', () => {265 type AppType = Hono<266 Env,267- Schema<267+ ToSchema<268 'post',269 '/api/posts/:id',270 {271 json: {194 unmodified lines466 type verify4 = Expect<Equal<'/api', path4>>467 })468 469 test('MergeSchemaPath', () => {470- type Sub = Schema<470+ type Sub = ToSchema<471 'post',472 '/posts',473 {474 json: {4 unmodified lines479 {480 message: string481 }482 > &483- Schema<483+ ToSchema<484 'get',485 '/posts',486 {},487 {32 unmodified linesmodified:src/types.ts+45 added-36 removed
64 unmodified lines65 66 export interface HandlerInterface<67 E extends Env = Env,68 M extends string = any,69- S = {},69+ S extends Schema = {},70 BasePath extends string = '/'71 > {72 //// app.get(...handlers[])73 3 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, P, I['in'], O>>, BasePath>81+ ): Hono<E, S & ToSchema<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>,2 unmodified lines88 I2 extends Input = I,89 I3 extends Input = I & I290 >(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>92+ ): Hono<E, S & ToSchema<M, P, I3['in'], O>, BasePath>93 94 // app.get(handler x 4)95 <96 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,3 unmodified lines100 I3 extends Input = I & I2,101 I4 extends Input = I & I2 & I3102 >(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>104+ ): Hono<E, S & ToSchema<M, P, I4['in'], O>, BasePath>105 106 // app.get(handler x 5)107 <108 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,4 unmodified lines113 I4 extends Input = I2 & I3,114 I5 extends Input = I & I2 & I3 & I4115 >(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>117+ ): Hono<E, S & ToSchema<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 = {}124 >(125 ...handlers: Handler<E, P, I, O>[]126- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I['in'], O>>, BasePath>126+ ): Hono<E, S & ToSchema<M, P, I['in'], O>, BasePath>127 128 //// app.get(path, ...handlers[])129 130 // app.get(path, handler)131 <P extends string, O = {}, I extends Input = {}>(132 path: P,133 handler: H<E, MergePath<BasePath, P>, I, O>134- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>134+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>135 136 // app.get(path, handler, handler)137 <P extends string, O = {}, I extends Input = {}>(138 path: P,139 ...handlers: [H<E, MergePath<BasePath, P>, I, O>, H<E, MergePath<BasePath, P>, I, O>]140- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>140+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>141 142 // app.get(path, handler x3)143 <P extends string, O = {}, I extends Input = {}, I2 extends Input = I, I3 extends Input = I & I2>(144 path: P,1 unmodified line146 H<E, MergePath<BasePath, P>, I, O>,147 H<E, MergePath<BasePath, P>, I2, O>,148 H<E, MergePath<BasePath, P>, I3, O>149 ]150- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I3['in'], O>>, BasePath>150+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], O>, BasePath>151 152 // app.get(path, handler x4)153 <154 P extends string,9 unmodified lines164 H<E, MergePath<BasePath, P>, I2, O>,165 H<E, MergePath<BasePath, P>, I3, O>,166 H<E, MergePath<BasePath, P>, I4, O>167 ]168- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I4['in'], O>>, BasePath>168+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], O>, BasePath>169 170 // app.get(path, handler x5)171 <172 P extends string,11 unmodified lines184 H<E, MergePath<BasePath, P>, I3, O>,185 H<E, MergePath<BasePath, P>, I4, O>,186 H<E, MergePath<BasePath, P>, I5, O>187 ]188- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I5['in'], O>>, BasePath>188+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], O>, BasePath>189 190 // app.get(path, ...handlers[])191 <P extends string, I extends Input = {}, O = {}>(192 path: P,193 ...handlers: H<E, MergePath<BasePath, P>, I, O>[]194- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>194+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>195 }196 197 ////////////////////////////////////////198 ////// //////2 unmodified lines201 ////////////////////////////////////////202 203 export interface MiddlewareHandlerInterface<204 E extends Env = Env,205- S = {},205+ S extends Schema = {},206 BasePath extends string = '/'207 > {208 //// app.get(...handlers[])209 (...handlers: MiddlewareHandler<E, MergePath<BasePath, ExtractKey<S>>>[]): Hono<E, S, BasePath>10 unmodified lines220 ////// OnHandlerInterface //////221 ////// //////222 ////////////////////////////////////////223 224-export interface OnHandlerInterface<E extends Env = Env, S = {}, BasePath extends string = '/'> {224+export interface OnHandlerInterface<225+ E extends Env = Env,226+ S extends Schema = {},227+ BasePath extends string = '/'228+> {229 // app.on(method, path, handler, handler)230 <M extends string, P extends string, O = {}, I extends Input = {}>(231 method: M,232 path: P,233 ...handlers: [H<E, MergePath<BasePath, P>, I, O>, H<E, MergePath<BasePath, P>, I, O>]230- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>234+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>235 236 // app.get(method, path, handler x3)237 <238 M extends string,9 unmodified lines248 H<E, MergePath<BasePath, P>, I, O>,249 H<E, MergePath<BasePath, P>, I2, O>,250 H<E, MergePath<BasePath, P>, I3, O>251 ]248- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I3['in'], O>>, BasePath>252+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], O>, BasePath>253 254 // app.get(method, path, handler x4)255 <256 M extends string,11 unmodified lines268 H<E, MergePath<BasePath, P>, I2, O>,269 H<E, MergePath<BasePath, P>, I3, O>,270 H<E, MergePath<BasePath, P>, I4, O>271 ]268- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I4['in'], O>>, BasePath>272+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], O>, BasePath>273 274 // app.get(method, path, handler x5)275 <276 M extends string,13 unmodified lines290 H<E, MergePath<BasePath, P>, I3, O>,291 H<E, MergePath<BasePath, P>, I4, O>,292 H<E, MergePath<BasePath, P>, I5, O>293 ]290- ): Hono<E, S | Schema<M, MergePath<BasePath, P>, I5['in'], O>, BasePath>294+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], O>, BasePath>295 296 <M extends string, P extends string, O extends {} = {}, I extends Input = {}>(297 method: M,298 path: P,299 ...handlers: H<E, MergePath<BasePath, P>, I, O>[]296- ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I['in'], O>>, BasePath>300+ ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], O>, BasePath>301 302 // app.on(method[], path, ...handler)303 <P extends string, O extends {} = {}, I extends Input = {}>(304 methods: string[],305 path: P,306 ...handlers: H<E, MergePath<BasePath, P>, I, O>[]303- ): Hono<E, RemoveBlankRecord<S | Schema<string, MergePath<BasePath, P>, I['in'], O>>, BasePath>307+ ): Hono<E, S & ToSchema<string, MergePath<BasePath, P>, I['in'], O>, BasePath>308 }309 310 type ExtractKey<S> = S extends Record<infer Key, unknown>311 ? Key extends string2 unmodified lines314 : string315 316 ////////////////////////////////////////317 ////// //////314-////// Schema //////318+////// ToSchema //////319 ////// //////320 ////////////////////////////////////////321 318-export type Schema<M extends string, P extends string, I extends Input['in'], O> = {319- [K in P]: AddDollar<{320- [K2 in M]: {322+export type ToSchema<M extends string, P extends string, I extends Input['in'], O> = {323+ [K in P]: {324+ [K2 in M as AddDollar<string & K2>]: {325 input: unknown extends I ? AddParam<{}, P> : AddParam<I, P>326 output: unknown extends O ? {} : O327 }324- }>328+ }329 }330 331+export type Schema = {332+ [Path: string]: {333+ [Method: `$${Lowercase<string>}`]: {334+ input: Partial<ValidationTargets> & {335+ param?: Record<string, string>336+ }337+ output: {}338+ }339+ }340+}341+342+export type MergeSchemaPath<OrigSchema, SubPath extends string> = {343+ [K in keyof OrigSchema as `${SubPath}${K & string}`]: OrigSchema[K]344+}345+346 export type AddParam<I, P extends string> = ParamKeys<P> extends never347 ? I348 : I & { param: UnionToIntersection<ParamKeyToRecord<ParamKeys<P>>> }349 331-export type AddDollar<T> = T extends Record<infer K, infer R>332- ? K extends string333- ? { [MethodName in `$${Lowercase<K>}`]: R }334- : never335- : never350+type AddDollar<T extends string> = `$${Lowercase<T>}`351 337-export type MergeSchemaPath<S, P extends string> = S extends Record<infer Key, infer T>338- ? Key extends string339- ? Record<MergePath<P, Key>, T>340- : never341- : never342-352 export type MergePath<A extends string, B extends string> = A extends ''353 ? B354 : A extends '/'355 ? B102 unmodified lines