1.0.6
parent
b1f5da385b
commit
7b82ba48dc
14
README.md
14
README.md
|
@ -93,8 +93,8 @@ npm i axios-miniprogram
|
|||
```typescript
|
||||
axios('/test', {
|
||||
validateStatus: function validateStatus(status) {
|
||||
// 这样,状态码在 100 到 400 之间都是请求成功
|
||||
return status >= 200 && status < 300;
|
||||
// 这样,状态码在 200 到 400 之间都是请求成功
|
||||
return status >= 200 && status < 400;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
@ -130,7 +130,7 @@ axios('/test', {
|
|||
|
||||
##### `config.adapter`自定义平台适配器
|
||||
|
||||
您可以自己适配您所在的平台
|
||||
您可以手动适配当前所处的平台
|
||||
|
||||
```typescript
|
||||
axios.defaults.adapter = function adapter(adapterConfig) {
|
||||
|
@ -166,7 +166,7 @@ axios.defaults.adapter = function adapter(adapterConfig) {
|
|||
} = adapterConfig;
|
||||
|
||||
// 在 adapterConfig 中选择您需要的参数发送请求
|
||||
wx.request({
|
||||
return wx.request({
|
||||
url,
|
||||
method,
|
||||
data,
|
||||
|
@ -187,7 +187,7 @@ axios.defaults.adapter = wx.request;
|
|||
```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';
|
||||
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
||||
```
|
||||
|
||||
##### 自定义实例默认配置
|
||||
|
@ -209,7 +209,7 @@ const instance = axios.create({
|
|||
// 也可以创建后修改
|
||||
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';
|
||||
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
||||
```
|
||||
|
||||
##### 配置优先顺序
|
||||
|
@ -224,8 +224,6 @@ instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlenco
|
|||
|statusText|String|状态文本|是|
|
||||
|data|String/Object/ArrayBuffer|开发者服务器返回的数据|是|
|
||||
|headers|Object|响应头|是|
|
||||
|response|Object|通用响应体|是|
|
||||
|request|Object|通用请求配置|是|
|
||||
|config|Object|Axios 请求配置|是|
|
||||
|cookies|Array<.String>|开发者服务器返回的 cookies,格式为字符串数组| |
|
||||
|profile|Object|网络请求过程中一些关键时间点的耗时信息| |
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "axios-miniprogram",
|
||||
"version": "1.0.5",
|
||||
"version": "1.0.6",
|
||||
"description": "基于 Promise 的 HTTP 请求库,适用于各大小程序平台。",
|
||||
"main": "package/index.js",
|
||||
"miniprogram": "package",
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-17 15:05:43
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-18 15:51:02
|
||||
* @LastEditTime: 2020-04-19 15:53:36
|
||||
*/
|
||||
import { Method, Data, Headers, AdapterMethod, AxiosRequestConfig, RequestConfig } from '../types';
|
||||
import { pick } from '../helper/utils';
|
||||
import transformURL from '../helper/transformURL';
|
||||
|
||||
/**
|
||||
|
@ -24,7 +25,7 @@ function methodUppercase(config: AxiosRequestConfig): AdapterMethod {
|
|||
* @param config Axios 请求配置
|
||||
*/
|
||||
export default function requestConfigOk(config: AxiosRequestConfig): RequestConfig {
|
||||
const { headers, data, dataType, responseType, timeout, enableHttp2, enableQuic, enableCache, sslVerify } = config;
|
||||
const { headers, data } = config;
|
||||
const url = transformURL(config);
|
||||
const method = methodUppercase(config);
|
||||
|
||||
|
@ -34,12 +35,9 @@ export default function requestConfigOk(config: AxiosRequestConfig): RequestConf
|
|||
header: headers as Headers,
|
||||
headers: headers as Headers,
|
||||
data: data as Data,
|
||||
dataType,
|
||||
responseType,
|
||||
timeout,
|
||||
enableHttp2,
|
||||
enableQuic,
|
||||
enableCache,
|
||||
sslVerify,
|
||||
...pick<
|
||||
AxiosRequestConfig,
|
||||
'dataType' | 'responseType' | 'timeout' | 'enableHttp2' | 'enableQuic' | 'enableCache' | 'sslVerify'
|
||||
>(config, 'dataType', 'responseType', 'timeout', 'enableHttp2', 'enableQuic', 'enableCache', 'sslVerify'),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-17 14:09:16
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-18 16:03:37
|
||||
* @LastEditTime: 2020-04-19 15:47:36
|
||||
*/
|
||||
import { RequestConfig, AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
||||
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
||||
import { pick } from '../helper/utils';
|
||||
|
||||
/**
|
||||
* 各大平台通用响应体转成 Axios 响应体
|
||||
|
@ -15,27 +16,16 @@ import { RequestConfig, AxiosRequestConfig, AxiosResponse, Response } from '../t
|
|||
* @param request 通用请求配置
|
||||
* @param config Axios 请求配置
|
||||
*/
|
||||
|
||||
export default function responseOk(
|
||||
response: Response,
|
||||
request: RequestConfig,
|
||||
config: AxiosRequestConfig
|
||||
): AxiosResponse {
|
||||
response.status = response.status ?? response.statusCode;
|
||||
response.headers = response.headers ?? response.header;
|
||||
|
||||
const { status, headers, data, cookies, profile } = response;
|
||||
export default function responseOk(response: Response, config: AxiosRequestConfig): AxiosResponse {
|
||||
const status = response.status ?? response.statusCode;
|
||||
const headers = response.headers ?? response.header;
|
||||
const statusText = status === 200 ? 'OK' : status === 400 ? 'Bad Adapter' : '';
|
||||
|
||||
return {
|
||||
status,
|
||||
statusText,
|
||||
headers,
|
||||
data,
|
||||
response,
|
||||
request,
|
||||
config,
|
||||
cookies,
|
||||
profile,
|
||||
...pick<Response, 'data' | 'cookies' | 'profile'>(response, 'data', 'cookies', 'profile'),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-15 17:50:50
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 02:22:28
|
||||
* @LastEditTime: 2020-04-19 13:35:47
|
||||
*/
|
||||
import {
|
||||
InterceptorResolved,
|
||||
|
@ -24,7 +24,7 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
|
|||
/**
|
||||
* 拦截器集合
|
||||
*/
|
||||
private interceptors: Record<string, Interceptor<T>>;
|
||||
private interceptors: Record<number, Interceptor<T>>;
|
||||
|
||||
constructor() {
|
||||
this.id = 0;
|
||||
|
@ -38,7 +38,7 @@ export default class InterceptorManagerStatic<T> implements InterceptorManager<T
|
|||
* @param rejected 失败的回调函数
|
||||
*/
|
||||
public use(resolved: InterceptorResolved<T>, rejected: InterceptorRejected = (err: any) => Promise.reject(err)) {
|
||||
this.interceptors[this.id++] = {
|
||||
this.interceptors[++this.id] = {
|
||||
resolved,
|
||||
rejected,
|
||||
};
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-13 18:01:16
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-18 15:53:05
|
||||
* @LastEditTime: 2020-04-19 14:52:28
|
||||
*/
|
||||
import { AxiosRequestConfig, AxiosResponse } from '../types';
|
||||
import flattenHeaders from '../helper/flattenHeaders';
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-15 22:09:38
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-18 20:34:41
|
||||
* @LastEditTime: 2020-04-19 14:47:18
|
||||
*/
|
||||
import { AxiosRequestConfig } from '../types';
|
||||
import adaptive from '../adapter/adaptive';
|
||||
|
@ -21,10 +21,10 @@ const defaults: AxiosRequestConfig = {
|
|||
head: {},
|
||||
delete: {},
|
||||
post: {
|
||||
'Context-Type': 'application/x-www-form-urlencoded',
|
||||
'Context-Type': 'application/x-www-form-urlencoded; charset=utf-8',
|
||||
},
|
||||
put: {
|
||||
'Context-Type': 'application/x-www-form-urlencoded',
|
||||
'Context-Type': 'application/x-www-form-urlencoded; charset=utf-8',
|
||||
},
|
||||
},
|
||||
validateStatus: function validateStatus(status) {
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-18 12:00:01
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 02:32:15
|
||||
* @LastEditTime: 2020-04-19 15:20:17
|
||||
*/
|
||||
import { AliasMethod, Headers, AxiosRequestConfig } from '../types';
|
||||
import { merge } from './utils';
|
||||
import { omit } from './utils';
|
||||
|
||||
/**
|
||||
* 拉平请求头
|
||||
|
@ -13,14 +13,17 @@ import { merge } from './utils';
|
|||
* @param config Axios 请求配置
|
||||
*/
|
||||
export default function flattenHeaders(config: AxiosRequestConfig): Headers {
|
||||
let { headers = {} } = config;
|
||||
const { headers } = config;
|
||||
|
||||
if (headers === undefined) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const method = (config.method as string).toLowerCase() as AliasMethod;
|
||||
|
||||
headers = merge(headers.common ?? {}, headers[method] ?? {}, headers);
|
||||
|
||||
['common', 'options', 'delete', 'get', 'head', 'post', 'put', 'trace', 'connect'].forEach((key: string) => {
|
||||
delete headers[key];
|
||||
});
|
||||
|
||||
return headers;
|
||||
return {
|
||||
...(headers.common ?? {}),
|
||||
...(headers[method] ?? {}),
|
||||
...omit(headers, 'common', 'options', 'delete', 'get', 'head', 'post', 'put', 'trace', 'connect'),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-13 21:55:40
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 01:56:21
|
||||
* @LastEditTime: 2020-04-19 16:08:40
|
||||
*/
|
||||
import { AnyObject } from '../types';
|
||||
|
||||
const _toString = Object.prototype.toString;
|
||||
|
||||
/**
|
||||
|
@ -44,29 +46,6 @@ export function combineURL(baseURL: string, url: string): string {
|
|||
return `${baseURL.replace(/\/*$/, '')}/${url.replace(/^\/*/, '')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 浅合并多个对象
|
||||
*
|
||||
* @param objs n 个对象
|
||||
*/
|
||||
export function merge(...objs: Record<string, any>[]): Record<string, any> {
|
||||
const result: Record<string, any> = {};
|
||||
|
||||
function assignValue(key: string, val: any): void {
|
||||
if (isPlainObject(result[key]) && isPlainObject(val)) {
|
||||
result[key] = merge(result[key], val);
|
||||
} else {
|
||||
result[key] = val;
|
||||
}
|
||||
}
|
||||
|
||||
objs.forEach((obj: Record<string, any>): void => {
|
||||
Object.entries(obj).forEach(([key, value]) => assignValue(key, value));
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 深度合并多个对象
|
||||
*
|
||||
|
@ -102,3 +81,31 @@ export function deepMerge(...objs: Record<string, any>[]): Record<string, any> {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从对象中提取一部分属性
|
||||
*
|
||||
* @param obj 源对象
|
||||
* @param keys 需要提取的 key
|
||||
*/
|
||||
export function pick<T extends AnyObject, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
|
||||
const _pick: Partial<T> = {};
|
||||
|
||||
keys.forEach((key: K) => (_pick[key] = obj[key]));
|
||||
|
||||
return _pick as Pick<T, K>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从对象中剔除一部分属性
|
||||
*
|
||||
* @param obj 源对象
|
||||
* @param keys 需要剔除的 key
|
||||
*/
|
||||
export function omit<T extends AnyObject, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K> {
|
||||
const _omit = { ...obj };
|
||||
|
||||
keys.forEach((key: K) => delete _omit[key]);
|
||||
|
||||
return _omit;
|
||||
}
|
||||
|
|
12
src/types.ts
12
src/types.ts
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-13 15:23:53
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 02:06:24
|
||||
* @LastEditTime: 2020-04-19 14:10:56
|
||||
*/
|
||||
/**
|
||||
* 任意值对象
|
||||
|
@ -367,16 +367,6 @@ export interface AxiosResponse<T extends Data = Data> {
|
|||
*/
|
||||
headers: Headers;
|
||||
|
||||
/**
|
||||
* 通用响应体
|
||||
*/
|
||||
response: Response;
|
||||
|
||||
/**
|
||||
* 通用请求配置
|
||||
*/
|
||||
request: RequestConfig;
|
||||
|
||||
/**
|
||||
* Axios 请求配置
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* @Author: early-autumn
|
||||
* @Date: 2020-04-19 14:34:13
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 14:40:57
|
||||
*/
|
||||
import buildURL from '../../src/helper/buildURL';
|
||||
|
||||
describe('测试 /helper/buildURL.ts', () => {
|
||||
it('url', () => {
|
||||
expect(buildURL('/test')).toBe('/test');
|
||||
expect(buildURL('/test?id=1')).toBe('/test?id=1');
|
||||
});
|
||||
|
||||
it('url + params', () => {
|
||||
expect(
|
||||
buildURL('/test', {
|
||||
test: 1,
|
||||
})
|
||||
).toBe('/test?test=1');
|
||||
expect(
|
||||
buildURL('/test?id=1', {
|
||||
test: 1,
|
||||
})
|
||||
).toBe('/test?id=1&test=1');
|
||||
});
|
||||
|
||||
it('url + params + paramsSerializer', () => {
|
||||
expect(
|
||||
buildURL(
|
||||
'/test',
|
||||
{
|
||||
test: 1,
|
||||
},
|
||||
() => 'paramsSerializer=ok'
|
||||
)
|
||||
).toBe('/test?paramsSerializer=ok');
|
||||
expect(
|
||||
buildURL(
|
||||
'/test?id=1',
|
||||
{
|
||||
test: 1,
|
||||
},
|
||||
() => 'paramsSerializer=ok'
|
||||
)
|
||||
).toBe('/test?id=1¶msSerializer=ok');
|
||||
});
|
||||
});
|
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* @Author: early-autumn
|
||||
* @Date: 2020-04-19 14:43:15
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 16:19:24
|
||||
*/
|
||||
import flattenHeaders from '../../src/helper/flattenHeaders';
|
||||
|
||||
describe('测试 src/helper/flattenHeaders.ts', () => {
|
||||
it('测试 容错', () => {
|
||||
expect(flattenHeaders({})).toEqual({
|
||||
Accept: 'application/json, test/plain, */*',
|
||||
});
|
||||
// 'Content-Type': 'application/json; charset=utf-8',
|
||||
});
|
||||
});
|
|
@ -1,13 +0,0 @@
|
|||
/*
|
||||
* @Author: early-autumn
|
||||
* @Date: 2020-04-14 23:43:45
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 03:05:38
|
||||
*/
|
||||
import { isDate } from '../src/helper/utils';
|
||||
|
||||
describe('', () => {
|
||||
it('?', () => {
|
||||
expect(isDate(new Date())).toBe(true);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue