devops
parent
7353418fee
commit
b3cb995c1c
|
@ -1,6 +1,7 @@
|
||||||
# axios-miniprogram
|
# axios-miniprogram
|
||||||
|
|
||||||
[](https://travis-ci.org/zjx0905/axios-miniprogram)
|
[](https://travis-ci.org/zjx0905/axios-miniprogram)
|
||||||
|
[](https://github.com/prettier/prettier)
|
||||||
[](https://coveralls.io/github/zjx0905/axios-miniprogram?branch=master)
|
[](https://coveralls.io/github/zjx0905/axios-miniprogram?branch=master)
|
||||||
[](https://badge.fury.io/js/axios-miniprogram)
|
[](https://badge.fury.io/js/axios-miniprogram)
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
"build": "rollup -c",
|
"build": "rollup -c",
|
||||||
"lint": "eslint 'src/**/*.{js,ts,tsx}'",
|
"lint": "eslint 'src/**/*.{js,ts,tsx}'",
|
||||||
"format": "eslint --fix 'src/**/*.{js,ts,tsx}'",
|
"format": "eslint --fix 'src/**/*.{js,ts,tsx}'",
|
||||||
"prettier": "prettier",
|
"prettier": "prettier -c --write 'src/**/*.{js,ts,tsx}'",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"test:watch": "yarn test -- --watch",
|
"test:watch": "yarn test -- --watch",
|
||||||
"test:cov": "yarn test -- --coverage",
|
"test:cov": "yarn test -- --coverage",
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
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';
|
||||||
import mergeConfig from './mergeConfig';
|
import mergeConfig from './mergeConfig';
|
||||||
import InterceptorManager from './InterceptorManager';
|
import InterceptorManager from './InterceptorManager';
|
||||||
|
@ -42,35 +50,64 @@ export default class AxiosClass implements Axios {
|
||||||
return promiseResponse as Promise<AxiosResponse<T>>;
|
return promiseResponse as Promise<AxiosResponse<T>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
public options<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public options<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutParams<T>('options', url, void 0, config);
|
return this._requestMethodWithoutParams<T>('options', url, void 0, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public get<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
params?: Params,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutParams<T>('get', url, params, config);
|
return this._requestMethodWithoutParams<T>('get', url, params, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public head<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public head<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
params?: Params,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutParams<T>('head', url, params, config);
|
return this._requestMethodWithoutParams<T>('head', url, params, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public post<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public post<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
data?: Data,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutData<T>('post', url, data, config);
|
return this._requestMethodWithoutData<T>('post', url, data, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public put<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public put<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
data?: Data,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutData<T>('put', url, data, config);
|
return this._requestMethodWithoutData<T>('put', url, data, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public delete<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public delete<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
params?: Params,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutParams<T>('delete', url, params, config);
|
return this._requestMethodWithoutParams<T>('delete', url, params, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public trace<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public trace<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutParams<T>('trace', url, void 0, config);
|
return this._requestMethodWithoutParams<T>('trace', url, void 0, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public connect<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
public connect<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>> {
|
||||||
return this._requestMethodWithoutParams<T>('connect', url, void 0, config);
|
return this._requestMethodWithoutParams<T>('connect', url, void 0, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,11 @@ export default function dispatchRequest(config: AxiosRequestConfig): Promise<Axi
|
||||||
throwIfCancellationRequested(config);
|
throwIfCancellationRequested(config);
|
||||||
|
|
||||||
if (reason.response !== void 0) {
|
if (reason.response !== void 0) {
|
||||||
reason.response.data = transformData(reason.response.data, reason.response.headers, config.transformResponse);
|
reason.response.data = transformData(
|
||||||
|
reason.response.data,
|
||||||
|
reason.response.headers,
|
||||||
|
config.transformResponse
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,17 @@ export default function flattenHeaders(config: AxiosRequestConfig): Headers {
|
||||||
return {
|
return {
|
||||||
...(headers.common ?? {}),
|
...(headers.common ?? {}),
|
||||||
...(headers[method] ?? {}),
|
...(headers[method] ?? {}),
|
||||||
...omit(headers, 'common', 'options', 'get', 'head', 'post', 'put', 'delete', 'trace', 'connect'),
|
...omit(
|
||||||
|
headers,
|
||||||
|
'common',
|
||||||
|
'options',
|
||||||
|
'get',
|
||||||
|
'head',
|
||||||
|
'post',
|
||||||
|
'put',
|
||||||
|
'delete',
|
||||||
|
'trace',
|
||||||
|
'connect'
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,11 @@ type DeepMergeConfigKey = 'params' | 'headers';
|
||||||
* @param config
|
* @param config
|
||||||
* @param config2
|
* @param config2
|
||||||
*/
|
*/
|
||||||
function onlyFromConfig2(keys: OnlyFromConfig2Key[], config: AxiosRequestConfig, config2: AxiosRequestConfig) {
|
function onlyFromConfig2(
|
||||||
|
keys: OnlyFromConfig2Key[],
|
||||||
|
config: AxiosRequestConfig,
|
||||||
|
config2: AxiosRequestConfig
|
||||||
|
) {
|
||||||
keys.forEach((key) => {
|
keys.forEach((key) => {
|
||||||
if (config2[key] !== void 0) {
|
if (config2[key] !== void 0) {
|
||||||
config[key] = config2[key] as any;
|
config[key] = config2[key] as any;
|
||||||
|
|
|
@ -9,7 +9,10 @@ import { pick } from '../helpers/utils';
|
||||||
* @param response 通用响应体
|
* @param response 通用响应体
|
||||||
* @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 ?? {};
|
||||||
|
|
|
@ -69,6 +69,10 @@ function paramsSerialization(params: AnyObject): string {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @param paramsSerializer 自定义参数序列化
|
* @param paramsSerializer 自定义参数序列化
|
||||||
*/
|
*/
|
||||||
export default function buildURL(url: string, params: Params = {}, paramsSerializer = paramsSerialization): string {
|
export default function buildURL(
|
||||||
|
url: string,
|
||||||
|
params: Params = {},
|
||||||
|
paramsSerializer = paramsSerialization
|
||||||
|
): string {
|
||||||
return generateURL(url, paramsSerializer(params));
|
return generateURL(url, paramsSerializer(params));
|
||||||
}
|
}
|
||||||
|
|
53
src/types.ts
53
src/types.ts
|
@ -8,12 +8,28 @@ export declare interface AnyObject<T extends any = any> {
|
||||||
/**
|
/**
|
||||||
* 请求方法
|
* 请求方法
|
||||||
*/
|
*/
|
||||||
export declare type AdapterMethod = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT';
|
export declare type AdapterMethod =
|
||||||
|
| 'OPTIONS'
|
||||||
|
| 'GET'
|
||||||
|
| 'HEAD'
|
||||||
|
| 'POST'
|
||||||
|
| 'PUT'
|
||||||
|
| 'DELETE'
|
||||||
|
| 'TRACE'
|
||||||
|
| 'CONNECT';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求方法别名
|
* 请求方法别名
|
||||||
*/
|
*/
|
||||||
export declare type AliasMethod = 'options' | 'get' | 'head' | 'post' | 'put' | 'delete' | 'trace' | 'connect';
|
export declare type AliasMethod =
|
||||||
|
| 'options'
|
||||||
|
| 'get'
|
||||||
|
| 'head'
|
||||||
|
| 'post'
|
||||||
|
| 'put'
|
||||||
|
| 'delete'
|
||||||
|
| 'trace'
|
||||||
|
| 'connect';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Axios 请求方法
|
* Axios 请求方法
|
||||||
|
@ -33,7 +49,8 @@ export declare type Data = string | AnyObject | ArrayBuffer;
|
||||||
/**
|
/**
|
||||||
* Axios 头
|
* Axios 头
|
||||||
*/
|
*/
|
||||||
export declare interface Headers extends Partial<Record<'common' | AliasMethod, AnyObject<string>>> {
|
export declare interface Headers
|
||||||
|
extends Partial<Record<'common' | AliasMethod, AnyObject<string>>> {
|
||||||
/**
|
/**
|
||||||
* 自定义配置
|
* 自定义配置
|
||||||
*/
|
*/
|
||||||
|
@ -466,7 +483,11 @@ export declare interface Axios {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @param config 额外配置
|
* @param config 额外配置
|
||||||
*/
|
*/
|
||||||
get<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
get<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
params?: Params,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 HTTP 请求 HEAD
|
* 发送 HTTP 请求 HEAD
|
||||||
|
@ -475,7 +496,11 @@ export declare interface Axios {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @param config 额外配置
|
* @param config 额外配置
|
||||||
*/
|
*/
|
||||||
head<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
head<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
params?: Params,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 HTTP 请求 POST
|
* 发送 HTTP 请求 POST
|
||||||
|
@ -484,7 +509,11 @@ export declare interface Axios {
|
||||||
* @param data 请求数据
|
* @param data 请求数据
|
||||||
* @param config 额外配置
|
* @param config 额外配置
|
||||||
*/
|
*/
|
||||||
post<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
post<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
data?: Data,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 HTTP 请求 PUT
|
* 发送 HTTP 请求 PUT
|
||||||
|
@ -493,7 +522,11 @@ export declare interface Axios {
|
||||||
* @param data 请求数据
|
* @param data 请求数据
|
||||||
* @param config 额外配置
|
* @param config 额外配置
|
||||||
*/
|
*/
|
||||||
put<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
put<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
data?: Data,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 HTTP 请求 DELETE
|
* 发送 HTTP 请求 DELETE
|
||||||
|
@ -502,7 +535,11 @@ export declare interface Axios {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @param config 额外配置
|
* @param config 额外配置
|
||||||
*/
|
*/
|
||||||
delete<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
delete<T extends Data>(
|
||||||
|
url: string,
|
||||||
|
params?: Params,
|
||||||
|
config?: AxiosRequestConfig
|
||||||
|
): Promise<AxiosResponse<T>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 HTTP 请求 TRACE
|
* 发送 HTTP 请求 TRACE
|
||||||
|
|
Loading…
Reference in New Issue