Skip to content

Commit

Permalink
[axios_v0.19.x] New options definition (#3681)
Browse files Browse the repository at this point in the history
  • Loading branch information
quanlinc authored and pascalduez committed Jan 1, 2020
1 parent 1ea39ed commit b09080b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
48 changes: 27 additions & 21 deletions definitions/npm/axios_v0.19.x/flow_v0.80.x-/axios_v0.19.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ declare module 'axios' {

declare type AxiosTransformer<T> = (
data: T,
headers?: { [key: string]: any }
) => any;
headers?: { [key: string]: mixed, ...},
) => mixed;

declare type ProxyConfig = {|
host: string,
port: number,
auth?: {
auth?: {|
username: string,
password: string,
},
|},
protocol?: string,
|};

Expand Down Expand Up @@ -62,25 +62,25 @@ declare module 'axios' {
| 'stream';

declare type AxiosAdapter = (
config: AxiosXHRConfig<any>
) => Promise<AxiosXHR<any>>;
config: AxiosXHRConfig<mixed>
) => Promise<AxiosXHR<mixed>>;

declare type AxiosXHRConfigBase<T, R = T> = {
adapter?: AxiosAdapter,
auth?: {
auth?: {|
username: string,
password: string,
},
|},
baseURL?: string,
cancelToken?: CancelToken,
headers?: { [key: string]: any },
headers?: { [key: string]: mixed, ...},
httpAgent?: HttpAgent,
httpsAgent?: HttpsAgent,
maxContentLength?: number,
maxRedirects?: number,
socketPath?: string | null,
params?: { [key: string]: any },
paramsSerializer?: (params: { [key: string]: any }) => string,
params?: { [key: string]: mixed, ...},
paramsSerializer?: (params: { [key: string]: mixed, ...}) => string,
onUploadProgress?: (progressEvent: ProgressEvent) => void,
onDownloadProgress?: (progressEvent: ProgressEvent) => void,
proxy?: ProxyConfig | false,
Expand All @@ -92,25 +92,27 @@ declare module 'axios' {
withCredentials?: boolean,
xsrfCookieName?: string,
xsrfHeaderName?: string,
...
};

declare type AxiosXHRConfig<T, R = T> = {|
declare type AxiosXHRConfig<T, R = T> = {
...$Exact<AxiosXHRConfigBase<T, R>>,
data?: T,
method?: Method,
url: string,
|};
...
};

declare type AxiosXHRConfigShape<T, R = T> = $Shape<AxiosXHRConfig<T, R>>;

declare type AxiosXHR<T, R = T> = {
declare type AxiosXHR<T, R = T> = {|
config: AxiosXHRConfig<T, R>,
data: R,
headers: ?{ [key: string]: any },
headers: ?{[key: string]: mixed, ...},
status: number,
statusText: string,
request: http$ClientRequest<> | XMLHttpRequest | mixed,
};
|};

declare type AxiosInterceptorIdent = number;

Expand Down Expand Up @@ -156,6 +158,10 @@ declare module 'axios' {
url: string,
config?: AxiosXHRConfigBase<mixed, R>
): AxiosPromise<mixed, R>;
options<R>(
url: string,
config?: AxiosXHRConfigBase<mixed, R>
): AxiosPromise<mixed, R>;
post<T, R>(
url: string,
data?: T,
Expand All @@ -171,13 +177,13 @@ declare module 'axios' {
data?: T,
config?: AxiosXHRConfigBase<T, R>
): AxiosPromise<T, R>;
interceptors: {
interceptors: {|
request: AxiosRequestInterceptor<mixed>,
response: AxiosResponseInterceptor<mixed>,
};
|};
defaults: {|
...$Exact<AxiosXHRConfigBase<mixed>>,
headers: { [key: string]: any },
headers: { [key: string]: mixed, ...},
|};
getUri<T, R>(config?: AxiosXHRConfig<T, R>): string;
}
Expand All @@ -198,8 +204,8 @@ declare module 'axios' {
Axios: typeof Axios;
Cancel: typeof Cancel;
CancelToken: typeof CancelToken;
isCancel(value: any): boolean;
create(config?: AxiosXHRConfigBase<any>): Axios;
isCancel(value: mixed): boolean;
create(config?: AxiosXHRConfigBase<T, R>): Axios;
all: typeof Promise.all;
spread<T, R>(callback: (...args: T) => R): (array: T) => R;
}
Expand Down
70 changes: 62 additions & 8 deletions definitions/npm/axios_v0.19.x/flow_v0.80.x-/test_axios_v0.19.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type RequestDataUser = {|
+name: string,
|};

type Header = {[key: string]: mixed,...};

const handleResponse = (response: $AxiosXHR<mixed>) => {
(response.data: mixed);
(response.status: number);
Expand All @@ -29,7 +31,7 @@ const handleResponse = (response: $AxiosXHR<mixed>) => {
(response.statusText: string);
// $ExpectError
(response.statusText: number);
(response.headers: ?{});
(response.headers: ?{...});
(response.config: $AxiosXHRConfig<mixed>);
// $ExpectError
(response.config: string);
Expand All @@ -55,16 +57,16 @@ const handleError = (error: $AxiosError<mixed>) => {
if (error.response) {
(error.response.data: mixed);
(error.response.status: number);
(error.response.headers: ?{});
(error.response.headers: ?{...});
} else {
(error.message: string);
}
};

const config: $AxiosXHRConfigBase<RequestDataUser, User> = {
baseURL: 'https://api.example.com/',
transformRequest: (data: RequestDataUser) => '{"foo":"bar"}',
transformResponse: [(data: User) => ({ baz: 'qux' })],
transformRequest: (data: RequestDataUser, headers?: Header) => '{"foo":"bar"}',
transformResponse: [(data: User, headers?: Header) => ({ baz: 'qux' })],
headers: { 'X-FOO': 'bar' },
params: { id: 12345 },
paramsSerializer: (params: Object) => 'id=12345',
Expand Down Expand Up @@ -105,6 +107,16 @@ describe('Config', () => {
});
});

describe('Headers', () => {
it('should take inexact kv pairs with string type as key and mixed type as values', () => {
const test: Header = { 'foo': 1 };
const test1: Header = { 'bar': 'baz'};

// $ExpectError
const test2: Header = { 1 : 'false'}
});
})

describe('Request methods', () => {
it('returns a promise', () => {
axios({
Expand Down Expand Up @@ -258,14 +270,30 @@ describe('Create instance', () => {
describe('Defaults', () => {
it('setters default config', () => {
axios.defaults.baseURL = 'https://api.example.com/';
axios.defaults.headers.common['Authorization'] = 'token';
axios.defaults.headers.post['X-FOO'] = 'bar';
axios.defaults.headers = {
'common': {
'Authorization': 'token'
}
}
axios.defaults.headers = {
'post': {
'X-FOO': 'bar'
}};
axios.defaults.timeout = 2500;

const instance: Axios = axios.create();
instance.defaults.baseURL = 'https://api.example.com/';
instance.defaults.headers.common['Authorization'] = 'token';
instance.defaults.headers.post['X-FOO'] = 'bar';
instance.defaults.headers = {
'common': {
'Authorization': 'token'
}
};
axios.defaults.headers = {
'post': {
'X-FOO': 'bar'
}
};

instance.defaults.timeout = 2500;
});
});
Expand Down Expand Up @@ -399,3 +427,29 @@ describe('getUri', () => {
const uri2: number = axios.getUri();
});
});

describe('options', () => {
it('accepts string url only', () => {
//$ExpectError
axios.options(123)
axios.options('a url')
});

it('takes a url and returns a promise', () => {
axios
.options('anyUrl')
.then(handleResponse)
.catch(handleError);
});

it('takes url and config, returns a promise', () => {
const axiosConfig:$AxiosXHRConfig<mixed> = {
url: '/foo',
method: 'OPTIONS',
};
axios
.options('a url', axiosConfig)
.then(handleResponse)
.catch(handleError)
});
});

0 comments on commit b09080b

Please sign in to comment.