mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
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: Claas Augner <github@caugner.de> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
+1
-14
@@ -12,19 +12,6 @@ module.exports = function(grunt) {
|
|||||||
dist: 'dist/**'
|
dist: 'dist/**'
|
||||||
},
|
},
|
||||||
|
|
||||||
ts: {
|
|
||||||
test: {
|
|
||||||
options: {
|
|
||||||
lib: [
|
|
||||||
'es5',
|
|
||||||
'es2015.promise',
|
|
||||||
'dom'
|
|
||||||
]
|
|
||||||
},
|
|
||||||
src: ['typings/index.d.ts', 'test/typescript/*.ts']
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
package2bower: {
|
package2bower: {
|
||||||
all: {
|
all: {
|
||||||
fields: [
|
fields: [
|
||||||
@@ -116,7 +103,7 @@ module.exports = function(grunt) {
|
|||||||
';'].join(''));
|
';'].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('build', 'Run webpack and bundle the source', ['clean', 'webpack']);
|
||||||
grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower', 'package2env']);
|
grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower', 'package2env']);
|
||||||
};
|
};
|
||||||
|
|||||||
Vendored
+30
-10
@@ -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> & {
|
export type AxiosResponseHeaders = Record<string, string> & {
|
||||||
"set-cookie"?: string[]
|
"set-cookie"?: string[]
|
||||||
}
|
};
|
||||||
|
|
||||||
export interface AxiosRequestTransformer {
|
export interface AxiosRequestTransformer {
|
||||||
(data: any, headers?: AxiosRequestHeaders): any;
|
(data: any, headers?: AxiosRequestHeaders): any;
|
||||||
@@ -26,7 +28,7 @@ export interface AxiosProxyConfig {
|
|||||||
port: number;
|
port: number;
|
||||||
auth?: {
|
auth?: {
|
||||||
username: string;
|
username: string;
|
||||||
password:string;
|
password: string;
|
||||||
};
|
};
|
||||||
protocol?: string;
|
protocol?: string;
|
||||||
}
|
}
|
||||||
@@ -41,7 +43,7 @@ export type Method =
|
|||||||
| 'patch' | 'PATCH'
|
| 'patch' | 'PATCH'
|
||||||
| 'purge' | 'PURGE'
|
| 'purge' | 'PURGE'
|
||||||
| 'link' | 'LINK'
|
| 'link' | 'LINK'
|
||||||
| 'unlink' | 'UNLINK'
|
| 'unlink' | 'UNLINK';
|
||||||
|
|
||||||
export type ResponseType =
|
export type ResponseType =
|
||||||
| 'arraybuffer'
|
| 'arraybuffer'
|
||||||
@@ -49,9 +51,9 @@ export type ResponseType =
|
|||||||
| 'document'
|
| 'document'
|
||||||
| 'json'
|
| 'json'
|
||||||
| 'text'
|
| 'text'
|
||||||
| 'stream'
|
| 'stream';
|
||||||
|
|
||||||
export interface TransitionalOptions{
|
export interface TransitionalOptions {
|
||||||
silentJSONParsing?: boolean;
|
silentJSONParsing?: boolean;
|
||||||
forcedJSONParsing?: boolean;
|
forcedJSONParsing?: boolean;
|
||||||
clarifyTimeoutError?: boolean;
|
clarifyTimeoutError?: boolean;
|
||||||
@@ -89,7 +91,25 @@ export interface AxiosRequestConfig<D = any> {
|
|||||||
decompress?: boolean;
|
decompress?: boolean;
|
||||||
transitional?: TransitionalOptions;
|
transitional?: TransitionalOptions;
|
||||||
signal?: AbortSignal;
|
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> {
|
export interface AxiosResponse<T = unknown, D = any> {
|
||||||
@@ -148,13 +168,13 @@ export interface AxiosInterceptorManager<V> {
|
|||||||
|
|
||||||
export class Axios {
|
export class Axios {
|
||||||
constructor(config?: AxiosRequestConfig);
|
constructor(config?: AxiosRequestConfig);
|
||||||
defaults: AxiosRequestConfig;
|
defaults: AxiosDefaults;
|
||||||
interceptors: {
|
interceptors: {
|
||||||
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
request: AxiosInterceptorManager<AxiosRequestConfig>;
|
||||||
response: AxiosInterceptorManager<AxiosResponse>;
|
response: AxiosInterceptorManager<AxiosResponse>;
|
||||||
};
|
};
|
||||||
getUri(config?: AxiosRequestConfig): string;
|
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>;
|
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>;
|
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>;
|
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;
|
Axios: typeof Axios;
|
||||||
readonly VERSION: string;
|
readonly VERSION: string;
|
||||||
isCancel(value: any): boolean;
|
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;
|
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
|
||||||
isAxiosError(payload: any): payload is AxiosError;
|
isAxiosError(payload: any): payload is AxiosError;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -5,7 +5,7 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "grunt test",
|
"test": "grunt test && dtslint",
|
||||||
"start": "node ./sandbox/server.js",
|
"start": "node ./sandbox/server.js",
|
||||||
"build": "NODE_ENV=production grunt build",
|
"build": "NODE_ENV=production grunt build",
|
||||||
"preversion": "grunt version && npm test",
|
"preversion": "grunt version && npm test",
|
||||||
@@ -35,6 +35,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"abortcontroller-polyfill": "^1.5.0",
|
"abortcontroller-polyfill": "^1.5.0",
|
||||||
"coveralls": "^3.0.0",
|
"coveralls": "^3.0.0",
|
||||||
|
"dtslint": "^4.1.6",
|
||||||
"es6-promise": "^4.2.4",
|
"es6-promise": "^4.2.4",
|
||||||
"grunt": "^1.3.0",
|
"grunt": "^1.3.0",
|
||||||
"grunt-banner": "^0.6.0",
|
"grunt-banner": "^0.6.0",
|
||||||
@@ -44,7 +45,6 @@
|
|||||||
"grunt-eslint": "^23.0.0",
|
"grunt-eslint": "^23.0.0",
|
||||||
"grunt-karma": "^4.0.0",
|
"grunt-karma": "^4.0.0",
|
||||||
"grunt-mocha-test": "^0.13.3",
|
"grunt-mocha-test": "^0.13.3",
|
||||||
"grunt-ts": "^6.0.0-beta.19",
|
|
||||||
"grunt-webpack": "^4.0.2",
|
"grunt-webpack": "^4.0.2",
|
||||||
"istanbul-instrumenter-loader": "^1.0.0",
|
"istanbul-instrumenter-loader": "^1.0.0",
|
||||||
"jasmine-core": "^2.4.1",
|
"jasmine-core": "^2.4.1",
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import axios, {
|
|||||||
CancelToken,
|
CancelToken,
|
||||||
CancelTokenSource,
|
CancelTokenSource,
|
||||||
Canceler
|
Canceler
|
||||||
} from '../../';
|
} from 'axios';
|
||||||
|
|
||||||
const config: AxiosRequestConfig = {
|
const config: AxiosRequestConfig = {
|
||||||
url: '/user',
|
url: '/user',
|
||||||
@@ -170,8 +170,8 @@ axios.patch<User>('/user', { name: 'foo', id: 1 })
|
|||||||
// (Typed methods) with custom response type
|
// (Typed methods) with custom response type
|
||||||
|
|
||||||
const handleStringResponse = (response: string) => {
|
const handleStringResponse = (response: string) => {
|
||||||
console.log(response)
|
console.log(response);
|
||||||
}
|
};
|
||||||
|
|
||||||
axios.get<User, string>('/user?id=12345')
|
axios.get<User, string>('/user?id=12345')
|
||||||
.then(handleStringResponse)
|
.then(handleStringResponse)
|
||||||
@@ -342,11 +342,11 @@ axios.get('/user')
|
|||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.catch((error: any) => 'foo')
|
.catch((error: any) => 'foo')
|
||||||
.then((value: string) => {});
|
.then((value) => {});
|
||||||
|
|
||||||
axios.get('/user')
|
axios.get('/user')
|
||||||
.catch((error: any) => Promise.resolve('foo'))
|
.catch((error: any) => Promise.resolve('foo'))
|
||||||
.then((value: string) => {});
|
.then((value) => {});
|
||||||
|
|
||||||
// Cancellation
|
// Cancellation
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "es2015",
|
||||||
|
"lib": ["dom", "es2015"],
|
||||||
|
"types": [],
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"strict": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"baseUrl": ".",
|
||||||
|
"paths": {
|
||||||
|
"axios": ["."]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"extends": "dtslint/dtslint.json",
|
||||||
|
"rules": {
|
||||||
|
"no-unnecessary-generics": false
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user