mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Updating TypeScript definition tests
This commit is contained in:
+137
-149
@@ -1,161 +1,123 @@
|
|||||||
import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance, AxiosAdapter } from '../../';
|
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError, AxiosInstance, AxiosAdapter } from '../../';
|
||||||
import { Promise } from 'es6-promise';
|
import { Promise } from 'es6-promise';
|
||||||
|
|
||||||
axios.get('/user?ID=12345')
|
const config: AxiosRequestConfig = {
|
||||||
.then(function (response) {
|
url: '/user',
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
axios.get('/user', {
|
|
||||||
params: {
|
|
||||||
ID: 12345
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
axios.head('/user', {
|
|
||||||
params: {
|
|
||||||
ID: 12345
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
axios.delete('/user', {
|
|
||||||
params: {
|
|
||||||
ID: 12345
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
axios.post('/user', {
|
|
||||||
firstName: 'Fred',
|
|
||||||
lastName: 'Flintstone'
|
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
axios.put('/user', {
|
|
||||||
firstName: 'Fred',
|
|
||||||
lastName: 'Flintstone'
|
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
axios.patch('/user', {
|
|
||||||
firstName: 'Fred',
|
|
||||||
lastName: 'Flintstone'
|
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response.data);
|
|
||||||
console.log(response.status + 324);
|
|
||||||
console.log(response.headers);
|
|
||||||
console.log(response.config);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
axios({
|
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: '/user/12345'
|
baseURL: 'https://api.example.com/',
|
||||||
});
|
transformRequest: (data: any) => '{"foo":"bar"}',
|
||||||
|
transformResponse: [
|
||||||
|
(data: any) => ({ baz: 'qux' })
|
||||||
|
],
|
||||||
|
headers: { 'X-FOO': 'bar' },
|
||||||
|
params: { id: 12345 },
|
||||||
|
paramsSerializer: (params: any) => 'id=12345',
|
||||||
|
data: { foo: 'bar' },
|
||||||
|
timeout: 10000,
|
||||||
|
withCredentials: true,
|
||||||
|
auth: {
|
||||||
|
username: 'janedoe',
|
||||||
|
password: 's00pers3cret'
|
||||||
|
},
|
||||||
|
responseType: 'json',
|
||||||
|
xsrfCookieName: 'XSRF-TOKEN',
|
||||||
|
xsrfHeaderName: 'X-XSRF-TOKEN',
|
||||||
|
progress: (progressEvent: any) => {},
|
||||||
|
maxContentLength: 2000,
|
||||||
|
validateStatus: (status: number) => status >= 200 && status < 300,
|
||||||
|
maxRedirects: 5
|
||||||
|
};
|
||||||
|
|
||||||
axios({
|
const handleResponse = (response: AxiosResponse) => {
|
||||||
method: 'get',
|
console.log(response.data);
|
||||||
url: '/user/12345',
|
console.log(response.status);
|
||||||
transformRequest: (data) => {
|
console.log(response.statusText);
|
||||||
return data.doSomething();
|
console.log(response.headers);
|
||||||
|
console.log(response.config);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleError = (error: AxiosError) => {
|
||||||
|
if (error.response) {
|
||||||
|
console.log(error.response.data);
|
||||||
|
console.log(error.response.status);
|
||||||
|
console.log(error.response.headers);
|
||||||
|
} else {
|
||||||
|
console.log(error.message);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
axios({
|
axios(config)
|
||||||
url: "hi",
|
.then(handleResponse)
|
||||||
headers: {'X-Requested-With': 'XMLHttpRequest'},
|
.catch(handleError);
|
||||||
params: {
|
|
||||||
ID: 12345
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
firstName: 'Fred'
|
|
||||||
},
|
|
||||||
withCredentials: false, // default
|
|
||||||
responseType: 'json', // default
|
|
||||||
xsrfCookieName: 'XSRF-TOKEN', // default
|
|
||||||
xsrfHeaderName: 'X-XSRF-TOKEN' // default
|
|
||||||
});
|
|
||||||
|
|
||||||
var instance = axios.create();
|
axios.get('/user?id=12345')
|
||||||
|
.then(handleResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
axios.create({
|
axios.get('/user', { params: { id: 12345 } })
|
||||||
transformRequest: (data) => {
|
.then(handleResponse)
|
||||||
return data.doSomething();
|
.catch(handleError);
|
||||||
},
|
|
||||||
transformResponse: (data) => {
|
|
||||||
return data.doSomethingElse();
|
|
||||||
},
|
|
||||||
headers: {'X-Requested-With': 'XMLHttpRequest'},
|
|
||||||
timeout: 1000,
|
|
||||||
withCredentials: false, // default
|
|
||||||
responseType: 'json', // default
|
|
||||||
xsrfCookieName: 'XSRF-TOKEN', // default
|
|
||||||
xsrfHeaderName: 'X-XSRF-TOKEN' // default
|
|
||||||
});
|
|
||||||
|
|
||||||
instance.request({
|
axios.head('/user')
|
||||||
method: 'get',
|
.then(handleResponse)
|
||||||
url: '/user/12345'
|
.catch(handleError);
|
||||||
})
|
|
||||||
.then(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
instance.get('/user?ID=12345')
|
axios.delete('/user')
|
||||||
.then(function (response) {
|
.then(handleResponse)
|
||||||
console.log(response);
|
.catch(handleError);
|
||||||
})
|
|
||||||
.catch(function (response) {
|
|
||||||
console.log(response);
|
|
||||||
});
|
|
||||||
|
|
||||||
instance.get('/user', {
|
axios.post('/user', { foo: 'bar' })
|
||||||
params: {
|
.then(handleResponse)
|
||||||
ID: 12345
|
.catch(handleError);
|
||||||
}
|
|
||||||
})
|
axios.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
||||||
.then(function (response) {
|
.then(handleResponse)
|
||||||
console.log(response);
|
.catch(handleError);
|
||||||
})
|
|
||||||
.catch(function (response) {
|
axios.put('/user', { foo: 'bar' })
|
||||||
console.log(response);
|
.then(handleResponse)
|
||||||
});
|
.catch(handleError);
|
||||||
|
|
||||||
|
axios.patch('/user', { foo: 'bar' })
|
||||||
|
.then(handleResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
// Instances
|
||||||
|
|
||||||
|
const instance1: AxiosInstance = axios.create();
|
||||||
|
const instance2: AxiosInstance = axios.create(config);
|
||||||
|
|
||||||
|
instance1.request(config)
|
||||||
|
.then(handleResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
instance1.get('/user?id=12345')
|
||||||
|
.then(handleResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
instance1.get('/user', { params: { id: 12345 } })
|
||||||
|
.then(handleResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
instance1.post('/user', { foo: 'bar' })
|
||||||
|
.then(handleResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
instance1.post('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
|
||||||
|
.then(handleResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
|
||||||
|
// Defaults
|
||||||
|
|
||||||
|
axios.defaults.baseURL = 'https://api.example.com/';
|
||||||
|
axios.defaults.headers.common['Authorization'] = 'token';
|
||||||
|
axios.defaults.headers.post['X-FOO'] = 'bar';
|
||||||
|
axios.defaults.timeout = 2500;
|
||||||
|
|
||||||
|
instance1.defaults.baseURL = 'https://api.example.com/';
|
||||||
|
instance1.defaults.headers.common['Authorization'] = 'token';
|
||||||
|
instance1.defaults.headers.post['X-FOO'] = 'bar';
|
||||||
|
instance1.defaults.timeout = 2500;
|
||||||
|
|
||||||
// Interceptors
|
// Interceptors
|
||||||
|
|
||||||
@@ -193,11 +155,11 @@ axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(res
|
|||||||
|
|
||||||
const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
|
const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
|
||||||
const response: AxiosResponse = {
|
const response: AxiosResponse = {
|
||||||
data: 'foo',
|
data: { foo: 'bar' },
|
||||||
status: 200,
|
status: 200,
|
||||||
statusText: 'OK',
|
statusText: 'OK',
|
||||||
headers: { 'X-FOO': 'bar' },
|
headers: { 'X-FOO': 'bar' },
|
||||||
config: config,
|
config
|
||||||
};
|
};
|
||||||
return Promise.resolve(response);
|
return Promise.resolve(response);
|
||||||
};
|
};
|
||||||
@@ -217,3 +179,29 @@ const promise: Promise<number[]> = axios.all(promises);
|
|||||||
|
|
||||||
const fn1 = (a: number, b: number, c: number) => `${a}-${b}-${c}`;
|
const fn1 = (a: number, b: number, c: number) => `${a}-${b}-${c}`;
|
||||||
const fn2: (arr: number[]) => string = axios.spread(fn1);
|
const fn2: (arr: number[]) => string = axios.spread(fn1);
|
||||||
|
|
||||||
|
// Promises
|
||||||
|
|
||||||
|
axios.get('/user')
|
||||||
|
.then((response: AxiosResponse) => 'foo')
|
||||||
|
.then((value: string) => {});
|
||||||
|
|
||||||
|
axios.get('/user')
|
||||||
|
.then((response: AxiosResponse) => Promise.resolve('foo'))
|
||||||
|
.then((value: string) => {});
|
||||||
|
|
||||||
|
axios.get('/user')
|
||||||
|
.then((response: AxiosResponse) => 'foo', (error: any) => 'bar')
|
||||||
|
.then((value: string) => {});
|
||||||
|
|
||||||
|
axios.get('/user')
|
||||||
|
.then((response: AxiosResponse) => 'foo', (error: any) => 123)
|
||||||
|
.then((value: string | number) => {});
|
||||||
|
|
||||||
|
axios.get('/user')
|
||||||
|
.catch((error: any) => 'foo')
|
||||||
|
.then((value: string) => {});
|
||||||
|
|
||||||
|
axios.get('/user')
|
||||||
|
.catch((error: any) => Promise.resolve('foo'))
|
||||||
|
.then((value: string) => {});
|
||||||
|
|||||||
Reference in New Issue
Block a user