Skip to content

Commit 94a9344

Browse files
remcohaszingcaugnerjasonsaayman
authored
Test types (axios#4140)
* Distinguish request and response data types * Fix Axios headers type `axios.headers` is not of the same type as `request.headers`, so a new type `AxiosDefaults` was introduced * Replace grunt-ts with dtslint This asserts that the type definitions are valid in the specified TypeScript version and above. This is the same tool that is used by DefinitelyTyped. * Remove grunt-ts * Restore typescript dependency * Fix missing semicolons Co-authored-by: Claas Augner <[email protected]> Co-authored-by: Jay <[email protected]>
1 parent fce210a commit 94a9344

File tree

6 files changed

+58
-31
lines changed

6 files changed

+58
-31
lines changed

Diff for: Gruntfile.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ module.exports = function(grunt) {
1212
dist: 'dist/**'
1313
},
1414

15-
ts: {
16-
test: {
17-
options: {
18-
lib: [
19-
'es5',
20-
'es2015.promise',
21-
'dom'
22-
]
23-
},
24-
src: ['typings/index.d.ts', 'test/typescript/*.ts']
25-
}
26-
},
27-
2815
package2bower: {
2916
all: {
3017
fields: [
@@ -116,7 +103,7 @@ module.exports = function(grunt) {
116103
';'].join(''));
117104
});
118105

119-
grunt.registerTask('test', 'Run the jasmine and mocha tests', ['eslint', 'mochaTest', 'karma:single', 'ts']);
106+
grunt.registerTask('test', 'Run the jasmine and mocha tests', ['eslint', 'mochaTest', 'karma:single']);
120107
grunt.registerTask('build', 'Run webpack and bundle the source', ['clean', 'webpack']);
121108
grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower', 'package2env']);
122109
};

Diff for: index.d.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
export type AxiosRequestHeaders = Record<string, string>
1+
// TypeScript Version: 3.0
2+
3+
export type AxiosRequestHeaders = Record<string, string>;
24

35
export type AxiosResponseHeaders = Record<string, string> & {
46
"set-cookie"?: string[]
5-
}
7+
};
68

79
export interface AxiosRequestTransformer {
810
(data: any, headers?: AxiosRequestHeaders): any;
@@ -26,7 +28,7 @@ export interface AxiosProxyConfig {
2628
port: number;
2729
auth?: {
2830
username: string;
29-
password:string;
31+
password: string;
3032
};
3133
protocol?: string;
3234
}
@@ -41,17 +43,17 @@ export type Method =
4143
| 'patch' | 'PATCH'
4244
| 'purge' | 'PURGE'
4345
| 'link' | 'LINK'
44-
| 'unlink' | 'UNLINK'
46+
| 'unlink' | 'UNLINK';
4547

4648
export type ResponseType =
4749
| 'arraybuffer'
4850
| 'blob'
4951
| 'document'
5052
| 'json'
5153
| 'text'
52-
| 'stream'
54+
| 'stream';
5355

54-
export interface TransitionalOptions{
56+
export interface TransitionalOptions {
5557
silentJSONParsing?: boolean;
5658
forcedJSONParsing?: boolean;
5759
clarifyTimeoutError?: boolean;
@@ -89,7 +91,25 @@ export interface AxiosRequestConfig<D = any> {
8991
decompress?: boolean;
9092
transitional?: TransitionalOptions;
9193
signal?: AbortSignal;
92-
insecureHTTPParser?: boolean
94+
insecureHTTPParser?: boolean;
95+
}
96+
97+
export interface HeadersDefaults {
98+
common: AxiosRequestHeaders;
99+
delete: AxiosRequestHeaders;
100+
get: AxiosRequestHeaders;
101+
head: AxiosRequestHeaders;
102+
post: AxiosRequestHeaders;
103+
put: AxiosRequestHeaders;
104+
patch: AxiosRequestHeaders;
105+
options?: AxiosRequestHeaders;
106+
purge?: AxiosRequestHeaders;
107+
link?: AxiosRequestHeaders;
108+
unlink?: AxiosRequestHeaders;
109+
}
110+
111+
export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
112+
headers: HeadersDefaults;
93113
}
94114

95115
export interface AxiosResponse<T = unknown, D = any> {
@@ -148,13 +168,13 @@ export interface AxiosInterceptorManager<V> {
148168

149169
export class Axios {
150170
constructor(config?: AxiosRequestConfig);
151-
defaults: AxiosRequestConfig;
171+
defaults: AxiosDefaults;
152172
interceptors: {
153173
request: AxiosInterceptorManager<AxiosRequestConfig>;
154174
response: AxiosInterceptorManager<AxiosResponse>;
155175
};
156176
getUri(config?: AxiosRequestConfig): string;
157-
request<T = unknown, R = AxiosResponse<T>, D = any> (config: AxiosRequestConfig<D>): Promise<R>;
177+
request<T = unknown, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
158178
get<T = unknown, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
159179
delete<T = unknown, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
160180
head<T = unknown, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
@@ -176,7 +196,7 @@ export interface AxiosStatic extends AxiosInstance {
176196
Axios: typeof Axios;
177197
readonly VERSION: string;
178198
isCancel(value: any): boolean;
179-
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
199+
all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
180200
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
181201
isAxiosError(payload: any): payload is AxiosError;
182202
}

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"types": "index.d.ts",
77
"scripts": {
8-
"test": "grunt test",
8+
"test": "grunt test && dtslint",
99
"start": "node ./sandbox/server.js",
1010
"build": "NODE_ENV=production grunt build",
1111
"preversion": "grunt version && npm test",
@@ -35,6 +35,7 @@
3535
"devDependencies": {
3636
"abortcontroller-polyfill": "^1.5.0",
3737
"coveralls": "^3.0.0",
38+
"dtslint": "^4.1.6",
3839
"es6-promise": "^4.2.4",
3940
"grunt": "^1.3.0",
4041
"grunt-banner": "^0.6.0",
@@ -44,7 +45,6 @@
4445
"grunt-eslint": "^23.0.0",
4546
"grunt-karma": "^4.0.0",
4647
"grunt-mocha-test": "^0.13.3",
47-
"grunt-ts": "^6.0.0-beta.19",
4848
"grunt-webpack": "^4.0.2",
4949
"istanbul-instrumenter-loader": "^1.0.0",
5050
"jasmine-core": "^2.4.1",

Diff for: test/typescript/axios.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import axios, {
88
CancelToken,
99
CancelTokenSource,
1010
Canceler
11-
} from '../../';
11+
} from 'axios';
1212

1313
const config: AxiosRequestConfig = {
1414
url: '/user',
@@ -170,8 +170,8 @@ axios.patch<User>('/user', { name: 'foo', id: 1 })
170170
// (Typed methods) with custom response type
171171

172172
const handleStringResponse = (response: string) => {
173-
console.log(response)
174-
}
173+
console.log(response);
174+
};
175175

176176
axios.get<User, string>('/user?id=12345')
177177
.then(handleStringResponse)
@@ -342,11 +342,11 @@ axios.get('/user')
342342

343343
axios.get('/user')
344344
.catch((error: any) => 'foo')
345-
.then((value: string) => {});
345+
.then((value) => {});
346346

347347
axios.get('/user')
348348
.catch((error: any) => Promise.resolve('foo'))
349-
.then((value: string) => {});
349+
.then((value) => {});
350350

351351
// Cancellation
352352

Diff for: tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"module": "es2015",
4+
"lib": ["dom", "es2015"],
5+
"types": [],
6+
"moduleResolution": "node",
7+
"strict": true,
8+
"noEmit": true,
9+
"baseUrl": ".",
10+
"paths": {
11+
"axios": ["."]
12+
}
13+
}
14+
}

Diff for: tslint.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"no-unnecessary-generics": false
5+
}
6+
}

0 commit comments

Comments
 (0)