From 9ec3fa7698809d72a826fbc385147273a89ec3d5 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Fri, 16 Aug 2024 21:59:43 +0200 Subject: [PATCH] types: add Autocomplete utility type --- types/dispatcher.d.ts | 3 +- types/header.d.ts | 110 ++++++++++++++++++++++-------------------- types/utility.d.ts | 7 +++ 3 files changed, 66 insertions(+), 54 deletions(-) create mode 100644 types/utility.d.ts diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index eafa2e5907e..a2cb6ba72d8 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -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; @@ -234,7 +235,7 @@ declare namespace Dispatcher { onBodySent?(chunkSize: number, totalBytesSent: number): void; } export type PipelineHandler = (data: PipelineHandlerData) => Readable; - export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH' | (string & Record); + export type HttpMethod = Autocomplete<'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'>; /** * @link https://fetch.spec.whatwg.org/#body-mixin diff --git a/types/header.d.ts b/types/header.d.ts index 355a46ea307..d514ede077e 100644 --- a/types/header.d.ts +++ b/types/header.d.ts @@ -1,9 +1,11 @@ +import { Autocomplete } from "./utility"; + /** * The header type declaration of `undici`. */ export type IncomingHttpHeaders = Record; -type HeaderNames = +type HeaderNames = Autocomplete< | 'Accept' | 'Accept-CH' | 'Accept-Charset' @@ -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 = { diff --git a/types/utility.d.ts b/types/utility.d.ts new file mode 100644 index 00000000000..9c2b5628e3a --- /dev/null +++ b/types/utility.d.ts @@ -0,0 +1,7 @@ +type AutocompletePrimitiveBaseType = + T extends string ? string : + T extends number ? number : + T extends boolean ? boolean : + never; + +export type Autocomplete = T | (AutocompletePrimitiveBaseType & Record);