Skip to content

Commit

Permalink
add autocompletable header types (#3462)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Aug 16, 2024
1 parent ab42afc commit 14ba3e8
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 2 deletions.
5 changes: 5 additions & 0 deletions test/types/fetch.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ expectType<Headers>(new Headers([]))
expectType<Headers>(new Headers(headers))
expectType<Headers>(new Headers(undefined))

expectAssignable<HeadersInit>({ 'a': 'b' } as Record<string, string>)
expectAssignable<HeadersInit>({ 'content-type': 'application/gzip' } satisfies HeadersInit)
expectAssignable<HeadersInit>({ 'Content-Type': 'nonstandard/mime' } satisfies HeadersInit)
expectNotAssignable<HeadersInit>([['1', '2', '3']])

expectType<Request>(new Request(request))
expectType<Request>(new Request('https://example.com'))
expectType<Request>(new Request(new URL('https://example.com')))
Expand Down
4 changes: 2 additions & 2 deletions types/fetch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Blob } from 'buffer'
import { URL, URLSearchParams } from 'url'
import { ReadableStream } from 'stream/web'
import { FormData } from './formdata'

import { HeaderRecord } from './header'
import Dispatcher from './dispatcher'

export type RequestInfo = string | URL | Request
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface SpecIterable<T> {
[Symbol.iterator](): SpecIterator<T>;
}

export type HeadersInit = string[][] | Record<string, string | ReadonlyArray<string>> | Headers
export type HeadersInit = [string, string][] | HeaderRecord | Headers

export declare class Headers implements SpecIterable<[string, string]> {
constructor (init?: HeadersInit)
Expand Down
152 changes: 152 additions & 0 deletions types/header.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,155 @@
* The header type declaration of `undici`.
*/
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>;

type HeaderNames =
| 'Accept'
| 'Accept-CH'
| 'Accept-Charset'
| 'Accept-Encoding'
| 'Accept-Language'
| 'Accept-Patch'
| 'Accept-Post'
| 'Accept-Ranges'
| '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'
| 'Alt-Svc'
| 'Alt-Used'
| 'Authorization'
| 'Cache-Control'
| 'Clear-Site-Data'
| 'Connection'
| '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'
| 'Device-Memory'
| 'ETag'
| 'Expect'
| 'Expect-CT'
| 'Expires'
| 'Forwarded'
| 'From'
| 'Host'
| 'If-Match'
| 'If-Modified-Since'
| 'If-None-Match'
| 'If-Range'
| 'If-Unmodified-Since'
| 'Keep-Alive'
| 'Last-Modified'
| 'Link'
| 'Location'
| 'Max-Forwards'
| 'Origin'
| 'Permissions-Policy'
| 'Priority'
| 'Proxy-Authenticate'
| 'Proxy-Authorization'
| 'Range'
| 'Referer'
| 'Referrer-Policy'
| 'Retry-After'
| 'Sec-Fetch-Dest'
| 'Sec-Fetch-Mode'
| 'Sec-Fetch-Site'
| 'Sec-Fetch-User'
| 'Sec-Purpose'
| 'Sec-WebSocket-Accept'
| 'Server'
| 'Server-Timing'
| 'Service-Worker-Navigation-Preload'
| 'Set-Cookie'
| 'SourceMap'
| 'Strict-Transport-Security'
| 'TE'
| 'Timing-Allow-Origin'
| 'Trailer'
| 'Transfer-Encoding'
| 'Upgrade'
| 'Upgrade-Insecure-Requests'
| 'User-Agent'
| 'Vary'
| 'Via'
| 'WWW-Authenticate'
| 'X-Content-Type-Options'
| 'X-Frame-Options'
| (string & {})

type KnownHeaderValues = {
'content-type':
| 'audio/aac'
| 'video/x-msvideo'
| 'image/avif'
| 'video/av1'
| 'application/octet-stream'
| 'image/bmp'
| 'text/css'
| 'text/csv'
| 'application/vnd.ms-fontobject'
| 'application/epub+zip'
| 'image/gif'
| 'application/gzip'
| 'text/html'
| 'image/x-icon'
| 'text/calendar'
| 'image/jpeg'
| 'text/javascript'
| 'application/json'
| 'application/ld+json'
| 'audio/x-midi'
| 'audio/mpeg'
| 'video/mp4'
| 'video/mpeg'
| 'audio/ogg'
| 'video/ogg'
| 'application/ogg'
| 'audio/opus'
| 'font/otf'
| 'application/pdf'
| 'image/png'
| 'application/rtf'
| 'image/svg+xml'
| 'image/tiff'
| 'video/mp2t'
| 'font/ttf'
| 'text/plain'
| 'application/wasm'
| 'video/webm'
| 'audio/webm'
| 'image/webp'
| 'font/woff'
| 'font/woff2'
| 'application/xhtml+xml'
| 'application/xml'
| 'application/zip'
| 'video/3gpp'
| 'video/3gpp2'
| 'model/gltf+json'
| 'model/gltf-binary'
| (string & {})
}

export type HeaderRecord = {
[K in HeaderNames | Lowercase<HeaderNames>]?: Lowercase<K> extends keyof KnownHeaderValues
? KnownHeaderValues[Lowercase<K>]
: string
}

0 comments on commit 14ba3e8

Please sign in to comment.