Skip to content
Myr

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

6 files+27 added−7 removed

modified:src/middleware/serve-static/serve-static.ts+1 added-1 removed
modified:src/compose.ts+4 added-1 removed
modified:src/context.test.ts-1 removed
129 unmodified lines
130 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 lines
modified:src/context.ts+8 added-2 removed
7 unmodified lines
8export type Env = Record<string, any>910export class Context<RequestParamKeyType extends string = string, E = Env> {11 req: Request<RequestParamKeyType>12 res: Response = undefined12 res: Response13 env: E14 event: FetchEvent1516 private _headers: Headers = {}
9 unmodified lines
26 notFound: () => Response | Promise<Response>2728 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)3536 if (!this.res) {37 const res = new Response(null, { status: 404 })38 res.finalized = false39 this.res = res40 }41 }4243 private initRequest<T extends string>(req: Request<T>): Request<T> {44 req.header = ((name?: string): string | Record<string, string> => {
115 unmodified lines
modified:src/hono.ts+13 added-1 removed
20 unmodified lines
21 (name: string): string22 (): Record<string, string>23 }24 }25 interface Response {26 finalized: boolean27 }28}2930export type Handler<RequestParamKeyType extends string = string, E = Env> = (31 c: Context<RequestParamKeyType, E>,
53 unmodified lines
85 private _router: Router<Handler<string, E>>86 private _tempPath: string87 private path: string = '/'8889 private _cacheResponse: Response9091 routes: Route<E>[] = []9293 constructor(init: Partial<Pick<Hono, 'routerClass' | 'strict'>> = {}) {94 super()
21 unmodified lines
116 Object.assign(this, init)117118 this._router = new this.routerClass()119 this._tempPath = null120121 this._cacheResponse = new Response(null, { status: 404 })122 this._cacheResponse.finalized = false123 }124125 private notFoundHandler: NotFoundHandler = (c: Context) => {126 const message = '404 Not Found'
71 unmodified lines
198 }199 }) as typeof request.param200 const handlers = result ? result.handlers : [this.notFoundHandler]201194 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)208209 const composed = compose<Context>(handlers, this.errorHandler, this.notFoundHandler)210 let context: Context
32 unmodified lines
modified:package.json+1 added-1 removed
9 unmodified lines
10 "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