2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-05 16:42:32 +03:00

fix(types): fixed AxiosRequestConfig header interface by refactoring it to RawAxiosRequestConfig; (#5420)

This commit is contained in:
Dmitriy Mozgovoy
2023-01-06 02:02:08 +02:00
committed by GitHub
parent 8651bf17d4
commit 08119634a2
5 changed files with 250 additions and 231 deletions
+65 -62
View File
@@ -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}`, {});
});
});
});
+3 -3
View File
@@ -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
};
+136 -128
View File
@@ -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: {