0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 02:07:30 +01:00

feat(header): suggest name of header and combine in one file (#3577)

* feat(header):ssuggest name of header and combine in one file

* chore: format and fix jsr path

* fix: fix types error of testing
This commit is contained in:
EdamAmex 2024-10-29 13:37:06 +09:00 committed by GitHub
parent cb8244aea9
commit e7732a5f23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 342 additions and 83 deletions

View File

@ -88,6 +88,7 @@
"./utils/encode": "./src/utils/encode.ts",
"./utils/filepath": "./src/utils/filepath.ts",
"./utils/handler": "./src/utils/handler.ts",
"./utils/headers": "./src/utils/headers.ts",
"./utils/html": "./src/utils/html.ts",
"./utils/http-status": "./src/utils/http-status.ts",
"./utils/jwt": "./src/utils/jwt/index.ts",

View File

@ -9,6 +9,7 @@ import type {
RouterRoute,
TypedResponse,
} from './types'
import type { ResponseHeader } from './utils/headers'
import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html'
import type { RedirectStatusCode, StatusCode } from './utils/http-status'
import type { BaseMime } from './utils/mime'
@ -236,77 +237,6 @@ interface SetHeadersOptions {
append?: boolean
}
type ResponseHeader =
| 'Access-Control-Allow-Credentials'
| 'Access-Control-Allow-Headers'
| 'Access-Control-Allow-Methods'
| 'Access-Control-Allow-Origin'
| 'Access-Control-Expose-Headers'
| 'Access-Control-Max-Age'
| 'Age'
| 'Allow'
| 'Cache-Control'
| 'Clear-Site-Data'
| 'Content-Disposition'
| 'Content-Encoding'
| 'Content-Language'
| 'Content-Length'
| 'Content-Location'
| 'Content-Range'
| 'Content-Security-Policy'
| 'Content-Security-Policy-Report-Only'
| 'Content-Type'
| 'Cookie'
| 'Cross-Origin-Embedder-Policy'
| 'Cross-Origin-Opener-Policy'
| 'Cross-Origin-Resource-Policy'
| 'Date'
| 'ETag'
| 'Expires'
| 'Last-Modified'
| 'Location'
| 'Permissions-Policy'
| 'Pragma'
| 'Retry-After'
| 'Save-Data'
| 'Sec-CH-Prefers-Color-Scheme'
| 'Sec-CH-Prefers-Reduced-Motion'
| 'Sec-CH-UA'
| 'Sec-CH-UA-Arch'
| 'Sec-CH-UA-Bitness'
| 'Sec-CH-UA-Form-Factor'
| 'Sec-CH-UA-Full-Version'
| 'Sec-CH-UA-Full-Version-List'
| 'Sec-CH-UA-Mobile'
| 'Sec-CH-UA-Model'
| 'Sec-CH-UA-Platform'
| 'Sec-CH-UA-Platform-Version'
| 'Sec-CH-UA-WoW64'
| 'Sec-Fetch-Dest'
| 'Sec-Fetch-Mode'
| 'Sec-Fetch-Site'
| 'Sec-Fetch-User'
| 'Sec-GPC'
| 'Server'
| 'Server-Timing'
| 'Service-Worker-Navigation-Preload'
| 'Set-Cookie'
| 'Strict-Transport-Security'
| 'Timing-Allow-Origin'
| 'Trailer'
| 'Transfer-Encoding'
| 'Upgrade'
| 'Vary'
| 'WWW-Authenticate'
| 'Warning'
| 'X-Content-Type-Options'
| 'X-DNS-Prefetch-Control'
| 'X-Frame-Options'
| 'X-Permitted-Cross-Domain-Policies'
| 'X-Powered-By'
| 'X-Robots-Tag'
| 'X-XSS-Protection'
interface SetHeaders {
(name: 'Content-Type', value?: BaseMime, options?: SetHeadersOptions): void
(name: ResponseHeader, value?: string, options?: SetHeadersOptions): void

View File

@ -1,13 +1,5 @@
import type { Context } from '../../context'
export type AcceptHeader =
| 'Accept'
| 'Accept-Charset'
| 'Accept-Encoding'
| 'Accept-Language'
| 'Accept-Patch'
| 'Accept-Post'
| 'Accept-Ranges'
import type { AcceptHeader } from '../../utils/headers'
export interface Accept {
type: string

View File

@ -93,7 +93,7 @@ export const setCookie = (c: Context, name: string, value: string, opt?: CookieO
} else {
cookie = serialize(name, value, { path: '/', ...opt })
}
c.header('set-cookie', cookie, { append: true })
c.header('Set-Cookie', cookie, { append: true })
}
export const setSignedCookie = async (

View File

@ -11,6 +11,7 @@ import type {
} from './types'
import { parseBody } from './utils/body'
import type { BodyData, ParseBodyOptions } from './utils/body'
import type { CustomHeader, RequestHeader } from './utils/headers'
import type { Simplify, UnionToIntersection } from './utils/types'
import { decodeURIComponent_, getQueryParam, getQueryParams } from './utils/url'
@ -171,8 +172,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* })
* ```
*/
header(name: RequestHeader): string | undefined
header(name: string): string | undefined
header(): Record<string, string>
header(): Record<RequestHeader | (string & CustomHeader), string>
header(name?: string) {
if (name) {
return this.raw.headers.get(name.toLowerCase()) ?? undefined

View File

@ -7,6 +7,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Context } from './context'
import type { Hono } from './hono'
import type { CustomHeader, RequestHeader } from './utils/headers'
import type { StatusCode } from './utils/http-status'
import type {
IfAnyThenEmptyObject,
@ -1924,7 +1925,7 @@ export type ValidationTargets<T extends FormValue = ParsedFormValue, P extends s
form: Record<string, T | T[]>
query: Record<string, string | string[]>
param: Record<P, P extends `${infer _}?` ? string | undefined : string>
header: Record<string, string>
header: Record<RequestHeader | CustomHeader, string>
cookie: Record<string, string>
}

333
src/utils/headers.ts Normal file
View File

@ -0,0 +1,333 @@
/**
* @module
* HTTP Headers utility.
*/
// note: https://www.iana.org/assignments/http-fields/http-fields.xhtml
export type RequestHeader =
| 'A-IM'
| 'Accept'
| 'Accept-Additions'
| 'Accept-CH'
| 'Accept-Charset'
| 'Accept-Datetime'
| 'Accept-Encoding'
| 'Accept-Features'
| 'Accept-Language'
| 'Accept-Patch'
| 'Accept-Post'
| 'Accept-Ranges'
| 'Accept-Signature'
| 'Access-Control'
| 'Access-Control-Allow-Credentials'
| 'Access-Control-Allow-Headers'
| 'Access-Control-Allow-Methods'
| 'Access-Control-Allow-Origin'
| 'Access-Control-Expose-Headers'
| 'Access-Control-Max-Age'
| 'Access-Control-Request-Headers'
| 'Access-Control-Request-Method'
| 'Age'
| 'Allow'
| 'ALPN'
| 'Alt-Svc'
| 'Alt-Used'
| 'Alternates'
| 'AMP-Cache-Transform'
| 'Apply-To-Redirect-Ref'
| 'Authentication-Control'
| 'Authentication-Info'
| 'Authorization'
| 'Available-Dictionary'
| 'C-Ext'
| 'C-Man'
| 'C-Opt'
| 'C-PEP'
| 'C-PEP-Info'
| 'Cache-Control'
| 'Cache-Status'
| 'Cal-Managed-ID'
| 'CalDAV-Timezones'
| 'Capsule-Protocol'
| 'CDN-Cache-Control'
| 'CDN-Loop'
| 'Cert-Not-After'
| 'Cert-Not-Before'
| 'Clear-Site-Data'
| 'Client-Cert'
| 'Client-Cert-Chain'
| 'Close'
| 'CMCD-Object'
| 'CMCD-Request'
| 'CMCD-Session'
| 'CMCD-Status'
| 'CMSD-Dynamic'
| 'CMSD-Static'
| 'Concealed-Auth-Export'
| 'Configuration-Context'
| 'Connection'
| 'Content-Base'
| 'Content-Digest'
| 'Content-Disposition'
| 'Content-Encoding'
| 'Content-ID'
| 'Content-Language'
| 'Content-Length'
| 'Content-Location'
| 'Content-MD5'
| 'Content-Range'
| 'Content-Script-Type'
| 'Content-Security-Policy'
| 'Content-Security-Policy-Report-Only'
| 'Content-Style-Type'
| 'Content-Type'
| 'Content-Version'
| 'Cookie'
| 'Cookie2'
| 'Cross-Origin-Embedder-Policy'
| 'Cross-Origin-Embedder-Policy-Report-Only'
| 'Cross-Origin-Opener-Policy'
| 'Cross-Origin-Opener-Policy-Report-Only'
| 'Cross-Origin-Resource-Policy'
| 'CTA-Common-Access-Token'
| 'DASL'
| 'Date'
| 'DAV'
| 'Default-Style'
| 'Delta-Base'
| 'Deprecation'
| 'Depth'
| 'Derived-From'
| 'Destination'
| 'Differential-ID'
| 'Dictionary-ID'
| 'Digest'
| 'DPoP'
| 'DPoP-Nonce'
| 'Early-Data'
| 'EDIINT-Features'
| 'ETag'
| 'Expect'
| 'Expect-CT'
| 'Expires'
| 'Ext'
| 'Forwarded'
| 'From'
| 'GetProfile'
| 'Hobareg'
| 'Host'
| 'HTTP2-Settings'
| 'If'
| 'If-Match'
| 'If-Modified-Since'
| 'If-None-Match'
| 'If-Range'
| 'If-Schedule-Tag-Match'
| 'If-Unmodified-Since'
| 'IM'
| 'Include-Referred-Token-Binding-ID'
| 'Isolation'
| 'Keep-Alive'
| 'Label'
| 'Last-Event-ID'
| 'Last-Modified'
| 'Link'
| 'Link-Template'
| 'Location'
| 'Lock-Token'
| 'Man'
| 'Max-Forwards'
| 'Memento-Datetime'
| 'Meter'
| 'Method-Check'
| 'Method-Check-Expires'
| 'MIME-Version'
| 'Negotiate'
| 'NEL'
| 'OData-EntityId'
| 'OData-Isolation'
| 'OData-MaxVersion'
| 'OData-Version'
| 'Opt'
| 'Optional-WWW-Authenticate'
| 'Ordering-Type'
| 'Origin'
| 'Origin-Agent-Cluster'
| 'OSCORE'
| 'OSLC-Core-Version'
| 'Overwrite'
| 'P3P'
| 'PEP'
| 'PEP-Info'
| 'Permissions-Policy'
| 'PICS-Label'
| 'Ping-From'
| 'Ping-To'
| 'Position'
| 'Pragma'
| 'Prefer'
| 'Preference-Applied'
| 'Priority'
| 'ProfileObject'
| 'Protocol'
| 'Protocol-Info'
| 'Protocol-Query'
| 'Protocol-Request'
| 'Proxy-Authenticate'
| 'Proxy-Authentication-Info'
| 'Proxy-Authorization'
| 'Proxy-Features'
| 'Proxy-Instruction'
| 'Proxy-Status'
| 'Public'
| 'Public-Key-Pins'
| 'Public-Key-Pins-Report-Only'
| 'Range'
| 'Redirect-Ref'
| 'Referer'
| 'Referer-Root'
| 'Referrer-Policy'
| 'Refresh'
| 'Repeatability-Client-ID'
| 'Repeatability-First-Sent'
| 'Repeatability-Request-ID'
| 'Repeatability-Result'
| 'Replay-Nonce'
| 'Reporting-Endpoints'
| 'Repr-Digest'
| 'Retry-After'
| 'Safe'
| 'Schedule-Reply'
| 'Schedule-Tag'
| 'Sec-GPC'
| 'Sec-Purpose'
| 'Sec-Token-Binding'
| 'Sec-WebSocket-Accept'
| 'Sec-WebSocket-Extensions'
| 'Sec-WebSocket-Key'
| 'Sec-WebSocket-Protocol'
| 'Sec-WebSocket-Version'
| 'Security-Scheme'
| 'Server'
| 'Server-Timing'
| 'Set-Cookie'
| 'Set-Cookie2'
| 'SetProfile'
| 'Signature'
| 'Signature-Input'
| 'SLUG'
| 'SoapAction'
| 'Status-URI'
| 'Strict-Transport-Security'
| 'Sunset'
| 'Surrogate-Capability'
| 'Surrogate-Control'
| 'TCN'
| 'TE'
| 'Timeout'
| 'Timing-Allow-Origin'
| 'Topic'
| 'Traceparent'
| 'Tracestate'
| 'Trailer'
| 'Transfer-Encoding'
| 'TTL'
| 'Upgrade'
| 'Urgency'
| 'URI'
| 'Use-As-Dictionary'
| 'User-Agent'
| 'Variant-Vary'
| 'Vary'
| 'Via'
| 'Want-Content-Digest'
| 'Want-Digest'
| 'Want-Repr-Digest'
| 'Warning'
| 'WWW-Authenticate'
| 'X-Content-Type-Options'
| 'X-Frame-Options'
export type ResponseHeader =
| 'Access-Control-Allow-Credentials'
| 'Access-Control-Allow-Headers'
| 'Access-Control-Allow-Methods'
| 'Access-Control-Allow-Origin'
| 'Access-Control-Expose-Headers'
| 'Access-Control-Max-Age'
| 'Age'
| 'Allow'
| 'Cache-Control'
| 'Clear-Site-Data'
| 'Content-Disposition'
| 'Content-Encoding'
| 'Content-Language'
| 'Content-Length'
| 'Content-Location'
| 'Content-Range'
| 'Content-Security-Policy'
| 'Content-Security-Policy-Report-Only'
| 'Content-Type'
| 'Cookie'
| 'Cross-Origin-Embedder-Policy'
| 'Cross-Origin-Opener-Policy'
| 'Cross-Origin-Resource-Policy'
| 'Date'
| 'ETag'
| 'Expires'
| 'Last-Modified'
| 'Location'
| 'Permissions-Policy'
| 'Pragma'
| 'Retry-After'
| 'Save-Data'
| 'Sec-CH-Prefers-Color-Scheme'
| 'Sec-CH-Prefers-Reduced-Motion'
| 'Sec-CH-UA'
| 'Sec-CH-UA-Arch'
| 'Sec-CH-UA-Bitness'
| 'Sec-CH-UA-Form-Factor'
| 'Sec-CH-UA-Full-Version'
| 'Sec-CH-UA-Full-Version-List'
| 'Sec-CH-UA-Mobile'
| 'Sec-CH-UA-Model'
| 'Sec-CH-UA-Platform'
| 'Sec-CH-UA-Platform-Version'
| 'Sec-CH-UA-WoW64'
| 'Sec-Fetch-Dest'
| 'Sec-Fetch-Mode'
| 'Sec-Fetch-Site'
| 'Sec-Fetch-User'
| 'Sec-GPC'
| 'Server'
| 'Server-Timing'
| 'Service-Worker-Navigation-Preload'
| 'Set-Cookie'
| 'Strict-Transport-Security'
| 'Timing-Allow-Origin'
| 'Trailer'
| 'Transfer-Encoding'
| 'Upgrade'
| 'Vary'
| 'WWW-Authenticate'
| 'Warning'
| 'X-Content-Type-Options'
| 'X-DNS-Prefetch-Control'
| 'X-Frame-Options'
| 'X-Permitted-Cross-Domain-Policies'
| 'X-Powered-By'
| 'X-Robots-Tag'
| 'X-XSS-Protection'
export type AcceptHeader =
| 'Accept'
| 'Accept-Charset'
| 'Accept-Encoding'
| 'Accept-Language'
| 'Accept-Patch'
| 'Accept-Post'
| 'Accept-Ranges'
// note: `X-${string}` is deprecated
export type CustomHeader = string & {}