From 94a93447992392441f1928dffc9f10529ecec417 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Tue, 12 Oct 2021 09:53:10 +0200 Subject: [PATCH] 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 Co-authored-by: Jay --- Gruntfile.js | 15 +-------------- index.d.ts | 40 ++++++++++++++++++++++++++++++---------- package.json | 4 ++-- test/typescript/axios.ts | 10 +++++----- tsconfig.json | 14 ++++++++++++++ tslint.json | 6 ++++++ 6 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 tsconfig.json create mode 100644 tslint.json diff --git a/Gruntfile.js b/Gruntfile.js index 6d8cf8c..c07e228 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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']); }; diff --git a/index.d.ts b/index.d.ts index 6fe2cf5..4255a72 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,8 +1,10 @@ -export type AxiosRequestHeaders = Record +// TypeScript Version: 3.0 + +export type AxiosRequestHeaders = Record; export type AxiosResponseHeaders = Record & { "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 { 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 extends Omit, 'headers'> { + headers: HeadersDefaults; } export interface AxiosResponse { @@ -148,13 +168,13 @@ export interface AxiosInterceptorManager { export class Axios { constructor(config?: AxiosRequestConfig); - defaults: AxiosRequestConfig; + defaults: AxiosDefaults; interceptors: { request: AxiosInterceptorManager; response: AxiosInterceptorManager; }; getUri(config?: AxiosRequestConfig): string; - request, D = any> (config: AxiosRequestConfig): Promise; + request, D = any>(config: AxiosRequestConfig): Promise; get, D = any>(url: string, config?: AxiosRequestConfig): Promise; delete, D = any>(url: string, config?: AxiosRequestConfig): Promise; head, D = any>(url: string, config?: AxiosRequestConfig): Promise; @@ -176,7 +196,7 @@ export interface AxiosStatic extends AxiosInstance { Axios: typeof Axios; readonly VERSION: string; isCancel(value: any): boolean; - all(values: (T | Promise)[]): Promise; + all(values: Array>): Promise; spread(callback: (...args: T[]) => R): (array: T[]) => R; isAxiosError(payload: any): payload is AxiosError; } diff --git a/package.json b/package.json index 7983b9a..3a40ba9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index 999b363..20cf058 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -8,7 +8,7 @@ import axios, { CancelToken, CancelTokenSource, Canceler -} from '../../'; +} from 'axios'; const config: AxiosRequestConfig = { url: '/user', @@ -170,8 +170,8 @@ axios.patch('/user', { name: 'foo', id: 1 }) // (Typed methods) with custom response type const handleStringResponse = (response: string) => { - console.log(response) -} + console.log(response); +}; axios.get('/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 diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..6665188 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "es2015", + "lib": ["dom", "es2015"], + "types": [], + "moduleResolution": "node", + "strict": true, + "noEmit": true, + "baseUrl": ".", + "paths": { + "axios": ["."] + } + } +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..3ec44a7 --- /dev/null +++ b/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "no-unnecessary-generics": false + } +}