feat: Improve route() (#929)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: return a sub app cloning itself from route() * feat: Add extra type parameter BasePath. * refactor: The type of hono.basePath may simply be a string. * refactor: first argument of route is required, default value is not required * chore: denoify
authored by Taku Amano · committed by GitHub · parent 86afe03 · Browse files · 10 files·+248 added−86 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+248 added−86 removed
- tests:+24 added−0 removed
- other:+224 added−86 removed
Provenance
- Commit
b0501e0- Tree
43a0c71- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
86afe03
10 files changed+248 added−86 removed
modified:deno_dist/client/client.ts+1 added-1 removed
99 unmodified lines10010110210310410410510610710817 unmodified lines99 unmodified lines }}
// eslint-disable-next-line @typescript-eslint/no-explicit-anyexport const hc = <T extends Hono<any>>(baseUrl: string, options?: RequestOptions) =>export const hc = <T extends Hono<any, any, any>>(baseUrl: string, options?: RequestOptions) => createProxy(async (opts) => { const parts = [...opts.path]
let method = ''17 unmodified linesmodified:deno_dist/client/types.ts+1 added-1 removed
50 unmodified lines5152535455555657585914 unmodified lines50 unmodified lines > }
// eslint-disable-next-line @typescript-eslint/no-explicit-anyexport type Client<T> = T extends Hono<any, infer S>export type Client<T> = T extends Hono<any, infer S, any> ? S extends Record<infer K, Endpoint> ? K extends string ? PathToChain<K, S> : never14 unmodified linesmodified:deno_dist/hono.ts+33 added-13 removed
16 unmodified lines17 Next,18 NotFoundHandler,19 OnHandlerInterface,20 TypedResponse,21+ MergePath,22 MergeSchemaPath,23 RemoveBlankRecord,24 } from './types.ts'25 import { getPathFromURL, mergePath } from './utils/url.ts'6 unmodified lines32 handler: H33 }34 35 function defineDynamicClass(): {35- new <E extends Env = Env, S = {}>(): {36- [M in Methods]: HandlerInterface<E, M, S>36+ new <E extends Env = Env, S = {}, BasePath extends string = ''>(): {37+ [M in Methods]: HandlerInterface<E, M, S, BasePath>38 } & {38- on: OnHandlerInterface<E, S>39+ on: OnHandlerInterface<E, S, BasePath>40 } & {40- use: MiddlewareHandlerInterface<E, S>41+ use: MiddlewareHandlerInterface<E, S, BasePath>42 }43 } {44 return class {} as never45 }10 unmodified lines56 const message = 'Internal Server Error'57 return c.text(message, 500)58 }59 59-export class Hono<E extends Env = Env, S = {}> extends defineDynamicClass()<E, S> {60+export class Hono<61+ E extends Env = Env,62+ S = {},63+ BasePath extends string = ''64+> extends defineDynamicClass()<E, S, BasePath> {65 readonly router: Router<H> = new SmartRouter({66 routers: [new RegExpRouter(), new TrieRouter()],67 })68 readonly strict: boolean = true // strict routing - default is true64- private _tempPath: string = ''69+ private basePath: string = ''70 private path: string = '*'71 72 routes: RouterRoute[] = []73 47 unmodified lines121 122 Object.assign(this, init)123 }124 125+ private clone(): Hono<E, S, BasePath> {126+ const clone = new Hono<E, S, BasePath>({127+ router: this.router,128+ strict: this.strict,129+ })130+ clone.routes = this.routes131+ return clone132+ }133+134 private notFoundHandler: NotFoundHandler = notFoundHandler135 private errorHandler: ErrorHandler = errorHandler136 137 route<SubPath extends string, SubEnv extends Env, SubSchema>(138 path: SubPath,139 app?: Hono<SubEnv, SubSchema>126- ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>> {127- this._tempPath = path140+ ): Hono<141+ E,142+ RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>,143+ MergePath<BasePath, SubPath>144+ > {145+ const subApp = this.clone()146+ subApp.basePath = mergePath(this.basePath, path)147+148 if (app) {149 app.routes.map((r) => {150 const handler =151 app.errorHandler === errorHandler152 ? r.handler153 : async (c: Context, next: Next) =>154 (await compose<Context>([r.handler], app.errorHandler)(c, next)).res135- this.addRoute(r.method, r.path, handler)155+ subApp.addRoute(r.method, r.path, handler)156 })137- this._tempPath = ''157 }158+159 // eslint-disable-next-line @typescript-eslint/no-explicit-any140- return this as any160+ return subApp as any161 }162 163 onError(handler: ErrorHandler<E>) {164 this.errorHandler = handler15 unmodified lines180 }181 182 private addRoute(method: string, path: string, handler: H) {183 method = method.toUpperCase()164- if (this._tempPath) {165- path = mergePath(this._tempPath, path)184+ if (this.basePath) {185+ path = mergePath(this.basePath, path)186 }187 this.router.add(method, path, handler)188 const r: RouterRoute = { path: path, method: method, handler: handler }189 this.routes.push(r)106 unmodified linesmodified:deno_dist/types.ts+77 added-28 removed
55 unmodified lines56 ////// HandlerInterface //////57 ////// //////58 ////////////////////////////////////////59 60-export interface HandlerInterface<E extends Env = Env, M extends string = any, S = {}> {60+export interface HandlerInterface<61+ E extends Env = Env,62+ M extends string = any,63+ S = {},64+ BasePath extends string = ''65+> {66 //// app.get(...handlers[])67 68 // app.get(handler, handler)69 <I = {}, O = {}>(...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I, O>]): Hono<37 unmodified lines107 108 // app.get(path, handler, handler)109 <P extends string, O = {}, I = {}>(path: P, ...handlers: [H<E, P, I, O>, H<E, P, I, O>]): Hono<110 E,106- RemoveBlankRecord<S | Schema<M, P, I, O>>111+ RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>112 >113 114 // app.get(path, handler x3)115 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(116 path: P,112- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]117+ ...handlers: [118+ H<E, MergePath<BasePath, P>, I, O>,119+ H<E, MergePath<BasePath, P>, I2, O>,120+ H<E, MergePath<BasePath, P>, I3, O>121+ ]122 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>123 124 // app.get(path, handler x4)125 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(126 path: P,118- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]127+ ...handlers: [128+ H<E, MergePath<BasePath, P>, I, O>,129+ H<E, MergePath<BasePath, P>, I2, O>,130+ H<E, MergePath<BasePath, P>, I3, O>,131+ H<E, MergePath<BasePath, P>, I4, O>132+ ]133 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>134 135 // app.get(path, handler x5)136 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3, I5 = I3 & I4>(137 path: P,124- ...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>]138+ ...handlers: [139+ H<E, MergePath<BasePath, P>, I, O>,140+ H<E, MergePath<BasePath, P>, I2, O>,141+ H<E, MergePath<BasePath, P>, I3, O>,142+ H<E, MergePath<BasePath, P>, I4, O>,143+ H<E, MergePath<BasePath, P>, I5, O>144+ ]145 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I5, O>>>146 147 // app.get(path, ...handlers[])128- <P extends string, I = {}, O = {}>(path: P, ...handlers: H<E, P, I, O>[]): Hono<129- E,130- RemoveBlankRecord<S | Schema<M, P, I, O>>131- >148+ <P extends string, I = {}, O = {}>(149+ path: P,150+ ...handlers: H<E, MergePath<BasePath, P>, I, O>[]151+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>>152 }153 154 ////////////////////////////////////////155 ////// //////156 ////// MiddlewareHandlerInterface //////157 ////// //////158 ////////////////////////////////////////159 140-export interface MiddlewareHandlerInterface<E extends Env = Env, S = {}> {160+export interface MiddlewareHandlerInterface<161+ E extends Env = Env,162+ S = {},163+ BasePath extends string = ''164+> {165 //// app.get(...handlers[])142- (...handlers: MiddlewareHandler<E, ExtractKey<S>>[]): Hono<E, S>166+ (...handlers: MiddlewareHandler<E, MergePath<BasePath, ExtractKey<S>>>[]): Hono<E, S, BasePath>167 //// app.get(path, ...handlers[])144- <P extends string>(path: P, ...handlers: MiddlewareHandler<E, P>[]): Hono<E, S>168+ <P extends string>(path: P, ...handlers: MiddlewareHandler<E, MergePath<BasePath, P>>[]): Hono<169+ E,170+ S,171+ BasePath172+ >173 }174 175 ////////////////////////////////////////176 ////// //////177 ////// OnHandlerInterface //////178 ////// //////179 ////////////////////////////////////////180 153-export interface OnHandlerInterface<E extends Env = Env, S = {}> {181+export interface OnHandlerInterface<E extends Env = Env, S = {}, BasePath extends string = ''> {182 // app.on(method, path, handler, handler)183 <M extends string, P extends string, O = {}, I = {}>(184 method: M,185 path: P,158- ...handlers: [H<E, P, I, O>, H<E, P, I, O>]159- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>186+ ...handlers: [H<E, MergePath<BasePath, P>, I, O>, H<E, MergePath<BasePath, P>, I, O>]187+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>, BasePath>188 189 // app.get(method, path, handler x3)190 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(191 method: M,192 path: P,165- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]166- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>193+ ...handlers: [194+ H<E, MergePath<BasePath, P>, I, O>,195+ H<E, MergePath<BasePath, P>, I2, O>,196+ H<E, MergePath<BasePath, P>, I3, O>197+ ]198+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I3, O>>, BasePath>199 200 // app.get(method, path, handler x4)201 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(202 method: M,203 path: P,172- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]173- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>204+ ...handlers: [205+ H<E, MergePath<BasePath, P>, I, O>,206+ H<E, MergePath<BasePath, P>, I2, O>,207+ H<E, MergePath<BasePath, P>, I3, O>,208+ H<E, MergePath<BasePath, P>, I4, O>209+ ]210+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I4, O>>, BasePath>211 212 // app.get(method, path, handler x5)213 <214 M extends string,6 unmodified lines221 I5 = I3 & I4222 >(223 method: M,224 path: P,188- ...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>]189- ): Hono<E, S | Schema<M, P, I5, O>>225+ ...handlers: [226+ H<E, MergePath<BasePath, P>, I, O>,227+ H<E, MergePath<BasePath, P>, I2, O>,228+ H<E, MergePath<BasePath, P>, I3, O>,229+ H<E, MergePath<BasePath, P>, I4, O>,230+ H<E, MergePath<BasePath, P>, I5, O>231+ ]232+ ): Hono<E, S | Schema<M, MergePath<BasePath, P>, I5, O>, BasePath>233 234 <M extends string, P extends string, O extends {} = {}, I = {}>(235 method: M,236 path: P,194- ...handlers: H<E, P, I, O>[]195- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>237+ ...handlers: H<E, MergePath<BasePath, P>, I, O>[]238+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>, BasePath>239 240 // app.on(method[], path, ...handler)241 <P extends string, O extends {} = {}, I = {}>(242 methods: string[],243 path: P,201- ...handlers: H<E, P, I, O>[]202- ): Hono<E, RemoveBlankRecord<S | Schema<string, P, I, O>>>244+ ...handlers: H<E, MergePath<BasePath, P>, I, O>[]245+ ): Hono<E, RemoveBlankRecord<S | Schema<string, MergePath<BasePath, P>, I, O>>, BasePath>246 }247 248 type ExtractKey<S> = S extends Record<infer Key, unknown>249 ? Key extends string26 unmodified lines276 ? Record<MergePath<P, Key>, T>277 : never278 : never279 237-export type MergePath<A extends string, B extends string> = A extends `${infer P}/`238- ? `${P}${B}`239- : `${A}${B}`280+export type MergePath<A extends string, B extends string> = A extends ''281+ ? B282+ : A extends `${infer P}/`283+ ? B extends `/${infer Q}`284+ ? `${P}/${Q}`285+ : `${P}/${B}`286+ : B extends `/${infer Q}`287+ ? `${A}/${Q}`288+ : `${A}/${B}`289 290 ////////////////////////////////////////291 ////// //////292 ////// TypedResponse //////77 unmodified linesmodified:src/client/client.test.ts+11 added
319 unmodified lines320 type verify = Expect<Equal<boolean, typeof data.ok>>321 expect(data.ok).toBe(true)322 })323 324+ it('Should have correct types - route() then get()', async () => {325+ const base = new Hono<Env>().route('/api')326+ const app = base.get('/search', (c) => c.jsonT({ ok: true }))327+ type AppType = typeof app328+ const client = hc<AppType>('http://localhost')329+ const res = await client.api.search.$get()330+ const data = await res.json()331+ type verify = Expect<Equal<boolean, typeof data.ok>>332+ expect(data.ok).toBe(true)333+ })334+335 describe('Multiple endpoints', () => {336 const api = new Hono()337 .get('/foo', (c) => c.jsonT({ foo: '' }))338 .post('/bar', (c) => c.jsonT({ bar: 0 }))44 unmodified linesmodified:src/client/client.ts+1 added-1 removed
99 unmodified lines100 }101 }102 103 // eslint-disable-next-line @typescript-eslint/no-explicit-any104-export const hc = <T extends Hono<any>>(baseUrl: string, options?: RequestOptions) =>104+export const hc = <T extends Hono<any, any, any>>(baseUrl: string, options?: RequestOptions) =>105 createProxy(async (opts) => {106 const parts = [...opts.path]107 108 let method = ''17 unmodified linesmodified:src/client/types.ts+1 added-1 removed
50 unmodified lines51 >52 }53 54 // eslint-disable-next-line @typescript-eslint/no-explicit-any55-export type Client<T> = T extends Hono<any, infer S>55+export type Client<T> = T extends Hono<any, infer S, any>56 ? S extends Record<infer K, Endpoint>57 ? K extends string58 ? PathToChain<K, S>59 : never14 unmodified linesmodified:src/hono.test.ts+13 added
174 unmodified lines175 const user = app.route('/user')176 user.get('/login', (c) => c.text('get /user/login'))177 user.post('/register', (c) => c.text('post /user/register'))178 179+ const appForEachUser = user.route(':id')180+ appForEachUser.get('/profile', (c) => c.text('get /user/' + c.req.param('id') + '/profile'))181+182+ app.get('/add-path-after-route-call', (c) => c.text('get /add-path-after-route-call'))183+184 let res = await app.request('http://localhost/book', { method: 'GET' })185 expect(res.status).toBe(200)186 expect(await res.text()).toBe('get /book')187 14 unmodified lines202 203 res = await app.request('http://localhost/user/register', { method: 'POST' })204 expect(res.status).toBe(200)205 expect(await res.text()).toBe('post /user/register')206+207+ res = await app.request('http://localhost/user/123/profile', { method: 'GET' })208+ expect(res.status).toBe(200)209+ expect(await res.text()).toBe('get /user/123/profile')210+211+ res = await app.request('http://localhost/add-path-after-route-call', { method: 'GET' })212+ expect(res.status).toBe(200)213+ expect(await res.text()).toBe('get /add-path-after-route-call')214 })215 216 describe('Nested route with middleware', () => {217 const api = new Hono()1520 unmodified linesmodified:src/hono.ts+33 added-13 removed
16 unmodified lines17 Next,18 NotFoundHandler,19 OnHandlerInterface,20 TypedResponse,21+ MergePath,22 MergeSchemaPath,23 RemoveBlankRecord,24 } from './types'25 import { getPathFromURL, mergePath } from './utils/url'6 unmodified lines32 handler: H33 }34 35 function defineDynamicClass(): {35- new <E extends Env = Env, S = {}>(): {36- [M in Methods]: HandlerInterface<E, M, S>36+ new <E extends Env = Env, S = {}, BasePath extends string = ''>(): {37+ [M in Methods]: HandlerInterface<E, M, S, BasePath>38 } & {38- on: OnHandlerInterface<E, S>39+ on: OnHandlerInterface<E, S, BasePath>40 } & {40- use: MiddlewareHandlerInterface<E, S>41+ use: MiddlewareHandlerInterface<E, S, BasePath>42 }43 } {44 return class {} as never45 }10 unmodified lines56 const message = 'Internal Server Error'57 return c.text(message, 500)58 }59 59-export class Hono<E extends Env = Env, S = {}> extends defineDynamicClass()<E, S> {60+export class Hono<61+ E extends Env = Env,62+ S = {},63+ BasePath extends string = ''64+> extends defineDynamicClass()<E, S, BasePath> {65 readonly router: Router<H> = new SmartRouter({66 routers: [new RegExpRouter(), new TrieRouter()],67 })68 readonly strict: boolean = true // strict routing - default is true64- private _tempPath: string = ''69+ private basePath: string = ''70 private path: string = '*'71 72 routes: RouterRoute[] = []73 47 unmodified lines121 122 Object.assign(this, init)123 }124 125+ private clone(): Hono<E, S, BasePath> {126+ const clone = new Hono<E, S, BasePath>({127+ router: this.router,128+ strict: this.strict,129+ })130+ clone.routes = this.routes131+ return clone132+ }133+134 private notFoundHandler: NotFoundHandler = notFoundHandler135 private errorHandler: ErrorHandler = errorHandler136 137 route<SubPath extends string, SubEnv extends Env, SubSchema>(138 path: SubPath,139 app?: Hono<SubEnv, SubSchema>126- ): Hono<E, RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>> {127- this._tempPath = path140+ ): Hono<141+ E,142+ RemoveBlankRecord<MergeSchemaPath<SubSchema, SubPath> | S>,143+ MergePath<BasePath, SubPath>144+ > {145+ const subApp = this.clone()146+ subApp.basePath = mergePath(this.basePath, path)147+148 if (app) {149 app.routes.map((r) => {150 const handler =151 app.errorHandler === errorHandler152 ? r.handler153 : async (c: Context, next: Next) =>154 (await compose<Context>([r.handler], app.errorHandler)(c, next)).res135- this.addRoute(r.method, r.path, handler)155+ subApp.addRoute(r.method, r.path, handler)156 })137- this._tempPath = ''157 }158+159 // eslint-disable-next-line @typescript-eslint/no-explicit-any140- return this as any160+ return subApp as any161 }162 163 onError(handler: ErrorHandler<E>) {164 this.errorHandler = handler15 unmodified lines180 }181 182 private addRoute(method: string, path: string, handler: H) {183 method = method.toUpperCase()164- if (this._tempPath) {165- path = mergePath(this._tempPath, path)184+ if (this.basePath) {185+ path = mergePath(this.basePath, path)186 }187 this.router.add(method, path, handler)188 const r: RouterRoute = { path: path, method: method, handler: handler }189 this.routes.push(r)106 unmodified linesmodified:src/types.ts+77 added-28 removed
55 unmodified lines56 ////// HandlerInterface //////57 ////// //////58 ////////////////////////////////////////59 60-export interface HandlerInterface<E extends Env = Env, M extends string = any, S = {}> {60+export interface HandlerInterface<61+ E extends Env = Env,62+ M extends string = any,63+ S = {},64+ BasePath extends string = ''65+> {66 //// app.get(...handlers[])67 68 // app.get(handler, handler)69 <I = {}, O = {}>(...handlers: [H<E, ExtractKey<S>, I, O>, H<E, ExtractKey<S>, I, O>]): Hono<37 unmodified lines107 108 // app.get(path, handler, handler)109 <P extends string, O = {}, I = {}>(path: P, ...handlers: [H<E, P, I, O>, H<E, P, I, O>]): Hono<110 E,106- RemoveBlankRecord<S | Schema<M, P, I, O>>111+ RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>112 >113 114 // app.get(path, handler x3)115 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(116 path: P,112- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]117+ ...handlers: [118+ H<E, MergePath<BasePath, P>, I, O>,119+ H<E, MergePath<BasePath, P>, I2, O>,120+ H<E, MergePath<BasePath, P>, I3, O>121+ ]122 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>123 124 // app.get(path, handler x4)125 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(126 path: P,118- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]127+ ...handlers: [128+ H<E, MergePath<BasePath, P>, I, O>,129+ H<E, MergePath<BasePath, P>, I2, O>,130+ H<E, MergePath<BasePath, P>, I3, O>,131+ H<E, MergePath<BasePath, P>, I4, O>132+ ]133 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>134 135 // app.get(path, handler x5)136 <P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3, I5 = I3 & I4>(137 path: P,124- ...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>]138+ ...handlers: [139+ H<E, MergePath<BasePath, P>, I, O>,140+ H<E, MergePath<BasePath, P>, I2, O>,141+ H<E, MergePath<BasePath, P>, I3, O>,142+ H<E, MergePath<BasePath, P>, I4, O>,143+ H<E, MergePath<BasePath, P>, I5, O>144+ ]145 ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I5, O>>>146 147 // app.get(path, ...handlers[])128- <P extends string, I = {}, O = {}>(path: P, ...handlers: H<E, P, I, O>[]): Hono<129- E,130- RemoveBlankRecord<S | Schema<M, P, I, O>>131- >148+ <P extends string, I = {}, O = {}>(149+ path: P,150+ ...handlers: H<E, MergePath<BasePath, P>, I, O>[]151+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>>152 }153 154 ////////////////////////////////////////155 ////// //////156 ////// MiddlewareHandlerInterface //////157 ////// //////158 ////////////////////////////////////////159 140-export interface MiddlewareHandlerInterface<E extends Env = Env, S = {}> {160+export interface MiddlewareHandlerInterface<161+ E extends Env = Env,162+ S = {},163+ BasePath extends string = ''164+> {165 //// app.get(...handlers[])142- (...handlers: MiddlewareHandler<E, ExtractKey<S>>[]): Hono<E, S>166+ (...handlers: MiddlewareHandler<E, MergePath<BasePath, ExtractKey<S>>>[]): Hono<E, S, BasePath>167 //// app.get(path, ...handlers[])144- <P extends string>(path: P, ...handlers: MiddlewareHandler<E, P>[]): Hono<E, S>168+ <P extends string>(path: P, ...handlers: MiddlewareHandler<E, MergePath<BasePath, P>>[]): Hono<169+ E,170+ S,171+ BasePath172+ >173 }174 175 ////////////////////////////////////////176 ////// //////177 ////// OnHandlerInterface //////178 ////// //////179 ////////////////////////////////////////180 153-export interface OnHandlerInterface<E extends Env = Env, S = {}> {181+export interface OnHandlerInterface<E extends Env = Env, S = {}, BasePath extends string = ''> {182 // app.on(method, path, handler, handler)183 <M extends string, P extends string, O = {}, I = {}>(184 method: M,185 path: P,158- ...handlers: [H<E, P, I, O>, H<E, P, I, O>]159- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>186+ ...handlers: [H<E, MergePath<BasePath, P>, I, O>, H<E, MergePath<BasePath, P>, I, O>]187+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>, BasePath>188 189 // app.get(method, path, handler x3)190 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2>(191 method: M,192 path: P,165- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>]166- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I3, O>>>193+ ...handlers: [194+ H<E, MergePath<BasePath, P>, I, O>,195+ H<E, MergePath<BasePath, P>, I2, O>,196+ H<E, MergePath<BasePath, P>, I3, O>197+ ]198+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I3, O>>, BasePath>199 200 // app.get(method, path, handler x4)201 <M extends string, P extends string, O = {}, I = {}, I2 = I, I3 = I & I2, I4 = I2 & I3>(202 method: M,203 path: P,172- ...handlers: [H<E, P, I, O>, H<E, P, I2, O>, H<E, P, I3, O>, H<E, P, I4, O>]173- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I4, O>>>204+ ...handlers: [205+ H<E, MergePath<BasePath, P>, I, O>,206+ H<E, MergePath<BasePath, P>, I2, O>,207+ H<E, MergePath<BasePath, P>, I3, O>,208+ H<E, MergePath<BasePath, P>, I4, O>209+ ]210+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I4, O>>, BasePath>211 212 // app.get(method, path, handler x5)213 <214 M extends string,6 unmodified lines221 I5 = I3 & I4222 >(223 method: M,224 path: P,188- ...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>]189- ): Hono<E, S | Schema<M, P, I5, O>>225+ ...handlers: [226+ H<E, MergePath<BasePath, P>, I, O>,227+ H<E, MergePath<BasePath, P>, I2, O>,228+ H<E, MergePath<BasePath, P>, I3, O>,229+ H<E, MergePath<BasePath, P>, I4, O>,230+ H<E, MergePath<BasePath, P>, I5, O>231+ ]232+ ): Hono<E, S | Schema<M, MergePath<BasePath, P>, I5, O>, BasePath>233 234 <M extends string, P extends string, O extends {} = {}, I = {}>(235 method: M,236 path: P,194- ...handlers: H<E, P, I, O>[]195- ): Hono<E, RemoveBlankRecord<S | Schema<M, P, I, O>>>237+ ...handlers: H<E, MergePath<BasePath, P>, I, O>[]238+ ): Hono<E, RemoveBlankRecord<S | Schema<M, MergePath<BasePath, P>, I, O>>, BasePath>239 240 // app.on(method[], path, ...handler)241 <P extends string, O extends {} = {}, I = {}>(242 methods: string[],243 path: P,201- ...handlers: H<E, P, I, O>[]202- ): Hono<E, RemoveBlankRecord<S | Schema<string, P, I, O>>>244+ ...handlers: H<E, MergePath<BasePath, P>, I, O>[]245+ ): Hono<E, RemoveBlankRecord<S | Schema<string, MergePath<BasePath, P>, I, O>>, BasePath>246 }247 248 type ExtractKey<S> = S extends Record<infer Key, unknown>249 ? Key extends string26 unmodified lines276 ? Record<MergePath<P, Key>, T>277 : never278 : never279 237-export type MergePath<A extends string, B extends string> = A extends `${infer P}/`238- ? `${P}${B}`239- : `${A}${B}`280+export type MergePath<A extends string, B extends string> = A extends ''281+ ? B282+ : A extends `${infer P}/`283+ ? B extends `/${infer Q}`284+ ? `${P}/${Q}`285+ : `${P}/${B}`286+ : B extends `/${infer Q}`287+ ? `${A}/${Q}`288+ : `${A}/${B}`289 290 ////////////////////////////////////////291 ////// //////292 ////// TypedResponse //////77 unmodified lines