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