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
Provenance
- Commit
1a30e37- Tree
fdd72a0- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
e288ac2
2 files changed+92 added−13 removed
modified:src/utils/http-status.ts+80 added-3 removed
12123456678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868733 unmodified linesexport const getStatusText = (statusNumber: number): string => { const text = statuses[statusNumber]export const getStatusText = (statusCode: StatusCode): string => { const text = statuses[statusCode] return text}
const statuses: { [key: number]: string } = {export type StatusCode = | 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 444 | 449 | 450 | 451 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 598 | 599
const statuses: Record<StatusCode | number, string> = { 100: 'Continue', 101: 'Switching Protocols', 102: 'Processing', 103: 'Early Hints', 200: 'OK', 201: 'Created', 202: 'Accepted', 204: 'No Content',33 unmodified linesmodified:src/context.ts+12 added-10 removed
1 import { getStatusText } from '@/utils/http-status'2+import type { StatusCode } from '@/utils/http-status'3 import { isAbsoluteURL } from '@/utils/url'4 5 type Headers = Record<string, string>6 type Data = string | ArrayBuffer | ReadableStream5 unmodified lines12 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 lines47 }48 this._headers[name] = value49 }50 50- 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 }59 60 pretty(prettyJSON: boolean, space: number = 2): void {61 this._pretty = prettyJSON1 unmodified line63 }64 65 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)69 70 init.headers = { ...this._headers, ...init.headers }71 72 // Content-Length10 unmodified lines83 84 return new Response(data, init)85 }86 85- 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 }93 92- 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 }101 100- 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._pretty2 unmodified lines109 headers['Content-Type'] ||= 'application/json; charset=UTF-8'110 return this.body(body, status, headers)111 }112 111- 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 }120 119- 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