Skip to content

Commit

Permalink
refactor: @ts-check every js file
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve committed May 7, 2019
1 parent a669662 commit b16b646
Show file tree
Hide file tree
Showing 25 changed files with 280 additions and 119 deletions.
38 changes: 17 additions & 21 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
const presets = [
[
'@babel/preset-env',
{ targets: { 'node': 'current' } },
],
]
const plugins = [
[
'@babel/plugin-proposal-decorators',
{ legacy: true },
],
'@babel/plugin-proposal-object-rest-spread',
]

module.exports = {
env: {
test: {
presets: [
[
'@babel/preset-env',
{ targets: { 'node': 'current' } },
],
],
plugins: [
[
'@babel/plugin-proposal-decorators',
{ legacy: true },
],
],
},
dev: { presets, plugins },
test: { presets, plugins },
production: {
presets: [
[
'@babel/preset-env',
{ modules: false },
],
],
plugins: [
[
'@babel/plugin-proposal-decorators',
{ legacy: true },
],
'@babel/plugin-proposal-object-rest-spread',
],
plugins,
},
},
}
17 changes: 12 additions & 5 deletions examples/apis-mp/fake-wx.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export default {
// 该参数表示请求的中间路径,建议与文件同名,以便后期维护。
prefix: 'fake-wx',

// 所有请求类型(可选值 OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT)
type: 'post',
// 所有请求类型
/** @type { import('../../src/').Method } */
type: ('post'),

// 所有请求都需要携带的参数,例如小程序中的所有接口都要携带以下参数 `from=miniprogram`
commonParams: { from: 'miniprogram' },
Expand All @@ -27,6 +28,9 @@ export default {
*/
{
path: 'fail',
/**
* @returns {Promise<any>}
*/
beforeFn: () => Promise.resolve({
header: { cookie: '123' },
}),
Expand All @@ -38,7 +42,8 @@ export default {
{
name: 'arrayData',
path: 'array-data',
type: 'get',
/** @type { import('../../src/').Method } */
type: ('get'),
params: ['param1', 'param2'],
},
/**
Expand Down Expand Up @@ -77,7 +82,8 @@ export default {
name: 'typeGet',
path: 'type-get',
// 这个接口单独配置类型
type: 'get',
/** @type { import('../../src/').Method } */
type: ('get'),
},
/**
* unknown-type
Expand All @@ -86,7 +92,8 @@ export default {
name: 'unknownType',
path: 'unknown-type',
// 这个接口单独配置类型
type: 'foo',
/** @type { import('../../src/').Method } */
type: ('foo'),
},
/**
* nav-loading
Expand Down
42 changes: 42 additions & 0 deletions examples/apis-mp/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// default response result
interface Result { code: number, data: any, msg?: string }
interface ReqFn {
key: string
mock: any
params: object | string[]
}
interface RuntimeOptions {
// for jsonp
callbackName?: string
[key: string]: any
}
interface ReqFnWithAnyParams extends ReqFn {
<T = Result>(params?: any, options?: RuntimeOptions): Promise<T>
}

export const mockApi: {
'foo': ReqFnWithAnyParams
'bar': ReqFnWithAnyParams
'null': ReqFnWithAnyParams
}

export const fakeWxApi: {
'fail': ReqFnWithAnyParams
'typeGet': ReqFnWithAnyParams
'noBeforeFn': ReqFnWithAnyParams
'navLoading': ReqFnWithAnyParams
'hideLoading': ReqFnWithAnyParams
'unknownType': ReqFnWithAnyParams
'arrayData': ReqFn & {
<T = Result>(
params: { param1?: any, param2?: any },
options?: RuntimeOptions
): Promise<T>
}
'objectData': ReqFn & {
<T = Result>(
params: { param1?: any, param2?: any, param3: any },
options?: RuntimeOptions
): Promise<T>
}
}
5 changes: 3 additions & 2 deletions examples/apis-mp/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export default {
// 该参数表示请求的中间路径,建议与文件同名,以便后期维护。
prefix: 'mock',

// 所有请求类型(可选值 OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT)
type: 'get',
// 所有请求类型
/** @type { import('../../src/').Method } */
type: ('get'),

// 公共 mock
mock: ({ __mockData__ }) => __mockData__,
Expand Down
22 changes: 16 additions & 6 deletions examples/apis-web/fake-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export default {
{
name: 'op',
path: 'object-params',
reqType: '',
/** @type { import('../../src/').ReqType } */
reqType: (''),
params: {
param1: 1217,
param2: 'steve',
Expand All @@ -69,7 +70,10 @@ export default {
name: 'acp',
path: 'async-common-params',
params: [],
// 在这里返回的 params 会和请求的 params 合并
/**
* 在这里返回的 params 会和请求的 params 合并
* @returns {Promise<any>}
*/
beforeFn: () => Promise.resolve({
params: { asyncCp: 'asyncCp' },
}),
Expand All @@ -82,8 +86,12 @@ export default {
path: 'req-type-axios',
// 用哪个包发起请求目前支持:jsonp、axios
// 如果不指定默认对于 get 请求使用 fetch-jsonp,post 请求使用 axios
reqType: 'axios',
// 在这里返回的 params 会和请求的 params 合并
/** @type { import('../../src/').ReqType } */
reqType: ('axios'),
/**
* 在这里返回的 params 会和请求的 params 合并
* @returns {Promise<any>}
*/
beforeFn: () => Promise.resolve({
params: { asyncCp: 'asyncCp' },
}),
Expand All @@ -94,7 +102,8 @@ export default {
{
name: 'irt',
path: 'invalid-req-type',
reqType: 'foobar',
/** @type { import('../../src/').ReqType } */
reqType: ('foobar'),
},
/**
* afterFn-data
Expand Down Expand Up @@ -126,7 +135,8 @@ export default {
{
name: 'mockFnData',
path: 'mock-function-data',
reqType: 'axios',
/** @type { import('../../src/').ReqType } */
reqType: ('axios'),
mock: ({ mockCode }) => ({ code: mockCode, data: {} }),
},
],
Expand Down
14 changes: 7 additions & 7 deletions examples/apis-web/fake-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default {
// 该参数表示请求的中间路径,建议与文件同名,以便后期维护。
prefix: 'fake-post',

/** @type { import('../../src/').Method } */
type: ('post'),

// 所有请求都需要携带的参数
commonParams: { common: 'params' },

Expand All @@ -19,16 +22,15 @@ export default {
{
name: 'eap',
path: 'empty-array-params',
type: 'post',
},
/**
* array-params
*/
{
name: 'ap',
path: 'array-params',
type: 'post',
reqType: 'axios',
/** @type { import('../../src/').ReqType } */
reqType: ('axios'),
params: ['param1', 'param2'],
},
/**
Expand All @@ -37,8 +39,8 @@ export default {
{
name: 'hap',
path: 'array-params',
type: 'post',
reqType: 'axios',
/** @type { import('../../src/').ReqType } */
reqType: ('axios'),
middleware: [
async (ctx, next) => {
ctx.req.host = 'http://custom-host.com/'
Expand All @@ -52,7 +54,6 @@ export default {
{
name: 'op',
path: 'object-params',
type: 'post',
params: {
param1: 1217,
param2: 'steve',
Expand All @@ -65,7 +66,6 @@ export default {
{
name: 'oh',
path: 'own-host',
type: 'post',
host: 'http://example-test.com/',
params: {},
// 表示这个接口不需要传递 commonParams
Expand Down
56 changes: 56 additions & 0 deletions examples/apis-web/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// default response result
interface Result { code: number, data: any, msg?: string }
interface ReqFn {
key: string
mock: any
params: object | string[]
}
interface RuntimeOptions {
// for jsonp
callbackName?: string
[key: string]: any
}
interface ReqFnWithAnyParams extends ReqFn {
<T = Result>(params?: any, options?: RuntimeOptions): Promise<T>
}

export const fakeGetApi: {
'acp': ReqFnWithAnyParams
'rta': ReqFnWithAnyParams
'irt': ReqFnWithAnyParams
'afterData': ReqFnWithAnyParams
'mockFnData': ReqFnWithAnyParams
'noAfterData': ReqFnWithAnyParams
'mockObjectData': ReqFnWithAnyParams
'empty-array-params': ReqFnWithAnyParams
'ap': ReqFn & {
<T = Result>(
params: { param1?: any, param2?: any },
options?: RuntimeOptions
): Promise<T>
}
'op': ReqFn & {
<T = Result>(
params: { param1?: any, param2?: any, param3: any },
options?: RuntimeOptions
): Promise<T>
}
}

export const fakePostApi: {
'oh': ReqFnWithAnyParams
'eap': ReqFnWithAnyParams
'hap': ReqFnWithAnyParams
'ap': ReqFn & {
<T = Result>(
params: { param1?: any, param2?: any },
options?: RuntimeOptions
): Promise<T>
}
'op': ReqFn & {
<T = Result>(
params: { param1?: any, param2?: any, param3: any },
options?: RuntimeOptions
): Promise<T>
}
}
21 changes: 21 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface Wx {
request: jest.Mocked<any>
hideLoading: jest.Mocked<any>
showLoading: jest.Mocked<any>
hideNavigationBarLoading: jest.Mocked<any>
showNavigationBarLoading: jest.Mocked<any>

// just for test
__TEST_DATA__: {
testData?: any
isTestFail?: boolean
}
}

declare const wx: Wx

declare namespace NodeJS {
interface Global {
wx: Wx
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@babel/preset-env": "^7.4.3",
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@types/jest": "^24.0.12",
"axios-mock-adapter": "^1.16.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
Expand All @@ -99,6 +100,7 @@
"rollup-plugin-node-resolve": "^4.2.3",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.2",
"typescript": "^3.4.5",
"vuepress": "^1.0.0-alpha.39"
},
"repository": {
Expand Down
5 changes: 4 additions & 1 deletion src/adapters/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import axios from 'axios'
import { DEFAULT_HEADER } from '../constants'
import { logger, isFormData, getParamStrFromObj } from '../utils'

// 获取使用 axios 发起请求后的 promise 对象
/**
* 获取使用 axios 发起请求后的 promise 对象
* @param {object} options
*/
export const getAxiosPromise = ({
url,
data,
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/jsonp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetchJsonp from 'fetch-jsonp'

import { logger } from '../utils'

const fetchJsonp = require('fetch-jsonp')

// 获取发起 jsonp 请求后的 promise 对象
export const getFetchJsonpPromise = ({ url, jsonpOptions }) => {
logger.log(`Jsonp Url: ${url}`)
Expand Down
Loading

0 comments on commit b16b646

Please sign in to comment.