Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add autocompletable header types #3462

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadonlyArray is just not correct. If we were to follow the logic in the PR that added it, the type should be any.

Any array pair that doesn't contain exactly 2 strings throws an error too, so string[][] is not strict enough.


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 =
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
| '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 & {})
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved

type KnownHeaderValues = {
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
'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
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
? KnownHeaderValues[Lowercase<K>]
: string
}
Loading