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
Provenance
- Commit
1b68195- Tree
3c15df1- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
9db4f6f
2 files changed+38 added−6 removed
modified:src/context.ts+3 added-3 removed
4 unmodified lines567891091011121314151617181920202122232498 unmodified lines4 unmodified linestype Data = string | ArrayBuffer | ReadableStream
export interface Env {}
export class Context { req: Requestexport class Context<RequestParamKeyType = string> { req: Request<RequestParamKeyType> res: Response env: Env event: FetchEvent private _headers: Headers private _status: number private _statusText: string
render: (template: string, params?: object, options?: object) => Promise<Response>
constructor(req: Request, opts?: { res: Response; env: Env; event: FetchEvent }) { constructor(req: Request<RequestParamKeyType>, opts?: { res: Response; env: Env; event: FetchEvent }) { this.req = req if (opts) { this.res = opts.res this.env = opts.env98 unmodified linesmodified:src/hono.ts+35 added-3 removed
6 unmodified lines7 8 const METHOD_NAME_OF_ALL = 'ALL'9 10 declare 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 }18 19-export type Handler = (c: Context, next?: Function) => Response | Promise<Response>19+export type Handler<RequestParamKeyType = string> = (20+ c: Context<RequestParamKeyType>,21+ next?: Function22+) => Response | Promise<Response>23 export type MiddlewareHandler = (c: Context, next: Function) => Promise<void>24 25+// eslint-disable-next-line @typescript-eslint/no-unused-vars26+type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}`27+ ? Name28+ : NameWithPattern29+30+type ParamKey<Component> = Component extends `:${infer NameWithPattern}`31+ ? ParamKeyName<NameWithPattern>32+ : never33+34+type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}`35+ ? ParamKey<Component> | ParamKeys<Rest>36+ : ParamKey<Path>37+38 export class Router<T> {39 node: Node<T>40 41 constructor() {20 unmodified lines62 this.tempPath = '/'63 }64 65 /* 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 lines115 search116 connect117 */118 119+ 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