Skip to content
Myr

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

2 files+16 added−16 removed

modified:deno_dist/context.ts+8 added-8 removed
1import type { ExecutionContext } from './types.ts'2import type { Environment, NotFoundHandler, ContextVariableMap, Bindings } from './types.ts'2import type { Environment, NotFoundHandler, ContextVariableMap } from './types.ts'3import type { CookieOptions } from './utils/cookie.ts'4import { serialize } from './utils/cookie.ts'5import type { StatusCode } from './utils/http-status.ts'6import type { Schema, SchemaToProp } from './validator/schema.ts'
12 unmodified lines
19 env: E['Bindings']20 finalized: boolean21 error: Error | undefined = undefined2223 _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> | undefined
7 unmodified lines
35 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 = env4142 this.notFoundHandler = notFoundHandler43 this.finalized = false44 }
78 unmodified lines
123 }124125 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 lines
163 }164165 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 }169170 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 }177178 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 }182183 redirect(location: string, status: StatusCode = 302): Response {184 return this.newResponse(null, status, {
44 unmodified lines
modified:src/context.ts+8 added-8 removed
1import type { ExecutionContext } from './types'2import type { Environment, NotFoundHandler, ContextVariableMap, Bindings } from './types'2import type { Environment, NotFoundHandler, ContextVariableMap } from './types'3import type { CookieOptions } from './utils/cookie'4import { serialize } from './utils/cookie'5import type { StatusCode } from './utils/http-status'6import type { Schema, SchemaToProp } from './validator/schema'
12 unmodified lines
19 env: E['Bindings']20 finalized: boolean21 error: Error | undefined = undefined2223 _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> | undefined
7 unmodified lines
35 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 = env4142 this.notFoundHandler = notFoundHandler43 this.finalized = false44 }
78 unmodified lines
123 }124125 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 lines
163 }164165 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 }169170 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 }177178 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 }182183 redirect(location: string, status: StatusCode = 302): Response {184 return this.newResponse(null, status, {
44 unmodified lines