Skip to content
Myr

feat: add type to c.req.param key. (#102)

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

* Added type to c.req.param key. * Also, added type to "all".

authored by Taku Amano · committed by GitHub · parent 9db4f6f · Browse files · 2 files+38 added−6 removed
This commit

State

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

Diff stats

2 files+38 added−6 removed

  • other:+38 added−6 removed

2 files+38 added−6 removed

modified:src/context.ts+3 added-3 removed
modified:src/hono.ts+35 added-3 removed
6 unmodified lines
78const METHOD_NAME_OF_ALL = 'ALL'910declare global {11 interface Request {12 param: (key: string) => string11 interface Request<ParamKeyType = string> {12 param: (key: ParamKeyType) => string13 query: (key: string) => string14 header: (name: string) => string15 parsedBody: any16 }17}1819export type Handler = (c: Context, next?: Function) => Response | Promise<Response>19export type Handler<RequestParamKeyType = string> = (20 c: Context<RequestParamKeyType>,21 next?: Function22) => Response | Promise<Response>23export type MiddlewareHandler = (c: Context, next: Function) => Promise<void>2425// eslint-disable-next-line @typescript-eslint/no-unused-vars26type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}`27 ? Name28 : NameWithPattern2930type ParamKey<Component> = Component extends `:${infer NameWithPattern}`31 ? ParamKeyName<NameWithPattern>32 : never3334type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}`35 ? ParamKey<Component> | ParamKeys<Rest>36 : ParamKey<Path>3738export class Router<T> {39 node: Node<T>4041 constructor() {
20 unmodified lines
62 this.tempPath = '/'63 }6465 /* HTTP METHODS */66 get<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;67 get(arg: Handler<never>, ...args: Handler<never>[]): Hono;68 get(arg: string | Handler, ...args: Handler[]): Hono {69 return this.addRoute('get', arg, ...args)70 }71 post<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;72 post(arg: Handler, ...args: Handler[]): Hono;73 post(arg: string | Handler, ...args: Handler[]): Hono {74 return this.addRoute('post', arg, ...args)75 }76 put<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;77 put(arg: Handler, ...args: Handler[]): Hono;78 put(arg: string | Handler, ...args: Handler[]): Hono {79 return this.addRoute('put', arg, ...args)80 }81 head<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;82 head(arg: Handler, ...args: Handler[]): Hono;83 head(arg: string | Handler, ...args: Handler[]): Hono {84 return this.addRoute('head', arg, ...args)85 }86 delete<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;87 delete(arg: Handler, ...args: Handler[]): Hono;88 delete(arg: string | Handler, ...args: Handler[]): Hono {89 return this.addRoute('delete', arg, ...args)90 }91 options<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;92 options(arg: Handler, ...args: Handler[]): Hono;93 options(arg: string | Handler, ...args: Handler[]): Hono {94 return this.addRoute('options', arg, ...args)95 }96 patch<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;97 patch(arg: Handler, ...args: Handler[]): Hono;98 patch(arg: string | Handler, ...args: Handler[]): Hono {99 return this.addRoute('patch', arg, ...args)100 }101
13 unmodified lines
115 search116 connect117 */118119 all<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;120 all(arg: Handler<never>, ...args: Handler<never>[]): Hono;121 all(arg: string | Handler, ...args: Handler[]): Hono {122 return this.addRoute('all', arg, ...args)123 }124
119 unmodified lines