mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
Fix/typescript tests (#5375)
* chore(ci): Add release-it script; * chore(ci): add `release:no-npm` script to release the package without making an npm release; * fix(test): fixed `d.ts` typings; refactor(test): refactor typescript tests;
This commit is contained in:
@@ -14,6 +14,16 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const exec = util.promisify(cp.exec);
|
||||
|
||||
const spawn = (command, args) => new Promise((resolve, reject) => {
|
||||
cp.spawn(command, args, {
|
||||
shell : true,
|
||||
stdio: 'inherit'
|
||||
}).once('error', reject).on(
|
||||
'close',
|
||||
(code) => code ? reject(new Error(`Exit code ${code}`)) : resolve()
|
||||
);
|
||||
});
|
||||
|
||||
const {Axios} = axiosFactory;
|
||||
|
||||
const ignoreList = ['default'];
|
||||
@@ -134,4 +144,40 @@ describe('module', function () {
|
||||
await exec(`npm test --prefix ${pkgPath}`, {});
|
||||
});
|
||||
});
|
||||
|
||||
describe('typings', () => {
|
||||
describe('ESM', ()=> {
|
||||
const pkgPath = path.join(__dirname, './typings/esm');
|
||||
|
||||
after(async ()=> {
|
||||
await remove(path.join(pkgPath, './node_modules'));
|
||||
});
|
||||
|
||||
it('should pass types check', async function () {
|
||||
this.timeout(30000);
|
||||
|
||||
await spawn(`npm test --prefix ${pkgPath}`, [], {
|
||||
shell : true,
|
||||
stdio: 'pipe'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('CommonJS', ()=> {
|
||||
const pkgPath = path.join(__dirname, './typings/cjs');
|
||||
|
||||
after(async ()=> {
|
||||
await remove(path.join(pkgPath, './node_modules'));
|
||||
});
|
||||
|
||||
it('should pass types check', async function () {
|
||||
this.timeout(30000);
|
||||
|
||||
await spawn(`npm test --prefix ${pkgPath}`, [], {
|
||||
shell : true,
|
||||
stdio: 'pipe'
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -65,48 +65,48 @@ const handleError = (error: axios.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 {
|
||||
@@ -117,49 +117,49 @@ interface User {
|
||||
// with default axios.AxiosResponse<T> result
|
||||
|
||||
const handleUserResponse = (response: axios.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
|
||||
|
||||
@@ -168,47 +168,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
|
||||
|
||||
@@ -216,32 +216,32 @@ const instance1: axios.AxiosInstance = axios.create();
|
||||
const instance2: axios.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
|
||||
|
||||
@@ -276,41 +276,41 @@ axios.create({
|
||||
// Interceptors
|
||||
|
||||
const requestInterceptorId: number = axios.interceptors.request.use(
|
||||
(config: axios.AxiosRequestConfig) => config,
|
||||
(error: any) => Promise.reject(error)
|
||||
(config: axios.AxiosRequestConfig) => config,
|
||||
(error: any) => Promise.reject(error)
|
||||
);
|
||||
|
||||
axios.interceptors.request.eject(requestInterceptorId);
|
||||
|
||||
axios.interceptors.request.use(
|
||||
(config: axios.AxiosRequestConfig) => Promise.resolve(config),
|
||||
(error: any) => Promise.reject(error)
|
||||
(config: axios.AxiosRequestConfig) => 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));
|
||||
|
||||
const responseInterceptorId: number = axios.interceptors.response.use(
|
||||
(response: axios.AxiosResponse) => response,
|
||||
(error: any) => Promise.reject(error)
|
||||
(response: axios.AxiosResponse) => response,
|
||||
(error: any) => Promise.reject(error)
|
||||
);
|
||||
|
||||
axios.interceptors.response.eject(responseInterceptorId);
|
||||
|
||||
axios.interceptors.response.use(
|
||||
(response: axios.AxiosResponse) => Promise.resolve(response),
|
||||
(error: any) => Promise.reject(error)
|
||||
(response: axios.AxiosResponse) => Promise.resolve(response),
|
||||
(error: any) => Promise.reject(error)
|
||||
);
|
||||
|
||||
const voidRequestInterceptorId = axios.interceptors.request.use(
|
||||
// @ts-expect-error -- Must return an axios.AxiosRequestConfig (or throw)
|
||||
(_response) => {},
|
||||
(error: any) => Promise.reject(error)
|
||||
// @ts-expect-error -- Must return an axios.AxiosRequestConfig (or throw)
|
||||
(_response) => {},
|
||||
(error: any) => Promise.reject(error)
|
||||
);
|
||||
const voidResponseInterceptorId = axios.interceptors.response.use(
|
||||
// @ts-expect-error -- Must return an axios.AxiosResponse (or throw)
|
||||
(_response) => {},
|
||||
(error: any) => Promise.reject(error)
|
||||
// @ts-expect-error -- Must return an axios.AxiosResponse (or throw)
|
||||
(_response) => {},
|
||||
(error: any) => Promise.reject(error)
|
||||
);
|
||||
axios.interceptors.request.eject(voidRequestInterceptorId);
|
||||
axios.interceptors.response.eject(voidResponseInterceptorId);
|
||||
@@ -353,28 +353,28 @@ const fn2: (arr: number[]) => string = axios.spread(fn1);
|
||||
// Promises
|
||||
|
||||
axios.get('/user')
|
||||
.then((response: axios.AxiosResponse) => 'foo')
|
||||
.then((value: string) => {});
|
||||
.then((response: axios.AxiosResponse) => 'foo')
|
||||
.then((value: string) => {});
|
||||
|
||||
axios.get('/user')
|
||||
.then((response: axios.AxiosResponse) => Promise.resolve('foo'))
|
||||
.then((value: string) => {});
|
||||
.then((response: axios.AxiosResponse) => Promise.resolve('foo'))
|
||||
.then((value: string) => {});
|
||||
|
||||
axios.get('/user')
|
||||
.then((response: axios.AxiosResponse) => 'foo', (error: any) => 'bar')
|
||||
.then((value: string) => {});
|
||||
.then((response: axios.AxiosResponse) => 'foo', (error: any) => 'bar')
|
||||
.then((value: string) => {});
|
||||
|
||||
axios.get('/user')
|
||||
.then((response: axios.AxiosResponse) => 'foo', (error: any) => 123)
|
||||
.then((value: string | number) => {});
|
||||
.then((response: axios.AxiosResponse) => 'foo', (error: any) => 123)
|
||||
.then((value: string | number) => {});
|
||||
|
||||
axios.get('/user')
|
||||
.catch((error: any) => 'foo')
|
||||
.then((value) => {});
|
||||
.catch((error: any) => 'foo')
|
||||
.then((value) => {});
|
||||
|
||||
axios.get('/user')
|
||||
.catch((error: any) => Promise.resolve('foo'))
|
||||
.then((value) => {});
|
||||
.catch((error: any) => Promise.resolve('foo'))
|
||||
.then((value) => {});
|
||||
|
||||
// axios.Cancellation
|
||||
|
||||
@@ -394,11 +394,11 @@ source.cancel('Operation has been axios.Canceled.');
|
||||
// axios.AxiosError
|
||||
|
||||
axios.get('/user')
|
||||
.catch((error) => {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const axiosError: axios.AxiosError = error;
|
||||
}
|
||||
});
|
||||
.catch((error) => {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const axiosError: axios.AxiosError = error;
|
||||
}
|
||||
});
|
||||
|
||||
// FormData
|
||||
|
||||
@@ -411,14 +411,14 @@ axios.get('/user', {signal: new AbortController().signal});
|
||||
// AxiosHeaders methods
|
||||
|
||||
axios.get('/user', {
|
||||
transformRequest: (data, headers) => {
|
||||
headers.setContentType('text/plain');
|
||||
headers['Foo'] = 'bar';
|
||||
},
|
||||
transformRequest: (data, headers) => {
|
||||
headers.setContentType('text/plain');
|
||||
headers['Foo'] = 'bar';
|
||||
},
|
||||
|
||||
transformResponse: (data, headers) => {
|
||||
headers.has('foo');
|
||||
}
|
||||
transformResponse: (data, headers) => {
|
||||
headers.has('foo');
|
||||
}
|
||||
});
|
||||
|
||||
// Max Rate
|
||||
@@ -434,7 +434,7 @@ axios.get('/user', {
|
||||
// Node progress
|
||||
|
||||
axios.get('/user', {
|
||||
onUploadProgress: (e) => {
|
||||
onUploadProgress: (e: axios.AxiosProgressEvent) => {
|
||||
console.log(e.loaded);
|
||||
console.log(e.total);
|
||||
console.log(e.progress);
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "commonjs-typings-test",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "npm i --no-save --no-package-lock && tsc -v && npm run test:types",
|
||||
"test:types": "tsc --noEmit"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/node": "^18.11.3",
|
||||
"axios": "file:../../../.."
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^4.9.4"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"checkJs": true,
|
||||
"module": "node16"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import axios, {
|
||||
AxiosRequestConfig,
|
||||
AxiosHeaders,
|
||||
AxiosRequestHeaders,
|
||||
AxiosResponseHeaders,
|
||||
RawAxiosRequestHeaders,
|
||||
AxiosResponse,
|
||||
AxiosError,
|
||||
AxiosInstance,
|
||||
@@ -293,8 +297,14 @@ axios.create({
|
||||
// Interceptors
|
||||
|
||||
const requestInterceptorId: number = axios.interceptors.request.use(
|
||||
(config: AxiosRequestConfig) => config,
|
||||
(error: any) => Promise.reject(error)
|
||||
async (config: AxiosRequestConfig) => {
|
||||
await axios.get('/foo', {
|
||||
headers: config.headers
|
||||
});
|
||||
return config;
|
||||
},
|
||||
(error: any) => Promise.reject(error),
|
||||
{synchronous: false}
|
||||
);
|
||||
|
||||
axios.interceptors.request.eject(requestInterceptorId);
|
||||
@@ -404,11 +414,11 @@ axios.get('/user')
|
||||
|
||||
axios.get('/user')
|
||||
.catch((error: any) => 'foo')
|
||||
.then((value) => {});
|
||||
.then((value: any) => {});
|
||||
|
||||
axios.get('/user')
|
||||
.catch((error: any) => Promise.resolve('foo'))
|
||||
.then((value) => {});
|
||||
.then((value: any) => {});
|
||||
|
||||
// Cancellation
|
||||
|
||||
@@ -434,7 +444,7 @@ source.cancel('Operation has been canceled.');
|
||||
// AxiosError
|
||||
|
||||
axios.get('/user')
|
||||
.catch((error) => {
|
||||
.catch((error: AxiosError) => {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const axiosError: AxiosError = error;
|
||||
}
|
||||
@@ -467,16 +477,56 @@ axios.get('/user', {signal: new AbortController().signal});
|
||||
// AxiosHeaders methods
|
||||
|
||||
axios.get('/user', {
|
||||
transformRequest: (data, headers) => {
|
||||
headers.setContentType('text/plain');
|
||||
headers['Foo'] = 'bar';
|
||||
transformRequest: [
|
||||
(data: any, headers) => {
|
||||
headers.setContentType('text/plain');
|
||||
return 'baz';
|
||||
},
|
||||
|
||||
transformResponse: (data, headers) => {
|
||||
headers.has('foo');
|
||||
(data: any, headers) => {
|
||||
headers['foo'] = 'bar';
|
||||
return 'baz'
|
||||
}
|
||||
],
|
||||
|
||||
transformResponse: [(data: any, headers: AxiosResponseHeaders) => {
|
||||
headers.has('foo');
|
||||
}]
|
||||
});
|
||||
|
||||
// config headers
|
||||
|
||||
axios.get('/user', {
|
||||
headers: new AxiosHeaders({x:1})
|
||||
});
|
||||
|
||||
axios.get('/user', {
|
||||
headers: {
|
||||
foo : 1
|
||||
}
|
||||
});
|
||||
|
||||
// issue #5034
|
||||
|
||||
function getRequestConfig1(options: AxiosRequestConfig): AxiosRequestConfig {
|
||||
return {
|
||||
...options,
|
||||
headers: {
|
||||
...(options.headers as RawAxiosRequestHeaders),
|
||||
Authorization: `Bearer ...`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function getRequestConfig2(options: AxiosRequestConfig): AxiosRequestConfig {
|
||||
return {
|
||||
...options,
|
||||
headers: {
|
||||
...(options.headers as AxiosHeaders).toJSON(),
|
||||
Authorization: `Bearer ...`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Max Rate
|
||||
|
||||
axios.get('/user', {
|
||||
@@ -490,7 +540,7 @@ axios.get('/user', {
|
||||
// Node progress
|
||||
|
||||
axios.get('/user', {
|
||||
onUploadProgress: (e) => {
|
||||
onUploadProgress: (e: AxiosProgressEvent) => {
|
||||
console.log(e.loaded);
|
||||
console.log(e.total);
|
||||
console.log(e.progress);
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "esm-typings-test",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test:types": "tsc --noEmit",
|
||||
"test": "npm i --no-save --no-package-lock && tsc -v && npm run test:types"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/node": "^18.11.3",
|
||||
"axios": "file:../../../.."
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^4.9.4"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"checkJs": true,
|
||||
"module": "node16"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user