🎨 优化
parent
67c9b49976
commit
7b20ad9661
|
@ -0,0 +1,16 @@
|
|||
# http://editorconfig.org
|
||||
# root = true
|
||||
|
||||
# [*]
|
||||
# indent_style = space
|
||||
# indent_size = 2
|
||||
# end_of_line = lf
|
||||
# charset = utf-8
|
||||
# trim_trailing_whitespace = true
|
||||
# insert_final_newline = true
|
||||
|
||||
# [*.md]
|
||||
# trim_trailing_whitespace = false
|
||||
|
||||
# [Makefile]
|
||||
# indent_style = tab
|
14
README.md
14
README.md
|
@ -501,4 +501,18 @@ const instance = new axios.Axios({
|
|||
instance.get('/test');
|
||||
```
|
||||
|
||||
## 执行流程
|
||||
|
||||
```typescript
|
||||
axios('/test').then().catch();
|
||||
|
||||
// 请求成功
|
||||
// axios => axios.interceptors.request => config.transformRequest => config.paramsSerializer => config.adapter => config.validateStatus => config.transformResponse => axios.interceptors.response => then
|
||||
|
||||
// 请求失败
|
||||
// axios => axios.interceptors.request => config.transformRequest => config.paramsSerializer => config.adapter => config.validateStatus => config.transformResponse => config.errorHandler => axios.interceptors.response => catch
|
||||
```
|
||||
|
||||
## Typescript
|
||||
|
||||

|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "axios-miniprogram",
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.2",
|
||||
"description": "基于 Promise 的 HTTP 请求库,适用于各大小程序平台。",
|
||||
"main": "package/index.js",
|
||||
"miniprogram": "package",
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-17 12:18:25
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-22 09:37:49
|
||||
* @LastEditTime: 2020-04-22 13:24:28
|
||||
*/
|
||||
import { Adapter, Platform } from './types';
|
||||
|
||||
|
@ -34,21 +34,13 @@ const stack = [
|
|||
function adaptive(): Adapter | undefined {
|
||||
let adapter: Adapter | undefined;
|
||||
|
||||
const platform = stack.shift();
|
||||
|
||||
if (platform === undefined) {
|
||||
return;
|
||||
while (stack.length !== 0 && adapter === undefined) {
|
||||
try {
|
||||
adapter = (stack.shift() as () => Adapter | undefined)();
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
try {
|
||||
adapter = platform();
|
||||
} catch (err) {}
|
||||
|
||||
if (adapter !== undefined) {
|
||||
return adapter;
|
||||
}
|
||||
|
||||
return adaptive();
|
||||
return adapter;
|
||||
}
|
||||
|
||||
export default adaptive;
|
||||
|
|
18
src/axios.ts
18
src/axios.ts
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-15 12:45:18
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-21 14:38:43
|
||||
* @LastEditTime: 2020-04-22 16:07:56
|
||||
*/
|
||||
import { AxiosRequestConfig, Data, AxiosResponse, AxiosBaseInstance, AxiosInstance } from './types';
|
||||
import Axios from './core/Axios';
|
||||
|
@ -78,3 +78,19 @@ axios.CancelToken = CancelToken;
|
|||
axios.isCancel = isCancel;
|
||||
|
||||
export default axios;
|
||||
|
||||
// axios
|
||||
// .extractData<{}>(
|
||||
// axios.get(
|
||||
// '/test',
|
||||
// {
|
||||
// id: 1,
|
||||
// },
|
||||
// {
|
||||
// headers: { aaa: 'aaa' },
|
||||
// }
|
||||
// )
|
||||
// )
|
||||
// .then((data) => {
|
||||
// console.log(data);
|
||||
// });
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-13 18:00:27
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-21 20:19:49
|
||||
* @LastEditTime: 2020-04-22 15:59:22
|
||||
*/
|
||||
import { Method, Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types';
|
||||
import buildURL from '../helpers/buildURL';
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
/*
|
||||
* @Author: early-autumn
|
||||
* @Date: 2020-04-22 15:59:25
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-22 15:59:42
|
||||
*/
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-16 00:48:45
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-21 09:49:45
|
||||
* @LastEditTime: 2020-04-22 16:02:22
|
||||
*/
|
||||
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
||||
import transformRequest from './transformRequest';
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-17 14:09:16
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-20 09:05:01
|
||||
* @LastEditTime: 2020-04-22 13:06:27
|
||||
*/
|
||||
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
||||
import { pick } from '../helpers/utils';
|
||||
|
@ -20,13 +20,12 @@ export default function transformResponse(response: Response, config: AxiosReque
|
|||
const status = response.statusCode ?? response.status ?? 400;
|
||||
const headers = response.header ?? response.headers ?? {};
|
||||
const statusText = status === 200 ? 'OK' : status === 400 ? 'Bad Adapter' : '';
|
||||
const pickResponse = pick<Response, 'data' | 'cookies' | 'profile'>(response, 'data', 'cookies', 'profile');
|
||||
|
||||
return {
|
||||
status,
|
||||
statusText,
|
||||
headers,
|
||||
config,
|
||||
...pickResponse,
|
||||
...pick<Response, 'data' | 'cookies' | 'profile'>(response, 'data', 'cookies', 'profile'),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-13 21:45:45
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-20 21:21:23
|
||||
* @LastEditTime: 2020-04-22 15:51:28
|
||||
*/
|
||||
import { AnyObject, Params } from '../types';
|
||||
import { encode, isPlainObject, isDate } from './utils';
|
||||
|
@ -16,6 +16,7 @@ import { encode, isPlainObject, isDate } from './utils';
|
|||
function generateURL(url: string, serializedParams: string): string {
|
||||
// 移除 hash
|
||||
const hashIndex = url.indexOf('#');
|
||||
|
||||
if (hashIndex !== -1) {
|
||||
url = url.slice(0, hashIndex);
|
||||
}
|
||||
|
@ -26,9 +27,10 @@ function generateURL(url: string, serializedParams: string): string {
|
|||
|
||||
// 拼接前缀
|
||||
const prefix = url.indexOf('?') === -1 ? '?' : '&';
|
||||
|
||||
serializedParams = `${prefix}${serializedParams}`;
|
||||
|
||||
return `${url.replace(/\/*$/, '')}${serializedParams}`;
|
||||
return `${url}${serializedParams}`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,7 +41,7 @@ function generateURL(url: string, serializedParams: string): string {
|
|||
function paramsSerialization(params: AnyObject): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
Object.entries(params).forEach(([key, value]): void => {
|
||||
Object.entries(params).forEach(function encodeKeyValue([key, value]): void {
|
||||
if (value === null || value === undefined || value !== value) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-13 21:55:40
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-20 10:24:00
|
||||
* @LastEditTime: 2020-04-22 13:02:06
|
||||
*/
|
||||
import { AnyObject } from '../types';
|
||||
|
||||
|
@ -68,8 +68,10 @@ export function deepMerge(...objs: Record<string, any>[]): Record<string, any> {
|
|||
}
|
||||
}
|
||||
|
||||
objs.forEach((obj: Record<string, any>): void => {
|
||||
Object.entries(obj).forEach(([key, value]) => assignValue(key, value));
|
||||
objs.forEach(function assignObj(obj: Record<string, any>): void {
|
||||
Object.entries(obj).forEach(function assignKey([key, value]) {
|
||||
assignValue(key, value);
|
||||
});
|
||||
});
|
||||
|
||||
return result;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-14 23:22:52
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-19 02:31:59
|
||||
* @LastEditTime: 2020-04-22 16:42:48
|
||||
*/
|
||||
import axios from './axios';
|
||||
|
||||
|
|
63
src/types.ts
63
src/types.ts
|
@ -2,8 +2,11 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-13 15:23:53
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-21 14:38:31
|
||||
* @LastEditTime: 2020-04-22 16:08:07
|
||||
*/
|
||||
|
||||
import axios from './axios';
|
||||
|
||||
/**
|
||||
* 任意值对象
|
||||
*/
|
||||
|
@ -37,7 +40,7 @@ export declare type Data = string | AnyObject | ArrayBuffer;
|
|||
/**
|
||||
* Axios 头
|
||||
*/
|
||||
export interface Headers {
|
||||
export declare interface Headers {
|
||||
/**
|
||||
* 通用配置
|
||||
*/
|
||||
|
@ -92,7 +95,7 @@ export interface Headers {
|
|||
/**
|
||||
* 通用请求配置
|
||||
*/
|
||||
export interface RequestConfig {
|
||||
export declare interface RequestConfig {
|
||||
/**
|
||||
* 接口地址
|
||||
*/
|
||||
|
@ -157,7 +160,7 @@ export interface RequestConfig {
|
|||
/**
|
||||
* 通用响应体
|
||||
*/
|
||||
export interface Response {
|
||||
export declare interface Response {
|
||||
/**
|
||||
* 响应状态码
|
||||
*/
|
||||
|
@ -197,7 +200,7 @@ export interface Response {
|
|||
/**
|
||||
* 适配器请求配置
|
||||
*/
|
||||
export interface AdapterRequestConfig extends RequestConfig {
|
||||
export declare interface AdapterRequestConfig extends RequestConfig {
|
||||
/**
|
||||
* 成功的响应函数
|
||||
*/
|
||||
|
@ -212,7 +215,7 @@ export interface AdapterRequestConfig extends RequestConfig {
|
|||
/**
|
||||
* 适配器请求任务
|
||||
*/
|
||||
export interface AdapterRequestTask {
|
||||
export declare interface AdapterRequestTask {
|
||||
/**
|
||||
* 取消请求
|
||||
*/
|
||||
|
@ -222,28 +225,28 @@ export interface AdapterRequestTask {
|
|||
/**
|
||||
* 适配器
|
||||
*/
|
||||
export interface Adapter {
|
||||
export declare interface Adapter {
|
||||
(config: AdapterRequestConfig): AdapterRequestTask | void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台
|
||||
*/
|
||||
export interface Platform {
|
||||
export declare interface Platform {
|
||||
request: Adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换数据函数
|
||||
*/
|
||||
export interface TransformData {
|
||||
export declare interface TransformData {
|
||||
(data: Data, headers: Headers): Data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误处理程序
|
||||
*/
|
||||
export interface ErrorHandler {
|
||||
export declare interface ErrorHandler {
|
||||
(error: any): Promise<any> | any;
|
||||
}
|
||||
|
||||
|
@ -355,7 +358,7 @@ export declare interface AxiosRequestConfig {
|
|||
/**
|
||||
* Axios 响应体
|
||||
*/
|
||||
export interface AxiosResponse<T extends Data = Data> {
|
||||
export declare interface AxiosResponse<T extends Data = Data> {
|
||||
/**
|
||||
* 状态码
|
||||
*/
|
||||
|
@ -395,21 +398,21 @@ export interface AxiosResponse<T extends Data = Data> {
|
|||
/**
|
||||
* 拦截器成功的回调函数
|
||||
*/
|
||||
export interface InterceptorResolved<T = any> {
|
||||
export declare interface InterceptorResolved<T = any> {
|
||||
(value: T): Promise<T> | T;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截器失败的回调函数
|
||||
*/
|
||||
export interface InterceptorRejected {
|
||||
export declare interface InterceptorRejected {
|
||||
(error: any): Promise<any> | any;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截器
|
||||
*/
|
||||
export interface Interceptor<T = any> {
|
||||
export declare interface Interceptor<T = any> {
|
||||
/**
|
||||
* 拦截器成功的回调函数
|
||||
*/
|
||||
|
@ -423,14 +426,14 @@ export interface Interceptor<T = any> {
|
|||
/**
|
||||
* 拦截器执行器
|
||||
*/
|
||||
export interface InterceptorExecutor<T = any> {
|
||||
export declare interface InterceptorExecutor<T = any> {
|
||||
(interceptor: Interceptor<T>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截器管理器
|
||||
*/
|
||||
export interface InterceptorManager<T = any> {
|
||||
export declare interface InterceptorManager<T = any> {
|
||||
/**
|
||||
* 添加拦截器
|
||||
*
|
||||
|
@ -458,7 +461,7 @@ export interface InterceptorManager<T = any> {
|
|||
/**
|
||||
* Axios 拦截器
|
||||
*/
|
||||
export interface Interceptors {
|
||||
export declare interface Interceptors {
|
||||
/**
|
||||
* request 请求前置拦截器
|
||||
*/
|
||||
|
@ -473,7 +476,7 @@ export interface Interceptors {
|
|||
/**
|
||||
* Axios 实例
|
||||
*/
|
||||
export interface Axios {
|
||||
export declare interface Axios {
|
||||
/**
|
||||
* 默认配置
|
||||
*/
|
||||
|
@ -571,14 +574,14 @@ export interface Axios {
|
|||
/**
|
||||
* Axios 类接口
|
||||
*/
|
||||
export interface AxiosConstructor {
|
||||
export declare interface AxiosConstructor {
|
||||
new (config: AxiosRequestConfig): Axios;
|
||||
}
|
||||
|
||||
/**
|
||||
* AxiosError 类继承自 Error
|
||||
*/
|
||||
export interface AxiosError extends Error {
|
||||
export declare interface AxiosError extends Error {
|
||||
/**
|
||||
* 是 Axios 错误
|
||||
*/
|
||||
|
@ -603,7 +606,7 @@ export interface AxiosError extends Error {
|
|||
/**
|
||||
* 取消请求
|
||||
*/
|
||||
export interface Cancel {
|
||||
export declare interface Cancel {
|
||||
/**
|
||||
* 取消信息
|
||||
*/
|
||||
|
@ -618,28 +621,28 @@ export interface Cancel {
|
|||
/**
|
||||
* 取消请求类接口
|
||||
*/
|
||||
export interface CancelConstructor {
|
||||
export declare interface CancelConstructor {
|
||||
new (message?: string): Cancel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消操作
|
||||
*/
|
||||
export interface CancelAction {
|
||||
export declare interface CancelAction {
|
||||
(message?: string): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消操作执行器
|
||||
*/
|
||||
export interface CancelExecutor {
|
||||
export declare interface CancelExecutor {
|
||||
(cancel: CancelAction): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消令牌
|
||||
*/
|
||||
export interface CancelToken {
|
||||
export declare interface CancelToken {
|
||||
/**
|
||||
* 取消请求
|
||||
*/
|
||||
|
@ -659,7 +662,7 @@ export interface CancelToken {
|
|||
/**
|
||||
* 取消令牌 source
|
||||
*/
|
||||
export interface CancelTokenSource {
|
||||
export declare interface CancelTokenSource {
|
||||
/**
|
||||
* 取消令牌
|
||||
*/
|
||||
|
@ -674,7 +677,7 @@ export interface CancelTokenSource {
|
|||
/**
|
||||
* 取消令牌类接口
|
||||
*/
|
||||
export interface CancelTokenConstructor {
|
||||
export declare interface CancelTokenConstructor {
|
||||
new (executor: CancelExecutor): CancelToken;
|
||||
|
||||
/**
|
||||
|
@ -696,7 +699,7 @@ export interface CancelTokenConstructor {
|
|||
*
|
||||
* * 支持两种函数调用方式
|
||||
*/
|
||||
export interface AxiosBaseInstance extends Axios {
|
||||
export declare interface AxiosBaseInstance extends Axios {
|
||||
/**
|
||||
* 调用方式一
|
||||
*
|
||||
|
@ -720,7 +723,7 @@ export interface AxiosBaseInstance extends Axios {
|
|||
*
|
||||
* * 同时拓展了一些静态属性和方法
|
||||
*/
|
||||
export interface AxiosInstance extends AxiosBaseInstance {
|
||||
export declare interface AxiosInstance extends AxiosBaseInstance {
|
||||
/**
|
||||
* 创建 Axios 实例基础增强
|
||||
*
|
||||
|
@ -744,4 +747,6 @@ export interface AxiosInstance extends AxiosBaseInstance {
|
|||
* @param value 判断的值
|
||||
*/
|
||||
isCancel: (value: any) => boolean;
|
||||
|
||||
// extractData: <T extends Data>(response: Promise<AxiosResponse<T>>) => Promise<T>;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-20 20:47:09
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-21 09:34:14
|
||||
* @LastEditTime: 2020-04-22 16:04:16
|
||||
*/
|
||||
import Axios from '../../src/core/Axios';
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* @Author: early-autumn
|
||||
* @Date: 2020-04-20 21:08:23
|
||||
* @LastEditors: early-autumn
|
||||
* @LastEditTime: 2020-04-20 21:17:31
|
||||
* @LastEditTime: 2020-04-22 15:51:48
|
||||
*/
|
||||
import transformRequest from '../../src/core/transformRequest';
|
||||
|
||||
|
@ -36,7 +36,7 @@ describe('测试 src/core/transformRequest.ts', () => {
|
|||
const request2 = transformRequest({
|
||||
baseURL: 'https://www.xxx.com',
|
||||
method: 'get',
|
||||
url: 'https://www.yyy.com/test/',
|
||||
url: 'https://www.yyy.com/test',
|
||||
params: {
|
||||
id: 1,
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue