diff --git a/src/context.ts b/src/context.ts index fb16effa..81e39207 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,4 +1,5 @@ import { getStatusText } from '@/utils/http-status' +import type { StatusCode } from '@/utils/http-status' import { isAbsoluteURL } from '@/utils/url' type Headers = Record @@ -12,7 +13,7 @@ export class Context { env: Env event: FetchEvent private _headers: Headers - private _status: number + private _status: StatusCode private _statusText: string private _pretty: boolean private _prettySpace: number = 2 @@ -47,13 +48,13 @@ export class Context { this._headers[name] = value } - status(number: number): void { + status(status: StatusCode): void { if (this.res) { console.warn('c.res.status is already set.') return } - this._status = number - this._statusText = getStatusText(number) + this._status = status + this._statusText = getStatusText(status) } pretty(prettyJSON: boolean, space: number = 2): void { @@ -63,7 +64,8 @@ export class Context { newResponse(data: Data, init: ResponseInit = {}): Response { init.status = init.status || this._status || 200 - init.statusText = init.statusText || this._statusText || getStatusText(init.status) + init.statusText = + init.statusText || this._statusText || getStatusText(init.status as StatusCode) init.headers = { ...this._headers, ...init.headers } @@ -82,14 +84,14 @@ export class Context { return new Response(data, init) } - body(data: Data, status: number = this._status, headers: Headers = this._headers): Response { + body(data: Data, status: StatusCode = this._status, headers: Headers = this._headers): Response { return this.newResponse(data, { status: status, headers: headers, }) } - text(text: string, status: number = this._status, headers: Headers = {}): Response { + text(text: string, status: StatusCode = this._status, headers: Headers = {}): Response { if (typeof text !== 'string') { throw new TypeError('text method arg must be a string!') } @@ -97,7 +99,7 @@ export class Context { return this.body(text, status, headers) } - json(object: object, status: number = this._status, headers: Headers = {}): Response { + json(object: object, status: StatusCode = this._status, headers: Headers = {}): Response { if (typeof object !== 'object') { throw new TypeError('json method arg must be a object!') } @@ -108,7 +110,7 @@ export class Context { return this.body(body, status, headers) } - html(html: string, status: number = this._status, headers: Headers = {}): Response { + html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response { if (typeof html !== 'string') { throw new TypeError('html method arg must be a string!') } @@ -116,7 +118,7 @@ export class Context { return this.body(html, status, headers) } - redirect(location: string, status: number = 302): Response { + redirect(location: string, status: StatusCode = 302): Response { if (typeof location !== 'string') { throw new TypeError('location must be a string!') } diff --git a/src/utils/http-status.ts b/src/utils/http-status.ts index 631fcac2..aef81904 100644 --- a/src/utils/http-status.ts +++ b/src/utils/http-status.ts @@ -1,9 +1,86 @@ -export 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 = { + 100: 'Continue', + 101: 'Switching Protocols', + 102: 'Processing', + 103: 'Early Hints', 200: 'OK', 201: 'Created', 202: 'Accepted',