From a70c67d67325b0ae0c071058c4f5f56d9a019dd6 Mon Sep 17 00:00:00 2001 From: StEve Young <2747745470@qq.com> Date: Thu, 18 Apr 2019 12:25:12 +0800 Subject: [PATCH] Add index.d.ts (#31) * refactor: src/TuaApi.js -> src/index.js * feat(src): #19, add index.d.ts * chore: upgrade deps --- .npmignore | 5 -- examples/apis-mp/index.js | 2 +- examples/apis-web/index.js | 2 +- package.json | 44 ++++++----- rollup.config.js | 4 +- src/index.d.ts | 152 ++++++++++++++++++++++++++++++++++++ src/{TuaApi.js => index.js} | 2 +- src/utils/env.js | 3 +- test/__tests__/core.test.js | 2 +- test/__tests__/wx.test.js | 2 +- tsconfig.json | 3 +- 11 files changed, 186 insertions(+), 35 deletions(-) delete mode 100644 .npmignore create mode 100644 src/index.d.ts rename src/{TuaApi.js => index.js} (99%) diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 8636337..0000000 --- a/.npmignore +++ /dev/null @@ -1,5 +0,0 @@ -src -test -*.log -coverage -.circleci diff --git a/examples/apis-mp/index.js b/examples/apis-mp/index.js index 49d462c..973689d 100644 --- a/examples/apis-mp/index.js +++ b/examples/apis-mp/index.js @@ -1,4 +1,4 @@ -import TuaApi from '../../src/TuaApi' +import TuaApi from '../../src' const tuaApi = new TuaApi() diff --git a/examples/apis-web/index.js b/examples/apis-web/index.js index 1d7abf0..97b55dd 100644 --- a/examples/apis-web/index.js +++ b/examples/apis-web/index.js @@ -1,4 +1,4 @@ -import TuaApi from '../../src/TuaApi' +import TuaApi from '../../src' const tuaApi = new TuaApi({ host: 'http://example-base.com/', diff --git a/package.json b/package.json index dce11fa..f594088 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,12 @@ { "name": "tua-api", - "version": "1.1.1", + "version": "1.2.0", "description": "🏗 A common tool helps converting configs to api functions", "main": "dist/TuaApi.cjs.js", "module": "dist/TuaApi.esm.js", "unpkg": "dist/TuaApi.umd.js", "jsdelivr": "dist/TuaApi.umd.js", + "types": "src/index.d.ts", "files": [ "src", "dist", @@ -51,12 +52,13 @@ }, "collectCoverage": true, "collectCoverageFrom": [ - "src/**" + "src/**", + "!src/index.d.ts" ] }, "eslintIgnore": [ - "package.json", - "dist/*" + "dist/*", + "package.json" ], "dependencies": { "axios": "^0.18.0", @@ -64,37 +66,37 @@ "koa-compose": "^4.1.0" }, "devDependencies": { - "@babel/core": "^7.3.3", + "@babel/core": "^7.4.3", "@babel/plugin-external-helpers": "^7.2.0", - "@babel/plugin-proposal-decorators": "^7.3.0", - "@babel/plugin-proposal-object-rest-spread": "^7.3.2", - "@babel/preset-env": "^7.3.1", + "@babel/plugin-proposal-decorators": "^7.4.0", + "@babel/plugin-proposal-object-rest-spread": "^7.4.3", + "@babel/preset-env": "^7.4.3", "@commitlint/cli": "^7.5.2", "@commitlint/config-conventional": "^7.5.0", "axios-mock-adapter": "^1.16.0", "babel-core": "^7.0.0-bridge.0", "babel-eslint": "^10.0.1", - "babel-jest": "^24.1.0", - "codecov": "^3.2.0", + "babel-jest": "^24.7.1", + "codecov": "^3.3.0", "cross-env": "^5.2.0", - "eslint": "^5.14.0", + "eslint": "^5.16.0", "eslint-config-standard": "^12.0.0", - "eslint-plugin-import": "^2.16.0", + "eslint-plugin-import": "^2.17.2", "eslint-plugin-node": "^8.0.1", - "eslint-plugin-promise": "^4.0.1", + "eslint-plugin-promise": "^4.1.1", "eslint-plugin-standard": "^4.0.0", "gh-pages": "^2.0.1", "husky": "^1.3.1", - "jest": "^24.1.0", - "lint-staged": "^8.1.4", + "jest": "^24.7.1", + "lint-staged": "^8.1.5", "rimraf": "^2.6.3", - "rollup": "^1.2.2", + "rollup": "^1.10.0", "rollup-plugin-babel": "^4.3.2", - "rollup-plugin-commonjs": "^9.2.0", - "rollup-plugin-eslint": "^5.0.0", - "rollup-plugin-json": "^3.1.0", - "rollup-plugin-node-resolve": "^4.0.0", - "rollup-plugin-replace": "^2.1.0", + "rollup-plugin-commonjs": "^9.3.4", + "rollup-plugin-eslint": "^5.1.0", + "rollup-plugin-json": "^4.0.0", + "rollup-plugin-node-resolve": "^4.2.3", + "rollup-plugin-replace": "^2.2.0", "rollup-plugin-uglify": "^6.0.2", "vuepress": "^1.0.0-alpha.39" }, diff --git a/rollup.config.js b/rollup.config.js index d01dd36..756accb 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -8,7 +8,7 @@ import nodeResolve from 'rollup-plugin-node-resolve' import pkg from './package.json' -const input = `src/TuaApi.js` +const input = `src/index.js` const banner = `/* ${pkg.name} version ${pkg.version} */` const output = { @@ -24,7 +24,7 @@ const output = { format: 'esm', }, umd: { - file: 'dist/TuaApi.umd.js', + file: pkg.unpkg, name: 'TuaApi', banner, format: 'umd', diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..2f71b41 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,152 @@ +import { Options as JsonpOptions } from 'fetch-jsonp' +import { + AxiosResponse, + AxiosRequestConfig as AxiosOptions, +} from 'axios' + +/* -- types -- */ +export type Mock = AnyFunction | any + +export type ApiConfig = WxApiConfig | WebApiConfig + +export type AnyFunction = (...args: any[]) => any + +export type ParamsConfig = string[] | ParamsObject + +export type Middleware = (ctx: T, next: () => Promise) => Promise + +export type RuntimeOptions = WxRuntimeOptions | WebRuntimeOptions + +export type ReqType = ( + | 'wx' | 'WX' + | 'axios' | 'AXIOS' + | 'jsonp' | 'JSONP' +) + +export type Method = ( + | 'get' | 'GET' + | 'put' | 'PUT' + | 'head' | 'HEAD' + | 'post' | 'POST' + | 'trace' | 'TRACE' + | 'delete' | 'DELETE' + | 'connect' | 'CONNECT' + | 'options' | 'OPTIONS' +) + +/* -- interfaces -- */ + +export interface ParamsObject { + [k: string]: ( + | { required: boolean } + | { isRequired: boolean } + | any + ) +} + +export interface CtxReq { + host: string + mock: Mock + type: Method + path: string + prefix: string + reqType: ReqType + reqParams: object + reqFnParams: object + callbackName: string + axiosOptions: AxiosOptions + jsonpOptions: JsonpOptions + [k: string]: any +} +export interface CtxRes extends AxiosResponse { + data: any + error?: Error + [k: string]: any +} + +export interface Ctx { + req: CtxReq + res: CtxRes + endTime: number + reqTime: number + startTime: number + [k: string]: any +} + +export interface BaseApiConfig { + mock?: Mock + host?: string + type?: Method + prefix?: string + reqType?: ReqType + afterFn?: (args: [U, Ctx]) => Promise + beforeFn?: () => Promise + middleware?: Middleware[] + commonParams?: object + axiosOptions?: AxiosOptions + jsonpOptions?: JsonpOptions + useGlobalMiddleware?: boolean + [k: string]: any +} + +// for web +export interface WebApiConfig extends BaseApiConfig { + pathList: (BaseApiConfig & { + path: string + name?: string + params?: ParamsConfig + })[] +} + +// for wechat miniprogram +export interface WxApiConfig extends WebApiConfig { + isShowLoading?: boolean + showLoadingFn?: AnyFunction + hideLoadingFn?: AnyFunction +} + +export interface RuntimeOptionsOnly { + apiName?: string + fullPath?: string + callbackName?: string +} +export interface WxRuntimeOptions extends WxApiConfig, RuntimeOptionsOnly {} +export interface WebRuntimeOptions extends WebApiConfig, RuntimeOptionsOnly {} + +export interface Api { + key: string + mock: Mock + params: ParamsConfig + ( + params?: U, + runtimeOptions?: RuntimeOptions + ): Promise +} +export interface Apis { [k: string]: SyncFnMap } +export interface SyncFnMap { [k: string]: Api } + +export interface TuaApiClass { + new (args?: { + host?: string + reqType?: string + middleware?: Middleware[] + axiosOptions?: AxiosOptions + jsonpOptions?: JsonpOptions + defaultErrorData?: any + }): TuaApiInstance +} + +export interface TuaApiInstance { + use: (fn: Middleware) => TuaApiInstance + getApi: (apiConfig: ApiConfig) => SyncFnMap +} + +/* -- export utils -- */ + +export function getSyncFnMapByApis(apis: Apis): SyncFnMap +export function getPreFetchFnKeysBySyncFnMap(syncFnMap: SyncFnMap): Api[] + +/* -- export default -- */ + +declare const TuaApi: TuaApiClass +export default TuaApi diff --git a/src/TuaApi.js b/src/index.js similarity index 99% rename from src/TuaApi.js rename to src/index.js index 2dff02c..3d56377 100644 --- a/src/TuaApi.js +++ b/src/index.js @@ -180,7 +180,7 @@ class TuaApi { formatReqParamsMiddleware, // 业务侧中间件函数数组 ...middlewareFns, - // 生成 fullUrl 参数 + // 生成 _reqFn 参数 setReqFnParamsMiddleware, // 统一转换响应数据为对象 formatResDataMiddleware, diff --git a/src/utils/env.js b/src/utils/env.js index 896de2a..6a1ad39 100644 --- a/src/utils/env.js +++ b/src/utils/env.js @@ -1,3 +1,4 @@ -export const isWx = () => +export const isWx = () => ( typeof wx !== 'undefined' && typeof wx.request === 'function' +) diff --git a/test/__tests__/core.test.js b/test/__tests__/core.test.js index 9911f06..9e77857 100644 --- a/test/__tests__/core.test.js +++ b/test/__tests__/core.test.js @@ -1,4 +1,4 @@ -import TuaApi from '@/TuaApi' +import TuaApi from '@/' import { ERROR_STRINGS } from '@/constants' describe('error handling', () => { diff --git a/test/__tests__/wx.test.js b/test/__tests__/wx.test.js index 64f572a..a736550 100644 --- a/test/__tests__/wx.test.js +++ b/test/__tests__/wx.test.js @@ -1,5 +1,5 @@ import '../__mocks__/wxMock' -import TuaApi from '@/TuaApi' +import TuaApi from '@/' import { ERROR_STRINGS } from '@/constants' import fakeWx from '@examples/apis-mp/fake-wx' diff --git a/tsconfig.json b/tsconfig.json index 1ba4c70..784ca0c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,9 @@ { "compilerOptions": { - "experimentalDecorators": true, + "lib": ["es2015"], "allowJs": true, "baseUrl": ".", + "experimentalDecorators": true, "paths":{ "@/*": ["src/*"], "@examples/*": ["examples/*"]