未验证 提交 94a93447 编写于 作者: R Remco Haszing 提交者: GitHub

Test types (#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: NClaas Augner <github@caugner.de>
Co-authored-by: NJay <jasonsaayman@gmail.com>
上级 fce210a6
......@@ -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: [
......@@ -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']);
};
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;
......@@ -26,7 +28,7 @@ export interface AxiosProxyConfig {
port: number;
auth?: {
username: string;
password:string;
password: string;
};
protocol?: string;
}
......@@ -41,7 +43,7 @@ export type Method =
| 'patch' | 'PATCH'
| 'purge' | 'PURGE'
| 'link' | 'LINK'
| 'unlink' | 'UNLINK'
| 'unlink' | 'UNLINK';
export type ResponseType =
| 'arraybuffer'
......@@ -49,9 +51,9 @@ export type ResponseType =
| 'document'
| 'json'
| 'text'
| 'stream'
| 'stream';
export interface TransitionalOptions{
export interface TransitionalOptions {
silentJSONParsing?: boolean;
forcedJSONParsing?: boolean;
clarifyTimeoutError?: boolean;
......@@ -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> {
......@@ -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>;
......@@ -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;
}
......
......@@ -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",
......@@ -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",
......@@ -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",
......
......@@ -8,7 +8,7 @@ import axios, {
CancelToken,
CancelTokenSource,
Canceler
} from '../../';
} from 'axios';
const config: AxiosRequestConfig = {
url: '/user',
......@@ -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)
......@@ -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
......
{
"compilerOptions": {
"module": "es2015",
"lib": ["dom", "es2015"],
"types": [],
"moduleResolution": "node",
"strict": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"axios": ["."]
}
}
}
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-unnecessary-generics": false
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册