mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 10:51:01 +00:00
feat: add types for HTTP status code (#163)
* feat: add types for HTTP status code
This commit is contained in:
parent
e288ac2983
commit
1a30e37dfa
@ -1,4 +1,5 @@
|
|||||||
import { getStatusText } from '@/utils/http-status'
|
import { getStatusText } from '@/utils/http-status'
|
||||||
|
import type { StatusCode } from '@/utils/http-status'
|
||||||
import { isAbsoluteURL } from '@/utils/url'
|
import { isAbsoluteURL } from '@/utils/url'
|
||||||
|
|
||||||
type Headers = Record<string, string>
|
type Headers = Record<string, string>
|
||||||
@ -12,7 +13,7 @@ export class Context<RequestParamKeyType = string> {
|
|||||||
env: Env
|
env: Env
|
||||||
event: FetchEvent
|
event: FetchEvent
|
||||||
private _headers: Headers
|
private _headers: Headers
|
||||||
private _status: number
|
private _status: StatusCode
|
||||||
private _statusText: string
|
private _statusText: string
|
||||||
private _pretty: boolean
|
private _pretty: boolean
|
||||||
private _prettySpace: number = 2
|
private _prettySpace: number = 2
|
||||||
@ -47,13 +48,13 @@ export class Context<RequestParamKeyType = string> {
|
|||||||
this._headers[name] = value
|
this._headers[name] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
status(number: number): void {
|
status(status: StatusCode): void {
|
||||||
if (this.res) {
|
if (this.res) {
|
||||||
console.warn('c.res.status is already set.')
|
console.warn('c.res.status is already set.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this._status = number
|
this._status = status
|
||||||
this._statusText = getStatusText(number)
|
this._statusText = getStatusText(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
pretty(prettyJSON: boolean, space: number = 2): void {
|
pretty(prettyJSON: boolean, space: number = 2): void {
|
||||||
@ -63,7 +64,8 @@ export class Context<RequestParamKeyType = string> {
|
|||||||
|
|
||||||
newResponse(data: Data, init: ResponseInit = {}): Response {
|
newResponse(data: Data, init: ResponseInit = {}): Response {
|
||||||
init.status = init.status || this._status || 200
|
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 }
|
init.headers = { ...this._headers, ...init.headers }
|
||||||
|
|
||||||
@ -82,14 +84,14 @@ export class Context<RequestParamKeyType = string> {
|
|||||||
return new Response(data, init)
|
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, {
|
return this.newResponse(data, {
|
||||||
status: status,
|
status: status,
|
||||||
headers: headers,
|
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') {
|
if (typeof text !== 'string') {
|
||||||
throw new TypeError('text method arg must be a string!')
|
throw new TypeError('text method arg must be a string!')
|
||||||
}
|
}
|
||||||
@ -97,7 +99,7 @@ export class Context<RequestParamKeyType = string> {
|
|||||||
return this.body(text, status, headers)
|
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') {
|
if (typeof object !== 'object') {
|
||||||
throw new TypeError('json method arg must be a object!')
|
throw new TypeError('json method arg must be a object!')
|
||||||
}
|
}
|
||||||
@ -108,7 +110,7 @@ export class Context<RequestParamKeyType = string> {
|
|||||||
return this.body(body, status, headers)
|
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') {
|
if (typeof html !== 'string') {
|
||||||
throw new TypeError('html method arg must be a string!')
|
throw new TypeError('html method arg must be a string!')
|
||||||
}
|
}
|
||||||
@ -116,7 +118,7 @@ export class Context<RequestParamKeyType = string> {
|
|||||||
return this.body(html, status, headers)
|
return this.body(html, status, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
redirect(location: string, status: number = 302): Response {
|
redirect(location: string, status: StatusCode = 302): Response {
|
||||||
if (typeof location !== 'string') {
|
if (typeof location !== 'string') {
|
||||||
throw new TypeError('location must be a string!')
|
throw new TypeError('location must be a string!')
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,86 @@
|
|||||||
export const getStatusText = (statusNumber: number): string => {
|
export const getStatusText = (statusCode: StatusCode): string => {
|
||||||
const text = statuses[statusNumber]
|
const text = statuses[statusCode]
|
||||||
return text
|
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',
|
200: 'OK',
|
||||||
201: 'Created',
|
201: 'Created',
|
||||||
202: 'Accepted',
|
202: 'Accepted',
|
||||||
|
Loading…
Reference in New Issue
Block a user