no message
parent
50ccebec4a
commit
c487d7ce37
|
@ -0,0 +1,2 @@
|
||||||
|
# Default ignored files
|
||||||
|
/workspace.xml
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="JavaScriptSettings">
|
||||||
|
<option name="languageLevel" value="ES6" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/axios-miniprogram.iml" filepath="$PROJECT_DIR$/.idea/axios-miniprogram.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
12
README.md
12
README.md
|
@ -259,9 +259,9 @@ axios('/test', {
|
||||||
```typescript
|
```typescript
|
||||||
axios.defaults.adapter = function adapter(adapterConfig) {
|
axios.defaults.adapter = function adapter(adapterConfig) {
|
||||||
const {
|
const {
|
||||||
// 接口地址
|
// 请求地址
|
||||||
url,
|
url,
|
||||||
// HTTP 请求方法
|
// 请求方法
|
||||||
method,
|
method,
|
||||||
// 请求数据
|
// 请求数据
|
||||||
data,
|
data,
|
||||||
|
@ -269,9 +269,9 @@ axios.defaults.adapter = function adapter(adapterConfig) {
|
||||||
header,
|
header,
|
||||||
// 请求头 同 header
|
// 请求头 同 header
|
||||||
headers,
|
headers,
|
||||||
// 返回的数据格式
|
// 响应数据格式
|
||||||
dataType,
|
dataType,
|
||||||
// 响应的数据类型
|
// 响应数据类型
|
||||||
responseType,
|
responseType,
|
||||||
// 超时时间
|
// 超时时间
|
||||||
timeout,
|
timeout,
|
||||||
|
@ -283,9 +283,9 @@ axios.defaults.adapter = function adapter(adapterConfig) {
|
||||||
enableCache,
|
enableCache,
|
||||||
// 验证 ssl 证书
|
// 验证 ssl 证书
|
||||||
sslVerify,
|
sslVerify,
|
||||||
// 成功的响应函数
|
// 成功的回调函数
|
||||||
success,
|
success,
|
||||||
// 失败的响应函数
|
// 失败的回调函数
|
||||||
fail
|
fail
|
||||||
} = adapterConfig;
|
} = adapterConfig;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-03-06 20:40:30
|
* @Date: 2020-03-06 20:40:30
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-22 09:50:55
|
* @LastEditTime: 2020-04-28 13:18:34
|
||||||
*/
|
*/
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-17 12:18:25
|
* @Date: 2020-04-17 12:18:25
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-24 09:30:33
|
* @LastEditTime: 2020-04-25 19:10:04
|
||||||
*/
|
*/
|
||||||
import { Adapter, Platform } from './types';
|
import { Adapter, Platform } from './types';
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ declare let qq: Platform;
|
||||||
/**
|
/**
|
||||||
* 自适应当前平台
|
* 自适应当前平台
|
||||||
*/
|
*/
|
||||||
function adaptive(): Adapter | undefined {
|
export default function adaptive(): Adapter | undefined {
|
||||||
const stack = [
|
const stack = [
|
||||||
() => uni.request,
|
() => uni.request,
|
||||||
() => wx.request,
|
() => wx.request,
|
||||||
|
@ -42,5 +42,3 @@ function adaptive(): Adapter | undefined {
|
||||||
|
|
||||||
return adapter;
|
return adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default adaptive;
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 21:14:53
|
* @Date: 2020-04-13 21:14:53
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-23 20:56:34
|
* @LastEditTime: 2020-04-25 09:26:10
|
||||||
*/
|
*/
|
||||||
import { Cancel } from '../types';
|
import { Cancel } from '../types';
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ export default class CancelStatic implements Cancel {
|
||||||
/**
|
/**
|
||||||
* @param message 取消信息
|
* @param message 取消信息
|
||||||
*/
|
*/
|
||||||
constructor(public message?: string) {}
|
public constructor(public message?: string) {}
|
||||||
|
|
||||||
public toString() {
|
public toString() {
|
||||||
const message = this.message ? `: ${this.message}` : '';
|
const message = this.message ? `: ${this.message}` : '';
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 20:00:08
|
* @Date: 2020-04-13 20:00:08
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-22 17:39:44
|
* @LastEditTime: 2020-04-25 09:22:56
|
||||||
*/
|
*/
|
||||||
import { CancelToken, CancelAction, CancelExecutor, CancelTokenSource } from '../types';
|
import { CancelToken, CancelAction, CancelExecutor, CancelTokenSource } from '../types';
|
||||||
import Cancel from './Cancel';
|
import Cancel from './Cancel';
|
||||||
|
@ -15,7 +15,7 @@ export default class CancelTokenStatic implements CancelToken {
|
||||||
|
|
||||||
public listener: Promise<Cancel>;
|
public listener: Promise<Cancel>;
|
||||||
|
|
||||||
constructor(executor: CancelExecutor) {
|
public constructor(executor: CancelExecutor) {
|
||||||
let action!: CancelAction;
|
let action!: CancelAction;
|
||||||
|
|
||||||
this.listener = new Promise<Cancel>((resolve) => {
|
this.listener = new Promise<Cancel>((resolve) => {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 18:00:27
|
* @Date: 2020-04-13 18:00:27
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-23 23:43:00
|
* @LastEditTime: 2020-04-25 09:23:14
|
||||||
*/
|
*/
|
||||||
import { Method, Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types';
|
import { Method, Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types';
|
||||||
import buildURL from '../helpers/buildURL';
|
import buildURL from '../helpers/buildURL';
|
||||||
|
@ -22,7 +22,7 @@ export default class AxiosStatic implements Axios {
|
||||||
/**
|
/**
|
||||||
* @param defaults 自定义默认配置
|
* @param defaults 自定义默认配置
|
||||||
*/
|
*/
|
||||||
constructor(public defaults: AxiosRequestConfig = {}) {}
|
public constructor(public defaults: AxiosRequestConfig = {}) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据配置中的 url 和 params 生成一个 URI
|
* 根据配置中的 url 和 params 生成一个 URI
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-15 17:50:50
|
* @Date: 2020-04-15 17:50:50
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-23 20:54:24
|
* @LastEditTime: 2020-05-02 14:30:56
|
||||||
*/
|
*/
|
||||||
import {
|
import {
|
||||||
InterceptorResolved,
|
InterceptorResolved,
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-14 22:23:39
|
* @Date: 2020-04-14 22:23:39
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-23 23:47:26
|
* @LastEditTime: 2020-04-25 09:23:40
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, RequestConfig, AxiosResponse } from '../types';
|
import { AxiosRequestConfig, RequestConfig, AxiosResponse } from '../types';
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ class AxiosError extends Error {
|
||||||
* @param request 通用请求配置
|
* @param request 通用请求配置
|
||||||
* @param response Axios 响应体
|
* @param response Axios 响应体
|
||||||
*/
|
*/
|
||||||
constructor(
|
public constructor(
|
||||||
message: string,
|
message: string,
|
||||||
public config: AxiosRequestConfig,
|
public config: AxiosRequestConfig,
|
||||||
public request: RequestConfig,
|
public request: RequestConfig,
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 18:01:16
|
* @Date: 2020-04-13 18:01:16
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-23 09:31:00
|
* @LastEditTime: 2020-04-25 09:24:20
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, AxiosResponse } from '../types';
|
import { AxiosRequestConfig, AxiosResponse } from '../types';
|
||||||
import isCancel from '../cancel/isCancel';
|
import isCancel from '../cancel/isCancel';
|
||||||
|
@ -50,7 +50,7 @@ export default function dispatchRequest(config: AxiosRequestConfig): Promise<Axi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.errorHandler !== undefined ? config.errorHandler(reason) : Promise.reject(reason);
|
return config.errorHandler?.(reason) ?? Promise.reject(reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
return request(config).then(onResolved, onRejected);
|
return request(config).then(onResolved, onRejected);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-16 00:48:45
|
* @Date: 2020-04-16 00:48:45
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-22 16:02:22
|
* @LastEditTime: 2020-05-02 14:33:49
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
||||||
import transformRequest from './transformRequest';
|
import transformRequest from './transformRequest';
|
||||||
|
@ -17,7 +17,7 @@ import createError from './createError';
|
||||||
export default function request(config: AxiosRequestConfig): Promise<AxiosResponse> {
|
export default function request(config: AxiosRequestConfig): Promise<AxiosResponse> {
|
||||||
return new Promise(function dispatchAdapter(resolve, reject): void {
|
return new Promise(function dispatchAdapter(resolve, reject): void {
|
||||||
const { adapter, cancelToken } = config;
|
const { adapter, cancelToken } = config;
|
||||||
const request = transformRequest(config);
|
const requestConfig = transformRequest(config);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 捕获错误
|
* 捕获错误
|
||||||
|
@ -30,7 +30,7 @@ export default function request(config: AxiosRequestConfig): Promise<AxiosRespon
|
||||||
message = '配置不正确或者网络异常';
|
message = '配置不正确或者网络异常';
|
||||||
}
|
}
|
||||||
|
|
||||||
reject(createError(message, config, request, response));
|
reject(createError(message, config, requestConfig, response));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (adapter === undefined) {
|
if (adapter === undefined) {
|
||||||
|
@ -56,7 +56,7 @@ export default function request(config: AxiosRequestConfig): Promise<AxiosRespon
|
||||||
|
|
||||||
// 使用适配器发送请求
|
// 使用适配器发送请求
|
||||||
const task = adapter({
|
const task = adapter({
|
||||||
...request,
|
...requestConfig,
|
||||||
success: handleResponse,
|
success: handleResponse,
|
||||||
fail: catchError,
|
fail: catchError,
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,16 +2,16 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-16 22:37:44
|
* @Date: 2020-04-16 22:37:44
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-23 23:09:24
|
* @LastEditTime: 2020-04-25 09:30:42
|
||||||
*/
|
*/
|
||||||
import { Data, Headers, TransformData } from '../types';
|
import { Data, Headers, TransformData } from '../types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换数据
|
* 转换数据
|
||||||
*
|
*
|
||||||
* @param data 请求数据或响应数据
|
* @param data 请求数据/响应数据
|
||||||
* @param headers 请求头或响应头
|
* @param headers 请求头/响应头
|
||||||
* @param transforms 请求数据转换函数或响应数据转换函数
|
* @param transforms 请求数据转换函数/响应数据转换函数
|
||||||
*/
|
*/
|
||||||
export default function transformData(data: Data, headers: Headers, transforms?: TransformData | TransformData[]) {
|
export default function transformData(data: Data, headers: Headers, transforms?: TransformData | TransformData[]) {
|
||||||
if (transforms === undefined) {
|
if (transforms === undefined) {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-17 15:05:43
|
* @Date: 2020-04-17 15:05:43
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-24 08:53:57
|
* @LastEditTime: 2020-04-25 09:27:30
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, RequestConfig } from '../types';
|
import { AxiosRequestConfig, RequestConfig } from '../types';
|
||||||
import { pick } from '../helpers/utils';
|
import { pick } from '../helpers/utils';
|
||||||
|
@ -33,8 +33,11 @@ function transformURL(config: AxiosRequestConfig): string {
|
||||||
export default function transformRequest(config: AxiosRequestConfig): RequestConfig {
|
export default function transformRequest(config: AxiosRequestConfig): RequestConfig {
|
||||||
return {
|
return {
|
||||||
url: transformURL(config),
|
url: transformURL(config),
|
||||||
|
|
||||||
method: methodToUppercase(config.method),
|
method: methodToUppercase(config.method),
|
||||||
|
|
||||||
header: config.headers,
|
header: config.headers,
|
||||||
|
|
||||||
...pick(
|
...pick(
|
||||||
config,
|
config,
|
||||||
'data',
|
'data',
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-17 14:09:16
|
* @Date: 2020-04-17 14:09:16
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-24 08:54:42
|
* @LastEditTime: 2020-04-25 09:29:50
|
||||||
*/
|
*/
|
||||||
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
||||||
import { pick } from '../helpers/utils';
|
import { pick } from '../helpers/utils';
|
||||||
|
@ -13,12 +13,13 @@ import { pick } from '../helpers/utils';
|
||||||
* 抹平差异
|
* 抹平差异
|
||||||
*
|
*
|
||||||
* @param response 通用响应体
|
* @param response 通用响应体
|
||||||
* @param request 通用请求配置
|
|
||||||
* @param config Axios 请求配置
|
* @param config Axios 请求配置
|
||||||
*/
|
*/
|
||||||
export default function transformResponse(response: Response, config: AxiosRequestConfig): AxiosResponse {
|
export default function transformResponse(response: Response, config: AxiosRequestConfig): AxiosResponse {
|
||||||
const status = response.statusCode ?? response.status ?? 400;
|
const status = response.statusCode ?? response.status ?? 400;
|
||||||
|
|
||||||
const headers = response.header ?? response.headers ?? {};
|
const headers = response.header ?? response.headers ?? {};
|
||||||
|
|
||||||
const statusText = status === 200 ? 'OK' : status === 400 ? 'Bad Adapter' : '';
|
const statusText = status === 200 ? 'OK' : status === 400 ? 'Bad Adapter' : '';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-14 23:22:52
|
* @Date: 2020-04-14 23:22:52
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-23 10:31:27
|
* @LastEditTime: 2020-04-28 13:06:14
|
||||||
*/
|
*/
|
||||||
import axios from './axios';
|
import axios from './axios';
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* @Author: early-autumn
|
* @Author: early-autumn
|
||||||
* @Date: 2020-04-13 15:23:53
|
* @Date: 2020-04-13 15:23:53
|
||||||
* @LastEditors: early-autumn
|
* @LastEditors: early-autumn
|
||||||
* @LastEditTime: 2020-04-24 09:00:02
|
* @LastEditTime: 2020-05-02 14:37:02
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -181,7 +181,7 @@ export declare interface AdapterRequestTask {
|
||||||
* 适配器
|
* 适配器
|
||||||
*/
|
*/
|
||||||
export declare interface Adapter {
|
export declare interface Adapter {
|
||||||
(config: AdapterRequestConfig): AdapterRequestTask | void;
|
(config: AdapterRequestConfig): AdapterRequestTask | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -419,12 +419,12 @@ export declare interface InterceptorManager<T = any> {
|
||||||
*/
|
*/
|
||||||
export declare interface Interceptors {
|
export declare interface Interceptors {
|
||||||
/**
|
/**
|
||||||
* request 请求前置拦截器
|
* request 请求拦截器
|
||||||
*/
|
*/
|
||||||
request: InterceptorManager<AxiosRequestConfig>;
|
request: InterceptorManager<AxiosRequestConfig>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* response 请求后置拦截器
|
* response 响应拦截器
|
||||||
*/
|
*/
|
||||||
response: InterceptorManager<AxiosResponse>;
|
response: InterceptorManager<AxiosResponse>;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue