Skip to content
Myr

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 file

Take it

refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636

line 1why, one step back
1import { isAbsoluteURL } from './utils/url'

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

line 2why, one step back
2import { getStatusText } from './utils/http-status'

Fix redirect method (#37)Yusuke Wadabefore adoption4a795da

line 3why, one step back
3

refactor: do some refactoring (#104)Yusuke Wadabefore adoption7efda2e

line 4why, one step back
4type Headers = Record<string, string>

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

line 5why, one step back
5type Data = string | ArrayBuffer | ReadableStream

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

line 6why, one step back
6

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 7–8why, one step back
7export interface Env {}
8

feat: add type to c.req.param key. (#102)Taku Amanobefore adoption1b68195

lines 9–10why, one step back
9export class Context<RequestParamKeyType = string> {
10 req: Request<RequestParamKeyType>

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

line 11why, one step back
11 res: Response

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 12–13why, one step back
12 env: Env
13 event: FetchEvent

feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6

lines 14–16why, one step back
14 private _headers: Headers
15 private _status: number16 private _statusText: string

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

line 17why, one step back
17

feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5

line 18why, one step back
18 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 back
19 notFound: () => Response

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

line 20why, one step back
20

refactor: do some refactoring (#104)Yusuke Wadabefore adoption7efda2e

lines 21–24why, one step back
21 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 back
25 this.req = req

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

lines 26–30why, one step back
26 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 back
31 this._headers = {}

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

lines 32–33why, one step back
32 }
33

feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6

line 34why, one step back
34 header(name: string, value: string): void {

refactor: remove default middleware (#91)Yusuke Wadabefore adoption0574740

lines 35–44why, one step back
35 /*
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 back
45 this._headers[name] = value
46 }4748 status(number: number): void {

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

line 49why, one step back
49 if (this.res) {

fix: typoYusuke Wadabefore adoptionf51d0f0

line 50why, one step back
50 console.warn('c.res.status is already set.')

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

lines 51–52why, one step back
51 return
52 }

feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6

line 53why, one step back
53 this._status = number

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

line 54why, one step back
54 this._statusText = getStatusText(number)

feat: Add new shortcuts for request/response (#62)Yusuke Wadabefore adoption68444b6

lines 55–56why, one step back
55 }
56

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

line 57why, one step back
57 newResponse(data: Data, init: ResponseInit = {}): Response {

fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a

lines 58–59why, one step back
58 init.status = init.status || this._status
59 init.statusText = init.statusText || this._statusText

perf: Speed up to calculate content-length (#67)Yusuke Wadabefore adoptionff5a83b

line 60why, one step back
60

feat: serve static middleware (Cloudflare only) (#76)Yusuke Wadabefore adoptionffbcb3e

lines 61–73why, one step back
61 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 back
74
75 return new Response(data, init)

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

lines 76–77why, one step back
76 }
77

feat: auto set `statusText` (#93)Yusuke Wadabefore adoptiona72660b

line 78why, one step back
78 body(data: Data, status: number = this._status, headers: Headers = this._headers): Response {

feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5

lines 79–84why, one step back
79 return this.newResponse(data, {
80 status: status,81 headers: headers,82 })83 }84

fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a

line 85why, one step back
85 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 back
86 if (typeof text !== 'string') {

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

lines 87–88why, one step back
87 throw new TypeError('text method arg must be a string!')
88 }

fix: default content-type (#88)Yusuke Wadabefore adoptionc8184b7

line 89why, one step back
89 headers['Content-Type'] ||= 'text/plain; charset=UTF-8'

feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5

line 90why, one step back
90 return this.body(text, status, headers)

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

lines 91–92why, one step back
91 }
92

fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a

line 93why, one step back
93 json(object: object, status: number = this._status, headers: Headers = {}): Response {

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

lines 94–96why, one step back
94 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 back
97 const body = JSON.stringify(object)

fix: default content-type (#88)Yusuke Wadabefore adoptionc8184b7

line 98why, one step back
98 headers['Content-Type'] ||= 'application/json; charset=UTF-8'

feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5

line 99why, one step back
99 return this.body(body, status, headers)

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

line 100why, one step back
100 }

Feature/html method (#31)Yusuke Wadabefore adoption74bab8b

line 101why, one step back
101

fix: Fixed context headers (#69)Yusuke Wadabefore adoption3017a4a

line 102why, one step back
102 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 back
103 if (typeof html !== 'string') {

Feature/html method (#31)Yusuke Wadabefore adoption74bab8b

lines 104–105why, one step back
104 throw new TypeError('html method arg must be a string!')
105 }

fix: default content-type (#88)Yusuke Wadabefore adoptionc8184b7

line 106why, one step back
106 headers['Content-Type'] ||= 'text/html; charset=UTF-8'

feat: Mustache Middleware (#72)Yusuke Wadabefore adoption29f67c5

line 107why, one step back
107 return this.body(html, status, headers)

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

lines 108–109why, one step back
108 }
109

Fix redirect method (#37)Yusuke Wadabefore adoption4a795da

line 110why, one step back
110 redirect(location: string, status: number = 302): Response {

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

lines 111–113why, one step back
111 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 back
114 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 back
120 status: status,

Fix redirect method (#37)Yusuke Wadabefore adoption4a795da

lines 121–123why, one step back
121 headers: {
122 Location: location,123 },

Feature/html method (#31)Yusuke Wadabefore adoption74bab8b

lines 124–125why, one step back
124 })
125 }

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

line 126why, one step back
126}