Skip to content
Myr

fix(type): change the type definition of `req` in Context (#658)

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

authored by Yusuke Wada · committed by GitHub · parent b82547f · Browse files · 7 files+23 added−43 removed
This commit

State

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

Diff stats

7 files+23 added−43 removed

  • other:+23 added−43 removed

7 files+23 added−43 removed

modified:deno_dist/utils/encode.ts+1 added-1 removed
modified:deno_dist/context.ts+3 added-3 removed
10 unmodified lines
1112export class Context<13 P extends string = string,14 E extends Partial<Environment> = Environment,15 S extends Partial<Schema> | unknown = Schema15 S extends Partial<Schema> = Schema16> {17 req: Request<P, S extends Schema ? SchemaToProp<S> : any>17 req: Request<P, S extends Schema ? SchemaToProp<S> : S>18 env: E['Bindings']19 finalized: boolean20 error: Error | undefined = undefined21
12 unmodified lines
34 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,35 notFoundHandler: NotFoundHandler<E> = () => new Response()36 ) {37 this._executionCtx = executionCtx38 this.req = req as Request<P, S extends Schema ? SchemaToProp<S> : any>38 this.req = req as Request<P, S extends Schema ? SchemaToProp<S> : S>39 this.env = env || ({} as Bindings)4041 this.notFoundHandler = notFoundHandler42 this.finalized = false
185 unmodified lines
modified:deno_dist/hono.ts+7 added-7 removed
112 unmodified lines
113 route(path: string, app?: Hono<any>) {114 this._tempPath = path115 if (app) {116 app.routes.map((r) => {117 this.addRoute(r.method, r.path, r.handler as unknown as Handler<P, E>)117 this.addRoute(r.method, r.path, r.handler as unknown as Handler<P, E, S>)118 })119 this._tempPath = ''120 }121 return this
59 unmodified lines
181 private matchRoute(method: string, path: string) {182 return this.router.match(method, path)183 }184185 private handleError(err: unknown, c: Context<string, E, S>) {185 private handleError(err: unknown, c: Context<string, E>) {186 if (err instanceof Error) {187 return this.errorHandler(err, c)188 }189 throw err
18 unmodified lines
208 let res: ReturnType<Handler<P>>209210 try {211 res = handler(c, async () => {})212 if (!res) return this.notFoundHandler(c)212 if (!res) return this.notFoundHandler(c as Context)213 } catch (err) {214 return this.handleError(err, c)214 return this.handleError(err, c as Context)215 }216217 if (res instanceof Response) return res218
1 unmodified line
220 let awaited: Response | undefined | void221 try {222 awaited = await res223 if (!awaited) {224 return this.notFoundHandler(c)224 return this.notFoundHandler(c as Context)225 }226 } catch (err) {227 return this.handleError(err, c)227 return this.handleError(err, c as Context)228 }229 return awaited230 })()231 }
11 unmodified lines
243 )244 }245 return context.res246 } catch (err) {247 return this.handleError(err, c)247 return this.handleError(err, c as Context)248 }249 })()250 }251
13 unmodified lines
modified:deno_dist/types.ts+1 added-11 removed
modified:src/context.ts+3 added-3 removed
10 unmodified lines
1112export class Context<13 P extends string = string,14 E extends Partial<Environment> = Environment,15 S extends Partial<Schema> | unknown = Schema15 S extends Partial<Schema> = Schema16> {17 req: Request<P, S extends Schema ? SchemaToProp<S> : any>17 req: Request<P, S extends Schema ? SchemaToProp<S> : S>18 env: E['Bindings']19 finalized: boolean20 error: Error | undefined = undefined21
12 unmodified lines
34 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,35 notFoundHandler: NotFoundHandler<E> = () => new Response()36 ) {37 this._executionCtx = executionCtx38 this.req = req as Request<P, S extends Schema ? SchemaToProp<S> : any>38 this.req = req as Request<P, S extends Schema ? SchemaToProp<S> : S>39 this.env = env || ({} as Bindings)4041 this.notFoundHandler = notFoundHandler42 this.finalized = false
185 unmodified lines
modified:src/hono.ts+7 added-7 removed
112 unmodified lines
113 route(path: string, app?: Hono<any>) {114 this._tempPath = path115 if (app) {116 app.routes.map((r) => {117 this.addRoute(r.method, r.path, r.handler as unknown as Handler<P, E>)117 this.addRoute(r.method, r.path, r.handler as unknown as Handler<P, E, S>)118 })119 this._tempPath = ''120 }121 return this
59 unmodified lines
181 private matchRoute(method: string, path: string) {182 return this.router.match(method, path)183 }184185 private handleError(err: unknown, c: Context<string, E, S>) {185 private handleError(err: unknown, c: Context<string, E>) {186 if (err instanceof Error) {187 return this.errorHandler(err, c)188 }189 throw err
18 unmodified lines
208 let res: ReturnType<Handler<P>>209210 try {211 res = handler(c, async () => {})212 if (!res) return this.notFoundHandler(c)212 if (!res) return this.notFoundHandler(c as Context)213 } catch (err) {214 return this.handleError(err, c)214 return this.handleError(err, c as Context)215 }216217 if (res instanceof Response) return res218
1 unmodified line
220 let awaited: Response | undefined | void221 try {222 awaited = await res223 if (!awaited) {224 return this.notFoundHandler(c)224 return this.notFoundHandler(c as Context)225 }226 } catch (err) {227 return this.handleError(err, c)227 return this.handleError(err, c as Context)228 }229 return awaited230 })()231 }
11 unmodified lines
243 )244 }245 return context.res246 } catch (err) {247 return this.handleError(err, c)247 return this.handleError(err, c as Context)248 }249 })()250 }251
13 unmodified lines
modified:src/types.ts+1 added-11 removed
71 unmodified lines
72 : E73 : E extends Partial<Schema>74 ? Partial<Environment>75 : Environment,76 P extends Partial<Schema>77 ? P78 : E extends Partial<Schema>79 ? P extends Partial<Environment>80 ? E extends Partial<Environment>81 ? unknown82 : E83 : unknown84 : S extends Partial<Schema>85 ? S86 : never76 S extends Schema ? S : P extends Schema ? P : E extends Schema ? E : any77 >,78 next: Next79 ): Response | Promise<Response | undefined | void>80}