From c7a76ddbf277db864ee6cfb4ef17b8a08ffbe3f5 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 9 Apr 2026 18:49:52 +0200 Subject: [PATCH] fix(types): make response headers case insensative (#10677) --- index.d.cts | 30 +++++- index.d.ts | 306 +++++++++++++++++++++------------------------------- 2 files changed, 152 insertions(+), 184 deletions(-) diff --git a/index.d.cts b/index.d.cts index 8f086191..f36ef066 100644 --- a/index.d.cts +++ b/index.d.cts @@ -39,6 +39,8 @@ type CommonResponseHeadersList = | 'Cache-Control' | 'Content-Encoding'; +type CommonResponseHeaderKey = CommonResponseHeadersList | Lowercase; + type BrowserProgressEvent = any; declare class AxiosHeaders { @@ -307,7 +309,7 @@ declare namespace axios { type AxiosHeaderValue = AxiosHeaders | string | string[] | number | boolean | null; type RawCommonResponseHeaders = { - [Key in CommonResponseHeadersList]: AxiosHeaderValue; + [Key in CommonResponseHeaderKey]: AxiosHeaderValue; } & { 'set-cookie': string[]; }; @@ -345,13 +347,35 @@ declare namespace axios { protocol?: string; } - type UppercaseMethod = "GET" | "DELETE" | "HEAD" | "OPTIONS" | "POST" | "PUT" | "PATCH" | "PURGE" | "LINK" | "UNLINK"; + type UppercaseMethod = + | 'GET' + | 'DELETE' + | 'HEAD' + | 'OPTIONS' + | 'POST' + | 'PUT' + | 'PATCH' + | 'PURGE' + | 'LINK' + | 'UNLINK'; type Method = (UppercaseMethod | Lowercase) & {}; type ResponseType = 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream' | 'formdata'; - type UppercaseResponseEncoding = "ASCII" | "ANSI" | "BINARY" | "BASE64" | "BASE64URL" | "HEX" | "LATIN1" | "UCS-2" | "UCS2" | "UTF-8" | "UTF8" | "UTF16LE"; + type UppercaseResponseEncoding = + | 'ASCII' + | 'ANSI' + | 'BINARY' + | 'BASE64' + | 'BASE64URL' + | 'HEX' + | 'LATIN1' + | 'UCS-2' + | 'UCS2' + | 'UTF-8' + | 'UTF8' + | 'UTF16LE'; type responseEncoding = (UppercaseResponseEncoding | Lowercase) & {}; diff --git a/index.d.ts b/index.d.ts index 935356cd..d3f646ea 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,13 +1,7 @@ // TypeScript Version: 4.7 type StringLiteralsOrString = Literals | (string & {}); -export type AxiosHeaderValue = - | AxiosHeaders - | string - | string[] - | number - | boolean - | null; +export type AxiosHeaderValue = AxiosHeaders | string | string[] | number | boolean | null; interface RawAxiosHeaders { [key: string]: AxiosHeaderValue; @@ -24,11 +18,7 @@ type AxiosHeaderMatcher = | RegExp | ((this: AxiosHeaders, value: string, name: string) => boolean); -type AxiosHeaderParser = ( - this: AxiosHeaders, - value: AxiosHeaderValue, - header: string, -) => any; +type AxiosHeaderParser = (this: AxiosHeaders, value: AxiosHeaderValue, header: string) => any; export class AxiosHeaders { constructor(headers?: RawAxiosHeaders | AxiosHeaders | string); @@ -38,12 +28,9 @@ export class AxiosHeaders { set( headerName?: string, value?: AxiosHeaderValue, - rewrite?: boolean | AxiosHeaderMatcher, - ): AxiosHeaders; - set( - headers?: RawAxiosHeaders | AxiosHeaders | string, - rewrite?: boolean, + rewrite?: boolean | AxiosHeaderMatcher ): AxiosHeaders; + set(headers?: RawAxiosHeaders | AxiosHeaders | string, rewrite?: boolean): AxiosHeaders; get(headerName: string, parser: RegExp): RegExpExecArray | null; get(headerName: string, matcher?: true | AxiosHeaderParser): AxiosHeaderValue; @@ -57,9 +44,7 @@ export class AxiosHeaders { normalize(format: boolean): AxiosHeaders; concat( - ...targets: Array< - AxiosHeaders | RawAxiosHeaders | string | undefined | null - > + ...targets: Array ): AxiosHeaders; toJSON(asStrings?: boolean): RawAxiosHeaders; @@ -69,55 +54,35 @@ export class AxiosHeaders { static accessor(header: string | string[]): AxiosHeaders; static concat( - ...targets: Array< - AxiosHeaders | RawAxiosHeaders | string | undefined | null - > + ...targets: Array ): AxiosHeaders; - setContentType( - value: ContentType, - rewrite?: boolean | AxiosHeaderMatcher, - ): AxiosHeaders; + setContentType(value: ContentType, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders; getContentType(parser?: RegExp): RegExpExecArray | null; getContentType(matcher?: AxiosHeaderMatcher): AxiosHeaderValue; hasContentType(matcher?: AxiosHeaderMatcher): boolean; - setContentLength( - value: AxiosHeaderValue, - rewrite?: boolean | AxiosHeaderMatcher, - ): AxiosHeaders; + setContentLength(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders; getContentLength(parser?: RegExp): RegExpExecArray | null; getContentLength(matcher?: AxiosHeaderMatcher): AxiosHeaderValue; hasContentLength(matcher?: AxiosHeaderMatcher): boolean; - setAccept( - value: AxiosHeaderValue, - rewrite?: boolean | AxiosHeaderMatcher, - ): AxiosHeaders; + setAccept(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders; getAccept(parser?: RegExp): RegExpExecArray | null; getAccept(matcher?: AxiosHeaderMatcher): AxiosHeaderValue; hasAccept(matcher?: AxiosHeaderMatcher): boolean; - setUserAgent( - value: AxiosHeaderValue, - rewrite?: boolean | AxiosHeaderMatcher, - ): AxiosHeaders; + setUserAgent(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders; getUserAgent(parser?: RegExp): RegExpExecArray | null; getUserAgent(matcher?: AxiosHeaderMatcher): AxiosHeaderValue; hasUserAgent(matcher?: AxiosHeaderMatcher): boolean; - setContentEncoding( - value: AxiosHeaderValue, - rewrite?: boolean | AxiosHeaderMatcher, - ): AxiosHeaders; + setContentEncoding(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders; getContentEncoding(parser?: RegExp): RegExpExecArray | null; getContentEncoding(matcher?: AxiosHeaderMatcher): AxiosHeaderValue; hasContentEncoding(matcher?: AxiosHeaderMatcher): boolean; - setAuthorization( - value: AxiosHeaderValue, - rewrite?: boolean | AxiosHeaderMatcher, - ): AxiosHeaders; + setAuthorization(value: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders; getAuthorization(parser?: RegExp): RegExpExecArray | null; getAuthorization(matcher?: AxiosHeaderMatcher): AxiosHeaderValue; hasAuthorization(matcher?: AxiosHeaderMatcher): boolean; @@ -128,57 +93,53 @@ export class AxiosHeaders { } type CommonRequestHeadersList = - | "Accept" - | "Content-Length" - | "User-Agent" - | "Content-Encoding" - | "Authorization" - | "Location"; + | 'Accept' + | 'Content-Length' + | 'User-Agent' + | 'Content-Encoding' + | 'Authorization' + | 'Location'; type ContentType = | AxiosHeaderValue - | "text/html" - | "text/plain" - | "multipart/form-data" - | "application/json" - | "application/x-www-form-urlencoded" - | "application/octet-stream"; + | 'text/html' + | 'text/plain' + | 'multipart/form-data' + | 'application/json' + | 'application/x-www-form-urlencoded' + | 'application/octet-stream'; export type RawAxiosRequestHeaders = Partial< RawAxiosHeaders & { [Key in CommonRequestHeadersList]: AxiosHeaderValue; } & { - "Content-Type": ContentType; + 'Content-Type': ContentType; } >; export type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders; type CommonResponseHeadersList = - | "Server" - | "Content-Type" - | "Content-Length" - | "Cache-Control" - | "Content-Encoding"; + | 'Server' + | 'Content-Type' + | 'Content-Length' + | 'Cache-Control' + | 'Content-Encoding'; + +type CommonResponseHeaderKey = CommonResponseHeadersList | Lowercase; type RawCommonResponseHeaders = { - [Key in CommonResponseHeadersList]: AxiosHeaderValue; + [Key in CommonResponseHeaderKey]: AxiosHeaderValue; } & { - "set-cookie": string[]; + 'set-cookie': string[]; }; -export type RawAxiosResponseHeaders = Partial< - RawAxiosHeaders & RawCommonResponseHeaders ->; +export type RawAxiosResponseHeaders = Partial; export type AxiosResponseHeaders = RawAxiosResponseHeaders & AxiosHeaders; export interface AxiosRequestTransformer { - ( - this: InternalAxiosRequestConfig, - data: any, - headers: AxiosRequestHeaders, - ): any; + (this: InternalAxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any; } export interface AxiosResponseTransformer { @@ -186,7 +147,7 @@ export interface AxiosResponseTransformer { this: InternalAxiosRequestConfig, data: any, headers: AxiosResponseHeaders, - status?: number, + status?: number ): any; } @@ -272,22 +233,47 @@ export enum HttpStatusCode { NetworkAuthenticationRequired = 511, } -type UppercaseMethod = "GET" | "DELETE" | "HEAD" | "OPTIONS" | "POST" | "PUT" | "PATCH" | "PURGE" | "LINK" | "UNLINK"; +type UppercaseMethod = + | 'GET' + | 'DELETE' + | 'HEAD' + | 'OPTIONS' + | 'POST' + | 'PUT' + | 'PATCH' + | 'PURGE' + | 'LINK' + | 'UNLINK'; export type Method = (UppercaseMethod | Lowercase) & {}; export type ResponseType = - | "arraybuffer" - | "blob" - | "document" - | "json" - | "text" - | "stream" - | "formdata"; + | 'arraybuffer' + | 'blob' + | 'document' + | 'json' + | 'text' + | 'stream' + | 'formdata'; -type UppercaseResponseEncoding = "ASCII" | "ANSI" | "BINARY" | "BASE64" | "BASE64URL" | "HEX" | "LATIN1" | "UCS-2" | "UCS2" | "UTF-8" | "UTF8" | "UTF16LE"; +type UppercaseResponseEncoding = + | 'ASCII' + | 'ANSI' + | 'BINARY' + | 'BASE64' + | 'BASE64URL' + | 'HEX' + | 'LATIN1' + | 'UCS-2' + | 'UCS2' + | 'UTF-8' + | 'UTF8' + | 'UTF16LE'; -export type responseEncoding = (UppercaseResponseEncoding | Lowercase) & {}; +export type responseEncoding = ( + | UppercaseResponseEncoding + | Lowercase +) & {}; export interface TransitionalOptions { silentJSONParsing?: boolean; @@ -315,7 +301,7 @@ export interface SerializerVisitor { value: any, key: string | number, path: null | Array, - helpers: FormDataVisitorHelpers, + helpers: FormDataVisitorHelpers ): boolean; } @@ -363,7 +349,7 @@ export interface AxiosProgressEvent { type Milliseconds = number; -type AxiosAdapterName = StringLiteralsOrString<"xhr" | "http" | "fetch">; +type AxiosAdapterName = StringLiteralsOrString<'xhr' | 'http' | 'fetch'>; type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName; @@ -408,7 +394,7 @@ export interface AxiosRequestConfig { responseDetails: { headers: Record; statusCode: HttpStatusCode; - }, + } ) => void; socketPath?: string | null; transport?: any; @@ -422,24 +408,11 @@ export interface AxiosRequestConfig { insecureHTTPParser?: boolean; env?: { FormData?: new (...args: any[]) => object; - fetch?: ( - input: URL | Request | string, - init?: RequestInit, - ) => Promise; - Request?: new ( - input: URL | Request | string, - init?: RequestInit, - ) => Request; + fetch?: (input: URL | Request | string, init?: RequestInit) => Promise; + Request?: new (input: URL | Request | string, init?: RequestInit) => Request; Response?: new ( - body?: - | ArrayBuffer - | ArrayBufferView - | Blob - | FormData - | URLSearchParams - | string - | null, - init?: ResponseInit, + body?: ArrayBuffer | ArrayBufferView | Blob | FormData | URLSearchParams | string | null, + init?: ResponseInit ) => Response; }; formSerializer?: FormSerializerOptions; @@ -451,26 +424,18 @@ export interface AxiosRequestConfig { cb: ( err: Error | null, address: LookupAddress | LookupAddress[], - family?: AddressFamily, - ) => void, + family?: AddressFamily + ) => void ) => void) | (( hostname: string, - options: object, + options: object ) => Promise< - | [ - address: LookupAddressEntry | LookupAddressEntry[], - family?: AddressFamily, - ] - | LookupAddress + [address: LookupAddressEntry | LookupAddressEntry[], family?: AddressFamily] | LookupAddress >); - withXSRFToken?: - | boolean - | ((config: InternalAxiosRequestConfig) => boolean | undefined); + withXSRFToken?: boolean | ((config: InternalAxiosRequestConfig) => boolean | undefined); parseReviver?: (this: any, key: string, value: any) => any; - fetchOptions?: - | Omit - | Record; + fetchOptions?: Omit | Record; httpVersion?: 1 | 2; http2Options?: Record & { sessionTimeout?: number; @@ -480,9 +445,7 @@ export interface AxiosRequestConfig { // Alias export type RawAxiosRequestConfig = AxiosRequestConfig; -export interface InternalAxiosRequestConfig< - D = any, -> extends AxiosRequestConfig { +export interface InternalAxiosRequestConfig extends AxiosRequestConfig { headers: AxiosRequestHeaders; } @@ -500,17 +463,11 @@ export interface HeadersDefaults { unlink?: RawAxiosRequestHeaders; } -export interface AxiosDefaults extends Omit< - AxiosRequestConfig, - "headers" -> { +export interface AxiosDefaults extends Omit, 'headers'> { headers: HeadersDefaults; } -export interface CreateAxiosDefaults extends Omit< - AxiosRequestConfig, - "headers" -> { +export interface CreateAxiosDefaults extends Omit, 'headers'> { headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial; } @@ -529,7 +486,7 @@ export class AxiosError extends Error { code?: string, config?: InternalAxiosRequestConfig, request?: any, - response?: AxiosResponse, + response?: AxiosResponse ); config?: InternalAxiosRequestConfig; @@ -547,24 +504,24 @@ export class AxiosError extends Error { config?: InternalAxiosRequestConfig, request?: any, response?: AxiosResponse, - customProps?: object, + customProps?: object ): AxiosError; - static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS"; - static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE"; - static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION"; - static readonly ERR_NETWORK = "ERR_NETWORK"; - static readonly ERR_DEPRECATED = "ERR_DEPRECATED"; - static readonly ERR_BAD_RESPONSE = "ERR_BAD_RESPONSE"; - static readonly ERR_BAD_REQUEST = "ERR_BAD_REQUEST"; - static readonly ERR_NOT_SUPPORT = "ERR_NOT_SUPPORT"; - static readonly ERR_INVALID_URL = "ERR_INVALID_URL"; - static readonly ERR_CANCELED = "ERR_CANCELED"; - static readonly ECONNABORTED = "ECONNABORTED"; - static readonly ETIMEDOUT = "ETIMEDOUT"; + static readonly ERR_FR_TOO_MANY_REDIRECTS = 'ERR_FR_TOO_MANY_REDIRECTS'; + static readonly ERR_BAD_OPTION_VALUE = 'ERR_BAD_OPTION_VALUE'; + static readonly ERR_BAD_OPTION = 'ERR_BAD_OPTION'; + static readonly ERR_NETWORK = 'ERR_NETWORK'; + static readonly ERR_DEPRECATED = 'ERR_DEPRECATED'; + static readonly ERR_BAD_RESPONSE = 'ERR_BAD_RESPONSE'; + static readonly ERR_BAD_REQUEST = 'ERR_BAD_REQUEST'; + static readonly ERR_NOT_SUPPORT = 'ERR_NOT_SUPPORT'; + static readonly ERR_INVALID_URL = 'ERR_INVALID_URL'; + static readonly ERR_CANCELED = 'ERR_CANCELED'; + static readonly ECONNABORTED = 'ECONNABORTED'; + static readonly ETIMEDOUT = 'ETIMEDOUT'; } export class CanceledError extends AxiosError { - readonly name: "CanceledError"; + readonly name: 'CanceledError'; } export type AxiosPromise = Promise>; @@ -608,12 +565,12 @@ type AxiosInterceptorRejected = (error: any) => any; type AxiosRequestInterceptorUse = ( onFulfilled?: AxiosInterceptorFulfilled | null, onRejected?: AxiosInterceptorRejected | null, - options?: AxiosInterceptorOptions, + options?: AxiosInterceptorOptions ) => number; type AxiosResponseInterceptorUse = ( onFulfilled?: AxiosInterceptorFulfilled | null, - onRejected?: AxiosInterceptorRejected | null, + onRejected?: AxiosInterceptorRejected | null ) => number; interface AxiosInterceptorHandler { @@ -624,9 +581,7 @@ interface AxiosInterceptorHandler { } export interface AxiosInterceptorManager { - use: V extends AxiosResponse - ? AxiosResponseInterceptorUse - : AxiosRequestInterceptorUse; + use: V extends AxiosResponse ? AxiosResponseInterceptorUse : AxiosRequestInterceptorUse; eject(id: number): void; clear(): void; handlers?: Array>; @@ -640,68 +595,61 @@ export class Axios { response: AxiosInterceptorManager; }; getUri(config?: AxiosRequestConfig): string; - request, D = any>( - config: AxiosRequestConfig, - ): Promise; + request, D = any>(config: AxiosRequestConfig): Promise; get, D = any>( url: string, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; delete, D = any>( url: string, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; head, D = any>( url: string, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; options, D = any>( url: string, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; post, D = any>( url: string, data?: D, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; put, D = any>( url: string, data?: D, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; patch, D = any>( url: string, data?: D, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; postForm, D = any>( url: string, data?: D, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; putForm, D = any>( url: string, data?: D, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; patchForm, D = any>( url: string, data?: D, - config?: AxiosRequestConfig, + config?: AxiosRequestConfig ): Promise; } export interface AxiosInstance extends Axios { - , D = any>( - config: AxiosRequestConfig, - ): Promise; - , D = any>( - url: string, - config?: AxiosRequestConfig, - ): Promise; + , D = any>(config: AxiosRequestConfig): Promise; + , D = any>(url: string, config?: AxiosRequestConfig): Promise; create(config?: CreateAxiosDefaults): AxiosInstance; - defaults: Omit & { + defaults: Omit & { headers: HeadersDefaults & { [key: string]: AxiosHeaderValue; }; @@ -719,22 +667,18 @@ export interface GenericHTMLFormElement { } export function getAdapter( - adapters: AxiosAdapterConfig | AxiosAdapterConfig[] | undefined, + adapters: AxiosAdapterConfig | AxiosAdapterConfig[] | undefined ): AxiosAdapter; export function toFormData( sourceObj: object, targetFormData?: GenericFormData, - options?: FormSerializerOptions, + options?: FormSerializerOptions ): GenericFormData; -export function formToJSON( - form: GenericFormData | GenericHTMLFormElement, -): object; +export function formToJSON(form: GenericFormData | GenericHTMLFormElement): object; -export function isAxiosError( - payload: any, -): payload is AxiosError; +export function isAxiosError(payload: any): payload is AxiosError; export function spread(callback: (...args: T[]) => R): (array: T[]) => R; @@ -744,7 +688,7 @@ export function all(values: Array>): Promise; export function mergeConfig( config1: AxiosRequestConfig, - config2: AxiosRequestConfig, + config2: AxiosRequestConfig ): AxiosRequestConfig; export interface AxiosStatic extends AxiosInstance {