mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-23 12:57:25 +08:00
Merge pull request #7710 from marcelbuesing/master
Update axios definitions to v0.8.1
This commit is contained in:
@@ -8,21 +8,64 @@ interface Repository {
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface Issue {
|
||||
id: number;
|
||||
title: string;
|
||||
}
|
||||
|
||||
axios.interceptors.request.use<any>(config => {
|
||||
console.log("Method:" + config.method + " Url:" +config.url);
|
||||
return config;
|
||||
});
|
||||
|
||||
axios.interceptors.response.use<any>(config => {
|
||||
console.log("Status:" + config.status);
|
||||
return config;
|
||||
});
|
||||
|
||||
axios.get<Repository>("https://api.github.com/repos/mzabriskie/axios")
|
||||
.then(r => console.log(r.config.method));
|
||||
|
||||
axios<Repository>({
|
||||
var getRepoDetails = axios<Repository>({
|
||||
url: "https://api.github.com/repos/mzabriskie/axios",
|
||||
method: HttpMethod[HttpMethod.GET],
|
||||
headers: {},
|
||||
}).then(r => console.log("ID:" + r.data.id + " Name: " + r.data.name));
|
||||
}).then(r => {
|
||||
console.log("ID:" + r.data.id + " Name: " + r.data.name);
|
||||
return r;
|
||||
});
|
||||
|
||||
axios.post("http://example.com/", {}, {
|
||||
transformRequest: (data: any) => data
|
||||
});
|
||||
|
||||
axios.post("http://example.com/", {}, {
|
||||
axios.post("http://example.com/", {
|
||||
headers: {'X-Custom-Header': 'foobar'}
|
||||
}, {
|
||||
transformRequest: [
|
||||
(data: any) => data
|
||||
]
|
||||
});
|
||||
|
||||
var getRepoIssue = axios.get<Issue>("https://api.github.com/repos/mzabriskie/axios/issues/1");
|
||||
|
||||
var axiosInstance = axios.create({
|
||||
baseURL: "https://api.github.com/repos/mzabriskie/axios/",
|
||||
timeout: 1000
|
||||
});
|
||||
|
||||
axiosInstance.request({url: "issues/1"});
|
||||
|
||||
axios.all<Repository, Repository>([getRepoDetails, getRepoDetails]).then(([repo1, repo2]) => {
|
||||
var sumIds = repo1.data.id + repo2.data.id;
|
||||
console.log("Sum ID:" + sumIds);
|
||||
return sumIds;
|
||||
});
|
||||
|
||||
var repoSum = (repo1: Axios.AxiosXHR<Repository>, repo2: Axios.AxiosXHR<Repository>) => {
|
||||
var sumIds = repo1.data.id + repo2.data.id;
|
||||
console.log("Sum ID:" + sumIds);
|
||||
return sumIds;
|
||||
};
|
||||
|
||||
axios.all<Repository, Repository>([getRepoDetails, getRepoDetails]).then(axios.spread(repoSum));
|
||||
|
||||
134
axios/axios.d.ts
vendored
134
axios/axios.d.ts
vendored
@@ -1,9 +1,8 @@
|
||||
// Type definitions for axios 0.5.2
|
||||
// Type definitions for axios 0.8.1
|
||||
// Project: https://github.com/mzabriskie/axios
|
||||
// Definitions by: Marcel Buesing <https://github.com/marcelbuesing>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
|
||||
declare module Axios {
|
||||
|
||||
interface IThenable<R> {
|
||||
@@ -18,21 +17,24 @@ declare module Axios {
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP Basic auth details
|
||||
*/
|
||||
interface AxiosHttpBasicAuth {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Common axios XHR config interface
|
||||
* <T> - request body data type
|
||||
*/
|
||||
interface AxiosXHRConfigBase<T> {
|
||||
|
||||
/**
|
||||
* Change the request data before it is sent to the server.
|
||||
* This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||
* The last function in the array must return a string or an ArrayBuffer
|
||||
* will be prepended to `url` unless `url` is absolute.
|
||||
* It can be convenient to set `baseURL` for an instance
|
||||
* of axios to pass relative URLs to methods of that instance.
|
||||
*/
|
||||
transformRequest?: (<U>(data: T) => U) | [<U>(data: T) => U];
|
||||
|
||||
/**
|
||||
* change the response data to be made before it is passed to then/catch
|
||||
*/
|
||||
transformResponse?: <U>(data: T) => U;
|
||||
baseURL?: string;
|
||||
|
||||
/**
|
||||
* custom headers to be sent
|
||||
@@ -44,12 +46,32 @@ declare module Axios {
|
||||
*/
|
||||
params?: Object;
|
||||
|
||||
/**
|
||||
* optional function in charge of serializing `params`
|
||||
* (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
|
||||
*/
|
||||
paramsSerializer?: (params: Object) => string;
|
||||
|
||||
/**
|
||||
* specifies the number of milliseconds before the request times out.
|
||||
* If the request takes longer than `timeout`, the request will be aborted.
|
||||
*/
|
||||
timeout?: number;
|
||||
|
||||
/**
|
||||
* indicates whether or not cross-site Access-Control requests
|
||||
* should be made using credentials
|
||||
*/
|
||||
withCredentials?: boolean;
|
||||
|
||||
/**
|
||||
* indicates that HTTP Basic auth should be used, and supplies
|
||||
* credentials. This will set an `Authorization` header,
|
||||
* overwriting any existing `Authorization` custom headers you have
|
||||
* set using `headers`.
|
||||
*/
|
||||
auth?: AxiosHttpBasicAuth;
|
||||
|
||||
/**
|
||||
* indicates the type of data that the server will respond with
|
||||
* options are 'arraybuffer', 'blob', 'document', 'json', 'text'
|
||||
@@ -66,6 +88,17 @@ declare module Axios {
|
||||
*/
|
||||
xsrfHeaderName?: string;
|
||||
|
||||
/**
|
||||
* Change the request data before it is sent to the server.
|
||||
* This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||
* The last function in the array must return a string or an ArrayBuffer
|
||||
*/
|
||||
transformRequest?: (<U>(data: T) => U) | [<U>(data: T) => U];
|
||||
|
||||
/**
|
||||
* change the response data to be made before it is passed to then/catch
|
||||
*/
|
||||
transformResponse?: <U>(data: T) => U;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,7 +125,7 @@ declare module Axios {
|
||||
}
|
||||
|
||||
/**
|
||||
* <T> - expected response type,
|
||||
* <T> - expected response type,
|
||||
* <U> - request body data type
|
||||
*/
|
||||
interface AxiosXHR<T> {
|
||||
@@ -122,16 +155,77 @@ declare module Axios {
|
||||
config: AxiosXHRConfig<T>;
|
||||
}
|
||||
|
||||
interface Interceptor {
|
||||
/**
|
||||
* intercept request before it is sent
|
||||
*/
|
||||
request: RequestInterceptor;
|
||||
|
||||
/**
|
||||
* intercept response of request when it is received.
|
||||
*/
|
||||
response: ResponseInterceptor
|
||||
}
|
||||
|
||||
interface RequestInterceptor {
|
||||
/**
|
||||
* <U> - request body data type
|
||||
*/
|
||||
use<U>(fn: (config: AxiosXHRConfig<U>) => AxiosXHRConfig<U>): void;
|
||||
}
|
||||
|
||||
interface ResponseInterceptor {
|
||||
/**
|
||||
* <T> - expected response type
|
||||
*/
|
||||
use<T>(fn: (config: AxiosXHR<T>) => AxiosXHR<T>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* <T> - expected response type,
|
||||
* <T> - expected response type,
|
||||
* <U> - request body data type
|
||||
*/
|
||||
interface AxiosStatic {
|
||||
interface AxiosInstance {
|
||||
|
||||
/**
|
||||
* Send request as configured
|
||||
*/
|
||||
<T>(config: AxiosXHRConfig<T>): IPromise<AxiosXHR<T>>;
|
||||
|
||||
/**
|
||||
* Send request as configured
|
||||
*/
|
||||
new <T>(config: AxiosXHRConfig<T>): IPromise<AxiosXHR<T>>;
|
||||
|
||||
/**
|
||||
* Send request as configured
|
||||
*/
|
||||
request<T>(config: AxiosXHRConfig<T>): IPromise<AxiosXHR<T>>;
|
||||
|
||||
/**
|
||||
* intercept requests or responses before they are handled by then or catch
|
||||
*/
|
||||
interceptors: Interceptor;
|
||||
|
||||
/**
|
||||
* equivalent to `Promise.all`
|
||||
*/
|
||||
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>, T4 | IPromise<AxiosXHR<T4>>, T5 | IPromise<AxiosXHR<T5>>, T6 | IPromise<AxiosXHR<T6>>, T7 | IPromise<AxiosXHR<T7>>, T8 | IPromise<AxiosXHR<T8>>, T9 | IPromise<AxiosXHR<T9>>, T10 | IPromise<AxiosXHR<T10>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>, AxiosXHR<T4>, AxiosXHR<T5>, AxiosXHR<T6>, AxiosXHR<T7>, AxiosXHR<T8>, AxiosXHR<T9>, AxiosXHR<T10>]>;
|
||||
all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>, T4 | IPromise<AxiosXHR<T4>>, T5 | IPromise<AxiosXHR<T5>>, T6 | IPromise<AxiosXHR<T6>>, T7 | IPromise<AxiosXHR<T7>>, T8 | IPromise<AxiosXHR<T8>>, T9 | IPromise<AxiosXHR<T9>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>, AxiosXHR<T4>, AxiosXHR<T5>, AxiosXHR<T6>, AxiosXHR<T7>, AxiosXHR<T8>, AxiosXHR<T9>]>;
|
||||
all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>, T4 | IPromise<AxiosXHR<T4>>, T5 | IPromise<AxiosXHR<T5>>, T6 | IPromise<AxiosXHR<T6>>, T7 | IPromise<AxiosXHR<T7>>, T8 | IPromise<AxiosXHR<T8>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>, AxiosXHR<T4>, AxiosXHR<T5>, AxiosXHR<T6>, AxiosXHR<T7>, AxiosXHR<T8>]>;
|
||||
all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>, T4 | IPromise<AxiosXHR<T4>>, T5 | IPromise<AxiosXHR<T5>>, T6 | IPromise<AxiosXHR<T6>>, T7 | IPromise<AxiosXHR<T7>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>, AxiosXHR<T4>, AxiosXHR<T5>, AxiosXHR<T6>, AxiosXHR<T7>]>;
|
||||
all<T1, T2, T3, T4, T5, T6>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>, T4 | IPromise<AxiosXHR<T4>>, T5 | IPromise<AxiosXHR<T5>>, T6 | IPromise<AxiosXHR<T6>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>, AxiosXHR<T4>, AxiosXHR<T5>, AxiosXHR<T6>]>;
|
||||
all<T1, T2, T3, T4, T5>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>, T4 | IPromise<AxiosXHR<T4>>, T5 | IPromise<AxiosXHR<T5>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>, AxiosXHR<T4>, AxiosXHR<T5>]>;
|
||||
all<T1, T2, T3, T4>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>, T4 | IPromise<AxiosXHR<T4>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>, AxiosXHR<T4>]>;
|
||||
all<T1, T2, T3>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>, T3 | IPromise<AxiosXHR<T3>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>, AxiosXHR<T3>]>;
|
||||
all<T1, T2>(values: [T1 | IPromise<AxiosXHR<T1>>, T2 | IPromise<AxiosXHR<T2>>]): IPromise<[AxiosXHR<T1>, AxiosXHR<T2>]>;
|
||||
|
||||
/**
|
||||
* spread array parameter to `fn`.
|
||||
* note: alternative to `spread`, destructuring assignment.
|
||||
*/
|
||||
spread<T1, T2, U>(fn: (t1: T1, t2: T2) => U): (arr: ([T1, T2])) => U;
|
||||
|
||||
/**
|
||||
* convenience alias, method = GET
|
||||
*/
|
||||
@@ -163,6 +257,16 @@ declare module Axios {
|
||||
*/
|
||||
patch<T>(url: string, data?: any, config?: AxiosXHRConfigBase<T>): IPromise<AxiosXHR<T>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* <T> - expected response type,
|
||||
*/
|
||||
interface AxiosStatic extends AxiosInstance {
|
||||
/**
|
||||
* create a new instance of axios with a custom config
|
||||
*/
|
||||
create<T>(config: AxiosXHRConfigBase<T>): AxiosInstance;
|
||||
}
|
||||
}
|
||||
|
||||
declare var axios: Axios.AxiosStatic;
|
||||
|
||||
Reference in New Issue
Block a user