diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index e7e9d11..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Default ignored files -/workspace.xml diff --git a/.idea/axios-miniprogram.iml b/.idea/axios-miniprogram.iml deleted file mode 100644 index d6ebd48..0000000 --- a/.idea/axios-miniprogram.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 03d9549..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 28a804d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index be0275a..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 7dbe1a6..f7edd17 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,6 @@ cache: install: - npm install script: + - npm run prettier + - npm run lint - npm run coverage \ No newline at end of file diff --git a/README.md b/README.md index b020248..5cc3270 100644 --- a/README.md +++ b/README.md @@ -459,9 +459,9 @@ const uri = axios.getUri({ }); ``` -### `axios.create(config)` +### `axios.create(defaults)` -创建一个`自定义实例`,传入的自定义配置`config`会和`axios`的默认配置`axios.defaults`合并成`自定义实例`的默认配置。 +创建一个`自定义实例`,传入的自定义默认配置`defaults`会和`axios`的默认配置`axios.defaults`合并成`自定义实例`的默认配置。 `自定义实例`拥有和`axios`相同的调用方式和请求方法的别名。 @@ -484,9 +484,9 @@ instance.get('/test'); ### `axios.Axios` -`axios.Axios`是一个类,其实`axios`就是`axios.Axios`类的实例改造而来的,`axios.create(config)`创建的也是`axios.Axios`的实例。 +`axios.Axios`是一个类,其实`axios`就是`axios.Axios`类的实例改造而来的,`axios.create(defaults)`创建的也是`axios.Axios`的实例。 -直接实例化`axios.Axios`可以得到一个`纯净的实例`,不能当函数调用,传入的自定义配置就是`纯净的实例`的默认配置,而不会像`axios.create(config)`一样去合并`axios`中的默认配置。 +直接实例化`axios.Axios`可以得到一个`纯净的实例`,不能当函数调用,传入的自定义配置就是`纯净的实例`的默认配置,而不会像`axios.create(defaults)`一样去合并`axios`中的默认配置。 ```typescript axios.defaults.baseURL = 'https://www.xxx.com'; diff --git a/package.json b/package.json index f8584d8..81b979a 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ }, "husky": { "hooks": { - "pre-commit": "yarn lint && yarn prettier && yarn test:cov && git add ." + "pre-commit": "yarn prettier && yarn lint && yarn test:cov && git add ." } }, "jest": { diff --git a/rollup.config.js b/rollup.config.js index d2ea093..a83fc71 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -8,7 +8,7 @@ import fs from 'fs'; import path from 'path'; import nodeResolve from 'rollup-plugin-node-resolve'; import babel from 'rollup-plugin-babel'; -import typescript from 'rollup-plugin-typescript2'; +import typescript2 from 'rollup-plugin-typescript2'; function removeDir(name) { try { @@ -34,7 +34,7 @@ export default function() { }, plugins: [ nodeResolve({ extensions: ['.ts'] }), - typescript({ useTsconfigDeclarationDir: true }), + typescript2({ useTsconfigDeclarationDir: true }), babel({ extensions: ['.ts'] }), ], }; diff --git a/src/cancel/CancelToken.ts b/src/cancel/CancelToken.ts index d81c68b..b2db8a9 100644 --- a/src/cancel/CancelToken.ts +++ b/src/cancel/CancelToken.ts @@ -2,7 +2,7 @@ * @Author: early-autumn * @Date: 2020-04-13 20:00:08 * @LastEditors: early-autumn - * @LastEditTime: 2020-05-02 17:43:27 + * @LastEditTime: 2020-05-05 08:52:21 */ import { CancelToken, CancelAction, CancelExecutor, CancelTokenSource } from '../types'; import Cancel from './Cancel'; @@ -51,7 +51,7 @@ export default class CancelTokenClass implements CancelToken { * * 取消请求 CancelTokenSource.token */ - static source(): CancelTokenSource { + public static source(): CancelTokenSource { let cancel!: CancelAction; const token = new CancelTokenClass(function executor(action) { diff --git a/src/core/Axios.ts b/src/core/Axios.ts index a8d05e8..7832413 100644 --- a/src/core/Axios.ts +++ b/src/core/Axios.ts @@ -2,7 +2,7 @@ * @Author: early-autumn * @Date: 2020-04-13 18:00:27 * @LastEditors: early-autumn - * @LastEditTime: 2020-04-25 09:23:14 + * @LastEditTime: 2020-05-05 08:58:47 */ import { Method, Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types'; import buildURL from '../helpers/buildURL'; @@ -11,9 +11,6 @@ import InterceptorManager from './InterceptorManager'; import dispatchRequest from './dispatchRequest'; export default class AxiosClass implements Axios { - /** - * Axios 拦截器 - */ public interceptors: Interceptors = { request: new InterceptorManager(), response: new InterceptorManager(), @@ -24,22 +21,12 @@ export default class AxiosClass implements Axios { */ public constructor(public defaults: AxiosRequestConfig = {}) {} - /** - * 根据配置中的 url 和 params 生成一个 URI - * - * @param config Axios 请求配置 - */ public getUri(config: AxiosRequestConfig): string { const { url = '', params, paramsSerializer } = mergeConfig(this.defaults, config); return buildURL(url, params, paramsSerializer).replace(/^\?/, ''); } - /** - * 发送 HTTP 请求 - * - * @param config Axios 请求配置 - */ public request(config: AxiosRequestConfig): Promise> { const requestConfig = mergeConfig(this.defaults, config); @@ -51,97 +38,44 @@ export default class AxiosClass implements Axios { }, 'reverse'); // 发送请求 - let promiseResponse = promiseRequest.then(dispatchRequest) as Promise>; + let promiseResponse = promiseRequest.then(dispatchRequest); // 执行响应拦截器 this.interceptors.response.forEach(function executor({ resolved, rejected }) { promiseResponse = promiseResponse.then(resolved, rejected); }); - return promiseResponse; + return promiseResponse as Promise>; } - /** - * 发送 HTTP OPTIONS 请求 - * - * @param url 请求地址 - * @param config 额外配置 - */ public options(url: string, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutParams('options', url, undefined, config); } - /** - * 发送 HTTP GET 请求 - * - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ public get(url: string, params?: Params, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutParams('get', url, params, config); } - /** - * 发送 HTTP HEAD 请求 - * - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ public head(url: string, params?: Params, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutParams('head', url, params, config); } - /** - * 发送 HTTP POST 请求 - * - * @param url 请求地址 - * @param data 请求数据 - * @param config 额外配置 - */ public post(url: string, data?: Data, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutData('post', url, data, config); } - /** - * 发送 HTTP PUT 请求 - * - * @param url 请求地址 - * @param data 请求数据 - * @param config 额外配置 - */ public put(url: string, data?: Data, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutData('put', url, data, config); } - /** - * 发送 HTTP DELETE 请求 - * - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ public delete(url: string, params?: Params, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutParams('delete', url, params, config); } - /** - * 发送 HTTP TRACE 请求 - * - * @param url 请求地址 - * @param config 额外配置 - */ public trace(url: string, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutParams('trace', url, undefined, config); } - /** - * 发送 HTTP CONNECT 请求 - * - * @param url 请求地址 - * @param config 额外配置 - */ public connect(url: string, config?: AxiosRequestConfig): Promise> { return this._requestMethodWithoutParams('connect', url, undefined, config); } diff --git a/src/core/InterceptorManager.ts b/src/core/InterceptorManager.ts index 370d8ca..ec82b36 100644 --- a/src/core/InterceptorManager.ts +++ b/src/core/InterceptorManager.ts @@ -2,7 +2,7 @@ * @Author: early-autumn * @Date: 2020-04-15 17:50:50 * @LastEditors: early-autumn - * @LastEditTime: 2020-05-02 14:30:56 + * @LastEditTime: 2020-05-05 09:01:50 */ import { InterceptorResolved, @@ -26,12 +26,6 @@ export default class InterceptorManagerClass implements InterceptorManager */ private _interceptors: Record> = {}; - /** - * 添加拦截器 - * - * @param resolved 成功的回调函数 - * @param rejected 失败的回调函数 - */ public use(resolved: InterceptorResolved, rejected?: InterceptorRejected) { this._interceptors[++this._id] = { resolved, @@ -41,21 +35,10 @@ export default class InterceptorManagerClass implements InterceptorManager return this._id; } - /** - * 删除拦截器 - * - * @param id 拦截器 id - */ public eject(id: number): void { delete this._interceptors[id]; } - /** - * 遍历所有拦截器 - * - * @param executor 拦截器执行器 - * @param reverse 是否倒序遍历 - */ public forEach(executor: InterceptorExecutor, reverse?: 'reverse'): void { let interceptors: Interceptor[] = Object.values(this._interceptors); diff --git a/src/index.ts b/src/index.ts index 6a57b26..721d65b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ * @Author: early-autumn * @Date: 2020-04-14 23:22:52 * @LastEditors: early-autumn - * @LastEditTime: 2020-04-28 13:06:14 + * @LastEditTime: 2020-05-02 22:44:34 */ import axios from './axios'; diff --git a/src/types.ts b/src/types.ts index b35c330..d1718d2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,7 +2,7 @@ * @Author: early-autumn * @Date: 2020-04-13 15:23:53 * @LastEditors: early-autumn - * @LastEditTime: 2020-05-02 14:37:02 + * @LastEditTime: 2020-05-05 09:02:12 */ /** @@ -181,7 +181,7 @@ export declare interface AdapterRequestTask { * 适配器 */ export declare interface Adapter { - (config: AdapterRequestConfig): AdapterRequestTask | undefined; + (config: AdapterRequestConfig): AdapterRequestTask | void; } /** @@ -390,6 +390,7 @@ export declare interface InterceptorExecutor { * 拦截器管理器 */ export declare interface InterceptorManager { + [x: string]: any; /** * 添加拦截器 * @@ -682,7 +683,7 @@ export declare interface AxiosInstance extends AxiosBaseInstance { /** * 创建 Axios 实例基础拓展 * - * @param config 自定义默认配置 + * @param defaults 自定义默认配置 */ create(defaults?: AxiosRequestConfig): AxiosBaseInstance;