Skip to content

Commit

Permalink
Merge 21db0e1 into cfe33d4
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Dec 6, 2016
2 parents cfe33d4 + 21db0e1 commit 4dd1ce7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
34 changes: 17 additions & 17 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface AxiosTransformer {
}

export interface AxiosAdapter {
(config: AxiosRequestConfig): AxiosPromise;
(config: AxiosRequestConfig): AxiosPromise<any>;
}

export interface AxiosBasicCredentials {
Expand Down Expand Up @@ -44,8 +44,8 @@ export interface AxiosRequestConfig {
cancelToken?: CancelToken;
}

export interface AxiosResponse {
data: any;
export interface AxiosResponse<T> {
data: T;
status: number;
statusText: string;
headers: any;
Expand All @@ -55,7 +55,7 @@ export interface AxiosResponse {
export interface AxiosError extends Error {
config: AxiosRequestConfig;
code?: string;
response?: AxiosResponse;
response?: AxiosResponse<any>;
}

export interface Promise<V> {
Expand All @@ -64,7 +64,7 @@ export interface Promise<V> {
catch<R>(onRejected: (error: any) => R | Promise<R>): Promise<R>;
}

export interface AxiosPromise extends Promise<AxiosResponse> {
export interface AxiosPromise<T> extends Promise<AxiosResponse<T>> {
}

export interface CancelStatic {
Expand Down Expand Up @@ -104,20 +104,20 @@ export interface AxiosInstance {
defaults: AxiosRequestConfig;
interceptors: {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
response: AxiosInterceptorManager<AxiosResponse<any>>;
};
request(config: AxiosRequestConfig): AxiosPromise;
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
request<T>(config: AxiosRequestConfig): AxiosPromise<T>;
get<T>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
delete<T>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
head<T>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
post<T>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
put<T>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
patch<T>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
}

export interface AxiosStatic extends AxiosInstance {
(config: AxiosRequestConfig): AxiosPromise;
(url: string, config?: AxiosRequestConfig): AxiosPromise;
<T>(config: AxiosRequestConfig): AxiosPromise<T>;
<T>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
create(config?: AxiosRequestConfig): AxiosInstance;
Cancel: CancelStatic;
CancelToken: CancelTokenStatic;
Expand All @@ -126,6 +126,6 @@ export interface AxiosStatic extends AxiosInstance {
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
}

declare const Axios: AxiosStatic;
declare var axios: AxiosStatic;

export default Axios;
export default axios;
20 changes: 10 additions & 10 deletions test/typescript/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const config: AxiosRequestConfig = {
cancelToken: new axios.CancelToken((cancel: Canceler) => {})
};

const handleResponse = (response: AxiosResponse) => {
const handleResponse = (response: AxiosResponse<any>) => {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
Expand Down Expand Up @@ -154,24 +154,24 @@ axios.interceptors.request.use((config: AxiosRequestConfig) => config);
axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config));

const responseInterceptorId: number = axios.interceptors.response.use(
(response: AxiosResponse) => response,
(response: AxiosResponse<any>) => response,
(error: any) => Promise.reject(error)
);

axios.interceptors.response.eject(responseInterceptorId);

axios.interceptors.response.use(
(response: AxiosResponse) => Promise.resolve(response),
(response: AxiosResponse<any>) => Promise.resolve(response),
(error: any) => Promise.reject(error)
);

axios.interceptors.response.use((response: AxiosResponse) => response);
axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(response));
axios.interceptors.response.use((response: AxiosResponse<any>) => response);
axios.interceptors.response.use((response: AxiosResponse<any>) => Promise.resolve(response));

// Adapters

const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
const response: AxiosResponse<{foo: string}> = {
data: { foo: 'bar' },
status: 200,
statusText: 'OK',
Expand Down Expand Up @@ -200,19 +200,19 @@ const fn2: (arr: number[]) => string = axios.spread(fn1);
// Promises

axios.get('/user')
.then((response: AxiosResponse) => 'foo')
.then((response: AxiosResponse<any>) => 'foo')
.then((value: string) => {});

axios.get('/user')
.then((response: AxiosResponse) => Promise.resolve('foo'))
.then((response: AxiosResponse<any>) => Promise.resolve('foo'))
.then((value: string) => {});

axios.get('/user')
.then((response: AxiosResponse) => 'foo', (error: any) => 'bar')
.then((response: AxiosResponse<any>) => 'foo', (error: any) => 'bar')
.then((value: string) => {});

axios.get('/user')
.then((response: AxiosResponse) => 'foo', (error: any) => 123)
.then((response: AxiosResponse<any>) => 'foo', (error: any) => 123)
.then((value: string | number) => {});

axios.get('/user')
Expand Down
5 changes: 0 additions & 5 deletions typings.json

This file was deleted.

0 comments on commit 4dd1ce7

Please sign in to comment.