diff --git a/index.d.cts b/index.d.cts index a50a1a5110..5dcbc38098 100644 --- a/index.d.cts +++ b/index.d.cts @@ -4,13 +4,9 @@ interface RawAxiosHeaders { [key: string]: AxiosHeaderValue; } -type MethodsHeaders = { +type MethodsHeaders = Partial<{ [Key in axios.Method as Lowercase]: AxiosHeaders; -}; - -interface CommonHeaders { - common: AxiosHeaders; -} +} & {common: AxiosHeaders}>; type AxiosHeaderMatcher = (this: AxiosHeaders, value: string, name: string, headers: RawAxiosHeaders) => boolean; @@ -79,12 +75,12 @@ declare class AxiosError extends Error { constructor( message?: string, code?: string, - config?: axios.AxiosRequestConfig, + config?: axios.InternalAxiosRequestConfig, request?: any, response?: axios.AxiosResponse ); - config?: axios.AxiosRequestConfig; + config?: axios.InternalAxiosRequestConfig; code?: string; request?: any; response?: axios.AxiosResponse; @@ -110,24 +106,24 @@ declare class CanceledError extends AxiosError { } declare class Axios { - constructor(config?: axios.RawAxiosRequestConfig); + constructor(config?: axios.AxiosRequestConfig); defaults: axios.AxiosDefaults; interceptors: { - request: axios.AxiosInterceptorManager; + request: axios.AxiosInterceptorManager; response: axios.AxiosInterceptorManager; }; - getUri(config?: axios.RawAxiosRequestConfig): string; - request, D = any>(config: axios.RawAxiosRequestConfig): Promise; - get, D = any>(url: string, config?: axios.RawAxiosRequestConfig): Promise; - delete, D = any>(url: string, config?: axios.RawAxiosRequestConfig): Promise; - head, D = any>(url: string, config?: axios.RawAxiosRequestConfig): Promise; - options, D = any>(url: string, config?: axios.RawAxiosRequestConfig): Promise; - post, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig): Promise; - put, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig): Promise; - patch, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig): Promise; - postForm, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig): Promise; - putForm, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig): Promise; - patchForm, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig): Promise; + getUri(config?: axios.AxiosRequestConfig): string; + request, D = any>(config: axios.AxiosRequestConfig): Promise; + get, D = any>(url: string, config?: axios.AxiosRequestConfig): Promise; + delete, D = any>(url: string, config?: axios.AxiosRequestConfig): Promise; + head, D = any>(url: string, config?: axios.AxiosRequestConfig): Promise; + options, D = any>(url: string, config?: axios.AxiosRequestConfig): Promise; + post, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig): Promise; + put, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig): Promise; + patch, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig): Promise; + postForm, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig): Promise; + putForm, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig): Promise; + patchForm, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig): Promise; } declare enum HttpStatusCode { @@ -203,30 +199,34 @@ declare namespace axios { type CommonRequestHeadersList = 'Accept' | 'Content-Type' | 'Content-Length' | 'User-Agent'| 'Content-Encoding'; - type RawCommonRequestHeaders = { + type RawAxiosRequestHeaders = Partial; - type RawAxiosRequestHeaders = Partial; + type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders; - type AxiosRequestHeaders = Partial & AxiosHeaders; + type CommonResponseHeadersList = 'Server' | 'Content-Type' | 'Content-Length' | 'Cache-Control'| 'Content-Encoding'; - type RawAxiosResponseHeaders = Partial & { - "set-cookie"?: string[] - }>; + type RawCommonResponseHeaders = { + [Key in CommonResponseHeadersList]: AxiosHeaderValue; + } & { + "set-cookie": string[]; + }; + + type RawAxiosResponseHeaders = Partial; type AxiosResponseHeaders = RawAxiosResponseHeaders & AxiosHeaders; interface AxiosRequestTransformer { - (this: AxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any; + (this: InternalAxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any; } interface AxiosResponseTransformer { - (this: AxiosRequestConfig, data: any, headers: AxiosResponseHeaders, status?: number): any; + (this: InternalAxiosRequestConfig, data: any, headers: AxiosResponseHeaders, status?: number): any; } interface AxiosAdapter { - (config: AxiosRequestConfig): AxiosPromise; + (config: InternalAxiosRequestConfig): AxiosPromise; } interface AxiosBasicCredentials { @@ -355,13 +355,13 @@ declare namespace axios { type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName; - interface RawAxiosRequestConfig { + interface AxiosRequestConfig { url?: string; method?: Method | string; baseURL?: string; transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[]; transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[]; - headers?: RawAxiosRequestHeaders | AxiosHeaders; + headers?: (RawAxiosRequestHeaders & MethodsHeaders) | AxiosHeaders; params?: any; paramsSerializer?: ParamsSerializerOptions; data?: D; @@ -397,7 +397,10 @@ declare namespace axios { formSerializer?: FormSerializerOptions; } - interface AxiosRequestConfig extends RawAxiosRequestConfig { + // Alias + type RawAxiosRequestConfig = AxiosRequestConfig; + + interface InternalAxiosRequestConfig extends AxiosRequestConfig { headers: AxiosRequestHeaders; } @@ -415,11 +418,11 @@ declare namespace axios { unlink?: RawAxiosRequestHeaders; } - interface AxiosDefaults extends Omit, 'headers'> { + interface AxiosDefaults extends Omit, 'headers'> { headers: HeadersDefaults; } - interface CreateAxiosDefaults extends Omit, 'headers'> { + interface CreateAxiosDefaults extends Omit, 'headers'> { headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial; } @@ -428,7 +431,7 @@ declare namespace axios { status: number; statusText: string; headers: RawAxiosResponseHeaders | AxiosResponseHeaders; - config: AxiosRequestConfig; + config: InternalAxiosRequestConfig; request?: any; } @@ -443,7 +446,7 @@ declare namespace axios { } interface Canceler { - (message?: string, config?: RawAxiosRequestConfig, request?: any): void; + (message?: string, config?: AxiosRequestConfig, request?: any): void; } interface CancelTokenStatic { @@ -464,7 +467,7 @@ declare namespace axios { interface AxiosInterceptorOptions { synchronous?: boolean; - runWhen?: (config: AxiosRequestConfig) => boolean; + runWhen?: (config: InternalAxiosRequestConfig) => boolean; } interface AxiosInterceptorManager { @@ -474,8 +477,8 @@ declare namespace axios { } interface AxiosInstance extends Axios { - , D = any>(config: RawAxiosRequestConfig): Promise; - , D = any>(url: string, config?: RawAxiosRequestConfig): Promise; + , D = any>(config: AxiosRequestConfig): Promise; + , D = any>(url: string, config?: AxiosRequestConfig): Promise; defaults: Omit & { headers: HeadersDefaults & { diff --git a/index.d.ts b/index.d.ts index ed6a19bad8..4cde8ff07f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,13 +5,9 @@ interface RawAxiosHeaders { [key: string]: AxiosHeaderValue; } -type MethodsHeaders = { +type MethodsHeaders = Partial<{ [Key in Method as Lowercase]: AxiosHeaders; -}; - -interface CommonHeaders { - common: AxiosHeaders; -} +} & {common: AxiosHeaders}>; type AxiosHeaderMatcher = (this: AxiosHeaders, value: string, name: string, headers: RawAxiosHeaders) => boolean; @@ -78,30 +74,34 @@ export class AxiosHeaders { type CommonRequestHeadersList = 'Accept' | 'Content-Type' | 'Content-Length' | 'User-Agent'| 'Content-Encoding'; -type RawCommonRequestHeaders = { +export type RawAxiosRequestHeaders = Partial; -export type RawAxiosRequestHeaders = Partial; +export type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders; -export type AxiosRequestHeaders = Partial & AxiosHeaders; +type CommonResponseHeadersList = 'Server' | 'Content-Type' | 'Content-Length' | 'Cache-Control'| 'Content-Encoding'; -export type RawAxiosResponseHeaders = Partial & { - "set-cookie"?: string[] -}>; +type RawCommonResponseHeaders = { + [Key in CommonResponseHeadersList]: AxiosHeaderValue; +} & { + "set-cookie": string[]; +}; + +export type RawAxiosResponseHeaders = Partial; export type AxiosResponseHeaders = RawAxiosResponseHeaders & AxiosHeaders; export interface AxiosRequestTransformer { - (this: AxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any; + (this: InternalAxiosRequestConfig, data: any, headers: AxiosRequestHeaders): any; } export interface AxiosResponseTransformer { - (this: AxiosRequestConfig, data: any, headers: AxiosResponseHeaders, status?: number): any; + (this: InternalAxiosRequestConfig, data: any, headers: AxiosResponseHeaders, status?: number): any; } export interface AxiosAdapter { - (config: AxiosRequestConfig): AxiosPromise; + (config: InternalAxiosRequestConfig): AxiosPromise; } export interface AxiosBasicCredentials { @@ -296,13 +296,13 @@ type AxiosAdapterName = 'xhr' | 'http' | string; type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName; -export interface RawAxiosRequestConfig { +export interface AxiosRequestConfig { url?: string; method?: Method | string; baseURL?: string; transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[]; transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[]; - headers?: RawAxiosRequestHeaders | AxiosHeaders; + headers?: (RawAxiosRequestHeaders & MethodsHeaders) | AxiosHeaders; params?: any; paramsSerializer?: ParamsSerializerOptions; data?: D; @@ -338,7 +338,10 @@ export interface RawAxiosRequestConfig { formSerializer?: FormSerializerOptions; } -export interface AxiosRequestConfig extends RawAxiosRequestConfig { +// Alias +export type RawAxiosRequestConfig = AxiosRequestConfig; + +export interface InternalAxiosRequestConfig extends AxiosRequestConfig { headers: AxiosRequestHeaders; } @@ -356,11 +359,11 @@ export interface HeadersDefaults { unlink?: RawAxiosRequestHeaders; } -export interface AxiosDefaults extends Omit, 'headers'> { +export interface AxiosDefaults extends Omit, 'headers'> { headers: HeadersDefaults; } -export interface CreateAxiosDefaults extends Omit, 'headers'> { +export interface CreateAxiosDefaults extends Omit, 'headers'> { headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial; } @@ -369,7 +372,7 @@ export interface AxiosResponse { status: number; statusText: string; headers: RawAxiosResponseHeaders | AxiosResponseHeaders; - config: AxiosRequestConfig; + config: InternalAxiosRequestConfig; request?: any; } @@ -377,12 +380,12 @@ export class AxiosError extends Error { constructor( message?: string, code?: string, - config?: AxiosRequestConfig, + config?: InternalAxiosRequestConfig, request?: any, response?: AxiosResponse ); - config?: AxiosRequestConfig; + config?: InternalAxiosRequestConfig; code?: string; request?: any; response?: AxiosResponse; @@ -393,7 +396,7 @@ export class AxiosError extends Error { static from( error: Error | unknown, code?: string, - config?: AxiosRequestConfig, + config?: InternalAxiosRequestConfig, request?: any, response?: AxiosResponse, customProps?: object, @@ -426,7 +429,7 @@ export interface Cancel { } export interface Canceler { - (message?: string, config?: RawAxiosRequestConfig, request?: any): void; + (message?: string, config?: AxiosRequestConfig, request?: any): void; } export interface CancelTokenStatic { @@ -447,7 +450,7 @@ export interface CancelTokenSource { export interface AxiosInterceptorOptions { synchronous?: boolean; - runWhen?: (config: AxiosRequestConfig) => boolean; + runWhen?: (config: InternalAxiosRequestConfig) => boolean; } export interface AxiosInterceptorManager { @@ -457,29 +460,29 @@ export interface AxiosInterceptorManager { } export class Axios { - constructor(config?: RawAxiosRequestConfig); + constructor(config?: AxiosRequestConfig); defaults: AxiosDefaults; interceptors: { - request: AxiosInterceptorManager; + request: AxiosInterceptorManager; response: AxiosInterceptorManager; }; - getUri(config?: RawAxiosRequestConfig): string; - request, D = any>(config: RawAxiosRequestConfig): Promise; - get, D = any>(url: string, config?: RawAxiosRequestConfig): Promise; - delete, D = any>(url: string, config?: RawAxiosRequestConfig): Promise; - head, D = any>(url: string, config?: RawAxiosRequestConfig): Promise; - options, D = any>(url: string, config?: RawAxiosRequestConfig): Promise; - post, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig): Promise; - put, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig): Promise; - patch, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig): Promise; - postForm, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig): Promise; - putForm, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig): Promise; - patchForm, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig): Promise; + getUri(config?: AxiosRequestConfig): string; + request, D = any>(config: AxiosRequestConfig): Promise; + get, D = any>(url: string, config?: AxiosRequestConfig): Promise; + delete, D = any>(url: string, config?: AxiosRequestConfig): Promise; + head, D = any>(url: string, config?: AxiosRequestConfig): Promise; + options, D = any>(url: string, config?: AxiosRequestConfig): Promise; + post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + patch, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + postForm, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + putForm, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + patchForm, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; } export interface AxiosInstance extends Axios { - , D = any>(config: RawAxiosRequestConfig): Promise; - , D = any>(url: string, config?: RawAxiosRequestConfig): Promise; + , D = any>(config: AxiosRequestConfig): Promise; + , D = any>(url: string, config?: AxiosRequestConfig): Promise; defaults: Omit & { headers: HeadersDefaults & { diff --git a/test/module/typings/cjs/index.ts b/test/module/typings/cjs/index.ts index 322444729f..3a270cfe51 100644 --- a/test/module/typings/cjs/index.ts +++ b/test/module/typings/cjs/index.ts @@ -1,6 +1,6 @@ import axios = require('axios'); -const config: axios.RawAxiosRequestConfig = { +const config: axios.AxiosRequestConfig = { url: '/user', method: 'get', baseURL: 'https://api.example.com/', @@ -38,11 +38,11 @@ const config: axios.RawAxiosRequestConfig = { cancelToken: new axios.CancelToken((cancel: axios.Canceler) => {}) }; -const nullValidateStatusConfig: axios.RawAxiosRequestConfig = { +const nullValidateStatusConfig: axios.AxiosRequestConfig = { validateStatus: null }; -const undefinedValidateStatusConfig: axios.RawAxiosRequestConfig = { +const undefinedValidateStatusConfig: axios.AxiosRequestConfig = { validateStatus: undefined }; @@ -276,19 +276,19 @@ axios.create({ // Interceptors const requestInterceptorId: number = axios.interceptors.request.use( - (config: axios.AxiosRequestConfig) => config, + (config: axios.InternalAxiosRequestConfig) => config, (error: any) => Promise.reject(error) ); axios.interceptors.request.eject(requestInterceptorId); axios.interceptors.request.use( - (config: axios.AxiosRequestConfig) => Promise.resolve(config), + (config: axios.InternalAxiosRequestConfig) => Promise.resolve(config), (error: any) => Promise.reject(error) ); -axios.interceptors.request.use((config: axios.AxiosRequestConfig) => config); -axios.interceptors.request.use((config: axios.AxiosRequestConfig) => Promise.resolve(config)); +axios.interceptors.request.use((config: axios.InternalAxiosRequestConfig) => config); +axios.interceptors.request.use((config: axios.InternalAxiosRequestConfig) => Promise.resolve(config)); const responseInterceptorId: number = axios.interceptors.response.use( (response: axios.AxiosResponse) => response, @@ -302,6 +302,13 @@ axios.interceptors.response.use( (error: any) => Promise.reject(error) ); +axios.interceptors.request.use(req => { + // https://github.com/axios/axios/issues/5415 + req.headers.set('foo', 'bar'); + req.headers['Content-Type'] = 123; + return req; +}); + const voidRequestInterceptorId = axios.interceptors.request.use( // @ts-expect-error -- Must return an axios.AxiosRequestConfig (or throw) (_response) => {}, @@ -323,7 +330,7 @@ axios.interceptors.response.clear(); // Adapters -const adapter: axios.AxiosAdapter = (config: axios.AxiosRequestConfig) => { +const adapter: axios.AxiosAdapter = (config: axios.InternalAxiosRequestConfig) => { const response: axios.AxiosResponse = { data: { foo: 'bar' }, status: 200, diff --git a/test/module/typings/esm/index.ts b/test/module/typings/esm/index.ts index 6738aef2ac..c2e29da45e 100644 --- a/test/module/typings/esm/index.ts +++ b/test/module/typings/esm/index.ts @@ -1,5 +1,5 @@ import axios, { - RawAxiosRequestConfig, + InternalAxiosRequestConfig, AxiosRequestConfig, AxiosHeaders, AxiosRequestHeaders, @@ -22,7 +22,7 @@ import axios, { spread } from 'axios'; -const config: RawAxiosRequestConfig = { +const config: AxiosRequestConfig = { url: '/user', method: 'get', baseURL: 'https://api.example.com/', @@ -60,11 +60,11 @@ const config: RawAxiosRequestConfig = { cancelToken: new axios.CancelToken((cancel: Canceler) => {}) }; -const nullValidateStatusConfig: RawAxiosRequestConfig = { +const nullValidateStatusConfig: AxiosRequestConfig = { validateStatus: null }; -const undefinedValidateStatusConfig: RawAxiosRequestConfig = { +const undefinedValidateStatusConfig: AxiosRequestConfig = { validateStatus: undefined }; @@ -515,7 +515,7 @@ axios.get('/user', { // issue #5034 -function getRequestConfig1(options: RawAxiosRequestConfig): RawAxiosRequestConfig { +function getRequestConfig1(options: AxiosRequestConfig): AxiosRequestConfig { return { ...options, headers: { @@ -525,7 +525,7 @@ function getRequestConfig1(options: RawAxiosRequestConfig): RawAxiosRequestConfi }; } -function getRequestConfig2(options: RawAxiosRequestConfig): RawAxiosRequestConfig { +function getRequestConfig2(options: AxiosRequestConfig): AxiosRequestConfig { return { ...options, headers: { @@ -609,7 +609,7 @@ for (const [header, value] of headers) { config.headers.foo = "1"; config.headers.set('bar', '2'); config.headers.set({myHeader: "myValue"}) - config.headers = new axios.AxiosHeaders({myHeader: "myValue"}); + config.headers = new AxiosHeaders({myHeader: "myValue"}); config.headers = {...config.headers} as AxiosRequestHeaders; return config; },