From 5176dfdec52033d73333613b8884aa74a7f0b005 Mon Sep 17 00:00:00 2001 From: Nick Uraltsev Date: Tue, 9 Aug 2016 23:17:11 -0700 Subject: [PATCH] Converting TypeScript definitions to ES2015 module syntax --- axios.d.ts | 124 +++++++++++++++++++-------------------- lib/axios.js | 7 ++- package.json | 4 +- test/typescript/axios.ts | 4 +- 4 files changed, 70 insertions(+), 69 deletions(-) diff --git a/axios.d.ts b/axios.d.ts index 7018e95..920657a 100644 --- a/axios.d.ts +++ b/axios.d.ts @@ -1,65 +1,65 @@ -// Type definitions for Axios v0.8.1 -// Project: https://github.com/mzabriskie/axios - - - -declare var axios: axios.AxiosStatic - -declare module axios { - interface AxiosRequestMethods { - get(url: string, config?: any): axios.Promise; - delete(url: string, config?: any): axios.Promise; - head(url: string, config?: any): axios.Promise; - post(url: string, data: any, config?: any): axios.Promise; - put(url: string, data: any, config?: any): axios.Promise; - patch(url: string, data: any, config?: any): axios.Promise; - } - - interface AxiosStatic extends AxiosRequestMethods { - (options: axios.RequestOptions): axios.Promise; - create(defaultOptions?: axios.InstanceOptions): AxiosInstance; - all(iterable: any): axios.Promise; - spread(callback: any): axios.Promise; - } - - interface AxiosInstance extends AxiosRequestMethods { - request(options: axios.RequestOptions): axios.Promise; - } - - interface Response { - data?: any; - status?: number; - statusText?: string; - headers?: any; - config?: any; - } - - interface Promise { - then(onFulfilled:(response: axios.Response) => void): axios.Promise; - catch(onRejected:(response: axios.Response) => void): axios.Promise; - } - - interface InstanceOptions { - transformRequest?: (data: any) => any; - transformResponse?: (data: any) => any; - headers?: any; - timeout?: number; - withCredentials?: boolean; - responseType?: string; - xsrfCookieName?: string; - xsrfHeaderName?: string; - paramsSerializer?: (params: any) => string; - baseURL?: string; - } - - interface RequestOptions extends InstanceOptions { - url: string; - method?: string; - params?: any; - data?: any; - } +export interface Thenable { + then(onFulfilled?: (value: V) => R | Thenable, onRejected?: (reason: any) => R | Thenable): Thenable; + then(onFulfilled?: (value: V) => R | Thenable, onRejected?: (reason: any) => void): Thenable; + catch(onRejected?: (reason: any) => R | Thenable): Thenable; } -declare module "axios" { - export = axios; +export interface AxiosDataTransformer { + (data: any): any; } + +export interface AxiosRequestConfig { + url?: string; + method?: string; + baseURL?: string; + transformRequest?: AxiosDataTransformer | AxiosDataTransformer[]; + transformResponse?: AxiosDataTransformer | AxiosDataTransformer[]; + headers?: any; + params?: any; + paramsSerializer?: (params: any) => string; + data?: any; + timeout?: number; + withCredentials?: boolean; + adapter?: any; + auth?: any; + responseType?: string; + xsrfCookieName?: string; + xsrfHeaderName?: string; + progress?: (progressEvent: any) => void; + maxContentLength?: number; + validateStatus?: (status: number) => boolean; + maxRedirects?: number; + httpAgent?: any; + httpsAgent?: any; +} + +export interface AxiosResponse { + data: any; + status: number; + statusText: string; + headers: any; + config: AxiosRequestConfig; +} + +export interface AxiosInstance { + defaults: AxiosRequestConfig; + request(config: AxiosRequestConfig): Thenable; + get(url: string, config?: AxiosRequestConfig): Thenable; + delete(url: string, config?: AxiosRequestConfig): Thenable; + head(url: string, config?: AxiosRequestConfig): Thenable; + post(url: string, data?: any, config?: AxiosRequestConfig): Thenable; + put(url: string, data?: any, config?: AxiosRequestConfig): Thenable; + patch(url: string, data?: any, config?: AxiosRequestConfig): Thenable; +} + +export interface AxiosStatic extends AxiosInstance { + (config: AxiosRequestConfig): Thenable; + (url: string, config?: AxiosRequestConfig): Thenable; + create(config?: AxiosRequestConfig): AxiosInstance; + all(iterable: any): Thenable; + spread(callback: any): Thenable; +} + +declare const Axios: AxiosStatic; + +export default Axios; diff --git a/lib/axios.js b/lib/axios.js index b0febb0..8937b80 100644 --- a/lib/axios.js +++ b/lib/axios.js @@ -24,7 +24,7 @@ function createInstance(defaultConfig) { } // Create the default instance to be exported -var axios = module.exports = createInstance(); +var axios = createInstance(); // Expose Axios class to allow class inheritance axios.Axios = Axios; @@ -39,3 +39,8 @@ axios.all = function all(promises) { return Promise.all(promises); }; axios.spread = require('./helpers/spread'); + +module.exports = axios; + +// Allow use of default import syntax in TypeScript +module.exports.default = axios; diff --git a/package.json b/package.json index 351d0e1..d9ce313 100644 --- a/package.json +++ b/package.json @@ -69,9 +69,7 @@ "browser": { "./lib/adapters/http.js": "./lib/adapters/xhr.js" }, - "typescript": { - "definition": "./axios.d.ts" - }, + "typings": "./axios.d.ts", "dependencies": { "follow-redirects": "0.0.7" } diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index 83146a6..61a5ddd 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -1,6 +1,4 @@ -/// - -import axios = require('axios'); +import axios from '../../'; axios.get('/user?ID=12345') .then(function (response) {