Skip to content

Commit

Permalink
Add index.d.ts (#31)
Browse files Browse the repository at this point in the history
* refactor: src/TuaApi.js -> src/index.js

* feat(src): #19, add index.d.ts

* chore: upgrade deps
  • Loading branch information
BuptStEve authored Apr 18, 2019
1 parent 11af778 commit a70c67d
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 35 deletions.
5 changes: 0 additions & 5 deletions .npmignore

This file was deleted.

2 changes: 1 addition & 1 deletion examples/apis-mp/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TuaApi from '../../src/TuaApi'
import TuaApi from '../../src'

const tuaApi = new TuaApi()

Expand Down
2 changes: 1 addition & 1 deletion examples/apis-web/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TuaApi from '../../src/TuaApi'
import TuaApi from '../../src'

const tuaApi = new TuaApi({
host: 'http://example-base.com/',
Expand Down
44 changes: 23 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -51,50 +52,51 @@
},
"collectCoverage": true,
"collectCoverageFrom": [
"src/**"
"src/**",
"!src/index.d.ts"
]
},
"eslintIgnore": [
"package.json",
"dist/*"
"dist/*",
"package.json"
],
"dependencies": {
"axios": "^0.18.0",
"fetch-jsonp": "^1.1.3",
"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"
},
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -24,7 +24,7 @@ const output = {
format: 'esm',
},
umd: {
file: 'dist/TuaApi.umd.js',
file: pkg.unpkg,
name: 'TuaApi',
banner,
format: 'umd',
Expand Down
152 changes: 152 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<T> = (ctx: T, next: () => Promise<any>) => Promise<any>

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?: <T = any, U = any>(args: [U, Ctx]) => Promise<T>
beforeFn?: <T = any>() => Promise<T>
middleware?: Middleware<Ctx>[]
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
<T = object, U = object | void>(
params?: U,
runtimeOptions?: RuntimeOptions
): Promise<T>
}
export interface Apis { [k: string]: SyncFnMap }
export interface SyncFnMap { [k: string]: Api }

export interface TuaApiClass {
new (args?: {
host?: string
reqType?: string
middleware?: Middleware<Ctx>[]
axiosOptions?: AxiosOptions
jsonpOptions?: JsonpOptions
defaultErrorData?: any
}): TuaApiInstance
}

export interface TuaApiInstance {
use: (fn: Middleware<Ctx>) => 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
2 changes: 1 addition & 1 deletion src/TuaApi.js → src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class TuaApi {
formatReqParamsMiddleware,
// 业务侧中间件函数数组
...middlewareFns,
// 生成 fullUrl 参数
// 生成 _reqFn 参数
setReqFnParamsMiddleware,
// 统一转换响应数据为对象
formatResDataMiddleware,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const isWx = () =>
export const isWx = () => (
typeof wx !== 'undefined' &&
typeof wx.request === 'function'
)
2 changes: 1 addition & 1 deletion test/__tests__/core.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TuaApi from '@/TuaApi'
import TuaApi from '@/'
import { ERROR_STRINGS } from '@/constants'

describe('error handling', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/wx.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"lib": ["es2015"],
"allowJs": true,
"baseUrl": ".",
"experimentalDecorators": true,
"paths":{
"@/*": ["src/*"],
"@examples/*": ["examples/*"]
Expand Down

0 comments on commit a70c67d

Please sign in to comment.