mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(types): fixed AxiosRequestConfig header interface by refactoring it to RawAxiosRequestConfig; (#5420)
This commit is contained in:
+23
-19
@@ -103,24 +103,24 @@ declare class CanceledError<T> extends AxiosError<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
declare class Axios {
|
declare class Axios {
|
||||||
constructor(config?: axios.AxiosRequestConfig);
|
constructor(config?: axios.RawAxiosRequestConfig);
|
||||||
defaults: axios.AxiosDefaults;
|
defaults: axios.AxiosDefaults;
|
||||||
interceptors: {
|
interceptors: {
|
||||||
request: axios.AxiosInterceptorManager<axios.AxiosRequestConfig>;
|
request: axios.AxiosInterceptorManager<axios.AxiosRequestConfig>;
|
||||||
response: axios.AxiosInterceptorManager<axios.AxiosResponse>;
|
response: axios.AxiosInterceptorManager<axios.AxiosResponse>;
|
||||||
};
|
};
|
||||||
getUri(config?: axios.AxiosRequestConfig): string;
|
getUri(config?: axios.RawAxiosRequestConfig): string;
|
||||||
request<T = any, R = axios.AxiosResponse<T>, D = any>(config: axios.AxiosRequestConfig<D>): Promise<R>;
|
request<T = any, R = axios.AxiosResponse<T>, D = any>(config: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
get<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
get<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
delete<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
delete<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
head<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
head<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
options<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
options<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
post<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
post<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
put<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
put<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
patch<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
patch<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
postForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
postForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
putForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
putForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
patchForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
|
patchForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare enum HttpStatusCode {
|
declare enum HttpStatusCode {
|
||||||
@@ -342,7 +342,7 @@ declare namespace axios {
|
|||||||
|
|
||||||
type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
|
type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
|
||||||
|
|
||||||
interface AxiosRequestConfig<D = any> {
|
interface RawAxiosRequestConfig<D = any> {
|
||||||
url?: string;
|
url?: string;
|
||||||
method?: Method | string;
|
method?: Method | string;
|
||||||
baseURL?: string;
|
baseURL?: string;
|
||||||
@@ -384,6 +384,10 @@ declare namespace axios {
|
|||||||
formSerializer?: FormSerializerOptions;
|
formSerializer?: FormSerializerOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface AxiosRequestConfig<D = any> extends RawAxiosRequestConfig {
|
||||||
|
headers: AxiosRequestHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
interface HeadersDefaults {
|
interface HeadersDefaults {
|
||||||
common: RawAxiosRequestHeaders;
|
common: RawAxiosRequestHeaders;
|
||||||
delete: RawAxiosRequestHeaders;
|
delete: RawAxiosRequestHeaders;
|
||||||
@@ -398,11 +402,11 @@ declare namespace axios {
|
|||||||
unlink?: RawAxiosRequestHeaders;
|
unlink?: RawAxiosRequestHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
|
interface AxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
|
||||||
headers: HeadersDefaults;
|
headers: HeadersDefaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
|
interface CreateAxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
|
||||||
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
|
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -426,7 +430,7 @@ declare namespace axios {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Canceler {
|
interface Canceler {
|
||||||
(message?: string, config?: AxiosRequestConfig, request?: any): void;
|
(message?: string, config?: RawAxiosRequestConfig, request?: any): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CancelTokenStatic {
|
interface CancelTokenStatic {
|
||||||
@@ -457,8 +461,8 @@ declare namespace axios {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface AxiosInstance extends Axios {
|
interface AxiosInstance extends Axios {
|
||||||
<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
|
<T = any, R = AxiosResponse<T>, D = any>(config: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
|
|
||||||
defaults: Omit<AxiosDefaults, 'headers'> & {
|
defaults: Omit<AxiosDefaults, 'headers'> & {
|
||||||
headers: HeadersDefaults & {
|
headers: HeadersDefaults & {
|
||||||
|
|||||||
Vendored
+23
-19
@@ -283,7 +283,7 @@ type AxiosAdapterName = 'xhr' | 'http' | string;
|
|||||||
|
|
||||||
type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
|
type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;
|
||||||
|
|
||||||
export interface AxiosRequestConfig<D = any> {
|
export interface RawAxiosRequestConfig<D = any> {
|
||||||
url?: string;
|
url?: string;
|
||||||
method?: Method | string;
|
method?: Method | string;
|
||||||
baseURL?: string;
|
baseURL?: string;
|
||||||
@@ -325,6 +325,10 @@ export interface AxiosRequestConfig<D = any> {
|
|||||||
formSerializer?: FormSerializerOptions;
|
formSerializer?: FormSerializerOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AxiosRequestConfig<D = any> extends RawAxiosRequestConfig {
|
||||||
|
headers: AxiosRequestHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
export interface HeadersDefaults {
|
export interface HeadersDefaults {
|
||||||
common: RawAxiosRequestHeaders;
|
common: RawAxiosRequestHeaders;
|
||||||
delete: RawAxiosRequestHeaders;
|
delete: RawAxiosRequestHeaders;
|
||||||
@@ -339,11 +343,11 @@ export interface HeadersDefaults {
|
|||||||
unlink?: RawAxiosRequestHeaders;
|
unlink?: RawAxiosRequestHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
|
export interface AxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
|
||||||
headers: HeadersDefaults;
|
headers: HeadersDefaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
|
export interface CreateAxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
|
||||||
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
|
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -409,7 +413,7 @@ export interface Cancel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Canceler {
|
export interface Canceler {
|
||||||
(message?: string, config?: AxiosRequestConfig, request?: any): void;
|
(message?: string, config?: RawAxiosRequestConfig, request?: any): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CancelTokenStatic {
|
export interface CancelTokenStatic {
|
||||||
@@ -440,29 +444,29 @@ export interface AxiosInterceptorManager<V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Axios {
|
export class Axios {
|
||||||
constructor(config?: AxiosRequestConfig);
|
constructor(config?: RawAxiosRequestConfig);
|
||||||
defaults: AxiosDefaults;
|
defaults: AxiosDefaults;
|
||||||
interceptors: {
|
interceptors: {
|
||||||
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
||||||
response: AxiosInterceptorManager<AxiosResponse>;
|
response: AxiosInterceptorManager<AxiosResponse>;
|
||||||
};
|
};
|
||||||
getUri(config?: AxiosRequestConfig): string;
|
getUri(config?: RawAxiosRequestConfig): string;
|
||||||
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
|
request<T = any, R = AxiosResponse<T>, D = any>(config: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosInstance extends Axios {
|
export interface AxiosInstance extends Axios {
|
||||||
<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
|
<T = any, R = AxiosResponse<T>, D = any>(config: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
|
||||||
|
|
||||||
defaults: Omit<AxiosDefaults, 'headers'> & {
|
defaults: Omit<AxiosDefaults, 'headers'> & {
|
||||||
headers: HeadersDefaults & {
|
headers: HeadersDefaults & {
|
||||||
|
|||||||
+65
-62
@@ -58,90 +58,93 @@ describe('module', function () {
|
|||||||
await remove(BACKUP_PATH);
|
await remove(BACKUP_PATH);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have consistent ESM export', function () {
|
describe('export', function () {
|
||||||
const namedExport = {};
|
|
||||||
const factoryExport = {};
|
|
||||||
|
|
||||||
Object.entries(axiosFactory).forEach(([key, value]) => {
|
it('should have consistent ESM export', function () {
|
||||||
if(!utils.hasOwnProp(Axios, key) && !(key in instance) && ignoreList.indexOf(key) === -1) {
|
const namedExport = {};
|
||||||
factoryExport[key] = value;
|
const factoryExport = {};
|
||||||
}
|
|
||||||
|
Object.entries(axiosFactory).forEach(([key, value]) => {
|
||||||
|
if (!utils.hasOwnProp(Axios, key) && !(key in instance) && ignoreList.indexOf(key) === -1) {
|
||||||
|
factoryExport[key] = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.entries(axios).forEach(([key, value]) => {
|
||||||
|
key !== 'default' && ignoreList.indexOf(key) === -1 && (namedExport[key] = value);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepStrictEqual(namedExport, factoryExport);
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.entries(axios).forEach(([key, value]) => {
|
describe('CommonJS', () => {
|
||||||
key!=='default' && ignoreList.indexOf(key) === -1 && (namedExport[key] = value);
|
const pkgPath = path.join(__dirname, './cjs');
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await remove(path.join(pkgPath, './node_modules'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to be loaded with require', async function () {
|
||||||
|
this.timeout(30000);
|
||||||
|
|
||||||
|
await exec(`npm test --prefix ${pkgPath}`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.deepStrictEqual(namedExport, factoryExport);
|
describe('ESM', () => {
|
||||||
});
|
const pkgPath = path.join(__dirname, './esm');
|
||||||
|
|
||||||
describe('CommonJS', ()=> {
|
after(async () => {
|
||||||
const pkgPath = path.join(__dirname, './cjs');
|
await remove(path.join(pkgPath, './node_modules'));
|
||||||
|
});
|
||||||
|
|
||||||
after(async ()=> {
|
it('should be able to be loaded with import', async function () {
|
||||||
await remove(path.join(pkgPath, './node_modules'));
|
this.timeout(30000);
|
||||||
|
|
||||||
|
await exec(`npm test --prefix ${pkgPath}`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to be loaded with require', async function () {
|
describe('TS', () => {
|
||||||
this.timeout(30000);
|
const pkgPath = path.join(__dirname, './ts');
|
||||||
|
|
||||||
await exec(`npm test --prefix ${pkgPath}`);
|
after(async () => {
|
||||||
});
|
await remove(path.join(pkgPath, './node_modules'));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ESM', ()=> {
|
it('should be able to be loaded with import', async function () {
|
||||||
const pkgPath = path.join(__dirname, './esm');
|
this.timeout(30000);
|
||||||
|
|
||||||
after(async ()=> {
|
await exec(`npm test --prefix ${pkgPath}`, {});
|
||||||
await remove(path.join(pkgPath, './node_modules'));
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to be loaded with import', async function () {
|
describe('TS require(\'axios\')', () => {
|
||||||
this.timeout(30000);
|
const pkgPath = path.join(__dirname, './ts-require');
|
||||||
|
|
||||||
await exec(`npm test --prefix ${pkgPath}`);
|
after(async () => {
|
||||||
});
|
await remove(path.join(pkgPath, './node_modules'));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('TS', ()=> {
|
it('should be able to be loaded with require', async function () {
|
||||||
const pkgPath = path.join(__dirname, './ts');
|
this.timeout(30000);
|
||||||
|
|
||||||
after(async ()=> {
|
await exec(`npm test --prefix ${pkgPath}`, {});
|
||||||
await remove(path.join(pkgPath, './node_modules'));
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to be loaded with import', async function () {
|
describe('TS require(\'axios\').default', () => {
|
||||||
this.timeout(30000);
|
const pkgPath = path.join(__dirname, './ts-require-default');
|
||||||
|
|
||||||
await exec(`npm test --prefix ${pkgPath}`, {});
|
after(async () => {
|
||||||
});
|
await remove(path.join(pkgPath, './node_modules'));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('TS require(\'axios\')', ()=> {
|
it('should be able to be loaded with require', async function () {
|
||||||
const pkgPath = path.join(__dirname, './ts-require');
|
this.timeout(30000);
|
||||||
|
|
||||||
after(async ()=> {
|
await exec(`npm test --prefix ${pkgPath}`, {});
|
||||||
await remove(path.join(pkgPath, './node_modules'));
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('should be able to be loaded with require', async function () {
|
|
||||||
this.timeout(30000);
|
|
||||||
|
|
||||||
await exec(`npm test --prefix ${pkgPath}`, {});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('TS require(\'axios\').default', ()=> {
|
|
||||||
const pkgPath = path.join(__dirname, './ts-require-default');
|
|
||||||
|
|
||||||
after(async ()=> {
|
|
||||||
await remove(path.join(pkgPath, './node_modules'));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be able to be loaded with require', async function () {
|
|
||||||
this.timeout(30000);
|
|
||||||
|
|
||||||
await exec(`npm test --prefix ${pkgPath}`, {});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import axios = require('axios');
|
import axios = require('axios');
|
||||||
|
|
||||||
const config: axios.AxiosRequestConfig = {
|
const config: axios.RawAxiosRequestConfig = {
|
||||||
url: '/user',
|
url: '/user',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
baseURL: 'https://api.example.com/',
|
baseURL: 'https://api.example.com/',
|
||||||
@@ -38,11 +38,11 @@ const config: axios.AxiosRequestConfig = {
|
|||||||
cancelToken: new axios.CancelToken((cancel: axios.Canceler) => {})
|
cancelToken: new axios.CancelToken((cancel: axios.Canceler) => {})
|
||||||
};
|
};
|
||||||
|
|
||||||
const nullValidateStatusConfig: axios.AxiosRequestConfig = {
|
const nullValidateStatusConfig: axios.RawAxiosRequestConfig = {
|
||||||
validateStatus: null
|
validateStatus: null
|
||||||
};
|
};
|
||||||
|
|
||||||
const undefinedValidateStatusConfig: axios.AxiosRequestConfig = {
|
const undefinedValidateStatusConfig: axios.RawAxiosRequestConfig = {
|
||||||
validateStatus: undefined
|
validateStatus: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+136
-128
@@ -1,4 +1,5 @@
|
|||||||
import axios, {
|
import axios, {
|
||||||
|
RawAxiosRequestConfig,
|
||||||
AxiosRequestConfig,
|
AxiosRequestConfig,
|
||||||
AxiosHeaders,
|
AxiosHeaders,
|
||||||
AxiosRequestHeaders,
|
AxiosRequestHeaders,
|
||||||
@@ -21,7 +22,7 @@ import axios, {
|
|||||||
spread
|
spread
|
||||||
} from 'axios';
|
} from 'axios';
|
||||||
|
|
||||||
const config: AxiosRequestConfig = {
|
const config: RawAxiosRequestConfig = {
|
||||||
url: '/user',
|
url: '/user',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
baseURL: 'https://api.example.com/',
|
baseURL: 'https://api.example.com/',
|
||||||
@@ -59,11 +60,11 @@ const config: AxiosRequestConfig = {
|
|||||||
cancelToken: new axios.CancelToken((cancel: Canceler) => {})
|
cancelToken: new axios.CancelToken((cancel: Canceler) => {})
|
||||||
};
|
};
|
||||||
|
|
||||||
const nullValidateStatusConfig: AxiosRequestConfig = {
|
const nullValidateStatusConfig: RawAxiosRequestConfig = {
|
||||||
validateStatus: null
|
validateStatus: null
|
||||||
};
|
};
|
||||||
|
|
||||||
const undefinedValidateStatusConfig: AxiosRequestConfig = {
|
const undefinedValidateStatusConfig: RawAxiosRequestConfig = {
|
||||||
validateStatus: undefined
|
validateStatus: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -86,48 +87,48 @@ const handleError = (error: AxiosError) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
axios(config)
|
axios(config)
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.get('/user?id=12345')
|
axios.get('/user?id=12345')
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.get('/user', { params: { id: 12345 } })
|
axios.get('/user', { params: { id: 12345 } })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.head('/user')
|
axios.head('/user')
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.options('/user')
|
axios.options('/user')
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.delete('/user')
|
axios.delete('/user')
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post('/user', { foo: 'bar' })
|
axios.post('/user', { foo: 'bar' })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.put('/user', { foo: 'bar' })
|
axios.put('/user', { foo: 'bar' })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.patch('/user', { foo: 'bar' })
|
axios.patch('/user', { foo: 'bar' })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
// Typed methods
|
// Typed methods
|
||||||
interface UserCreationDef {
|
interface UserCreationDef {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
@@ -138,49 +139,49 @@ interface User {
|
|||||||
// with default AxiosResponse<T> result
|
// with default AxiosResponse<T> result
|
||||||
|
|
||||||
const handleUserResponse = (response: AxiosResponse<User>) => {
|
const handleUserResponse = (response: AxiosResponse<User>) => {
|
||||||
console.log(response.data.id);
|
console.log(response.data.id);
|
||||||
console.log(response.data.name);
|
console.log(response.data.name);
|
||||||
console.log(response.status);
|
console.log(response.status);
|
||||||
console.log(response.statusText);
|
console.log(response.statusText);
|
||||||
console.log(response.headers);
|
console.log(response.headers);
|
||||||
console.log(response.config);
|
console.log(response.config);
|
||||||
};
|
};
|
||||||
|
|
||||||
axios.get<User>('/user?id=12345')
|
axios.get<User>('/user?id=12345')
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.get<User>('/user', { params: { id: 12345 } })
|
axios.get<User>('/user', { params: { id: 12345 } })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.head<User>('/user')
|
axios.head<User>('/user')
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.options<User>('/user')
|
axios.options<User>('/user')
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.delete<User>('/user')
|
axios.delete<User>('/user')
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<User>('/user', { name: 'foo', id: 1 })
|
axios.post<User>('/user', { name: 'foo', id: 1 })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<User>('/user', { name: 'foo', id: 1 }, { headers: { 'X-FOO': 'bar' } })
|
axios.post<User>('/user', { name: 'foo', id: 1 }, { headers: { 'X-FOO': 'bar' } })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.put<User>('/user', { name: 'foo', id: 1 })
|
axios.put<User>('/user', { name: 'foo', id: 1 })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.patch<User>('/user', { name: 'foo', id: 1 })
|
axios.patch<User>('/user', { name: 'foo', id: 1 })
|
||||||
.then(handleUserResponse)
|
.then(handleUserResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
// (Typed methods) with custom response type
|
// (Typed methods) with custom response type
|
||||||
|
|
||||||
@@ -189,47 +190,47 @@ const handleStringResponse = (response: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
axios.get<User, string>('/user?id=12345')
|
axios.get<User, string>('/user?id=12345')
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.get<User, string>('/user', { params: { id: 12345 } })
|
axios.get<User, string>('/user', { params: { id: 12345 } })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.head<User, string>('/user')
|
axios.head<User, string>('/user')
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.options<User, string>('/user')
|
axios.options<User, string>('/user')
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.delete<User, string>('/user')
|
axios.delete<User, string>('/user')
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' }, { headers: { 'X-FOO': 'bar' } })
|
axios.post<Partial<UserCreationDef>, string>('/user', { name: 'foo' }, { headers: { 'X-FOO': 'bar' } })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.put<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
axios.put<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.patch<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
axios.patch<Partial<UserCreationDef>, string>('/user', { name: 'foo' })
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
axios.request<User, string>({
|
axios.request<User, string>({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: '/user?id=12345'
|
url: '/user?id=12345'
|
||||||
})
|
})
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
// Instances
|
// Instances
|
||||||
|
|
||||||
@@ -237,32 +238,32 @@ const instance1: AxiosInstance = axios.create();
|
|||||||
const instance2: AxiosInstance = axios.create(config);
|
const instance2: AxiosInstance = axios.create(config);
|
||||||
|
|
||||||
instance1(config)
|
instance1(config)
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
instance1.request(config)
|
instance1.request(config)
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
instance1.get('/user?id=12345')
|
instance1.get('/user?id=12345')
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
instance1.options('/user')
|
instance1.options('/user')
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
instance1.get('/user', { params: { id: 12345 } })
|
instance1.get('/user', { params: { id: 12345 } })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
instance1.post('/user', { foo: 'bar' })
|
instance1.post('/user', { foo: 'bar' })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
instance1.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
instance1.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
|
|
||||||
// Defaults
|
// Defaults
|
||||||
|
|
||||||
@@ -297,47 +298,54 @@ axios.create({
|
|||||||
// Interceptors
|
// Interceptors
|
||||||
|
|
||||||
const requestInterceptorId: number = axios.interceptors.request.use(
|
const requestInterceptorId: number = axios.interceptors.request.use(
|
||||||
async (config: AxiosRequestConfig) => {
|
async (config) => {
|
||||||
await axios.get('/foo', {
|
await axios.get('/foo', {
|
||||||
headers: config.headers
|
headers: config.headers
|
||||||
});
|
});
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
(error: any) => Promise.reject(error),
|
(error: any) => Promise.reject(error),
|
||||||
{synchronous: false}
|
{synchronous: false}
|
||||||
);
|
);
|
||||||
|
|
||||||
axios.interceptors.request.eject(requestInterceptorId);
|
axios.interceptors.request.eject(requestInterceptorId);
|
||||||
|
|
||||||
axios.interceptors.request.use(
|
axios.interceptors.request.use(
|
||||||
(config: AxiosRequestConfig) => Promise.resolve(config),
|
(config) => Promise.resolve(config),
|
||||||
(error: any) => Promise.reject(error)
|
(error: any) => Promise.reject(error)
|
||||||
);
|
);
|
||||||
|
|
||||||
axios.interceptors.request.use((config: AxiosRequestConfig) => config);
|
axios.interceptors.request.use((config) => config);
|
||||||
axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config));
|
axios.interceptors.request.use((config) => Promise.resolve(config));
|
||||||
|
|
||||||
const responseInterceptorId: number = axios.interceptors.response.use(
|
const responseInterceptorId: number = axios.interceptors.response.use(
|
||||||
(response: AxiosResponse) => response,
|
(response: AxiosResponse) => response,
|
||||||
(error: any) => Promise.reject(error)
|
(error: any) => Promise.reject(error)
|
||||||
);
|
);
|
||||||
|
|
||||||
axios.interceptors.response.eject(responseInterceptorId);
|
axios.interceptors.response.eject(responseInterceptorId);
|
||||||
|
|
||||||
axios.interceptors.response.use(
|
axios.interceptors.response.use(
|
||||||
(response: AxiosResponse) => Promise.resolve(response),
|
(response: AxiosResponse) => Promise.resolve(response),
|
||||||
(error: any) => Promise.reject(error)
|
(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(
|
const voidRequestInterceptorId = axios.interceptors.request.use(
|
||||||
// @ts-expect-error -- Must return an AxiosRequestConfig (or throw)
|
// @ts-expect-error -- Must return an AxiosRequestConfig (or throw)
|
||||||
(_response) => {},
|
(_response) => {},
|
||||||
(error: any) => Promise.reject(error)
|
(error: any) => Promise.reject(error)
|
||||||
);
|
);
|
||||||
const voidResponseInterceptorId = axios.interceptors.response.use(
|
const voidResponseInterceptorId = axios.interceptors.response.use(
|
||||||
// @ts-expect-error -- Must return an AxiosResponse (or throw)
|
// @ts-expect-error -- Must return an AxiosResponse (or throw)
|
||||||
(_response) => {},
|
(_response) => {},
|
||||||
(error: any) => Promise.reject(error)
|
(error: any) => Promise.reject(error)
|
||||||
);
|
);
|
||||||
axios.interceptors.request.eject(voidRequestInterceptorId);
|
axios.interceptors.request.eject(voidRequestInterceptorId);
|
||||||
axios.interceptors.response.eject(voidResponseInterceptorId);
|
axios.interceptors.response.eject(voidResponseInterceptorId);
|
||||||
@@ -350,7 +358,7 @@ axios.interceptors.response.clear();
|
|||||||
|
|
||||||
// Adapters
|
// Adapters
|
||||||
|
|
||||||
const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
|
const adapter: AxiosAdapter = (config) => {
|
||||||
const response: AxiosResponse = {
|
const response: AxiosResponse = {
|
||||||
data: { foo: 'bar' },
|
data: { foo: 'bar' },
|
||||||
status: 200,
|
status: 200,
|
||||||
@@ -397,28 +405,28 @@ const fn2: (arr: number[]) => string = axios.spread(fn1);
|
|||||||
// Promises
|
// Promises
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.then((response: AxiosResponse) => 'foo')
|
.then((response: AxiosResponse) => 'foo')
|
||||||
.then((value: string) => {});
|
.then((value: string) => {});
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.then((response: AxiosResponse) => Promise.resolve('foo'))
|
.then((response: AxiosResponse) => Promise.resolve('foo'))
|
||||||
.then((value: string) => {});
|
.then((value: string) => {});
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.then((response: AxiosResponse) => 'foo', (error: any) => 'bar')
|
.then((response: AxiosResponse) => 'foo', (error: any) => 'bar')
|
||||||
.then((value: string) => {});
|
.then((value: string) => {});
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.then((response: AxiosResponse) => 'foo', (error: any) => 123)
|
.then((response: AxiosResponse) => 'foo', (error: any) => 123)
|
||||||
.then((value: string | number) => {});
|
.then((value: string | number) => {});
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.catch((error: any) => 'foo')
|
.catch((error: any) => 'foo')
|
||||||
.then((value: any) => {});
|
.then((value: any) => {});
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.catch((error: any) => Promise.resolve('foo'))
|
.catch((error: any) => Promise.resolve('foo'))
|
||||||
.then((value: any) => {});
|
.then((value: any) => {});
|
||||||
|
|
||||||
// Cancellation
|
// Cancellation
|
||||||
|
|
||||||
@@ -444,17 +452,17 @@ source.cancel('Operation has been canceled.');
|
|||||||
// AxiosError
|
// AxiosError
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.catch((error: AxiosError) => {
|
.catch((error: AxiosError) => {
|
||||||
if (axios.isAxiosError(error)) {
|
if (axios.isAxiosError(error)) {
|
||||||
const axiosError: AxiosError = error;
|
const axiosError: AxiosError = error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// named export
|
// named export
|
||||||
|
|
||||||
if (isAxiosError(error)) {
|
if (isAxiosError(error)) {
|
||||||
const axiosError: AxiosError = error;
|
const axiosError: AxiosError = error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// FormData
|
// FormData
|
||||||
|
|
||||||
@@ -507,17 +515,17 @@ axios.get('/user', {
|
|||||||
|
|
||||||
// issue #5034
|
// issue #5034
|
||||||
|
|
||||||
function getRequestConfig1(options: AxiosRequestConfig): AxiosRequestConfig {
|
function getRequestConfig1(options: RawAxiosRequestConfig): RawAxiosRequestConfig {
|
||||||
return {
|
return {
|
||||||
...options,
|
...options,
|
||||||
headers: {
|
headers: {
|
||||||
...(options.headers as RawAxiosRequestHeaders),
|
...(options.headers as RawAxiosRequestHeaders),
|
||||||
Authorization: `Bearer ...`,
|
Authorization: `Bearer ...`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRequestConfig2(options: AxiosRequestConfig): AxiosRequestConfig {
|
function getRequestConfig2(options: RawAxiosRequestConfig): RawAxiosRequestConfig {
|
||||||
return {
|
return {
|
||||||
...options,
|
...options,
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Reference in New Issue
Block a user