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

types: add Autocomplete utility type #3479

Merged
merged 1 commit into from
Aug 19, 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
3 changes: 2 additions & 1 deletion types/dispatcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IncomingHttpHeaders } from './header'
import BodyReadable from './readable'
import { FormData } from './formdata'
import Errors from './errors'
import { Autocomplete } from './utility'

type AbortSignal = unknown;

Expand Down Expand Up @@ -234,7 +235,7 @@ declare namespace Dispatcher {
onBodySent?(chunkSize: number, totalBytesSent: number): void;
}
export type PipelineHandler<TOpaque = null> = (data: PipelineHandlerData<TOpaque>) => Readable;
export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH' | (string & Record<never, never>);
export type HttpMethod = Autocomplete<'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'>;

/**
* @link https://fetch.spec.whatwg.org/#body-mixin
Expand Down
110 changes: 57 additions & 53 deletions types/header.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Autocomplete } from "./utility";

/**
* The header type declaration of `undici`.
*/
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>;

type HeaderNames =
type HeaderNames = Autocomplete<
| 'Accept'
| 'Accept-CH'
| 'Accept-Charset'
Expand Down Expand Up @@ -93,60 +95,62 @@ type HeaderNames =
| 'WWW-Authenticate'
| 'X-Content-Type-Options'
| 'X-Frame-Options'
| (string & {})
>

type IANARegisteredMimeType = Autocomplete<
| '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'
>

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 & {})
'content-type': IANARegisteredMimeType
}

export type HeaderRecord = {
Expand Down
7 changes: 7 additions & 0 deletions types/utility.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type AutocompletePrimitiveBaseType<T> =
T extends string ? string :
T extends number ? number :
T extends boolean ? boolean :
never;

export type Autocomplete<T> = T | (AutocompletePrimitiveBaseType<T> & Record<never, never>);
Loading