diff --git a/.gitignore b/.gitignore index 22286b7..0bc4377 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,8 @@ dist coverage yarn-error.log + + +es +lib +types \ No newline at end of file diff --git a/README.md b/README.md index 9f4b09c..df9690f 100644 --- a/README.md +++ b/README.md @@ -22,16 +22,16 @@ $ npm i axios-miniprogram 小程序平台专用请求库,实现了 [axios](https://github.com/axios/axios) 大部分功能,用法只存在少许差异,如果您是 [axios](https://github.com/axios/axios) 的老用户,那么不需要学习就可以直接上手使用。 -* 支持 微信小程序、支付宝小程序、百度小程序、字节跳动小程序、QQ 小程序、uniapp。 -* 支持 `Typescript`,健全的类型系统,智能的 `IDE` 提示。 -* 支持 `Promise`。 -* 支持 拦截器。 -* 支持 取消请求。 -* 支持 自定义合法状态码。 -* 支持 自定义参数序列化。 -* 支持 自定义转换数据。 -* 支持 自定义错误处理。 -* 支持 自定义平台适配器 +- 支持 微信小程序、支付宝小程序、百度小程序、字节跳动小程序、QQ 小程序、uniapp。 +- 支持 `Typescript`,健全的类型系统,智能的 `IDE` 提示。 +- 支持 `Promise`。 +- 支持 拦截器。 +- 支持 取消请求。 +- 支持 自定义合法状态码。 +- 支持 自定义参数序列化。 +- 支持 自定义转换数据。 +- 支持 自定义错误处理。 +- 支持 自定义平台适配器 ## 使用 @@ -42,25 +42,33 @@ $ npm i axios-miniprogram ```typescript // 发送 GET 请求 axios({ - method: 'get', - url: '/test', - params: { test: 1 } -}).then((response) => { - // 请求成功后做些什么 -}).catch((error) => { - // 请求失败后做些什么 -}); + url: "/user", + method: "get", + params: { + id: 1, + }, +}) + .then((response) => { + // 请求成功后做些什么 + }) + .catch((error) => { + // 请求失败后做些什么 + }); // 发送 POST 请求 axios({ - method: 'post', - url: '/test', - data: { test: 1 } -}).then((response) => { - // 请求成功后做些什么 -}).catch((error) => { - // 请求失败后做些什么 -}); + url: "/user", + method: "post", + data: { + id: 1, + }, +}) + .then((response) => { + // 请求成功后做些什么 + }) + .catch((error) => { + // 请求失败后做些什么 + }); ``` ### `axios(url, config?)` @@ -69,129 +77,150 @@ axios({ ```typescript // 默认发送 GET 请求 -axios('/test/xxx').then((response) => { - // 请求成功后做些什么 -}).catch((error) => { - // 请求失败后做些什么 -}); +axios("/user") + .then((response) => { + // 请求成功后做些什么 + }) + .catch((error) => { + // 请求失败后做些什么 + }); // 发送 POST 请求 -axios('/test/xxx', { method: 'post' }).then((response) => { - // 请求成功后做些什么 -}).catch((error) => { - // 请求失败后做些什么 -}); +axios("/user", { + method: "post", +}) + .then((response) => { + // 请求成功后做些什么 + }) + .catch((error) => { + // 请求失败后做些什么 + }); ``` 还可以使用请求方法的别名来简化请求。 -* ##### axios.request(config) -* ##### axios.options(url, config?) -* ##### axios.get(url, params?, config?) -* ##### axios.head(url, params?, config?) -* ##### axios.post(url, data?, config?) -* ##### axios.put(url, data?, config?) -* ##### axios.delete(url, params?, config?) -* ##### axios.trace(url, config?) -* ##### axios.connect(url, config?) - +- ##### axios.request(config) +- ##### axios.options(url, config?) +- ##### axios.get(url, params?, config?) +- ##### axios.head(url, params?, config?) +- ##### axios.post(url, data?, config?) +- ##### axios.put(url, data?, config?) +- ##### axios.delete(url, params?, config?) +- ##### axios.trace(url, config?) +- ##### axios.connect(url, config?) 常用例子,其他同理: ```typescript // 发送 GET 请求 -axios.get('/test'); +axios.get("/user"); // 携带参数 -axios.get('/test', { test: 1 }); +axios.get("/user", { + test: 1, +}); // 携带额外配置 -axios.get('/test', { test: 1 }, { - headers: { - 'Content-Type': 'application/json; charset=utf-8' +axios.get( + "/user", + { + id: 1, + }, + { + headers: { + "Content-Type": "application/json; charset=utf-8", + }, } -}); +); // 发送 POST 请求 -axios.post('/test'); +axios.post("/user"); // 携带数据 -axios.post('/test', { test: 1 }); +axios.post("/user", { + id: 1, +}); // 携带额外配置 -axios.post('/test', { test: 1 }, { - headers: { - 'Content-Type': 'application/json; charset=utf-8' +axios.post( + "/user", + { + id: 1, + }, + { + headers: { + "Content-Type": "application/json; charset=utf-8", + }, } -}); +); ``` ## 配置`config` 非全平台兼容的属性只会在平台支持的情况下生效。 -|参数|类型|默认值|说明|全平台兼容| -|:-|:-|:-|:-|:-| -|adapter|Function|[查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts)|自定义适配器|是| -|baseURL|String| |基础地址|是| -|url|String| |请求地址|是| -|method|String|get|请求方法| | -|params|Object| |请求参数|是| -|data|String/Object/ArrayBuffer| |请求数据|是| -|headers|Object|[查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts)|请求头|是| -|validateStatus|Function|[查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts)|自定义合法状态码|是| -|paramsSerializer|Function| |自定义参数序列化|是| -|transformRequest|Function/Array<.Function>| |自定义转换请求数据|是| -|transformResponse|Function/Array<.Function>| |自定义转换响应数据|是| -|errorHandler|Function| |自定义错误处理|是| -|cancelToken|Object| |取消令牌|是| -|timeout|Number|10000|超时时间| | -|dataType|String|json|响应数据格式|是| -|responseType|String|text|响应数据类型|是| -|enableHttp2|Boolean|false|开启 http2| | -|enableQuic|Boolean|false|开启 quic| | -|enableCache|Boolean|false|开启 cache| | -|sslVerify|Boolean|true|验证 ssl 证书| | +| 参数 | 类型 | 默认值 | 说明 | 全平台兼容 | +| :---------------- | :------------------------ | :------------------------------------------------------------------------------------ | :----------------- | :--------- | +| adapter | Function | [查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts) | 自定义适配器 | 是 | +| baseURL | String | | 基础地址 | 是 | +| url | String | | 请求地址 | 是 | +| method | String | get | 请求方法 | | +| params | Object | | 请求参数 | 是 | +| data | String/Object/ArrayBuffer | | 请求数据 | 是 | +| headers | Object | [查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts) | 请求头 | 是 | +| validateStatus | Function | [查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts) | 自定义合法状态码 | 是 | +| paramsSerializer | Function | | 自定义参数序列化 | 是 | +| transformRequest | Function/Array<.Function> | | 自定义转换请求数据 | 是 | +| transformResponse | Function/Array<.Function> | | 自定义转换响应数据 | 是 | +| errorHandler | Function | | 自定义错误处理 | 是 | +| cancelToken | Object | | 取消令牌 | 是 | +| timeout | Number | 10000 | 超时时间 | | +| dataType | String | json | 响应数据格式 | 是 | +| responseType | String | text | 响应数据类型 | 是 | +| enableHttp2 | Boolean | false | 开启 http2 | | +| enableQuic | Boolean | false | 开启 quic | | +| enableCache | Boolean | false | 开启 cache | | +| sslVerify | Boolean | true | 验证 ssl 证书 | | #### `config.method`的合法值 可以使用大写,也可以使用小写。 -|值|说明|全平台兼容| -|:-|:-|:-| -|OPTIONS| | | -|GET| |是| -|HEAD| | | -|POST| |是| -|PUT| |是| -|DELETE| |是| -|TRACE| | | -|CONNECT| | | +| 值 | 说明 | 全平台兼容 | +| :------ | :--- | :--------- | +| OPTIONS | | | +| GET | | 是 | +| HEAD | | | +| POST | | 是 | +| PUT | | 是 | +| DELETE | | 是 | +| TRACE | | | +| CONNECT | | | #### `config.dataType`的合法值 -|值|说明|全平台兼容| -|:-|:-|:-| -|json|返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse|是| -|其他|不对返回的内容进行 JSON.parse|是| +| 值 | 说明 | 全平台兼容 | +| :--- | :--------------------------------------------------------- | :--------- | +| json | 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse | 是 | +| 其他 | 不对返回的内容进行 JSON.parse | 是 | -#### `config.responseType`的合法值 +#### `config.responseType`的合法值 -|值|说明|全平台兼容| -|:-|:-|:-| -|text|响应的数据为文本|是| -|arraybuffer|响应的数据为 ArrayBuffer|是| +| 值 | 说明 | 全平台兼容 | +| :---------- | :----------------------- | :--------- | +| text | 响应的数据为文本 | 是 | +| arraybuffer | 响应的数据为 ArrayBuffer | 是 | #### 自定义合法状态码`config.validateStatus` 可以让请求按照您的要求成功或者失败。 ```typescript -axios('/test', { +axios("/user", { validateStatus: function validateStatus(status) { // 这样,状态码在 200 到 400 之间都是请求成功 - return status >= 200 && status < 400; - } + return status >= 200 && status < 400; + }, }); ``` @@ -200,10 +229,12 @@ axios('/test', { 可以使用自己的规则去序列化参数。 ```typescript -axios('/test', { +axios("/user", { paramsSerializer: function paramsSerializer(params) { - return qs.stringify(params, {arrayFormat: 'brackets'}); - } + return qs.stringify(params, { + arrayFormat: "brackets", + }); + }, }); ``` @@ -212,15 +243,19 @@ axios('/test', { 可以在请求发出之前转换请求数据,在请求成功之后转换响应数据。 ```typescript -axios('/test', { - transformRequest: [function transformRequest(data, headers) { - // 转换请求数据 - return data; - }], - transformResponse: [function transformResponse(data) { - // 转换响应数据 - return data; - }], +axios("/user", { + transformRequest: [ + function transformRequest(data, headers) { + // 转换请求数据 + return data; + }, + ], + transformResponse: [ + function transformResponse(data) { + // 转换响应数据 + return data; + }, + ], }); ``` @@ -232,24 +267,24 @@ axios('/test', { axios.defaults.errorHandler = function errorHandler(error) { // 做一些想做的事情 return Promise.reject(error); -} +}; const instance = axios.create({ errorHandler: function errorHandler(error) { // 做一些想做的事情 return Promise.reject(error); - } + }, }); ``` 也可以发送请求时通过自定义配置传入。 ```typescript -axios('/test', { +axios("/user", { errorHandler: function errorHandler(error) { // 做一些想做的事情 return Promise.reject(error); - } + }, }); ``` @@ -287,7 +322,7 @@ axios.defaults.adapter = function adapter(adapterConfig) { // 成功的回调函数 success, // 失败的回调函数 - fail + fail, } = adapterConfig; // 在 adapterConfig 中选择您需要的参数发送请求 @@ -297,9 +332,9 @@ axios.defaults.adapter = function adapter(adapterConfig) { data, header, success, - fail + fail, }); -} +}; // 如果 adapterConfig 的数据结构适用于当前平台,则可以。 axios.defaults.adapter = wx.request; @@ -310,9 +345,10 @@ axios.defaults.adapter = wx.request; ##### 全局默认配置`axios.defaults` ```typescript -axios.defaults.baseURL = 'https://www.xxx.com'; -axios.defaults.headers.common['Accept'] = 'application/json, test/plain, */*'; -axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; +axios.defaults.baseURL = "https://www.api.com"; +axios.defaults.headers.common["Accept"] = "application/json, test/plain, */*"; +axios.defaults.headers.post["Content-Type"] = + "application/x-www-form-urlencoded; charset=utf-8"; ``` ##### 自定义实例默认配置 @@ -321,24 +357,26 @@ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded ```typescript const instance = axios.create({ - baseURL: 'https://www.xxx.com', + baseURL: "https://www.api.com", headers: { common: { - 'Accept': 'application/json, test/plain, */*' + Accept: "application/json, test/plain, */*", }, post: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' - } - } + "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", + }, + }, }); ``` 也可以创建后修改。 ```typescript -instance.defaults.baseURL = 'https://www.xxx.com'; -instance.defaults.headers.common['Accept'] = 'application/json, test/plain, */*'; -instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; +instance.defaults.baseURL = "https://www.api.com"; +instance.defaults.headers.common["Accept"] = + "application/json, test/plain, */*"; +instance.defaults.headers.post["Content-Type"] = + "application/x-www-form-urlencoded; charset=utf-8"; ``` ##### 配置优先顺序 @@ -349,63 +387,64 @@ instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlenco 非全平台兼容的属性只会在平台支持的情况下生效。 -|属性|类型|说明|全平台兼容| -|:-|:-|:-|:-| -|status|Number|状态码|是| -|statusText|String|状态文本|是| -|data|String/Object/ArrayBuffer|开发者服务器返回的数据|是| -|headers|Object|响应头|是| -|config|Object|Axios 请求配置|是| -|cookies|Array<.String>|开发者服务器返回的 cookies,格式为字符串数组| | -|profile|Object|网络请求过程中一些关键时间点的耗时信息| | - +| 属性 | 类型 | 说明 | 全平台兼容 | +| :--------- | :------------------------ | :------------------------------------------- | :--------- | +| status | Number | 状态码 | 是 | +| statusText | String | 状态文本 | 是 | +| data | String/Object/ArrayBuffer | 开发者服务器返回的数据 | 是 | +| headers | Object | 响应头 | 是 | +| config | Object | Axios 请求配置 | 是 | +| cookies | Array<.String> | 开发者服务器返回的 cookies,格式为字符串数组 | | +| profile | Object | 网络请求过程中一些关键时间点的耗时信息 | | ## API ### `axios.interceptors` -可以先拦截请求或响应,然后再由then或catch处理。 +可以先拦截请求或响应,然后再由 then 或 catch 处理。 ```typescript // 添加请求拦截器 -axios.interceptors.request.use(function (config) { - // 在发送请求之前做些什么 - console.log('request'); - return config; -}, function (error) { - //处理请求错误 - return Promise.reject(error); -}); +axios.interceptors.request.use( + function(config) { + // 在发送请求之前做些什么 + return config; + }, + function(error) { + //处理请求错误 + return Promise.reject(error); + } +); // 添加响应拦截器 -axios.interceptors.response.use(function (response) { - // 请求成功后做些什么 - console.log('response'); - return response; -}, function (error) { - // 处理响应错误 - return Promise.reject(error); -}); - -axios('/test').then(function (response){ - console.log('ok'); -}); - -// log 'request' 'response' 'ok' +axios.interceptors.response.use( + function(response) { + // 请求成功后做些什么 + return response; + }, + function(error) { + // 处理响应错误 + return Promise.reject(error); + } +); ``` 如果以后需要删除拦截器,则可以。 ```typescript -const myInterceptor = axios.interceptors.request.use(function () {/*...*/}); +const myInterceptor = axios.interceptors.request.use(function() { + // 在发送请求之前做些什么 +}); axios.interceptors.request.eject(myInterceptor); ``` 还可以将拦截器添加到`axios`的`自定义实例`中。 ```typescript -const myInterceptor = axios.interceptors.request.use(function () {/*...*/}); -axios.interceptors.request.eject(myInterceptor); +const myInterceptor = instance.interceptors.request.use(function() { + // 在发送请求之前做些什么 +}); +instance.interceptors.request.eject(myInterceptor); ``` ### `axios.CancelToken` @@ -415,13 +454,13 @@ axios.interceptors.request.eject(myInterceptor); ```typescript let cancel; -axios('/test', { - cancelToken: new axios.CancelToken(function (c){ +axios("/api", { + cancelToken: new axios.CancelToken(function(c) { cancel = c; - }) + }), }); -cancel('取消请求'); +cancel("取消请求"); ``` 还可以使用`CancelToken.source`工厂方法创建`CancelToken`。 @@ -429,11 +468,11 @@ cancel('取消请求'); ```typescript const source = axios.CancelToken.source(); -axios('/test', { - cancelToken: source.token +axios("/api", { + cancelToken: source.token, }); -source.cancel('取消请求'); +source.cancel("取消请求"); ``` ### `axios.isCancel` @@ -441,8 +480,8 @@ source.cancel('取消请求'); 可以判断当前错误是否来自取消请求 ```typescript -axios('/test').catch((error) => { - if(axios.isCancel(error)){ +axios("/user").catch((error) => { + if (axios.isCancel(error)) { // 请求被取消了 } }); @@ -452,12 +491,14 @@ axios('/test').catch((error) => { 根据配置中的`url`和`params`生成一个`URI`。 -```typescript -// uri === '/test?id=1' +```typescript const uri = axios.getUri({ - url: '/test', - params: { id: 1 } + url: "/user", + params: { + id: 1, + }, }); +// '/user?id=1' ``` ### `axios.create(defaults)` @@ -467,20 +508,16 @@ const uri = axios.getUri({ `自定义实例`拥有和`axios`相同的调用方式和请求方法的别名。 ```typescript -axios.defaults.baseURL = 'https://www.xxx.com'; +axios.defaults.baseURL = "https://www.api.com"; const instance = axios.create({ params: { - id: 1 - } + id: 1, + }, }); -// 最终请求的 URL 是这样的 => https://www.xxx.com/test?id=1 -// https://www.xxx.com 来自 axios.defaults.baseURL -// /test 来自传入的 '/test' -// id=1 来自 instance.defaults.params -instance('/test'); -instance.get('/test'); +instance("/user"); +// 'https://www.api.com/user?id=1' ``` ### `axios.Axios` @@ -490,22 +527,23 @@ instance.get('/test'); 直接实例化`axios.Axios`可以得到一个`原始实例`,不能当函数调用,传入的自定义配置就是`原始实例`的默认配置,而不会像`axios.create(defaults)`一样去合并`axios`中的默认配置。 ```typescript -axios.defaults.baseURL = 'https://www.xxx.com'; - -const instance = new axios.Axios({ - params: { value: 123 } +const instance = new axios.Axios({ + beseURL: "https://www.api.com", + params: { + id: 1, + }, }); -// 最终请求的 URL 是这样的 => /test?value=123 -// /test 来自传入的 '/test' -// value=123 来自 instance.defaults.params -instance.get('/test'); +instance.get("/user"); +// 'https://www.api.com/user?id=1' ``` ## 执行流程 ```typescript -axios('/test').then().catch(); +axios("/user") + .then() + .catch(); // 请求成功 // axios => axios.interceptors.request => config.transformRequest => config.paramsSerializer => config.adapter => config.validateStatus => config.transformResponse => axios.interceptors.response => then diff --git a/es/index.js b/es/index.js deleted file mode 100644 index 54656db..0000000 --- a/es/index.js +++ /dev/null @@ -1,1242 +0,0 @@ -function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } -} - -function _defineProperties(target, props) { - for (var i = 0; i < props.length; i++) { - var descriptor = props[i]; - descriptor.enumerable = descriptor.enumerable || false; - descriptor.configurable = true; - if ("value" in descriptor) descriptor.writable = true; - Object.defineProperty(target, descriptor.key, descriptor); - } -} - -function _createClass(Constructor, protoProps, staticProps) { - if (protoProps) _defineProperties(Constructor.prototype, protoProps); - if (staticProps) _defineProperties(Constructor, staticProps); - return Constructor; -} - -function _defineProperty(obj, key, value) { - if (key in obj) { - Object.defineProperty(obj, key, { - value: value, - enumerable: true, - configurable: true, - writable: true - }); - } else { - obj[key] = value; - } - - return obj; -} - -function ownKeys(object, enumerableOnly) { - var keys = Object.keys(object); - - if (Object.getOwnPropertySymbols) { - var symbols = Object.getOwnPropertySymbols(object); - if (enumerableOnly) symbols = symbols.filter(function (sym) { - return Object.getOwnPropertyDescriptor(object, sym).enumerable; - }); - keys.push.apply(keys, symbols); - } - - return keys; -} - -function _objectSpread2(target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i] != null ? arguments[i] : {}; - - if (i % 2) { - ownKeys(Object(source), true).forEach(function (key) { - _defineProperty(target, key, source[key]); - }); - } else if (Object.getOwnPropertyDescriptors) { - Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); - } else { - ownKeys(Object(source)).forEach(function (key) { - Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); - }); - } - } - - return target; -} - -function _inherits(subClass, superClass) { - if (typeof superClass !== "function" && superClass !== null) { - throw new TypeError("Super expression must either be null or a function"); - } - - subClass.prototype = Object.create(superClass && superClass.prototype, { - constructor: { - value: subClass, - writable: true, - configurable: true - } - }); - if (superClass) _setPrototypeOf(subClass, superClass); -} - -function _getPrototypeOf(o) { - _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { - return o.__proto__ || Object.getPrototypeOf(o); - }; - return _getPrototypeOf(o); -} - -function _setPrototypeOf(o, p) { - _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { - o.__proto__ = p; - return o; - }; - - return _setPrototypeOf(o, p); -} - -function _isNativeReflectConstruct() { - if (typeof Reflect === "undefined" || !Reflect.construct) return false; - if (Reflect.construct.sham) return false; - if (typeof Proxy === "function") return true; - - try { - Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); - return true; - } catch (e) { - return false; - } -} - -function _construct(Parent, args, Class) { - if (_isNativeReflectConstruct()) { - _construct = Reflect.construct; - } else { - _construct = function _construct(Parent, args, Class) { - var a = [null]; - a.push.apply(a, args); - var Constructor = Function.bind.apply(Parent, a); - var instance = new Constructor(); - if (Class) _setPrototypeOf(instance, Class.prototype); - return instance; - }; - } - - return _construct.apply(null, arguments); -} - -function _isNativeFunction(fn) { - return Function.toString.call(fn).indexOf("[native code]") !== -1; -} - -function _wrapNativeSuper(Class) { - var _cache = typeof Map === "function" ? new Map() : undefined; - - _wrapNativeSuper = function _wrapNativeSuper(Class) { - if (Class === null || !_isNativeFunction(Class)) return Class; - - if (typeof Class !== "function") { - throw new TypeError("Super expression must either be null or a function"); - } - - if (typeof _cache !== "undefined") { - if (_cache.has(Class)) return _cache.get(Class); - - _cache.set(Class, Wrapper); - } - - function Wrapper() { - return _construct(Class, arguments, _getPrototypeOf(this).constructor); - } - - Wrapper.prototype = Object.create(Class.prototype, { - constructor: { - value: Wrapper, - enumerable: false, - writable: true, - configurable: true - } - }); - return _setPrototypeOf(Wrapper, Class); - }; - - return _wrapNativeSuper(Class); -} - -function _assertThisInitialized(self) { - if (self === void 0) { - throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); - } - - return self; -} - -function _possibleConstructorReturn(self, call) { - if (call && (typeof call === "object" || typeof call === "function")) { - return call; - } - - return _assertThisInitialized(self); -} - -function _createSuper(Derived) { - var hasNativeReflectConstruct = _isNativeReflectConstruct(); - - return function _createSuperInternal() { - var Super = _getPrototypeOf(Derived), - result; - - if (hasNativeReflectConstruct) { - var NewTarget = _getPrototypeOf(this).constructor; - - result = Reflect.construct(Super, arguments, NewTarget); - } else { - result = Super.apply(this, arguments); - } - - return _possibleConstructorReturn(this, result); - }; -} - -function _slicedToArray(arr, i) { - return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); -} - -function _arrayWithHoles(arr) { - if (Array.isArray(arr)) return arr; -} - -function _iterableToArrayLimit(arr, i) { - if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; - var _arr = []; - var _n = true; - var _d = false; - var _e = undefined; - - try { - for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { - _arr.push(_s.value); - - if (i && _arr.length === i) break; - } - } catch (err) { - _d = true; - _e = err; - } finally { - try { - if (!_n && _i["return"] != null) _i["return"](); - } finally { - if (_d) throw _e; - } - } - - return _arr; -} - -function _unsupportedIterableToArray(o, minLen) { - if (!o) return; - if (typeof o === "string") return _arrayLikeToArray(o, minLen); - var n = Object.prototype.toString.call(o).slice(8, -1); - if (n === "Object" && o.constructor) n = o.constructor.name; - if (n === "Map" || n === "Set") return Array.from(o); - if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); -} - -function _arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) len = arr.length; - - for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; - - return arr2; -} - -function _nonIterableRest() { - throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); -} - -var _toString = Object.prototype.toString; -/** - * 对字符串进行编码转换 - * - * @param str 字符串 - */ - -function encode(str) { - return encodeURIComponent(str).replace(/%40/gi, '@').replace(/%3A/gi, ':').replace(/%24/g, '$').replace(/%2C/gi, ',').replace(/%20/g, '+').replace(/%5B/gi, '[').replace(/%5D/gi, ']'); -} -/** - * 是不是一个日期对象 - * - * @param date 判断目标 - */ - -function isDate(date) { - return _toString.call(date) === '[object Date]'; -} -/** - * 是不是一个普通对象 - * - * @param obj 判断目标 - */ - -function isPlainObject(obj) { - return _toString.call(obj) === '[object Object]'; -} -/** - * 深度合并多个对象 - * - * @param objs n 个对象 - */ - -function deepMerge() { - var result = {}; - - function assignValue(key, val) { - // 如果当前结果和当前值都为普通对象 - // 递归进行深度合并 - if (isPlainObject(result[key]) && isPlainObject(val)) { - result[key] = deepMerge(result[key], val); - } // 如果只有当前值为普通对象 - // 直接深拷贝当前值 - else if (isPlainObject(val)) { - result[key] = deepMerge({}, val); - } // 如果都不是普通对象 - // 直接赋值 - else { - result[key] = val; - } - } - - for (var _len = arguments.length, objs = new Array(_len), _key = 0; _key < _len; _key++) { - objs[_key] = arguments[_key]; - } - - objs.forEach(function assignObj(obj) { - Object.entries(obj).forEach(function assignKey(_ref) { - var _ref2 = _slicedToArray(_ref, 2), - key = _ref2[0], - value = _ref2[1]; - - assignValue(key, value); - }); - }); - return result; -} -/** - * 从对象中提取一部分属性 - * - * @param obj 源对象 - * @param keys 需要提取的 key - */ - -function pick(obj) { - var _pick = {}; - - for (var _len2 = arguments.length, keys = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { - keys[_key2 - 1] = arguments[_key2]; - } - - keys.forEach(function pickKey(key) { - _pick[key] = obj[key]; - }); - return _pick; -} -/** - * 从对象中剔除一部分属性 - * - * @param obj 源对象 - * @param keys 需要剔除的 key - */ - -function omit(obj) { - var _omit = _objectSpread2({}, obj); - - for (var _len3 = arguments.length, keys = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { - keys[_key3 - 1] = arguments[_key3]; - } - - keys.forEach(function omitKey(key) { - delete _omit[key]; - }); - return _omit; -} - -/** - * 通过请求地址和序列化参数生成新的请求地址 - * - * @param url 请求地址 - * @param serializedParams 序列化参数 - */ - -function generateURL(url, serializedParams) { - // 移除 hash - var hashIndex = url.indexOf('#'); - - if (hashIndex !== -1) { - url = url.slice(0, hashIndex); - } - - if (serializedParams === '') { - return url; - } // 拼接前缀 - - - var prefix = url.indexOf('?') === -1 ? '?' : '&'; - serializedParams = "".concat(prefix).concat(serializedParams); - return "".concat(url).concat(serializedParams); -} -/** - * 默认参数序列化 - * - * @param params 请求参数 - */ - - -function paramsSerialization(params) { - var parts = []; - Object.entries(params).forEach(function encodeKeyValue(_ref) { - var _ref2 = _slicedToArray(_ref, 2), - key = _ref2[0], - value = _ref2[1]; - - if (value === null || value === void 0 || value !== value) { - return; - } // 如果值是一个数组, 则特殊处理 key - - - if (Array.isArray(value)) { - key += '[]'; - } // 转成数组统一处理 - - - var values = [].concat(value); - values.forEach(function (val) { - if (isPlainObject(val)) { - val = JSON.stringify(val); - } else if (isDate(val)) { - val = val.toISOString(); - } - - parts.push("".concat(encode(key), "=").concat(encode(val))); - }); - }); - return parts.join('&'); -} -/** - * 处理 URL 参数 - * - * @param url 请求地址 - * @param params 请求参数 - * @param paramsSerializer 自定义参数序列化 - */ - - -function buildURL(url) { - var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - var paramsSerializer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : paramsSerialization; - return generateURL(url, paramsSerializer(params)); -} - -/** - * 只取 config2 中的配置 - * - * @param keys - * @param config - * @param config2 - */ - -function onlyFromConfig2(keys, config, config2) { - keys.forEach(function (key) { - if (config2[key] !== void 0) { - config[key] = config2[key]; - } - }); -} -/** - * 优先取 config2 中的配置, config2 中没有就取 config1 - * - * @param keys - * @param config - * @param config1 - * @param config2 - */ - - -function priorityFromConfig2(keys, config, config1, config2) { - keys.forEach(function (key) { - if (config2[key] !== void 0) { - config[key] = config2[key]; - } else if (config1[key] !== void 0) { - config[key] = config1[key]; - } - }); -} -/** - * 深度合并配置 - * - * @param keys - * @param config - * @param config1 - * @param config2 - */ - - -function deepMergeConfig(keys, config, config1, config2) { - keys.forEach(function (key) { - if (isPlainObject(config2[key])) { - var _config1$key; - - config[key] = deepMerge((_config1$key = config1[key]) !== null && _config1$key !== void 0 ? _config1$key : {}, config2[key]); - } else if (isPlainObject(config1[key])) { - config[key] = deepMerge(config1[key]); - } - }); -} -/** - * 合并 Axios 请求配置 - * - * @param config1 Axios 请求配置1 - * @param config2 Axios 请求配置2 - */ - - -function mergeConfig() { - var config1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - var config2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - var config = {}; - var onlyFromConfig2Keys = ['url', 'data']; - var priorityFromConfig2Keys = ['adapter', 'baseURL', 'method', 'validateStatus', 'paramsSerializer', 'transformRequest', 'transformResponse', 'errorHandler', 'cancelToken', 'dataType', 'responseType', 'timeout', 'enableHttp2', 'enableQuic', 'enableCache', 'sslVerify']; - var deepMergeConfigKeys = ['headers', 'params']; - onlyFromConfig2(onlyFromConfig2Keys, config, config2); - priorityFromConfig2(priorityFromConfig2Keys, config, config1, config2); - deepMergeConfig(deepMergeConfigKeys, config, config1, config2); - return config; -} - -/** - * 拦截器管理器 - */ -var InterceptorManagerClass = /*#__PURE__*/function () { - function InterceptorManagerClass() { - _classCallCheck(this, InterceptorManagerClass); - - /** - * 生成拦截器 id - */ - this._id = 0; - /** - * 拦截器集合 - */ - - this._interceptors = {}; - } - - _createClass(InterceptorManagerClass, [{ - key: "use", - value: function use(resolved, rejected) { - this._interceptors[++this._id] = { - resolved: resolved, - rejected: rejected - }; - return this._id; - } - }, { - key: "eject", - value: function eject(id) { - delete this._interceptors[id]; - } - }, { - key: "forEach", - value: function forEach(executor, reverse) { - var interceptors = Object.values(this._interceptors); - - if (reverse === 'reverse') { - interceptors = interceptors.reverse(); - } - - interceptors.forEach(executor); - } - }]); - - return InterceptorManagerClass; -}(); - -var CancelClass = /*#__PURE__*/function () { - /** - * @param message 取消信息 - */ - function CancelClass(message) { - _classCallCheck(this, CancelClass); - - this.message = message; - } - - _createClass(CancelClass, [{ - key: "toString", - value: function toString() { - var message = this.message ? ": ".concat(this.message) : ''; - return "Cancel".concat(message); - } - }]); - - return CancelClass; -}(); - -/** - * 是否是取消请求实例 - * - * @param value 判断的值 - */ - -function isCancel(value) { - return value instanceof CancelClass; -} - -/** - * 请求方法转全小写 - * - * @param config Axios 请求配置 - */ -function methodToLowercase() { - var method = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'get'; - return method.toLowerCase(); -} -/** - * 请求方法转全大写 - * - * @param config Axios 请求配置 - */ - -function methodToUppercase() { - var method = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'GET'; - return method.toUpperCase(); -} - -/** - * 拉平请求头 - * - * @param config Axios 请求配置 - */ - -function flattenHeaders(config) { - var _headers$common, _headers$method; - - var headers = config.headers; - - if (headers === void 0) { - return {}; - } - - var method = methodToLowercase(config.method); - return _objectSpread2(_objectSpread2(_objectSpread2({}, (_headers$common = headers.common) !== null && _headers$common !== void 0 ? _headers$common : {}), (_headers$method = headers[method]) !== null && _headers$method !== void 0 ? _headers$method : {}), omit(headers, 'common', 'options', 'get', 'head', 'post', 'put', 'delete', 'trace', 'connect')); -} - -/** - * 转换数据 - * - * @param data 请求数据/响应数据 - * @param headers 请求头/响应头 - * @param transforms 请求数据转换函数/响应数据转换函数 - */ -function transformData(data, headers, transforms) { - if (transforms === void 0) { - return data; - } - - if (!Array.isArray(transforms)) { - transforms = [transforms]; - } - - transforms.forEach(function (transform) { - data = transform(data, headers); - }); - return data; -} - -/** - * 检查是否是一个绝对 URL - * - * xxx:// 或者 "//" 开头, 视为绝对地址 - * - * @param url 需要检查的 URL - */ -function isAbsoluteURL(url) { - return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url); -} - -/** - * 拼接 baseURL 和 url 获得完整的 URL - * - * combineURL('1/2///','////3/4') => '1/2/3/4' - */ -function combineURL(baseURL, url) { - return "".concat(baseURL.replace(/\/*$/, ''), "/").concat(url.replace(/^\/*/, '')); -} - -/** - * 根据配置中的 baseURL 和 url 和 params 生成完整 URL - * - * @param config Axios 请求配置 - */ - -function transformURL(config) { - var _config$baseURL = config.baseURL, - baseURL = _config$baseURL === void 0 ? '' : _config$baseURL, - _config$url = config.url, - url = _config$url === void 0 ? '' : _config$url; - var fullURL = isAbsoluteURL(url) ? url : combineURL(baseURL, url); - return buildURL(fullURL, config.params, config.paramsSerializer); -} -/** - * Axios 请求配置转换成各大平台通用请求配置 - * - * 抹平差异 - * - * @param config Axios 请求配置 - */ - - -function transformRequest(config) { - return _objectSpread2({ - url: transformURL(config), - method: methodToUppercase(config.method), - header: config.headers - }, pick(config, 'data', 'headers', 'dataType', 'responseType', 'timeout', 'enableHttp2', 'enableQuic', 'enableCache', 'sslVerify')); -} - -/** - * 各大平台通用响应体转成 Axios 响应体 - * - * 抹平差异 - * - * @param response 通用响应体 - * @param config Axios 请求配置 - */ - -function transformResponse(response, config) { - var _ref, _response$statusCode, _ref2, _response$header; - - var status = (_ref = (_response$statusCode = response.statusCode) !== null && _response$statusCode !== void 0 ? _response$statusCode : response.status) !== null && _ref !== void 0 ? _ref : 400; - var headers = (_ref2 = (_response$header = response.header) !== null && _response$header !== void 0 ? _response$header : response.headers) !== null && _ref2 !== void 0 ? _ref2 : {}; - var statusText = status === 200 ? 'OK' : status === 400 ? 'Bad Adapter' : ''; - return _objectSpread2({ - status: status, - statusText: statusText, - headers: headers, - config: config - }, pick(response, 'data', 'cookies', 'profile')); -} - -/** - * AxiosError 继承自 Error - */ -var AxiosErrorClass = /*#__PURE__*/function (_Error) { - _inherits(AxiosErrorClass, _Error); - - var _super = _createSuper(AxiosErrorClass); - - /** - * @param message 错误信息 - * @param config Axios 请求配置 - * @param request 通用请求配置 - * @param response Axios 响应体 - */ - function AxiosErrorClass(message, config, request, response) { - var _this; - - _classCallCheck(this, AxiosErrorClass); - - _this = _super.call(this, message); - _this.config = config; - _this.request = request; - _this.response = response; - _this.isAxiosError = true; // 修复继承系统自带类 prototype 设置失败的问题 - - Object.setPrototypeOf(_assertThisInitialized(_this), AxiosErrorClass.prototype); - return _this; - } - - return AxiosErrorClass; -}( /*#__PURE__*/_wrapNativeSuper(Error)); -/** - * 创建 AxiosError 的工厂方法 - * - * 返回一个新的 AxiosError 对象 - * - * @param message 错误信息 - * @param config Axios 请求配置 - * @param request 通用请求配置 - * @param response Axios 响应体 - */ - - -function createError(message, config, request, response) { - return new AxiosErrorClass(message, config, request, response); -} - -/** - * 请求函数 - * - * @param config Axios 请求配置 - */ - -function request(config) { - return new Promise(function dispatchAdapter(resolve, reject) { - var adapter = config.adapter, - cancelToken = config.cancelToken; - var requestConfig = transformRequest(config); - /** - * 捕获错误 - * - * @param message 错误信息 - * @param response Axios 响应体 - */ - - function catchError(message, response) { - if (typeof message !== 'string') { - message = '配置不正确或者网络异常'; - } - - reject(createError(message, config, requestConfig, response)); - } - - if (adapter === void 0) { - catchError('平台适配失败,您需要参阅文档使用自定义适配器手动适配当前平台'); - return; - } - /** - * 效验状态码 - * - * @param res 请求结果 - */ - - - function handleResponse(res) { - var response = transformResponse(res, config); - - if (config.validateStatus === void 0 || config.validateStatus(response.status)) { - resolve(response); - } else { - catchError("\u8BF7\u6C42\u5931\u8D25\uFF0C\u72B6\u6001\u7801\u4E3A ".concat(response.status), response); - } - } // 使用适配器发送请求 - - - var task = adapter(_objectSpread2(_objectSpread2({}, requestConfig), {}, { - success: handleResponse, - fail: catchError - })); // 如果存在取消令牌 - // 则调用取消令牌里的 listener 监听用户的取消操作 - - if (cancelToken !== void 0) { - cancelToken.listener.then(function onCanceled(reason) { - if (task !== void 0) { - task.abort(); - } - - reject(reason); - }); - } - }); -} - -/** - * 如果已经取消, 则抛出取消对象 - * - * @param config Axios 请求配置 - */ - -function throwIfCancellationRequested(config) { - if (config.cancelToken) { - config.cancelToken.throwIfRequested(); - } -} -/** - * 发送请求 - * - * @param config Axios 请求配置 - */ - - -function dispatchRequest(config) { - var _config$data; - - throwIfCancellationRequested(config); - config.headers = flattenHeaders(config); - config.data = transformData((_config$data = config.data) !== null && _config$data !== void 0 ? _config$data : {}, config.headers, config.transformRequest); - - function onResolved(response) { - throwIfCancellationRequested(config); - response.data = transformData(response.data, response.headers, config.transformResponse); - return response; - } - - function onRejected(reason) { - var _config$errorHandler, _config$errorHandler2; - - if (!isCancel(reason)) { - throwIfCancellationRequested(config); - - if (reason.response !== void 0) { - reason.response.data = transformData(reason.response.data, reason.response.headers, config.transformResponse); - } - } - - return (_config$errorHandler = (_config$errorHandler2 = config.errorHandler) === null || _config$errorHandler2 === void 0 ? void 0 : _config$errorHandler2.call(config, reason)) !== null && _config$errorHandler !== void 0 ? _config$errorHandler : Promise.reject(reason); - } - - return request(config).then(onResolved, onRejected); -} - -var AxiosClass = /*#__PURE__*/function () { - /** - * @param defaults 自定义默认配置 - */ - function AxiosClass() { - var defaults = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - - _classCallCheck(this, AxiosClass); - - this.defaults = defaults; - this.interceptors = { - request: new InterceptorManagerClass(), - response: new InterceptorManagerClass() - }; - } - - _createClass(AxiosClass, [{ - key: "getUri", - value: function getUri(config) { - var _mergeConfig = mergeConfig(this.defaults, config), - _mergeConfig$url = _mergeConfig.url, - url = _mergeConfig$url === void 0 ? '' : _mergeConfig$url, - params = _mergeConfig.params, - paramsSerializer = _mergeConfig.paramsSerializer; - - return buildURL(url, params, paramsSerializer).replace(/^\?/, ''); - } - }, { - key: "request", - value: function request(config) { - var requestConfig = mergeConfig(this.defaults, config); - var promiseRequest = Promise.resolve(requestConfig); // 执行请求拦截器 - - this.interceptors.request.forEach(function executor(_ref) { - var resolved = _ref.resolved, - rejected = _ref.rejected; - promiseRequest = promiseRequest.then(resolved, rejected); - }, 'reverse'); // 发送请求 - - var promiseResponse = promiseRequest.then(dispatchRequest); // 执行响应拦截器 - - this.interceptors.response.forEach(function executor(_ref2) { - var resolved = _ref2.resolved, - rejected = _ref2.rejected; - promiseResponse = promiseResponse.then(resolved, rejected); - }); - return promiseResponse; - } - }, { - key: "options", - value: function options(url, config) { - return this._requestMethodWithoutParams('options', url, void 0, config); - } - }, { - key: "get", - value: function get(url, params, config) { - return this._requestMethodWithoutParams('get', url, params, config); - } - }, { - key: "head", - value: function head(url, params, config) { - return this._requestMethodWithoutParams('head', url, params, config); - } - }, { - key: "post", - value: function post(url, data, config) { - return this._requestMethodWithoutData('post', url, data, config); - } - }, { - key: "put", - value: function put(url, data, config) { - return this._requestMethodWithoutData('put', url, data, config); - } - }, { - key: "delete", - value: function _delete(url, params, config) { - return this._requestMethodWithoutParams('delete', url, params, config); - } - }, { - key: "trace", - value: function trace(url, config) { - return this._requestMethodWithoutParams('trace', url, void 0, config); - } - }, { - key: "connect", - value: function connect(url, config) { - return this._requestMethodWithoutParams('connect', url, void 0, config); - } - /** - * 合并配置后发送 HTTP 请求 - * - * @param method 请求方法 - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ - - }, { - key: "_requestMethodWithoutParams", - value: function _requestMethodWithoutParams(method, url, params) { - var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; - return this.request(_objectSpread2(_objectSpread2({}, config), {}, { - method: method, - url: url, - params: params - })); - } - /** - * 合并配置后发送 HTTP 请求 - * - * @param method 请求方法 - * @param url 请求地址 - * @param data 请求数据 - * @param config 额外配置 - */ - - }, { - key: "_requestMethodWithoutData", - value: function _requestMethodWithoutData(method, url, data) { - var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; - return this.request(_objectSpread2(_objectSpread2({}, config), {}, { - method: method, - url: url, - data: data - })); - } - }]); - - return AxiosClass; -}(); - -var CancelTokenClass = /*#__PURE__*/function () { - function CancelTokenClass(executor) { - var _this = this; - - _classCallCheck(this, CancelTokenClass); - - var action; - this.listener = new Promise(function (resolve) { - action = function action(message) { - // 防止重复取消 - if (_this._reason) { - return; - } - - _this._reason = new CancelClass(message); - resolve(_this._reason); - }; - }); - executor(action); - } - - _createClass(CancelTokenClass, [{ - key: "throwIfRequested", - value: function throwIfRequested() { - if (this._reason) { - throw this._reason; - } - } - /** - * 返回一个 CancelTokenSource - * - * CancelTokenSource.token 是一个 CancelToken 对象 - * - * CancelTokenSource.cancel 是一个 CancelAction 函数 - * - * 调用 CancelTokenSource.cancel('这里可以填写您的错误信息') - * - * 取消请求 CancelTokenSource.token - */ - - }], [{ - key: "source", - value: function source() { - var cancel; - var token = new CancelTokenClass(function executor(action) { - cancel = action; - }); - return { - token: token, - cancel: cancel - }; - } - }]); - - return CancelTokenClass; -}(); - -/** - * 自适应当前平台 - */ -function adaptive() { - var stack = [function () { - return uni.request; - }, function () { - return wx.request; - }, function () { - return my.request; - }, function () { - return swan.request; - }, function () { - return tt.request; - }, function () { - return qq.request; - }]; - var adapter; - - while (stack.length !== 0 && adapter === void 0) { - try { - adapter = stack.shift()(); - } catch (err) {} - } - - return adapter; -} - -var defaults = { - /** - * 适配器 - */ - adapter: adaptive(), - - /** - * 请求方法 - */ - method: 'get', - - /** - * 请求头 - */ - headers: { - common: { - Accept: 'application/json, test/plain, */*' - }, - options: {}, - get: {}, - head: {}, - post: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' - }, - put: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' - }, - "delete": {}, - trace: {}, - connect: {} - }, - - /** - * 状态码效验 - * - * @param status 状态码 - */ - validateStatus: function validateStatus(status) { - return status >= 200 && status < 300; - }, - - /** - * 超时时间 - */ - timeout: 10000, - - /** - * 响应数据格式 - */ - dataType: 'json', - - /** - * 响应数据类型 - */ - responseType: 'text', - - /** - * 开启 http2 - */ - enableHttp2: false, - - /** - * 开启 quic - */ - enableQuic: false, - - /** - * 开启 cache - */ - enableCache: false, - - /** - * 验证 ssl 证书 - */ - sslVerify: true -}; - -/** - * 创建一个新的 Axios 实例 - * - * 返回一个 Axios 实例增强 - */ - -function createInstance(defaults) { - var instance = new AxiosClass(defaults); - /** - * 支持重载的 axios 函数 - */ - - function axios(url) { - var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - - // 调用方式一处理请求配置 - if (typeof url !== 'string') { - config = url; - } // 调用方式二处理请求配置 - else { - config = _objectSpread2(_objectSpread2({}, config), {}, { - url: url - }); - } - - return instance.request(config); - } // instance 的属性合并到 axios 函数中 - - - Object.assign(axios, instance); // instance 的方法合并到 axios 函数中 - - Object.setPrototypeOf(axios, Object.getPrototypeOf(instance)); - return axios; -} -/** - * Axios 实例拓展 - */ - - -var axios = createInstance(defaults); // 添加 create 工厂方法 - -axios.create = function create() { - var defaults = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - return createInstance(mergeConfig(axios.defaults, defaults)); -}; // 添加 Axios 类 - - -axios.Axios = AxiosClass; // 添加 CancelToken 类 - -axios.CancelToken = CancelTokenClass; // 添加 检查错误是否来自取消请求 方法 - -axios.isCancel = isCancel; - -export default axios; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 2a141bb..0000000 --- a/lib/index.js +++ /dev/null @@ -1,1244 +0,0 @@ -'use strict'; - -function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } -} - -function _defineProperties(target, props) { - for (var i = 0; i < props.length; i++) { - var descriptor = props[i]; - descriptor.enumerable = descriptor.enumerable || false; - descriptor.configurable = true; - if ("value" in descriptor) descriptor.writable = true; - Object.defineProperty(target, descriptor.key, descriptor); - } -} - -function _createClass(Constructor, protoProps, staticProps) { - if (protoProps) _defineProperties(Constructor.prototype, protoProps); - if (staticProps) _defineProperties(Constructor, staticProps); - return Constructor; -} - -function _defineProperty(obj, key, value) { - if (key in obj) { - Object.defineProperty(obj, key, { - value: value, - enumerable: true, - configurable: true, - writable: true - }); - } else { - obj[key] = value; - } - - return obj; -} - -function ownKeys(object, enumerableOnly) { - var keys = Object.keys(object); - - if (Object.getOwnPropertySymbols) { - var symbols = Object.getOwnPropertySymbols(object); - if (enumerableOnly) symbols = symbols.filter(function (sym) { - return Object.getOwnPropertyDescriptor(object, sym).enumerable; - }); - keys.push.apply(keys, symbols); - } - - return keys; -} - -function _objectSpread2(target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i] != null ? arguments[i] : {}; - - if (i % 2) { - ownKeys(Object(source), true).forEach(function (key) { - _defineProperty(target, key, source[key]); - }); - } else if (Object.getOwnPropertyDescriptors) { - Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); - } else { - ownKeys(Object(source)).forEach(function (key) { - Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); - }); - } - } - - return target; -} - -function _inherits(subClass, superClass) { - if (typeof superClass !== "function" && superClass !== null) { - throw new TypeError("Super expression must either be null or a function"); - } - - subClass.prototype = Object.create(superClass && superClass.prototype, { - constructor: { - value: subClass, - writable: true, - configurable: true - } - }); - if (superClass) _setPrototypeOf(subClass, superClass); -} - -function _getPrototypeOf(o) { - _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { - return o.__proto__ || Object.getPrototypeOf(o); - }; - return _getPrototypeOf(o); -} - -function _setPrototypeOf(o, p) { - _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { - o.__proto__ = p; - return o; - }; - - return _setPrototypeOf(o, p); -} - -function _isNativeReflectConstruct() { - if (typeof Reflect === "undefined" || !Reflect.construct) return false; - if (Reflect.construct.sham) return false; - if (typeof Proxy === "function") return true; - - try { - Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); - return true; - } catch (e) { - return false; - } -} - -function _construct(Parent, args, Class) { - if (_isNativeReflectConstruct()) { - _construct = Reflect.construct; - } else { - _construct = function _construct(Parent, args, Class) { - var a = [null]; - a.push.apply(a, args); - var Constructor = Function.bind.apply(Parent, a); - var instance = new Constructor(); - if (Class) _setPrototypeOf(instance, Class.prototype); - return instance; - }; - } - - return _construct.apply(null, arguments); -} - -function _isNativeFunction(fn) { - return Function.toString.call(fn).indexOf("[native code]") !== -1; -} - -function _wrapNativeSuper(Class) { - var _cache = typeof Map === "function" ? new Map() : undefined; - - _wrapNativeSuper = function _wrapNativeSuper(Class) { - if (Class === null || !_isNativeFunction(Class)) return Class; - - if (typeof Class !== "function") { - throw new TypeError("Super expression must either be null or a function"); - } - - if (typeof _cache !== "undefined") { - if (_cache.has(Class)) return _cache.get(Class); - - _cache.set(Class, Wrapper); - } - - function Wrapper() { - return _construct(Class, arguments, _getPrototypeOf(this).constructor); - } - - Wrapper.prototype = Object.create(Class.prototype, { - constructor: { - value: Wrapper, - enumerable: false, - writable: true, - configurable: true - } - }); - return _setPrototypeOf(Wrapper, Class); - }; - - return _wrapNativeSuper(Class); -} - -function _assertThisInitialized(self) { - if (self === void 0) { - throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); - } - - return self; -} - -function _possibleConstructorReturn(self, call) { - if (call && (typeof call === "object" || typeof call === "function")) { - return call; - } - - return _assertThisInitialized(self); -} - -function _createSuper(Derived) { - var hasNativeReflectConstruct = _isNativeReflectConstruct(); - - return function _createSuperInternal() { - var Super = _getPrototypeOf(Derived), - result; - - if (hasNativeReflectConstruct) { - var NewTarget = _getPrototypeOf(this).constructor; - - result = Reflect.construct(Super, arguments, NewTarget); - } else { - result = Super.apply(this, arguments); - } - - return _possibleConstructorReturn(this, result); - }; -} - -function _slicedToArray(arr, i) { - return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); -} - -function _arrayWithHoles(arr) { - if (Array.isArray(arr)) return arr; -} - -function _iterableToArrayLimit(arr, i) { - if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; - var _arr = []; - var _n = true; - var _d = false; - var _e = undefined; - - try { - for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { - _arr.push(_s.value); - - if (i && _arr.length === i) break; - } - } catch (err) { - _d = true; - _e = err; - } finally { - try { - if (!_n && _i["return"] != null) _i["return"](); - } finally { - if (_d) throw _e; - } - } - - return _arr; -} - -function _unsupportedIterableToArray(o, minLen) { - if (!o) return; - if (typeof o === "string") return _arrayLikeToArray(o, minLen); - var n = Object.prototype.toString.call(o).slice(8, -1); - if (n === "Object" && o.constructor) n = o.constructor.name; - if (n === "Map" || n === "Set") return Array.from(o); - if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); -} - -function _arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) len = arr.length; - - for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; - - return arr2; -} - -function _nonIterableRest() { - throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); -} - -var _toString = Object.prototype.toString; -/** - * 对字符串进行编码转换 - * - * @param str 字符串 - */ - -function encode(str) { - return encodeURIComponent(str).replace(/%40/gi, '@').replace(/%3A/gi, ':').replace(/%24/g, '$').replace(/%2C/gi, ',').replace(/%20/g, '+').replace(/%5B/gi, '[').replace(/%5D/gi, ']'); -} -/** - * 是不是一个日期对象 - * - * @param date 判断目标 - */ - -function isDate(date) { - return _toString.call(date) === '[object Date]'; -} -/** - * 是不是一个普通对象 - * - * @param obj 判断目标 - */ - -function isPlainObject(obj) { - return _toString.call(obj) === '[object Object]'; -} -/** - * 深度合并多个对象 - * - * @param objs n 个对象 - */ - -function deepMerge() { - var result = {}; - - function assignValue(key, val) { - // 如果当前结果和当前值都为普通对象 - // 递归进行深度合并 - if (isPlainObject(result[key]) && isPlainObject(val)) { - result[key] = deepMerge(result[key], val); - } // 如果只有当前值为普通对象 - // 直接深拷贝当前值 - else if (isPlainObject(val)) { - result[key] = deepMerge({}, val); - } // 如果都不是普通对象 - // 直接赋值 - else { - result[key] = val; - } - } - - for (var _len = arguments.length, objs = new Array(_len), _key = 0; _key < _len; _key++) { - objs[_key] = arguments[_key]; - } - - objs.forEach(function assignObj(obj) { - Object.entries(obj).forEach(function assignKey(_ref) { - var _ref2 = _slicedToArray(_ref, 2), - key = _ref2[0], - value = _ref2[1]; - - assignValue(key, value); - }); - }); - return result; -} -/** - * 从对象中提取一部分属性 - * - * @param obj 源对象 - * @param keys 需要提取的 key - */ - -function pick(obj) { - var _pick = {}; - - for (var _len2 = arguments.length, keys = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { - keys[_key2 - 1] = arguments[_key2]; - } - - keys.forEach(function pickKey(key) { - _pick[key] = obj[key]; - }); - return _pick; -} -/** - * 从对象中剔除一部分属性 - * - * @param obj 源对象 - * @param keys 需要剔除的 key - */ - -function omit(obj) { - var _omit = _objectSpread2({}, obj); - - for (var _len3 = arguments.length, keys = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { - keys[_key3 - 1] = arguments[_key3]; - } - - keys.forEach(function omitKey(key) { - delete _omit[key]; - }); - return _omit; -} - -/** - * 通过请求地址和序列化参数生成新的请求地址 - * - * @param url 请求地址 - * @param serializedParams 序列化参数 - */ - -function generateURL(url, serializedParams) { - // 移除 hash - var hashIndex = url.indexOf('#'); - - if (hashIndex !== -1) { - url = url.slice(0, hashIndex); - } - - if (serializedParams === '') { - return url; - } // 拼接前缀 - - - var prefix = url.indexOf('?') === -1 ? '?' : '&'; - serializedParams = "".concat(prefix).concat(serializedParams); - return "".concat(url).concat(serializedParams); -} -/** - * 默认参数序列化 - * - * @param params 请求参数 - */ - - -function paramsSerialization(params) { - var parts = []; - Object.entries(params).forEach(function encodeKeyValue(_ref) { - var _ref2 = _slicedToArray(_ref, 2), - key = _ref2[0], - value = _ref2[1]; - - if (value === null || value === void 0 || value !== value) { - return; - } // 如果值是一个数组, 则特殊处理 key - - - if (Array.isArray(value)) { - key += '[]'; - } // 转成数组统一处理 - - - var values = [].concat(value); - values.forEach(function (val) { - if (isPlainObject(val)) { - val = JSON.stringify(val); - } else if (isDate(val)) { - val = val.toISOString(); - } - - parts.push("".concat(encode(key), "=").concat(encode(val))); - }); - }); - return parts.join('&'); -} -/** - * 处理 URL 参数 - * - * @param url 请求地址 - * @param params 请求参数 - * @param paramsSerializer 自定义参数序列化 - */ - - -function buildURL(url) { - var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - var paramsSerializer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : paramsSerialization; - return generateURL(url, paramsSerializer(params)); -} - -/** - * 只取 config2 中的配置 - * - * @param keys - * @param config - * @param config2 - */ - -function onlyFromConfig2(keys, config, config2) { - keys.forEach(function (key) { - if (config2[key] !== void 0) { - config[key] = config2[key]; - } - }); -} -/** - * 优先取 config2 中的配置, config2 中没有就取 config1 - * - * @param keys - * @param config - * @param config1 - * @param config2 - */ - - -function priorityFromConfig2(keys, config, config1, config2) { - keys.forEach(function (key) { - if (config2[key] !== void 0) { - config[key] = config2[key]; - } else if (config1[key] !== void 0) { - config[key] = config1[key]; - } - }); -} -/** - * 深度合并配置 - * - * @param keys - * @param config - * @param config1 - * @param config2 - */ - - -function deepMergeConfig(keys, config, config1, config2) { - keys.forEach(function (key) { - if (isPlainObject(config2[key])) { - var _config1$key; - - config[key] = deepMerge((_config1$key = config1[key]) !== null && _config1$key !== void 0 ? _config1$key : {}, config2[key]); - } else if (isPlainObject(config1[key])) { - config[key] = deepMerge(config1[key]); - } - }); -} -/** - * 合并 Axios 请求配置 - * - * @param config1 Axios 请求配置1 - * @param config2 Axios 请求配置2 - */ - - -function mergeConfig() { - var config1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - var config2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - var config = {}; - var onlyFromConfig2Keys = ['url', 'data']; - var priorityFromConfig2Keys = ['adapter', 'baseURL', 'method', 'validateStatus', 'paramsSerializer', 'transformRequest', 'transformResponse', 'errorHandler', 'cancelToken', 'dataType', 'responseType', 'timeout', 'enableHttp2', 'enableQuic', 'enableCache', 'sslVerify']; - var deepMergeConfigKeys = ['headers', 'params']; - onlyFromConfig2(onlyFromConfig2Keys, config, config2); - priorityFromConfig2(priorityFromConfig2Keys, config, config1, config2); - deepMergeConfig(deepMergeConfigKeys, config, config1, config2); - return config; -} - -/** - * 拦截器管理器 - */ -var InterceptorManagerClass = /*#__PURE__*/function () { - function InterceptorManagerClass() { - _classCallCheck(this, InterceptorManagerClass); - - /** - * 生成拦截器 id - */ - this._id = 0; - /** - * 拦截器集合 - */ - - this._interceptors = {}; - } - - _createClass(InterceptorManagerClass, [{ - key: "use", - value: function use(resolved, rejected) { - this._interceptors[++this._id] = { - resolved: resolved, - rejected: rejected - }; - return this._id; - } - }, { - key: "eject", - value: function eject(id) { - delete this._interceptors[id]; - } - }, { - key: "forEach", - value: function forEach(executor, reverse) { - var interceptors = Object.values(this._interceptors); - - if (reverse === 'reverse') { - interceptors = interceptors.reverse(); - } - - interceptors.forEach(executor); - } - }]); - - return InterceptorManagerClass; -}(); - -var CancelClass = /*#__PURE__*/function () { - /** - * @param message 取消信息 - */ - function CancelClass(message) { - _classCallCheck(this, CancelClass); - - this.message = message; - } - - _createClass(CancelClass, [{ - key: "toString", - value: function toString() { - var message = this.message ? ": ".concat(this.message) : ''; - return "Cancel".concat(message); - } - }]); - - return CancelClass; -}(); - -/** - * 是否是取消请求实例 - * - * @param value 判断的值 - */ - -function isCancel(value) { - return value instanceof CancelClass; -} - -/** - * 请求方法转全小写 - * - * @param config Axios 请求配置 - */ -function methodToLowercase() { - var method = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'get'; - return method.toLowerCase(); -} -/** - * 请求方法转全大写 - * - * @param config Axios 请求配置 - */ - -function methodToUppercase() { - var method = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'GET'; - return method.toUpperCase(); -} - -/** - * 拉平请求头 - * - * @param config Axios 请求配置 - */ - -function flattenHeaders(config) { - var _headers$common, _headers$method; - - var headers = config.headers; - - if (headers === void 0) { - return {}; - } - - var method = methodToLowercase(config.method); - return _objectSpread2(_objectSpread2(_objectSpread2({}, (_headers$common = headers.common) !== null && _headers$common !== void 0 ? _headers$common : {}), (_headers$method = headers[method]) !== null && _headers$method !== void 0 ? _headers$method : {}), omit(headers, 'common', 'options', 'get', 'head', 'post', 'put', 'delete', 'trace', 'connect')); -} - -/** - * 转换数据 - * - * @param data 请求数据/响应数据 - * @param headers 请求头/响应头 - * @param transforms 请求数据转换函数/响应数据转换函数 - */ -function transformData(data, headers, transforms) { - if (transforms === void 0) { - return data; - } - - if (!Array.isArray(transforms)) { - transforms = [transforms]; - } - - transforms.forEach(function (transform) { - data = transform(data, headers); - }); - return data; -} - -/** - * 检查是否是一个绝对 URL - * - * xxx:// 或者 "//" 开头, 视为绝对地址 - * - * @param url 需要检查的 URL - */ -function isAbsoluteURL(url) { - return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url); -} - -/** - * 拼接 baseURL 和 url 获得完整的 URL - * - * combineURL('1/2///','////3/4') => '1/2/3/4' - */ -function combineURL(baseURL, url) { - return "".concat(baseURL.replace(/\/*$/, ''), "/").concat(url.replace(/^\/*/, '')); -} - -/** - * 根据配置中的 baseURL 和 url 和 params 生成完整 URL - * - * @param config Axios 请求配置 - */ - -function transformURL(config) { - var _config$baseURL = config.baseURL, - baseURL = _config$baseURL === void 0 ? '' : _config$baseURL, - _config$url = config.url, - url = _config$url === void 0 ? '' : _config$url; - var fullURL = isAbsoluteURL(url) ? url : combineURL(baseURL, url); - return buildURL(fullURL, config.params, config.paramsSerializer); -} -/** - * Axios 请求配置转换成各大平台通用请求配置 - * - * 抹平差异 - * - * @param config Axios 请求配置 - */ - - -function transformRequest(config) { - return _objectSpread2({ - url: transformURL(config), - method: methodToUppercase(config.method), - header: config.headers - }, pick(config, 'data', 'headers', 'dataType', 'responseType', 'timeout', 'enableHttp2', 'enableQuic', 'enableCache', 'sslVerify')); -} - -/** - * 各大平台通用响应体转成 Axios 响应体 - * - * 抹平差异 - * - * @param response 通用响应体 - * @param config Axios 请求配置 - */ - -function transformResponse(response, config) { - var _ref, _response$statusCode, _ref2, _response$header; - - var status = (_ref = (_response$statusCode = response.statusCode) !== null && _response$statusCode !== void 0 ? _response$statusCode : response.status) !== null && _ref !== void 0 ? _ref : 400; - var headers = (_ref2 = (_response$header = response.header) !== null && _response$header !== void 0 ? _response$header : response.headers) !== null && _ref2 !== void 0 ? _ref2 : {}; - var statusText = status === 200 ? 'OK' : status === 400 ? 'Bad Adapter' : ''; - return _objectSpread2({ - status: status, - statusText: statusText, - headers: headers, - config: config - }, pick(response, 'data', 'cookies', 'profile')); -} - -/** - * AxiosError 继承自 Error - */ -var AxiosErrorClass = /*#__PURE__*/function (_Error) { - _inherits(AxiosErrorClass, _Error); - - var _super = _createSuper(AxiosErrorClass); - - /** - * @param message 错误信息 - * @param config Axios 请求配置 - * @param request 通用请求配置 - * @param response Axios 响应体 - */ - function AxiosErrorClass(message, config, request, response) { - var _this; - - _classCallCheck(this, AxiosErrorClass); - - _this = _super.call(this, message); - _this.config = config; - _this.request = request; - _this.response = response; - _this.isAxiosError = true; // 修复继承系统自带类 prototype 设置失败的问题 - - Object.setPrototypeOf(_assertThisInitialized(_this), AxiosErrorClass.prototype); - return _this; - } - - return AxiosErrorClass; -}( /*#__PURE__*/_wrapNativeSuper(Error)); -/** - * 创建 AxiosError 的工厂方法 - * - * 返回一个新的 AxiosError 对象 - * - * @param message 错误信息 - * @param config Axios 请求配置 - * @param request 通用请求配置 - * @param response Axios 响应体 - */ - - -function createError(message, config, request, response) { - return new AxiosErrorClass(message, config, request, response); -} - -/** - * 请求函数 - * - * @param config Axios 请求配置 - */ - -function request(config) { - return new Promise(function dispatchAdapter(resolve, reject) { - var adapter = config.adapter, - cancelToken = config.cancelToken; - var requestConfig = transformRequest(config); - /** - * 捕获错误 - * - * @param message 错误信息 - * @param response Axios 响应体 - */ - - function catchError(message, response) { - if (typeof message !== 'string') { - message = '配置不正确或者网络异常'; - } - - reject(createError(message, config, requestConfig, response)); - } - - if (adapter === void 0) { - catchError('平台适配失败,您需要参阅文档使用自定义适配器手动适配当前平台'); - return; - } - /** - * 效验状态码 - * - * @param res 请求结果 - */ - - - function handleResponse(res) { - var response = transformResponse(res, config); - - if (config.validateStatus === void 0 || config.validateStatus(response.status)) { - resolve(response); - } else { - catchError("\u8BF7\u6C42\u5931\u8D25\uFF0C\u72B6\u6001\u7801\u4E3A ".concat(response.status), response); - } - } // 使用适配器发送请求 - - - var task = adapter(_objectSpread2(_objectSpread2({}, requestConfig), {}, { - success: handleResponse, - fail: catchError - })); // 如果存在取消令牌 - // 则调用取消令牌里的 listener 监听用户的取消操作 - - if (cancelToken !== void 0) { - cancelToken.listener.then(function onCanceled(reason) { - if (task !== void 0) { - task.abort(); - } - - reject(reason); - }); - } - }); -} - -/** - * 如果已经取消, 则抛出取消对象 - * - * @param config Axios 请求配置 - */ - -function throwIfCancellationRequested(config) { - if (config.cancelToken) { - config.cancelToken.throwIfRequested(); - } -} -/** - * 发送请求 - * - * @param config Axios 请求配置 - */ - - -function dispatchRequest(config) { - var _config$data; - - throwIfCancellationRequested(config); - config.headers = flattenHeaders(config); - config.data = transformData((_config$data = config.data) !== null && _config$data !== void 0 ? _config$data : {}, config.headers, config.transformRequest); - - function onResolved(response) { - throwIfCancellationRequested(config); - response.data = transformData(response.data, response.headers, config.transformResponse); - return response; - } - - function onRejected(reason) { - var _config$errorHandler, _config$errorHandler2; - - if (!isCancel(reason)) { - throwIfCancellationRequested(config); - - if (reason.response !== void 0) { - reason.response.data = transformData(reason.response.data, reason.response.headers, config.transformResponse); - } - } - - return (_config$errorHandler = (_config$errorHandler2 = config.errorHandler) === null || _config$errorHandler2 === void 0 ? void 0 : _config$errorHandler2.call(config, reason)) !== null && _config$errorHandler !== void 0 ? _config$errorHandler : Promise.reject(reason); - } - - return request(config).then(onResolved, onRejected); -} - -var AxiosClass = /*#__PURE__*/function () { - /** - * @param defaults 自定义默认配置 - */ - function AxiosClass() { - var defaults = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - - _classCallCheck(this, AxiosClass); - - this.defaults = defaults; - this.interceptors = { - request: new InterceptorManagerClass(), - response: new InterceptorManagerClass() - }; - } - - _createClass(AxiosClass, [{ - key: "getUri", - value: function getUri(config) { - var _mergeConfig = mergeConfig(this.defaults, config), - _mergeConfig$url = _mergeConfig.url, - url = _mergeConfig$url === void 0 ? '' : _mergeConfig$url, - params = _mergeConfig.params, - paramsSerializer = _mergeConfig.paramsSerializer; - - return buildURL(url, params, paramsSerializer).replace(/^\?/, ''); - } - }, { - key: "request", - value: function request(config) { - var requestConfig = mergeConfig(this.defaults, config); - var promiseRequest = Promise.resolve(requestConfig); // 执行请求拦截器 - - this.interceptors.request.forEach(function executor(_ref) { - var resolved = _ref.resolved, - rejected = _ref.rejected; - promiseRequest = promiseRequest.then(resolved, rejected); - }, 'reverse'); // 发送请求 - - var promiseResponse = promiseRequest.then(dispatchRequest); // 执行响应拦截器 - - this.interceptors.response.forEach(function executor(_ref2) { - var resolved = _ref2.resolved, - rejected = _ref2.rejected; - promiseResponse = promiseResponse.then(resolved, rejected); - }); - return promiseResponse; - } - }, { - key: "options", - value: function options(url, config) { - return this._requestMethodWithoutParams('options', url, void 0, config); - } - }, { - key: "get", - value: function get(url, params, config) { - return this._requestMethodWithoutParams('get', url, params, config); - } - }, { - key: "head", - value: function head(url, params, config) { - return this._requestMethodWithoutParams('head', url, params, config); - } - }, { - key: "post", - value: function post(url, data, config) { - return this._requestMethodWithoutData('post', url, data, config); - } - }, { - key: "put", - value: function put(url, data, config) { - return this._requestMethodWithoutData('put', url, data, config); - } - }, { - key: "delete", - value: function _delete(url, params, config) { - return this._requestMethodWithoutParams('delete', url, params, config); - } - }, { - key: "trace", - value: function trace(url, config) { - return this._requestMethodWithoutParams('trace', url, void 0, config); - } - }, { - key: "connect", - value: function connect(url, config) { - return this._requestMethodWithoutParams('connect', url, void 0, config); - } - /** - * 合并配置后发送 HTTP 请求 - * - * @param method 请求方法 - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ - - }, { - key: "_requestMethodWithoutParams", - value: function _requestMethodWithoutParams(method, url, params) { - var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; - return this.request(_objectSpread2(_objectSpread2({}, config), {}, { - method: method, - url: url, - params: params - })); - } - /** - * 合并配置后发送 HTTP 请求 - * - * @param method 请求方法 - * @param url 请求地址 - * @param data 请求数据 - * @param config 额外配置 - */ - - }, { - key: "_requestMethodWithoutData", - value: function _requestMethodWithoutData(method, url, data) { - var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; - return this.request(_objectSpread2(_objectSpread2({}, config), {}, { - method: method, - url: url, - data: data - })); - } - }]); - - return AxiosClass; -}(); - -var CancelTokenClass = /*#__PURE__*/function () { - function CancelTokenClass(executor) { - var _this = this; - - _classCallCheck(this, CancelTokenClass); - - var action; - this.listener = new Promise(function (resolve) { - action = function action(message) { - // 防止重复取消 - if (_this._reason) { - return; - } - - _this._reason = new CancelClass(message); - resolve(_this._reason); - }; - }); - executor(action); - } - - _createClass(CancelTokenClass, [{ - key: "throwIfRequested", - value: function throwIfRequested() { - if (this._reason) { - throw this._reason; - } - } - /** - * 返回一个 CancelTokenSource - * - * CancelTokenSource.token 是一个 CancelToken 对象 - * - * CancelTokenSource.cancel 是一个 CancelAction 函数 - * - * 调用 CancelTokenSource.cancel('这里可以填写您的错误信息') - * - * 取消请求 CancelTokenSource.token - */ - - }], [{ - key: "source", - value: function source() { - var cancel; - var token = new CancelTokenClass(function executor(action) { - cancel = action; - }); - return { - token: token, - cancel: cancel - }; - } - }]); - - return CancelTokenClass; -}(); - -/** - * 自适应当前平台 - */ -function adaptive() { - var stack = [function () { - return uni.request; - }, function () { - return wx.request; - }, function () { - return my.request; - }, function () { - return swan.request; - }, function () { - return tt.request; - }, function () { - return qq.request; - }]; - var adapter; - - while (stack.length !== 0 && adapter === void 0) { - try { - adapter = stack.shift()(); - } catch (err) {} - } - - return adapter; -} - -var defaults = { - /** - * 适配器 - */ - adapter: adaptive(), - - /** - * 请求方法 - */ - method: 'get', - - /** - * 请求头 - */ - headers: { - common: { - Accept: 'application/json, test/plain, */*' - }, - options: {}, - get: {}, - head: {}, - post: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' - }, - put: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' - }, - "delete": {}, - trace: {}, - connect: {} - }, - - /** - * 状态码效验 - * - * @param status 状态码 - */ - validateStatus: function validateStatus(status) { - return status >= 200 && status < 300; - }, - - /** - * 超时时间 - */ - timeout: 10000, - - /** - * 响应数据格式 - */ - dataType: 'json', - - /** - * 响应数据类型 - */ - responseType: 'text', - - /** - * 开启 http2 - */ - enableHttp2: false, - - /** - * 开启 quic - */ - enableQuic: false, - - /** - * 开启 cache - */ - enableCache: false, - - /** - * 验证 ssl 证书 - */ - sslVerify: true -}; - -/** - * 创建一个新的 Axios 实例 - * - * 返回一个 Axios 实例增强 - */ - -function createInstance(defaults) { - var instance = new AxiosClass(defaults); - /** - * 支持重载的 axios 函数 - */ - - function axios(url) { - var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - - // 调用方式一处理请求配置 - if (typeof url !== 'string') { - config = url; - } // 调用方式二处理请求配置 - else { - config = _objectSpread2(_objectSpread2({}, config), {}, { - url: url - }); - } - - return instance.request(config); - } // instance 的属性合并到 axios 函数中 - - - Object.assign(axios, instance); // instance 的方法合并到 axios 函数中 - - Object.setPrototypeOf(axios, Object.getPrototypeOf(instance)); - return axios; -} -/** - * Axios 实例拓展 - */ - - -var axios = createInstance(defaults); // 添加 create 工厂方法 - -axios.create = function create() { - var defaults = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - return createInstance(mergeConfig(axios.defaults, defaults)); -}; // 添加 Axios 类 - - -axios.Axios = AxiosClass; // 添加 CancelToken 类 - -axios.CancelToken = CancelTokenClass; // 添加 检查错误是否来自取消请求 方法 - -axios.isCancel = isCancel; - -module.exports = axios; diff --git a/src/adaptive.ts b/src/adaptive.ts index 0151557..41f1947 100644 --- a/src/adaptive.ts +++ b/src/adaptive.ts @@ -1,4 +1,5 @@ import { Adapter, Platform } from './types'; +import { isUndefined } from './helpers/utils'; // uniapp declare let uni: Platform; @@ -28,7 +29,7 @@ export default function adaptive(): Adapter | undefined { let adapter: Adapter | undefined; - while (stack.length !== 0 && adapter === void 0) { + while (stack.length !== 0 && isUndefined(adapter)) { try { adapter = (stack.shift() as () => Adapter)(); } catch (err) {} diff --git a/src/core/flattenHeaders.ts b/src/core/flattenHeaders.ts index 09da081..99f76da 100644 --- a/src/core/flattenHeaders.ts +++ b/src/core/flattenHeaders.ts @@ -1,5 +1,5 @@ import { Headers, AxiosRequestConfig } from '../types'; -import { omit } from '../helpers/utils'; +import { isUndefined, omit } from '../helpers/utils'; import { methodToLowercase } from './transformMethod'; /** @@ -10,7 +10,7 @@ import { methodToLowercase } from './transformMethod'; export default function flattenHeaders(config: AxiosRequestConfig): Headers { const { headers } = config; - if (headers === void 0) { + if (isUndefined(headers)) { return {}; } diff --git a/src/core/mergeConfig.ts b/src/core/mergeConfig.ts index f10bf72..4ab067b 100644 --- a/src/core/mergeConfig.ts +++ b/src/core/mergeConfig.ts @@ -79,9 +79,9 @@ function deepMergeConfig( ) { keys.forEach((key) => { if (isPlainObject(config2[key])) { - config[key] = deepMerge(config1[key] ?? {}, config2[key] as AnyObject); + config[key] = deepMerge(config1[key] ?? {}, config2[key] as AnyObject); } else if (isPlainObject(config1[key])) { - config[key] = deepMerge(config1[key] as AnyObject); + config[key] = deepMerge(config1[key] as AnyObject); } }); } diff --git a/src/core/request.ts b/src/core/request.ts index f395fc9..a577de8 100644 --- a/src/core/request.ts +++ b/src/core/request.ts @@ -1,4 +1,5 @@ import { AxiosRequestConfig, AxiosResponse, Response } from '../types'; +import { isString, isUndefined } from '../helpers/utils'; import transformRequest from './transformRequest'; import transformResponse from './transformResponse'; import createError from './createError'; @@ -20,14 +21,14 @@ export default function request(config: AxiosRequestConfig): Promise { - return _toString.call(obj) === '[object Object]'; +export function isPlainObject( + value: unknown +): value is [T] extends never[] ? AnyObject : T { + return _toString.call(value) === '[object Object]'; +} + +/** + * 是否是 undefined + */ +export function isUndefined(value: unknown): value is undefined { + return typeof value === 'undefined'; +} + +/** + * 是否是字符型 + */ +export function isString(value: unknown): value is number { + return typeof value === 'string'; +} + +/** + * 是否是 Null + */ +export function isNull(value: unknown): value is null { + return value === null; } /** @@ -41,19 +60,19 @@ export function isPlainObject(obj: unknown): obj is Record { * * @param objs n 个对象 */ -export function deepMerge(...objs: Record[]): Record { - const result: Record = {}; +export function deepMerge(...objs: ObjectTree[]): [T] extends never[] ? ObjectTree : T { + const result: ObjectTree = {}; - function assignValue(key: string, val: any) { + function assignValue(key: string, val: unknown) { // 如果当前结果和当前值都为普通对象 // 递归进行深度合并 if (isPlainObject(result[key]) && isPlainObject(val)) { - result[key] = deepMerge(result[key], val); + result[key] = deepMerge(result[key] as ObjectTree, val as ObjectTree); } // 如果只有当前值为普通对象 // 直接深拷贝当前值 else if (isPlainObject(val)) { - result[key] = deepMerge({}, val); + result[key] = deepMerge({}, val as ObjectTree); } // 如果都不是普通对象 // 直接赋值 @@ -62,13 +81,13 @@ export function deepMerge(...objs: Record[]): Record { } } - objs.forEach(function assignObj(obj: Record): void { - Object.entries(obj).forEach(function assignKey([key, value]) { + objs.forEach(function assignObj(obj: ObjectTree): void { + Object.entries(obj).forEach(function ([key, value]) { assignValue(key, value); }); }); - return result; + return result as [T] extends never[] ? ObjectTree : T; } /** diff --git a/src/types.ts b/src/types.ts index ce1f7de..8c5a35f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -191,7 +191,7 @@ export declare interface AdapterRequestTask { * 适配器 */ export declare interface Adapter { - (config: AdapterRequestConfig): AdapterRequestTask | void; + (config: AdapterRequestConfig): AdapterRequestTask | undefined; } /** @@ -318,6 +318,8 @@ export declare interface AxiosRequestConfig { * 验证 ssl 证书 */ sslVerify?: boolean; + + [key: string]: unknown; } /** @@ -734,3 +736,7 @@ export declare interface AxiosInstance extends AxiosBaseInstance { */ isCancel: (value: any) => boolean; } + +export interface ObjectTree { + [key: string]: T | ObjectTree; +} diff --git a/types/adaptive.d.ts b/types/adaptive.d.ts deleted file mode 100644 index 06873fd..0000000 --- a/types/adaptive.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { Adapter } from './types'; -/** - * 自适应当前平台 - */ -export default function adaptive(): Adapter | undefined; diff --git a/types/axios.d.ts b/types/axios.d.ts deleted file mode 100644 index 7326c25..0000000 --- a/types/axios.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { AxiosInstance } from './types'; -/** - * Axios 实例拓展 - */ -declare const axios: AxiosInstance; -export default axios; diff --git a/types/cancel/Cancel.d.ts b/types/cancel/Cancel.d.ts deleted file mode 100644 index 4867d24..0000000 --- a/types/cancel/Cancel.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Cancel } from '../types'; -export default class CancelClass implements Cancel { - message?: string | undefined; - /** - * @param message 取消信息 - */ - constructor(message?: string | undefined); - toString(): string; -} diff --git a/types/cancel/CancelToken.d.ts b/types/cancel/CancelToken.d.ts deleted file mode 100644 index 70557be..0000000 --- a/types/cancel/CancelToken.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { CancelToken, CancelExecutor, CancelTokenSource } from '../types'; -import Cancel from './Cancel'; -export default class CancelTokenClass implements CancelToken { - /** - * 取消请求 - */ - private _reason?; - listener: Promise; - constructor(executor: CancelExecutor); - throwIfRequested(): void; - /** - * 返回一个 CancelTokenSource - * - * CancelTokenSource.token 是一个 CancelToken 对象 - * - * CancelTokenSource.cancel 是一个 CancelAction 函数 - * - * 调用 CancelTokenSource.cancel('这里可以填写您的错误信息') - * - * 取消请求 CancelTokenSource.token - */ - static source(): CancelTokenSource; -} diff --git a/types/cancel/isCancel.d.ts b/types/cancel/isCancel.d.ts deleted file mode 100644 index 364e6e2..0000000 --- a/types/cancel/isCancel.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Cancel from './Cancel'; -/** - * 是否是取消请求实例 - * - * @param value 判断的值 - */ -export default function isCancel(value: unknown): value is Cancel; diff --git a/types/core/Axios.d.ts b/types/core/Axios.d.ts deleted file mode 100644 index 62b1496..0000000 --- a/types/core/Axios.d.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types'; -export default class AxiosClass implements Axios { - defaults: AxiosRequestConfig; - interceptors: Interceptors; - /** - * @param defaults 自定义默认配置 - */ - constructor(defaults?: AxiosRequestConfig); - getUri(config: AxiosRequestConfig): string; - request(config: AxiosRequestConfig): Promise>; - options(url: string, config?: AxiosRequestConfig): Promise>; - get(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; - head(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; - post(url: string, data?: Data, config?: AxiosRequestConfig): Promise>; - put(url: string, data?: Data, config?: AxiosRequestConfig): Promise>; - delete(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; - trace(url: string, config?: AxiosRequestConfig): Promise>; - connect(url: string, config?: AxiosRequestConfig): Promise>; - /** - * 合并配置后发送 HTTP 请求 - * - * @param method 请求方法 - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ - private _requestMethodWithoutParams; - /** - * 合并配置后发送 HTTP 请求 - * - * @param method 请求方法 - * @param url 请求地址 - * @param data 请求数据 - * @param config 额外配置 - */ - private _requestMethodWithoutData; -} diff --git a/types/core/InterceptorManager.d.ts b/types/core/InterceptorManager.d.ts deleted file mode 100644 index 0805229..0000000 --- a/types/core/InterceptorManager.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { InterceptorResolved, InterceptorRejected, InterceptorExecutor, InterceptorManager } from '../types'; -/** - * 拦截器管理器 - */ -export default class InterceptorManagerClass implements InterceptorManager { - /** - * 生成拦截器 id - */ - private _id; - /** - * 拦截器集合 - */ - private _interceptors; - use(resolved: InterceptorResolved, rejected?: InterceptorRejected): number; - eject(id: number): void; - forEach(executor: InterceptorExecutor, reverse?: 'reverse'): void; -} diff --git a/types/core/createError.d.ts b/types/core/createError.d.ts deleted file mode 100644 index b6a1f2f..0000000 --- a/types/core/createError.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { AxiosRequestConfig, RequestConfig, AxiosResponse, AxiosError } from '../types'; -/** - * 创建 AxiosError 的工厂方法 - * - * 返回一个新的 AxiosError 对象 - * - * @param message 错误信息 - * @param config Axios 请求配置 - * @param request 通用请求配置 - * @param response Axios 响应体 - */ -export default function createError(message: string, config: AxiosRequestConfig, request: RequestConfig, response?: AxiosResponse): AxiosError; diff --git a/types/core/dispatchRequest.d.ts b/types/core/dispatchRequest.d.ts deleted file mode 100644 index d89d9af..0000000 --- a/types/core/dispatchRequest.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { AxiosRequestConfig, AxiosResponse } from '../types'; -/** - * 发送请求 - * - * @param config Axios 请求配置 - */ -export default function dispatchRequest(config: AxiosRequestConfig): Promise; diff --git a/types/core/flattenHeaders.d.ts b/types/core/flattenHeaders.d.ts deleted file mode 100644 index 89de838..0000000 --- a/types/core/flattenHeaders.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Headers, AxiosRequestConfig } from '../types'; -/** - * 拉平请求头 - * - * @param config Axios 请求配置 - */ -export default function flattenHeaders(config: AxiosRequestConfig): Headers; diff --git a/types/core/mergeConfig.d.ts b/types/core/mergeConfig.d.ts deleted file mode 100644 index c9dfa74..0000000 --- a/types/core/mergeConfig.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { AxiosRequestConfig } from '../types'; -/** - * 合并 Axios 请求配置 - * - * @param config1 Axios 请求配置1 - * @param config2 Axios 请求配置2 - */ -export default function mergeConfig(config1?: AxiosRequestConfig, config2?: AxiosRequestConfig): AxiosRequestConfig; diff --git a/types/core/request.d.ts b/types/core/request.d.ts deleted file mode 100644 index c5e9a5e..0000000 --- a/types/core/request.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { AxiosRequestConfig, AxiosResponse } from '../types'; -/** - * 请求函数 - * - * @param config Axios 请求配置 - */ -export default function request(config: AxiosRequestConfig): Promise; diff --git a/types/core/transformData.d.ts b/types/core/transformData.d.ts deleted file mode 100644 index 3dafb15..0000000 --- a/types/core/transformData.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Data, Headers, TransformData } from '../types'; -/** - * 转换数据 - * - * @param data 请求数据/响应数据 - * @param headers 请求头/响应头 - * @param transforms 请求数据转换函数/响应数据转换函数 - */ -export default function transformData(data: Data, headers: Headers, transforms?: TransformData | TransformData[]): Data; diff --git a/types/core/transformMethod.d.ts b/types/core/transformMethod.d.ts deleted file mode 100644 index 019245f..0000000 --- a/types/core/transformMethod.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { AliasMethod, AdapterMethod, Method } from '../types'; -/** - * 请求方法转全小写 - * - * @param config Axios 请求配置 - */ -export declare function methodToLowercase(method?: Method): AliasMethod; -/** - * 请求方法转全大写 - * - * @param config Axios 请求配置 - */ -export declare function methodToUppercase(method?: Method): AdapterMethod; diff --git a/types/core/transformRequest.d.ts b/types/core/transformRequest.d.ts deleted file mode 100644 index 299b979..0000000 --- a/types/core/transformRequest.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { AxiosRequestConfig, RequestConfig } from '../types'; -/** - * Axios 请求配置转换成各大平台通用请求配置 - * - * 抹平差异 - * - * @param config Axios 请求配置 - */ -export default function transformRequest(config: AxiosRequestConfig): RequestConfig; diff --git a/types/core/transformResponse.d.ts b/types/core/transformResponse.d.ts deleted file mode 100644 index ee2446b..0000000 --- a/types/core/transformResponse.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { AxiosRequestConfig, AxiosResponse, Response } from '../types'; -/** - * 各大平台通用响应体转成 Axios 响应体 - * - * 抹平差异 - * - * @param response 通用响应体 - * @param config Axios 请求配置 - */ -export default function transformResponse(response: Response, config: AxiosRequestConfig): AxiosResponse; diff --git a/types/defaults.d.ts b/types/defaults.d.ts deleted file mode 100644 index ab86472..0000000 --- a/types/defaults.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { AxiosRequestConfig } from './types'; -declare const defaults: AxiosRequestConfig; -export default defaults; diff --git a/types/helpers/buildURL.d.ts b/types/helpers/buildURL.d.ts deleted file mode 100644 index e3597bd..0000000 --- a/types/helpers/buildURL.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { AnyObject, Params } from '../types'; -/** - * 默认参数序列化 - * - * @param params 请求参数 - */ -declare function paramsSerialization(params: AnyObject): string; -/** - * 处理 URL 参数 - * - * @param url 请求地址 - * @param params 请求参数 - * @param paramsSerializer 自定义参数序列化 - */ -export default function buildURL(url: string, params?: Params, paramsSerializer?: typeof paramsSerialization): string; -export {}; diff --git a/types/helpers/combineURL.d.ts b/types/helpers/combineURL.d.ts deleted file mode 100644 index e4156df..0000000 --- a/types/helpers/combineURL.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * 拼接 baseURL 和 url 获得完整的 URL - * - * combineURL('1/2///','////3/4') => '1/2/3/4' - */ -export default function combineURL(baseURL: string, url: string): string; diff --git a/types/helpers/isAbsoluteURL.d.ts b/types/helpers/isAbsoluteURL.d.ts deleted file mode 100644 index 5444e4d..0000000 --- a/types/helpers/isAbsoluteURL.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * 检查是否是一个绝对 URL - * - * xxx:// 或者 "//" 开头, 视为绝对地址 - * - * @param url 需要检查的 URL - */ -export default function isAbsoluteURL(url: string): boolean; diff --git a/types/helpers/utils.d.ts b/types/helpers/utils.d.ts deleted file mode 100644 index 5c09ae4..0000000 --- a/types/helpers/utils.d.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { AnyObject } from '../types'; -/** - * 对字符串进行编码转换 - * - * @param str 字符串 - */ -export declare function encode(str: string): string; -/** - * 是不是一个日期对象 - * - * @param date 判断目标 - */ -export declare function isDate(date: unknown): date is Date; -/** - * 是不是一个普通对象 - * - * @param obj 判断目标 - */ -export declare function isPlainObject(obj: unknown): obj is Record; -/** - * 深度合并多个对象 - * - * @param objs n 个对象 - */ -export declare function deepMerge(...objs: Record[]): Record; -/** - * 从对象中提取一部分属性 - * - * @param obj 源对象 - * @param keys 需要提取的 key - */ -export declare function pick(obj: T, ...keys: K[]): Pick; -/** - * 从对象中剔除一部分属性 - * - * @param obj 源对象 - * @param keys 需要剔除的 key - */ -export declare function omit(obj: T, ...keys: K[]): Omit; diff --git a/types/index.d.ts b/types/index.d.ts deleted file mode 100644 index 53c6af9..0000000 --- a/types/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -import axios from './axios'; -export * from './types'; -export default axios; diff --git a/types/types.d.ts b/types/types.d.ts deleted file mode 100644 index 3846406..0000000 --- a/types/types.d.ts +++ /dev/null @@ -1,596 +0,0 @@ -/** - * 任意值对象 - */ -export declare interface AnyObject { - [x: string]: T; -} -/** - * 请求方法 - */ -export declare type AdapterMethod = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'; -/** - * 请求方法别名 - */ -export declare type AliasMethod = 'options' | 'get' | 'head' | 'post' | 'put' | 'delete' | 'trace' | 'connect'; -/** - * Axios 请求方法 - */ -export declare type Method = AliasMethod | AdapterMethod; -/** - * Axios 参数 - */ -export declare type Params = AnyObject; -/** - * Axios 数据 - */ -export declare type Data = string | AnyObject | ArrayBuffer; -/** - * Axios 头 - */ -export declare interface Headers extends Partial>> { - /** - * 自定义配置 - */ - [x: string]: AnyObject | string | undefined; -} -/** - * 通用请求配置 - */ -export declare interface RequestConfig { - /** - * 接口地址 - */ - url: string; - /** - * HTTP 请求方法 - */ - method: AdapterMethod; - /** - * 请求数据 - */ - data: Data; - /** - * 请求头 同 headers - */ - header: AnyObject; - /** - * 请求头 同 header - */ - headers: AnyObject; - /** - * 返回的数据格式 - */ - dataType?: 'json' | '其他'; - /** - * 响应的数据类型 - */ - responseType?: 'text' | 'arraybuffer'; - /** - * 超时时间 - */ - timeout?: number; - /** - * 开启 http2 - */ - enableHttp2?: boolean; - /** - * 开启 quic - */ - enableQuic?: boolean; - /** - * 开启 cache - */ - enableCache?: boolean; - /** - * 验证 ssl 证书 - */ - sslVerify?: boolean; -} -/** - * 通用响应体 - */ -export declare interface Response { - /** - * 响应状态码 - */ - statusCode?: number; - /** - * 响应状态码 - */ - status?: number; - /** - * 响应头 Headers - */ - header?: AnyObject; - /** - * 响应头 Headers - */ - headers?: Headers; - /** - * 响应数据 - */ - data: Data; - /** - * 开发者服务器返回的 cookies,格式为字符串数组 - */ - cookies?: string[]; - /** - * 网络请求过程中一些关键时间点的耗时信息 - */ - profile?: AnyObject; -} -/** - * 适配器请求配置 - */ -export declare interface AdapterRequestConfig extends RequestConfig { - /** - * 成功的响应函数 - */ - success: (res: Response) => void; - /** - * 失败的响应函数 - */ - fail: (err: unknown) => void; -} -/** - * 适配器请求任务 - */ -export declare interface AdapterRequestTask { - /** - * 取消请求 - */ - abort(): void; -} -/** - * 适配器 - */ -export declare interface Adapter { - (config: AdapterRequestConfig): AdapterRequestTask | void; -} -/** - * 平台 - */ -export declare interface Platform { - request: Adapter; -} -/** - * 转换数据函数 - */ -export declare interface TransformData { - (data: Data, headers: Headers): Data; -} -/** - * 错误处理程序 - */ -export declare interface ErrorHandler { - (error: any): Promise | any; -} -/** - * Axios 请求配置 - */ -export declare interface AxiosRequestConfig { - /** - * 自定义适配器 - */ - adapter?: Adapter; - /** - * 基础地址 - */ - baseURL?: string; - /** - * 请求地址 - */ - url?: string; - /** - * 请求方法 - */ - method?: Method; - /** - * 请求参数 - */ - params?: Params; - /** - * 请求数据 - */ - data?: Data; - /** - * 请求头 - */ - headers?: Headers; - /** - * 自定义合法状态码 - */ - validateStatus?: (status: number) => boolean; - /** - * 自定义参数序列化 - */ - paramsSerializer?: (params?: AnyObject) => string; - /** - * 转换请求数据 - */ - transformRequest?: TransformData | TransformData[]; - /** - * 转换响应数据 - */ - transformResponse?: TransformData | TransformData[]; - /** - * 自定义错误处理 - */ - errorHandler?: ErrorHandler; - /** - * 取消令牌 - */ - cancelToken?: CancelToken; - /** - * 超时时间 - */ - timeout?: number; - /** - * 响应数据格式 - */ - dataType?: 'json' | '其他'; - /** - * 响应数据类型 - */ - responseType?: 'text' | 'arraybuffer'; - /** - * 开启 http2 - */ - enableHttp2?: boolean; - /** - * 开启 quic - */ - enableQuic?: boolean; - /** - * 开启 cache - */ - enableCache?: boolean; - /** - * 验证 ssl 证书 - */ - sslVerify?: boolean; -} -/** - * Axios 响应体 - */ -export declare interface AxiosResponse { - /** - * 状态码 - */ - status: number; - /** - * 状态文本 - */ - statusText: string; - /** - * 服务端返回的数据 - */ - data: T; - /** - * 响应头 - */ - headers: Headers; - /** - * Axios 请求配置 - */ - config: AxiosRequestConfig; - /** - * 开发者服务器返回的 cookies,格式为字符串数组 - */ - cookies?: string[]; - /** - * 网络请求过程中一些关键时间点的耗时信息 - */ - profile?: AnyObject; -} -/** - * 拦截器成功的回调函数 - */ -export declare interface InterceptorResolved { - (value: T): Promise | T; -} -/** - * 拦截器失败的回调函数 - */ -export declare interface InterceptorRejected { - (error: any): any; -} -/** - * 拦截器 - */ -export declare interface Interceptor { - /** - * 拦截器成功的回调函数 - */ - resolved: InterceptorResolved; - /** - * 拦截器失败的回调函数 - */ - rejected?: InterceptorRejected; -} -/** - * 拦截器执行器 - */ -export declare interface InterceptorExecutor { - (interceptor: Interceptor): void; -} -/** - * 拦截器管理器 - */ -export declare interface InterceptorManager { - [x: string]: any; - /** - * 添加拦截器 - * - * @param resolved 成功的回调函数 - * @param rejected 失败的回调函数 - */ - use(resolved: InterceptorResolved, rejected?: InterceptorRejected): number; - /** - * 删除拦截器 - * - * @param id 拦截器 id - */ - eject(id: number): void; - /** - * 遍历所有拦截器 - * - * @param executor 拦截器执行器 - * @param reverse 是否倒序遍历 - */ - forEach(executor: InterceptorExecutor, reverse?: 'reverse'): void; -} -/** - * Axios 拦截器 - */ -export declare interface Interceptors { - /** - * request 请求拦截器 - */ - request: InterceptorManager; - /** - * response 响应拦截器 - */ - response: InterceptorManager; -} -/** - * Axios 实例 - */ -export declare interface Axios { - /** - * 默认配置 - */ - defaults: AxiosRequestConfig; - /** - * Axios 拦截器 - */ - interceptors: Interceptors; - /** - * 根据配置中的 url 和 params 生成一个 URI - * - * @param config 请求配置 - */ - getUri(config: AxiosRequestConfig): string; - /** - * 发送 HTTP 请求 - * - * @param config 请求配置 - */ - request(config: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 OPTIONS - * - * @param url 请求地址 - * @param config 额外配置 - */ - options(url: string, config?: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 GET - * - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ - get(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 HEAD - * - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ - head(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 POST - * - * @param url 请求地址 - * @param data 请求数据 - * @param config 额外配置 - */ - post(url: string, data?: Data, config?: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 PUT - * - * @param url 请求地址 - * @param data 请求数据 - * @param config 额外配置 - */ - put(url: string, data?: Data, config?: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 DELETE - * - * @param url 请求地址 - * @param params 请求参数 - * @param config 额外配置 - */ - delete(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 TRACE - * - * @param url 请求地址 - * @param config 额外配置 - */ - trace(url: string, config?: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 CONNECT - * - * @param url 请求地址 - * @param config 额外配置 - */ - connect(url: string, config?: AxiosRequestConfig): Promise>; -} -/** - * Axios 类接口 - */ -export declare interface AxiosConstructor { - new (config?: AxiosRequestConfig): Axios; -} -/** - * AxiosError 类继承自 Error - */ -export declare interface AxiosError extends Error { - /** - * 是 Axios 错误 - */ - isAxiosError: boolean; - /** - * 请求配置 - */ - config: AxiosRequestConfig; - /** - * 通用请求配置 - */ - request: RequestConfig; - /** - * Axios 响应体 - */ - response?: AxiosResponse; -} -/** - * 取消请求 - */ -export declare interface Cancel { - /** - * 取消信息 - */ - message?: string; - /** - * 序列化 - */ - toString(): string; -} -/** - * 取消请求类接口 - */ -export declare interface CancelConstructor { - new (message?: string): Cancel; -} -/** - * 取消操作 - */ -export declare interface CancelAction { - (message?: string): void; -} -/** - * 取消操作执行器 - */ -export declare interface CancelExecutor { - (cancel: CancelAction): void; -} -/** - * 取消令牌 - */ -export declare interface CancelToken { - /** - * 取消时被触发 - */ - listener: Promise; - /** - * 如果已经取消, 则抛出取消对象 - */ - throwIfRequested(): void; -} -/** - * 取消令牌 source - */ -export declare interface CancelTokenSource { - /** - * 取消令牌 - */ - token: CancelToken; - /** - * 取消操作 - */ - cancel: CancelAction; -} -/** - * 取消令牌类接口 - */ -export declare interface CancelTokenConstructor { - new (executor: CancelExecutor): CancelToken; - /** - * 返回一个 CancelTokenSource - * - * CancelTokenSource.token 是一个 CancelToken 对象 - * - * CancelTokenSource.cancel 是一个 CancelAction 函数 - * - * 调用 CancelTokenSource.cancel('这里可以填写您的错误信息') - * - * 取消请求 CancelTokenSource.token - */ - source(): CancelTokenSource; -} -/** - * Axios 实例基础拓展 - * - * * 支持两种函数调用方式 - */ -export declare interface AxiosBaseInstance extends Axios { - /** - * 发送 HTTP 请求 - * - * 调用方式一 - * - * @param config 请求配置 - */ - (config: AxiosRequestConfig): Promise>; - /** - * 发送 HTTP 请求 - * - * 调用方式二 - * - * @param url 请求地址 - * @param config 额外配置 - */ - (url: string, config?: AxiosRequestConfig): Promise>; -} -/** - * Axios 实例拓展 - * - * * 支持两种函数调用方式 - * - * * 同时拓展了一些静态属性和方法 - */ -export declare interface AxiosInstance extends AxiosBaseInstance { - /** - * 创建 Axios 实例基础拓展 - * - * @param defaults 自定义默认配置 - */ - create(defaults?: AxiosRequestConfig): AxiosBaseInstance; - /** - * Axios 类 - */ - Axios: AxiosConstructor; - /** - * 取消令牌 类 - */ - CancelToken: CancelTokenConstructor; - /** - * 检查错误是否来自取消请求 - * - * @param value 判断的值 - */ - isCancel: (value: any) => boolean; -}