Skip to content

Commit

Permalink
Test types (#4140)
Browse files Browse the repository at this point in the history
* 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 <github@caugner.de>
Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
3 people committed Oct 12, 2021
1 parent fce210a commit 94a9344
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
15 changes: 1 addition & 14 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ module.exports = function(grunt) {
dist: 'dist/**'
},

ts: {
test: {
options: {
lib: [
'es5',
'es2015.promise',
'dom'
]
},
src: ['typings/index.d.ts', 'test/typescript/*.ts']
}
},

package2bower: {
all: {
fields: [
Expand Down Expand Up @@ -116,7 +103,7 @@ module.exports = function(grunt) {
';'].join(''));
});

grunt.registerTask('test', 'Run the jasmine and mocha tests', ['eslint', 'mochaTest', 'karma:single', 'ts']);
grunt.registerTask('test', 'Run the jasmine and mocha tests', ['eslint', 'mochaTest', 'karma:single']);
grunt.registerTask('build', 'Run webpack and bundle the source', ['clean', 'webpack']);
grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower', 'package2env']);
};
40 changes: 30 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export type AxiosRequestHeaders = Record<string, string>
// TypeScript Version: 3.0

export type AxiosRequestHeaders = Record<string, string>;

export type AxiosResponseHeaders = Record<string, string> & {
"set-cookie"?: string[]
}
};

export interface AxiosRequestTransformer {
(data: any, headers?: AxiosRequestHeaders): any;
Expand All @@ -26,7 +28,7 @@ export interface AxiosProxyConfig {
port: number;
auth?: {
username: string;
password:string;
password: string;
};
protocol?: string;
}
Expand All @@ -41,17 +43,17 @@ export type Method =
| 'patch' | 'PATCH'
| 'purge' | 'PURGE'
| 'link' | 'LINK'
| 'unlink' | 'UNLINK'
| 'unlink' | 'UNLINK';

export type ResponseType =
| 'arraybuffer'
| 'blob'
| 'document'
| 'json'
| 'text'
| 'stream'
| 'stream';

export interface TransitionalOptions{
export interface TransitionalOptions {
silentJSONParsing?: boolean;
forcedJSONParsing?: boolean;
clarifyTimeoutError?: boolean;
Expand Down Expand Up @@ -89,7 +91,25 @@ export interface AxiosRequestConfig<D = any> {
decompress?: boolean;
transitional?: TransitionalOptions;
signal?: AbortSignal;
insecureHTTPParser?: boolean
insecureHTTPParser?: boolean;
}

export interface HeadersDefaults {
common: AxiosRequestHeaders;
delete: AxiosRequestHeaders;
get: AxiosRequestHeaders;
head: AxiosRequestHeaders;
post: AxiosRequestHeaders;
put: AxiosRequestHeaders;
patch: AxiosRequestHeaders;
options?: AxiosRequestHeaders;
purge?: AxiosRequestHeaders;
link?: AxiosRequestHeaders;
unlink?: AxiosRequestHeaders;
}

export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
headers: HeadersDefaults;
}

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

export class Axios {
constructor(config?: AxiosRequestConfig);
defaults: AxiosRequestConfig;
defaults: AxiosDefaults;
interceptors: {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
getUri(config?: AxiosRequestConfig): string;
request<T = unknown, R = AxiosResponse<T>, D = any> (config: AxiosRequestConfig<D>): Promise<R>;
request<T = unknown, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
get<T = unknown, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
delete<T = unknown, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
head<T = unknown, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
Expand All @@ -176,7 +196,7 @@ export interface AxiosStatic extends AxiosInstance {
Axios: typeof Axios;
readonly VERSION: string;
isCancel(value: any): boolean;
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
isAxiosError(payload: any): payload is AxiosError;
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "grunt test",
"test": "grunt test && dtslint",
"start": "node ./sandbox/server.js",
"build": "NODE_ENV=production grunt build",
"preversion": "grunt version && npm test",
Expand Down Expand Up @@ -35,6 +35,7 @@
"devDependencies": {
"abortcontroller-polyfill": "^1.5.0",
"coveralls": "^3.0.0",
"dtslint": "^4.1.6",
"es6-promise": "^4.2.4",
"grunt": "^1.3.0",
"grunt-banner": "^0.6.0",
Expand All @@ -44,7 +45,6 @@
"grunt-eslint": "^23.0.0",
"grunt-karma": "^4.0.0",
"grunt-mocha-test": "^0.13.3",
"grunt-ts": "^6.0.0-beta.19",
"grunt-webpack": "^4.0.2",
"istanbul-instrumenter-loader": "^1.0.0",
"jasmine-core": "^2.4.1",
Expand Down
10 changes: 5 additions & 5 deletions test/typescript/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import axios, {
CancelToken,
CancelTokenSource,
Canceler
} from '../../';
} from 'axios';

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

const handleStringResponse = (response: string) => {
console.log(response)
}
console.log(response);
};

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

axios.get('/user')
.catch((error: any) => 'foo')
.then((value: string) => {});
.then((value) => {});

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

// Cancellation

Expand Down
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "es2015",
"lib": ["dom", "es2015"],
"types": [],
"moduleResolution": "node",
"strict": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"axios": ["."]
}
}
}
6 changes: 6 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-unnecessary-generics": false
}
}

0 comments on commit 94a9344

Please sign in to comment.