Skip to content
Myr

feat: add types for HTTP status code (#163)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat: add types for HTTP status code

authored by Yusuke Wada · committed by GitHub · parent e288ac2 · Browse files · 2 files+92 added−13 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+92 added−13 removed

  • other:+92 added−13 removed

2 files+92 added−13 removed

modified:src/utils/http-status.ts+80 added-3 removed
modified:src/context.ts+12 added-10 removed
1import { getStatusText } from '@/utils/http-status'2import type { StatusCode } from '@/utils/http-status'3import { isAbsoluteURL } from '@/utils/url'45type Headers = Record<string, string>6type Data = string | ArrayBuffer | ReadableStream
5 unmodified lines
12 res: Response13 env: Env14 event: FetchEvent15 private _headers: Headers15 private _status: number16 private _status: StatusCode17 private _statusText: string18 private _pretty: boolean19 private _prettySpace: number = 220
26 unmodified lines
47 }48 this._headers[name] = value49 }5050 status(number: number): void {51 status(status: StatusCode): void {52 if (this.res) {53 console.warn('c.res.status is already set.')54 return55 }55 this._status = number56 this._statusText = getStatusText(number)56 this._status = status57 this._statusText = getStatusText(status)58 }5960 pretty(prettyJSON: boolean, space: number = 2): void {61 this._pretty = prettyJSON
1 unmodified line
63 }6465 newResponse(data: Data, init: ResponseInit = {}): Response {66 init.status = init.status || this._status || 20066 init.statusText = init.statusText || this._statusText || getStatusText(init.status)67 init.statusText =68 init.statusText || this._statusText || getStatusText(init.status as StatusCode)6970 init.headers = { ...this._headers, ...init.headers }7172 // Content-Length
10 unmodified lines
8384 return new Response(data, init)85 }8685 body(data: Data, status: number = this._status, headers: Headers = this._headers): Response {87 body(data: Data, status: StatusCode = this._status, headers: Headers = this._headers): Response {88 return this.newResponse(data, {89 status: status,90 headers: headers,91 })92 }9392 text(text: string, status: number = this._status, headers: Headers = {}): Response {94 text(text: string, status: StatusCode = this._status, headers: Headers = {}): Response {95 if (typeof text !== 'string') {96 throw new TypeError('text method arg must be a string!')97 }98 headers['Content-Type'] ||= 'text/plain; charset=UTF-8'99 return this.body(text, status, headers)100 }101100 json(object: object, status: number = this._status, headers: Headers = {}): Response {102 json(object: object, status: StatusCode = this._status, headers: Headers = {}): Response {103 if (typeof object !== 'object') {104 throw new TypeError('json method arg must be a object!')105 }106 const body = this._pretty
2 unmodified lines
109 headers['Content-Type'] ||= 'application/json; charset=UTF-8'110 return this.body(body, status, headers)111 }112111 html(html: string, status: number = this._status, headers: Headers = {}): Response {113 html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response {114 if (typeof html !== 'string') {115 throw new TypeError('html method arg must be a string!')116 }117 headers['Content-Type'] ||= 'text/html; charset=UTF-8'118 return this.body(html, status, headers)119 }120119 redirect(location: string, status: number = 302): Response {121 redirect(location: string, status: StatusCode = 302): Response {122 if (typeof location !== 'string') {123 throw new TypeError('location must be a string!')124 }125 if (!isAbsoluteURL(location)) {
12 unmodified lines