refactor(context): refactor `context.ts` (#674)
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 1eb49b8 · Browse files · 2 files·+16 added−16 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+16 added−16 removed
- other:+16 added−16 removed
Provenance
- Commit
a3e0eb2- Tree
301add5- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
1eb49b8
2 files changed+16 added−16 removed
modified:deno_dist/context.ts+8 added-8 removed
1 import type { ExecutionContext } from './types.ts'2-import type { Environment, NotFoundHandler, ContextVariableMap, Bindings } from './types.ts'2+import type { Environment, NotFoundHandler, ContextVariableMap } from './types.ts'3 import type { CookieOptions } from './utils/cookie.ts'4 import { serialize } from './utils/cookie.ts'5 import type { StatusCode } from './utils/http-status.ts'6 import type { Schema, SchemaToProp } from './validator/schema.ts'12 unmodified lines19 env: E['Bindings']20 finalized: boolean21 error: Error | undefined = undefined22 23- _status: StatusCode = 20023+ private _status: StatusCode = 20024 private _executionCtx: FetchEvent | ExecutionContext | undefined25 private _pretty: boolean = false26 private _prettySpace: number = 227 private _map: Record<string, unknown> | undefined7 unmodified lines35 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,36 notFoundHandler: NotFoundHandler<E> = () => new Response()37 ) {38 this._executionCtx = executionCtx39- this.req = req as unknown as Request<unknown, P, S extends Schema ? SchemaToProp<S> : S>40- this.env = env || ({} as Bindings)39+ this.req = req as Request<unknown, P, S extends Schema ? SchemaToProp<S> : S>40+ this.env = env41 42 this.notFoundHandler = notFoundHandler43 this.finalized = false44 }78 unmodified lines123 }124 125 newResponse(data: Data | null, status: StatusCode, headers: Headers = {}): Response {126 return new Response(data, {127- status: status || this._status || 200,127+ status,128 headers: this._finalizeHeaders(headers),129 })130 }131 31 unmodified lines163 }164 165 text(text: string, status: StatusCode = this._status, headers: Headers = {}): Response {166 headers['content-type'] = 'text/plain; charset=UTF-8'167- return this.body(text, status, headers)167+ return this.newResponse(text, status, headers)168 }169 170 json<T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response {171 const body = this._pretty172 ? JSON.stringify(object, null, this._prettySpace)173 : JSON.stringify(object)174 headers['content-type'] = 'application/json; charset=UTF-8'175- return this.body(body, status, headers)175+ return this.newResponse(body, status, headers)176 }177 178 html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response {179 headers['content-type'] = 'text/html; charset=UTF-8'180- return this.body(html, status, headers)180+ return this.newResponse(html, status, headers)181 }182 183 redirect(location: string, status: StatusCode = 302): Response {184 return this.newResponse(null, status, {44 unmodified linesmodified:src/context.ts+8 added-8 removed
1 import type { ExecutionContext } from './types'2-import type { Environment, NotFoundHandler, ContextVariableMap, Bindings } from './types'2+import type { Environment, NotFoundHandler, ContextVariableMap } from './types'3 import type { CookieOptions } from './utils/cookie'4 import { serialize } from './utils/cookie'5 import type { StatusCode } from './utils/http-status'6 import type { Schema, SchemaToProp } from './validator/schema'12 unmodified lines19 env: E['Bindings']20 finalized: boolean21 error: Error | undefined = undefined22 23- _status: StatusCode = 20023+ private _status: StatusCode = 20024 private _executionCtx: FetchEvent | ExecutionContext | undefined25 private _pretty: boolean = false26 private _prettySpace: number = 227 private _map: Record<string, unknown> | undefined7 unmodified lines35 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,36 notFoundHandler: NotFoundHandler<E> = () => new Response()37 ) {38 this._executionCtx = executionCtx39- this.req = req as unknown as Request<unknown, P, S extends Schema ? SchemaToProp<S> : S>40- this.env = env || ({} as Bindings)39+ this.req = req as Request<unknown, P, S extends Schema ? SchemaToProp<S> : S>40+ this.env = env41 42 this.notFoundHandler = notFoundHandler43 this.finalized = false44 }78 unmodified lines123 }124 125 newResponse(data: Data | null, status: StatusCode, headers: Headers = {}): Response {126 return new Response(data, {127- status: status || this._status || 200,127+ status,128 headers: this._finalizeHeaders(headers),129 })130 }131 31 unmodified lines163 }164 165 text(text: string, status: StatusCode = this._status, headers: Headers = {}): Response {166 headers['content-type'] = 'text/plain; charset=UTF-8'167- return this.body(text, status, headers)167+ return this.newResponse(text, status, headers)168 }169 170 json<T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response {171 const body = this._pretty172 ? JSON.stringify(object, null, this._prettySpace)173 : JSON.stringify(object)174 headers['content-type'] = 'application/json; charset=UTF-8'175- return this.body(body, status, headers)175+ return this.newResponse(body, status, headers)176 }177 178 html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response {179 headers['content-type'] = 'text/html; charset=UTF-8'180- return this.body(html, status, headers)180+ return this.newResponse(html, status, headers)181 }182 183 redirect(location: string, status: StatusCode = 302): Response {184 return this.newResponse(null, status, {44 unmodified lines