Skip to content

Commit

Permalink
feat(header): suggest name of header and combine in one file (honojs#…
Browse files Browse the repository at this point in the history
…3577)

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

* chore: format and fix jsr path

* fix: fix types error of testing
  • Loading branch information
EdamAme-x authored and TinsFox committed Nov 11, 2024
1 parent 4ab29b3 commit 41ae912
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 83 deletions.
1 change: 1 addition & 0 deletions jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
72 changes: 1 addition & 71 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions src/helper/accepts/accepts.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/helper/cookie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 3 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>
}

Expand Down
Loading

0 comments on commit 41ae912

Please sign in to comment.