refactor(context): `c.res` is not optional (#255)
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 7a13160 · Browse files · 6 files·+27 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+27 added−7 removed
- tests:+0 added−1 removed
- other:+27 added−6 removed
Provenance
- Commit
563aeec- Tree
ce7753a- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
7a13160
6 files changed+27 added−7 removed
modified:src/middleware/serve-static/serve-static.ts+1 added-1 removed
13 unmodified lines1415161718181920212224 unmodified lines13 unmodified lines// This middleware is available only on Cloudflare Workers.export const serveStatic = (options: ServeStaticOptions = { root: '' }): Handler => { return async (c: Context, next: Next) => { // Do nothing if Response is already set if (c.res) { if (c.res && c.res.finalized) { await next() }
const url = new URL(c.req.url)24 unmodified linesmodified:src/compose.ts+4 added-1 removed
17 unmodified lines181920212222232425262728293031323334353637383940414243444546475 unmodified lines17 unmodified lines index = i if (i === middleware.length) handler = next
if (handler === undefined) { if (context instanceof Context && context.res === undefined) { if (context instanceof Context && context.res.finalized === false) { context.res = onNotFound(context) context.res.finalized = true } return Promise.resolve(context) }
return Promise.resolve(handler(context, dispatch.bind(null, i + 1))) .then(async (res: Response) => { // If handler return Response like `return c.text('foo')` if (res && context instanceof Context) { context.res = res context.res.finalized = true dispatch(i + 1) // <--- Call next() } return context }) .catch((err) => { if (onError && context instanceof Context) { if (err instanceof Error) { context.res = onError(err, context) context.res.finalized = true } return context } else { throw err5 unmodified linesmodified:src/context.test.ts-1 removed
129 unmodified lines130 const ctx = new Context(req, {131 env: {132 API_KEY: key,133 },134- res: null,134 event: null,135 })136 expect(ctx.env.API_KEY).toBe(key)137 })11 unmodified linesmodified:src/context.ts+8 added-2 removed
7 unmodified lines8 export type Env = Record<string, any>9 10 export class Context<RequestParamKeyType extends string = string, E = Env> {11 req: Request<RequestParamKeyType>12- res: Response = undefined12+ res: Response13 env: E14 event: FetchEvent15 16 private _headers: Headers = {}9 unmodified lines26 notFound: () => Response | Promise<Response>27 28 constructor(29 req: Request<RequestParamKeyType>,30- opts?: { res: Response; env: E; event: FetchEvent }30+ opts?: { env: E; event: FetchEvent; res?: Response }31 ) {32 this.req = this.initRequest<RequestParamKeyType>(req)33 this._map = {}34 Object.assign(this, opts)35+36+ if (!this.res) {37+ const res = new Response(null, { status: 404 })38+ res.finalized = false39+ this.res = res40+ }41 }42 43 private initRequest<T extends string>(req: Request<T>): Request<T> {44 req.header = ((name?: string): string | Record<string, string> => {115 unmodified linesmodified:src/hono.ts+13 added-1 removed
20 unmodified lines21 (name: string): string22 (): Record<string, string>23 }24 }25+ interface Response {26+ finalized: boolean27+ }28 }29 30 export type Handler<RequestParamKeyType extends string = string, E = Env> = (31 c: Context<RequestParamKeyType, E>,53 unmodified lines85 private _router: Router<Handler<string, E>>86 private _tempPath: string87 private path: string = '/'88 89+ private _cacheResponse: Response90+91 routes: Route<E>[] = []92 93 constructor(init: Partial<Pick<Hono, 'routerClass' | 'strict'>> = {}) {94 super()21 unmodified lines116 Object.assign(this, init)117 118 this._router = new this.routerClass()119 this._tempPath = null120+121+ this._cacheResponse = new Response(null, { status: 404 })122+ this._cacheResponse.finalized = false123 }124 125 private notFoundHandler: NotFoundHandler = (c: Context) => {126 const message = '404 Not Found'71 unmodified lines198 }199 }) as typeof request.param200 const handlers = result ? result.handlers : [this.notFoundHandler]201 194- const c = new Context<string, E>(request, { env: env, event: event, res: undefined })202+ const c = new Context<string, E>(request, {203+ env: env,204+ event: event,205+ res: this._cacheResponse,206+ })207 c.notFound = () => this.notFoundHandler(c)208 209 const composed = compose<Context>(handlers, this.errorHandler, this.notFoundHandler)210 let context: Context32 unmodified linesmodified:package.json+1 added-1 removed
9 unmodified lines10 "scripts": {11 "test": "jest",12 "lint": "eslint --ext js,ts src .eslintrc.js",13 "lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",14- "build": "rimraf dist && tsc --project tsconfig.build.json && tsc --project tsconfig.build.esm.json",14+ "build": "rimraf dist && tsc --project tsconfig.build.esm.json && tsc --project tsconfig.build.json",15 "watch": "tsc --project tsconfig.build.json -w",16 "prepublishOnly": "yarn build"17 },18 "exports": {129 unmodified lines