context.ts
This file
Path
src/context.ts
126 lines · 3.4 KiB · TypeScript
Last changed by
feat: `app.notFound` for default 404 not found (#110)Yusuke Wadabefore adoption49fedca
History of this fileTake it
refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636
line 1why, one step back1import { isAbsoluteURL } from './utils/url'feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
line 2why, one step back2import { getStatusText } from './utils/http-status'Fix redirect method (#37)Yusuke Wadabefore adoption4a795da
line 3why, one step back3refactor: do some refactoring (#104)Yusuke Wadabefore adoption7efda2e
line 4why, one step back4type Headers = Record<string, string>feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
line 5why, one step back5type Data = string | ArrayBuffer | ReadableStreamFeature/redirect (#36)Yusuke Wadabefore adoption4741fd3
line 6why, one step back6Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74
lines 7–8why, one step back7export interface Env {}8feat: add type to c.req.param key. (#102)Taku Amanobefore adoption1b68195
lines 9–10why, one step back9export class Context<RequestParamKeyType = string> {10 req: Request<RequestParamKeyType>json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
line 11why, one step back11 res: ResponseSupport module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74
lines 12–13why, one step back12 env: Env13 event: FetchEventfeat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6
lines 14–16why, one step back14 private _headers: Headers15 private _status: number16 private _statusText: stringfeat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
line 17why, one step back17feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5
line 18why, one step back18 render: (template: string, params?: object, options?: object) => Promise<Response>feat: `app.notFound` for default 404 not found (#110)Yusuke Wadabefore adoption49fedca
line 19why, one step back19 notFound: () => Responsejson method in Context (#23)Yusuke Wadabefore adoptionee80ae3
line 20why, one step back20refactor: do some refactoring (#104)Yusuke Wadabefore adoption7efda2e
lines 21–24why, one step back21 constructor(22 req: Request<RequestParamKeyType>,23 opts?: { res: Response; env: Env; event: FetchEvent }24 ) {json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
line 25why, one step back25 this.req = reqSupport module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74
lines 26–30why, one step back26 if (opts) {27 this.res = opts.res28 this.env = opts.env29 this.event = opts.event30 }feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6
line 31why, one step back31 this._headers = {}json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
lines 32–33why, one step back32 }33feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6
line 34why, one step back34 header(name: string, value: string): void {refactor: remove default middleware (#91)Yusuke Wadabefore adoption0574740
lines 35–44why, one step back35 /*36 XXX:37 app.use('*', (c, next) => {38 next()39 c.header('foo', 'bar') // => c.res.headers.set(...)40 })41 */42 if (this.res) {43 this.res.headers.set(name, value)44 }feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6
lines 45–48why, one step back45 this._headers[name] = value46 }4748 status(number: number): void {feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
line 49why, one step back49 if (this.res) {fix: typoYusuke Wadabefore adoptionf51d0f0
line 50why, one step back50 console.warn('c.res.status is already set.')feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
lines 51–52why, one step back51 return52 }feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6
line 53why, one step back53 this._status = numberfeat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
line 54why, one step back54 this._statusText = getStatusText(number)feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6
lines 55–56why, one step back55 }56feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
line 57why, one step back57 newResponse(data: Data, init: ResponseInit = {}): Response {fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a
lines 58–59why, one step back58 init.status = init.status || this._status59 init.statusText = init.statusText || this._statusTextperf: Speed up to calculate content-length (#67)Yusuke Wadabefore adoptionff5a83b
line 60why, one step back60feat: serve static middleware (Cloudflare only) (#76)Yusuke Wadabefore adoptionffbcb3e
lines 61–73why, one step back61 init.headers = { ...this._headers, ...init.headers }6263 // Content-Length64 let length = 065 if (data) {66 if (data instanceof ArrayBuffer) {67 length = data.byteLength68 } else if (typeof data == 'string') {69 const Encoder = new TextEncoder()70 length = Encoder.encode(data).byteLength || 071 }72 }73 init.headers = { ...init.headers, ...{ 'Content-Length': length.toString() } }perf: Speed up to calculate content-length (#67)Yusuke Wadabefore adoptionff5a83b
lines 74–75why, one step back7475 return new Response(data, init)json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
lines 76–77why, one step back76 }77feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b
line 78why, one step back78 body(data: Data, status: number = this._status, headers: Headers = this._headers): Response {feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5
lines 79–84why, one step back79 return this.newResponse(data, {80 status: status,81 headers: headers,82 })83 }84fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a
line 85why, one step back85 text(text: string, status: number = this._status, headers: Headers = {}): Response {status and headers args on context text/json/html method (#34)Yusuke Wadabefore adoption7920f9a
line 86why, one step back86 if (typeof text !== 'string') {json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
lines 87–88why, one step back87 throw new TypeError('text method arg must be a string!')88 }fix: default content-type (#88)Yusuke Wadabefore adoptionc8184b7
line 89why, one step back89 headers['Content-Type'] ||= 'text/plain; charset=UTF-8'feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5
line 90why, one step back90 return this.body(text, status, headers)json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
lines 91–92why, one step back91 }92fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a
line 93why, one step back93 json(object: object, status: number = this._status, headers: Headers = {}): Response {json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
lines 94–96why, one step back94 if (typeof object !== 'object') {95 throw new TypeError('json method arg must be a object!')96 }status and headers args on context text/json/html method (#34)Yusuke Wadabefore adoption7920f9a
line 97why, one step back97 const body = JSON.stringify(object)fix: default content-type (#88)Yusuke Wadabefore adoptionc8184b7
line 98why, one step back98 headers['Content-Type'] ||= 'application/json; charset=UTF-8'feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5
line 99why, one step back99 return this.body(body, status, headers)json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
line 100why, one step back100 }Feature/html method (#31)Yusuke Wadabefore adoption74bab8b
line 101why, one step back101fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a
line 102why, one step back102 html(html: string, status: number = this._status, headers: Headers = {}): Response {status and headers args on context text/json/html method (#34)Yusuke Wadabefore adoption7920f9a
line 103why, one step back103 if (typeof html !== 'string') {Feature/html method (#31)Yusuke Wadabefore adoption74bab8b
lines 104–105why, one step back104 throw new TypeError('html method arg must be a string!')105 }fix: default content-type (#88)Yusuke Wadabefore adoptionc8184b7
line 106why, one step back106 headers['Content-Type'] ||= 'text/html; charset=UTF-8'feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5
line 107why, one step back107 return this.body(html, status, headers)Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3
lines 108–109why, one step back108 }109Fix redirect method (#37)Yusuke Wadabefore adoption4a795da
line 110why, one step back110 redirect(location: string, status: number = 302): Response {Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3
lines 111–113why, one step back111 if (typeof location !== 'string') {112 throw new TypeError('location must be a string!')113 }Fix redirect method (#37)Yusuke Wadabefore adoption4a795da
lines 114–119why, one step back114 if (!isAbsoluteURL(location)) {115 const url = new URL(this.req.url)116 url.pathname = location117 location = url.toString()118 }119 return this.newResponse(null, {status and headers args on context text/json/html method (#34)Yusuke Wadabefore adoption7920f9a
line 120why, one step back120 status: status,Fix redirect method (#37)Yusuke Wadabefore adoption4a795da
lines 121–123why, one step back121 headers: {122 Location: location,123 },Feature/html method (#31)Yusuke Wadabefore adoption74bab8b
lines 124–125why, one step back124 })125 }json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
line 126why, one step back126}