diff --git a/axios/axios-tests.ts b/axios/axios-tests.ts index 3f692307a..184292c92 100644 --- a/axios/axios-tests.ts +++ b/axios/axios-tests.ts @@ -8,21 +8,64 @@ interface Repository { name: string; } +interface Issue { + id: number; + title: string; +} + +axios.interceptors.request.use(config => { + console.log("Method:" + config.method + " Url:" +config.url); + return config; +}); + +axios.interceptors.response.use(config => { + console.log("Status:" + config.status); + return config; +}); + axios.get("https://api.github.com/repos/mzabriskie/axios") .then(r => console.log(r.config.method)); -axios({ +var getRepoDetails = axios({ 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("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([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, repo2: Axios.AxiosXHR) => { + var sumIds = repo1.data.id + repo2.data.id; + console.log("Sum ID:" + sumIds); + return sumIds; +}; + +axios.all([getRepoDetails, getRepoDetails]).then(axios.spread(repoSum)); diff --git a/axios/axios.d.ts b/axios/axios.d.ts index fd19caf94..7348ec651 100644 --- a/axios/axios.d.ts +++ b/axios/axios.d.ts @@ -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 // Definitions: https://github.com/borisyankov/DefinitelyTyped - declare module Axios { interface IThenable { @@ -18,21 +17,24 @@ declare module Axios { } /** + * HTTP Basic auth details + */ + interface AxiosHttpBasicAuth { + username: string; + password: string; + } + + /** + * Common axios XHR config interface * - request body data type */ interface AxiosXHRConfigBase { - /** - * 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?: ((data: T) => U) | [(data: T) => U]; - - /** - * change the response data to be made before it is passed to then/catch - */ - transformResponse?: (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?: ((data: T) => U) | [(data: T) => U]; + + /** + * change the response data to be made before it is passed to then/catch + */ + transformResponse?: (data: T) => U; } /** @@ -92,7 +125,7 @@ declare module Axios { } /** - * - expected response type, + * - expected response type, * - request body data type */ interface AxiosXHR { @@ -122,16 +155,77 @@ declare module Axios { config: AxiosXHRConfig; } + interface Interceptor { + /** + * intercept request before it is sent + */ + request: RequestInterceptor; + + /** + * intercept response of request when it is received. + */ + response: ResponseInterceptor + } + + interface RequestInterceptor { + /** + * - request body data type + */ + use(fn: (config: AxiosXHRConfig) => AxiosXHRConfig): void; + } + + interface ResponseInterceptor { + /** + * - expected response type + */ + use(fn: (config: AxiosXHR) => AxiosXHR): void; + } + /** - * - expected response type, + * - expected response type, * - request body data type */ - interface AxiosStatic { + interface AxiosInstance { + /** + * Send request as configured + */ (config: AxiosXHRConfig): IPromise>; + /** + * Send request as configured + */ new (config: AxiosXHRConfig): IPromise>; + /** + * Send request as configured + */ + request(config: AxiosXHRConfig): IPromise>; + + /** + * intercept requests or responses before they are handled by then or catch + */ + interceptors: Interceptor; + + /** + * equivalent to `Promise.all` + */ + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>, T4 | IPromise>, T5 | IPromise>, T6 | IPromise>, T7 | IPromise>, T8 | IPromise>, T9 | IPromise>, T10 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>, T4 | IPromise>, T5 | IPromise>, T6 | IPromise>, T7 | IPromise>, T8 | IPromise>, T9 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>, T4 | IPromise>, T5 | IPromise>, T6 | IPromise>, T7 | IPromise>, T8 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>, T4 | IPromise>, T5 | IPromise>, T6 | IPromise>, T7 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>, T4 | IPromise>, T5 | IPromise>, T6 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>, T4 | IPromise>, T5 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>, T4 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>, T3 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR, AxiosXHR]>; + all(values: [T1 | IPromise>, T2 | IPromise>]): IPromise<[AxiosXHR, AxiosXHR]>; + + /** + * spread array parameter to `fn`. + * note: alternative to `spread`, destructuring assignment. + */ + spread(fn: (t1: T1, t2: T2) => U): (arr: ([T1, T2])) => U; + /** * convenience alias, method = GET */ @@ -163,6 +257,16 @@ declare module Axios { */ patch(url: string, data?: any, config?: AxiosXHRConfigBase): IPromise>; } + + /** + * - expected response type, + */ + interface AxiosStatic extends AxiosInstance { + /** + * create a new instance of axios with a custom config + */ + create(config: AxiosXHRConfigBase): AxiosInstance; + } } declare var axios: Axios.AxiosStatic;