Skip to content

Commit

Permalink
feat: v1.6.0 suport customFetch, support array args, add ctx.res.rawD…
Browse files Browse the repository at this point in the history
…ata (#47)

* feat: add customFetch, add ctx.res.rawData

* feat: support array args

* fix(utils): combineUrls will always return a string

* build: update deps, release v1.6.0

* fix(utils): combineUrls will treat null as empty string
  • Loading branch information
BuptStEve authored Jul 21, 2020
1 parent a83712a commit 0b6106b
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 84 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
module.exports = {
extends: 'standard',
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 10,
parser: 'babel-eslint',
},
rules: {
'indent': [2, 4],
indent: [2, 4],
'promise/param-names': 0,
'template-curly-spacing': 'off',
'comma-dangle': [2, 'always-multiline'],
},
globals: {
Expand Down
6 changes: 4 additions & 2 deletions docs/guide/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function (ctx, next) {
return next().then(() => {
// 注意这里才有响应!
ctx.res // 响应对象
ctx.res.data // 响应的数据
ctx.res.data // 响应格式化后的数据
ctx.res.rawData // 响应的原始数据
ctx.reqTime // 请求花费的时间
ctx.endTime // 收到响应的时间
})
Expand Down Expand Up @@ -68,7 +69,8 @@ async function (ctx, next) {
| req.reqFnParams | 发起请求时的参数对象(上面那些参数都会被放进来作为属性) |
| --- | --- |
| res | 响应 |
| res.data | 响应的数据 |
| res.data | 响应格式化后的数据 |
| res.rawData <badge text="1.6.0+" /> | 响应的原始数据 |
| res.error | 错误对象(可以取 stack 和 message) |
| res.* | [透传 axios 的配置](https://github.com/axios/axios#response-schema) |
| --- | --- |
Expand Down
10 changes: 9 additions & 1 deletion examples/apis-web/fake-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default {
name: 'ct',
path: 'custom-transformRequest',
axiosOptions: {
transformRequest: () => `ct`,
transformRequest: () => 'ct',
},
},
/**
Expand All @@ -88,5 +88,13 @@ export default {
name: 'pj',
path: 'post-json',
},
/**
* raw-data
*/
{
name: 'rd',
path: 'raw-data',
afterFn: ([, ctx]) => ctx.res.rawData,
},
],
}
64 changes: 32 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tua-api",
"version": "1.5.0",
"version": "1.6.0",
"description": "🏗 A common tool helps converting configs to api functions",
"main": "dist/TuaApi.cjs.js",
"module": "dist/TuaApi.esm.js",
Expand Down Expand Up @@ -50,42 +50,42 @@
"koa-compose": "^4.1.0"
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/plugin-external-helpers": "^7.8.3",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@types/jest": "^25.1.2",
"all-contributors-cli": "^6.13.0",
"axios-mock-adapter": "^1.17.0",
"@babel/core": "^7.10.5",
"@babel/plugin-external-helpers": "^7.10.4",
"@babel/plugin-proposal-decorators": "^7.10.5",
"@babel/plugin-proposal-object-rest-spread": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^9.1.1",
"@rollup/plugin-babel": "^5.1.0",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-replace": "^2.3.3",
"@types/jest": "^26.0.5",
"all-contributors-cli": "^6.16.1",
"axios-mock-adapter": "^1.18.2",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^25.1.0",
"codecov": "^3.6.5",
"cross-env": "^7.0.0",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-node": "^11.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.1.0",
"codecov": "^3.7.1",
"cross-env": "^7.0.2",
"eslint": "^7.5.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"gh-pages": "^2.2.0",
"husky": "^4.2.3",
"jest": "^25.1.0",
"lint-staged": "^10.0.7",
"gh-pages": "^3.1.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
"lint-staged": "^10.2.11",
"rimraf": "^3.0.2",
"rollup": "^1.31.1",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup": "^2.22.1",
"rollup-plugin-eslint": "^7.0.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.4",
"typescript": "^3.7.5",
"vuepress": "^1.3.0"
"rollup-plugin-terser": "^6.1.0",
"typescript": "^3.9.7",
"vuepress": "^1.5.2"
},
"repository": {
"type": "git",
Expand Down
21 changes: 13 additions & 8 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json from 'rollup-plugin-json'
import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace'
import commonjs from 'rollup-plugin-commonjs'
import json from '@rollup/plugin-json'
import babel from '@rollup/plugin-babel'
import replace from '@rollup/plugin-replace'
import commonjs from '@rollup/plugin-commonjs'
import { eslint } from 'rollup-plugin-eslint'
import { uglify } from 'rollup-plugin-uglify'
import nodeResolve from 'rollup-plugin-node-resolve'
import { terser } from 'rollup-plugin-terser'
import nodeResolve from '@rollup/plugin-node-resolve'

import pkg from './package.json'

Expand Down Expand Up @@ -40,7 +40,7 @@ const plugins = [
json(),
nodeResolve(),
commonjs(),
babel(),
babel({ babelHelpers: 'bundled' }),
]
const env = 'process.env.NODE_ENV'
const external = ['axios', 'fetch-jsonp']
Expand Down Expand Up @@ -68,6 +68,11 @@ export default [{
plugins: [
...plugins,
replace({ [env]: '"production"' }),
uglify(),
terser({
output: {
/* eslint-disable */
ascii_only: true,
},
}),
],
}]
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 支持的请求类型
const VALID_REQ_TYPES = ['wx', 'axios', 'jsonp']
const VALID_REQ_TYPES = ['wx', 'axios', 'jsonp', 'custom']

// 小程序中合法的请求方法
const WX_VALID_METHODS = ['OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', 'CONNECT']
Expand All @@ -12,6 +12,7 @@ const ERROR_STRINGS = {
noData: 'no data!',
argsType: 'the first parameter must be an object!',
middleware: 'middleware must be a function!',
reqTypeAndCustomFetch: 'reqType or customFetch only!',

reqTypeFn: (reqType) => `invalid reqType: "${reqType}", ` +
`support these reqTypes: ["${VALID_REQ_TYPES.join('", "')}"].`,
Expand Down
16 changes: 10 additions & 6 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type ReqType = (
| 'wx' | 'WX'
| 'axios' | 'AXIOS'
| 'jsonp' | 'JSONP'
| 'custom' | 'CUSTOM'
)

export type Method = (
Expand Down Expand Up @@ -66,6 +67,7 @@ export interface CtxReq {
}
export interface CtxRes extends AxiosResponse {
data: any
rawData: any
error?: Error
[k: string]: any
}
Expand Down Expand Up @@ -93,6 +95,7 @@ export interface BaseApiConfig {
afterFn?: <T = any, U = any>(args: [U?, Ctx?]) => Promise<T>
beforeFn?: <T = any>() => Promise<T>
middleware?: Middleware<Ctx>[]
customFetch?: AnyPromiseFunction
commonParams?: object
axiosOptions?: AxiosOptions
jsonpOptions?: JsonpOptions
Expand Down Expand Up @@ -121,14 +124,14 @@ export interface RuntimeOptionsOnly {
fullPath?: string
callbackName?: string
}
export interface WxRuntimeOptions extends WxApiConfig, RuntimeOptionsOnly {}
export interface WebRuntimeOptions extends WebApiConfig, RuntimeOptionsOnly {}
export interface WxRuntimeOptions extends WxApiConfig, RuntimeOptionsOnly { }
export interface WebRuntimeOptions extends WebApiConfig, RuntimeOptionsOnly { }

export interface Api {
key: string
mock: Mock
params: ParamsConfig
<T = object, U = object | void>(
<T = object, U = object | void> (
params?: U,
runtimeOptions?: RuntimeOptions
): Promise<T>
Expand All @@ -137,13 +140,14 @@ export interface Apis { [k: string]: SyncFnMap }
export interface SyncFnMap { [k: string]: Api }

export interface TuaApiClass {
new (args?: {
new(args?: {
// deprecated
host?: string
baseUrl?: string

reqType?: string
middleware?: Middleware<Ctx>[]
customFetch?: AnyPromiseFunction
axiosOptions?: AxiosOptions
jsonpOptions?: JsonpOptions
defaultErrorData?: any
Expand All @@ -157,8 +161,8 @@ export interface TuaApiInstance {

/* -- export utils -- */

export function getSyncFnMapByApis(apis: Apis): SyncFnMap
export function getPreFetchFnKeysBySyncFnMap(syncFnMap: SyncFnMap): Api[]
export function getSyncFnMapByApis (apis: Apis): SyncFnMap
export function getPreFetchFnKeysBySyncFnMap (syncFnMap: SyncFnMap): Api[]

/* -- export default -- */

Expand Down
Loading

0 comments on commit 0b6106b

Please sign in to comment.