From b3cb995c1cc002a9e9f1178ae65a636c24bcd1e9 Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Mon, 27 Jul 2020 13:05:12 +0800 Subject: [PATCH] devops --- README.md | 1 + package.json | 2 +- src/core/Axios.ts | 55 +++++++++++++++++++++++++++++------ src/core/dispatchRequest.ts | 6 +++- src/core/flattenHeaders.ts | 13 ++++++++- src/core/mergeConfig.ts | 6 +++- src/core/transformResponse.ts | 5 +++- src/helpers/buildURL.ts | 6 +++- src/types.ts | 53 ++++++++++++++++++++++++++++----- 9 files changed, 124 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8892a6f..9f4b09c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # axios-miniprogram [![build status](https://travis-ci.com/zjx0905/axios-miniprogram.svg?branch=master)](https://travis-ci.org/zjx0905/axios-miniprogram) +[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) [![Coverage Status](https://coveralls.io/repos/github/zjx0905/axios-miniprogram/badge.svg?branch=master)](https://coveralls.io/github/zjx0905/axios-miniprogram?branch=master) [![npm version](https://badge.fury.io/js/axios-miniprogram.svg)](https://badge.fury.io/js/axios-miniprogram) [![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT) diff --git a/package.json b/package.json index 953034d..d1750a6 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "build": "rollup -c", "lint": "eslint 'src/**/*.{js,ts,tsx}'", "format": "eslint --fix 'src/**/*.{js,ts,tsx}'", - "prettier": "prettier", + "prettier": "prettier -c --write 'src/**/*.{js,ts,tsx}'", "test": "jest", "test:watch": "yarn test -- --watch", "test:cov": "yarn test -- --coverage", diff --git a/src/core/Axios.ts b/src/core/Axios.ts index 17cc9ae..805fc89 100644 --- a/src/core/Axios.ts +++ b/src/core/Axios.ts @@ -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 mergeConfig from './mergeConfig'; import InterceptorManager from './InterceptorManager'; @@ -42,35 +50,64 @@ export default class AxiosClass implements Axios { return promiseResponse as Promise>; } - public options(url: string, config?: AxiosRequestConfig): Promise> { + public options( + url: string, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutParams('options', url, void 0, config); } - public get(url: string, params?: Params, config?: AxiosRequestConfig): Promise> { + public get( + url: string, + params?: Params, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutParams('get', url, params, config); } - public head(url: string, params?: Params, config?: AxiosRequestConfig): Promise> { + public head( + url: string, + params?: Params, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutParams('head', url, params, config); } - public post(url: string, data?: Data, config?: AxiosRequestConfig): Promise> { + public post( + url: string, + data?: Data, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutData('post', url, data, config); } - public put(url: string, data?: Data, config?: AxiosRequestConfig): Promise> { + public put( + url: string, + data?: Data, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutData('put', url, data, config); } - public delete(url: string, params?: Params, config?: AxiosRequestConfig): Promise> { + public delete( + url: string, + params?: Params, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutParams('delete', url, params, config); } - public trace(url: string, config?: AxiosRequestConfig): Promise> { + public trace( + url: string, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutParams('trace', url, void 0, config); } - public connect(url: string, config?: AxiosRequestConfig): Promise> { + public connect( + url: string, + config?: AxiosRequestConfig + ): Promise> { return this._requestMethodWithoutParams('connect', url, void 0, config); } diff --git a/src/core/dispatchRequest.ts b/src/core/dispatchRequest.ts index 54c2357..0e2b8be 100644 --- a/src/core/dispatchRequest.ts +++ b/src/core/dispatchRequest.ts @@ -40,7 +40,11 @@ export default function dispatchRequest(config: AxiosRequestConfig): Promise { if (config2[key] !== void 0) { config[key] = config2[key] as any; diff --git a/src/core/transformResponse.ts b/src/core/transformResponse.ts index 90c9208..0a81505 100644 --- a/src/core/transformResponse.ts +++ b/src/core/transformResponse.ts @@ -9,7 +9,10 @@ import { pick } from '../helpers/utils'; * @param response 通用响应体 * @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 headers = response.header ?? response.headers ?? {}; diff --git a/src/helpers/buildURL.ts b/src/helpers/buildURL.ts index 65cc703..9b5a162 100644 --- a/src/helpers/buildURL.ts +++ b/src/helpers/buildURL.ts @@ -69,6 +69,10 @@ function paramsSerialization(params: AnyObject): string { * @param params 请求参数 * @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)); } diff --git a/src/types.ts b/src/types.ts index 24d376f..ce1f7de 100644 --- a/src/types.ts +++ b/src/types.ts @@ -8,12 +8,28 @@ export declare interface AnyObject { /** * 请求方法 */ -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 请求方法 @@ -33,7 +49,8 @@ export declare type Data = string | AnyObject | ArrayBuffer; /** * Axios 头 */ -export declare interface Headers extends Partial>> { +export declare interface Headers + extends Partial>> { /** * 自定义配置 */ @@ -466,7 +483,11 @@ export declare interface Axios { * @param params 请求参数 * @param config 额外配置 */ - get(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; + get( + url: string, + params?: Params, + config?: AxiosRequestConfig + ): Promise>; /** * 发送 HTTP 请求 HEAD @@ -475,7 +496,11 @@ export declare interface Axios { * @param params 请求参数 * @param config 额外配置 */ - head(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; + head( + url: string, + params?: Params, + config?: AxiosRequestConfig + ): Promise>; /** * 发送 HTTP 请求 POST @@ -484,7 +509,11 @@ export declare interface Axios { * @param data 请求数据 * @param config 额外配置 */ - post(url: string, data?: Data, config?: AxiosRequestConfig): Promise>; + post( + url: string, + data?: Data, + config?: AxiosRequestConfig + ): Promise>; /** * 发送 HTTP 请求 PUT @@ -493,7 +522,11 @@ export declare interface Axios { * @param data 请求数据 * @param config 额外配置 */ - put(url: string, data?: Data, config?: AxiosRequestConfig): Promise>; + put( + url: string, + data?: Data, + config?: AxiosRequestConfig + ): Promise>; /** * 发送 HTTP 请求 DELETE @@ -502,7 +535,11 @@ export declare interface Axios { * @param params 请求参数 * @param config 额外配置 */ - delete(url: string, params?: Params, config?: AxiosRequestConfig): Promise>; + delete( + url: string, + params?: Params, + config?: AxiosRequestConfig + ): Promise>; /** * 发送 HTTP 请求 TRACE