Skip to content

Commit

Permalink
fix: 自写 mockjs 模板解析引擎提升解析精确度
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Feb 16, 2022
1 parent ea19f56 commit 254f0e1
Show file tree
Hide file tree
Showing 6 changed files with 3,500 additions and 276 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"got": "^11.5.1",
"json-schema-to-typescript": "^10.1.4",
"json5": "^2.1.0",
"mockjs": "^1.0.1-beta3",
"ora": "^3.4.0",
"prettier": "^2.2.1",
"prompts": "^2.0.4",
Expand All @@ -78,13 +77,12 @@
"to-json-schema": "^0.2.5",
"ts-node": "^8.0.2",
"typescript": "^4.2.3",
"vtils": "^4.33.0",
"vtils": "^4.57.0",
"yargs": "^16.2.0"
},
"devDependencies": {
"@types/fs-extra": "^5.0.4",
"@types/json5": "^0.0.30",
"@types/mockjs": "^1.0.2",
"@types/node": "^10.12.10",
"@types/prompts": "^1.2.0",
"@types/react": "^16.9.2",
Expand Down
43 changes: 38 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import JSON5 from 'json5'
import Mock from 'mockjs'
import path from 'path'
import prettier from 'prettier'
import toJsonSchema from 'to-json-schema'
Expand All @@ -13,6 +12,7 @@ import {
mapKeys,
memoize,
run,
traverse,
} from 'vtils'
import { compile, Options } from 'json-schema-to-typescript'
import { Defined, OneOrMore } from 'vtils/types'
Expand Down Expand Up @@ -273,10 +273,43 @@ export function mockjsTemplateToJsonSchema(
template: object,
customTypeMapping: Record<string, JSONSchema4TypeName>,
): JSONSchema4 {
return processJsonSchema(
Mock.toJSONSchema(template) as any,
customTypeMapping,
)
const actions: Array<() => void> = []
// https://github.com/nuysoft/Mock/blob/refactoring/src/mock/constant.js#L27
const keyRe = /(.+)\|(?:\+(\d+)|([+-]?\d+-?[+-]?\d*)?(?:\.(\d+-?\d*))?)/
// https://github.com/nuysoft/Mock/wiki/Mock.Random
const numberPatterns: string[] = [
'natural',
'integer',
'float',
'range',
'increment',
]
const boolPatterns: string[] = ['boolean', 'bool']
const normalizeValue = (value: any): any => {
if (typeof value === 'string' && value.startsWith('@')) {
const pattern = value.slice(1)
if (numberPatterns.some(p => pattern.startsWith(p))) {
return 1
}
if (boolPatterns.some(p => pattern.startsWith(p))) {
return true
}
}
return value
}
traverse(template, (value, key, parent) => {
if (typeof key === 'string') {
actions.push(() => {
delete parent[key]
parent[
// https://github.com/nuysoft/Mock/blob/refactoring/src/mock/schema/schema.js#L16
key.replace(keyRe, '$1')
] = normalizeValue(value)
})
}
})
actions.forEach(action => action())
return jsonToJsonSchema(template, customTypeMapping)
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/__mocks__/got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,39 @@ const mockData: Record<string, () => any> = {
res_body:
'{\n "code": 200,\n "data|1-10": [\n {\n "id": "@id", // id\n "name": "@cname", // 名字\n "template|1-10": "*", // 模板\n "string|3": "***", // 遍历三次\n "number|1-100": 200,\n "boolean": "@boolean",\n "image": "@image"\n }\n ]\n}',
},
{
query_path: {
path: '/mockjs2',
params: [],
},
edit_uid: 0,
status: 'undone',
type: 'static',
req_body_is_json_schema: false,
res_body_is_json_schema: false,
api_opened: false,
index: 0,
tag: [],
_id: 1831,
method: 'GET',
catid: 20,
title: 'mockjs2',
path: '/mockjs2',
project_id: 11,
req_params: [],
res_body_type: 'json',
uid: 11,
add_time: 1645001853,
up_time: 1645001866,
req_query: [],
req_headers: [],
req_body_form: [],
__v: 0,
desc: '',
markdown: '',
res_body:
'{\n "pageNo": 1,\n "pageSize": 5,\n "count": 16,\n "first": 1,\n "last": 1,\n "prev": 1,\n "next": 1,\n "firstPage": true,\n "lastPage": true,\n "length": 8,\n "slider": 1,\n "orderBy": "",\n "funcName": "page",\n "funcParam": "",\n "message": "",\n "html": "",\n "totalPage": 1,\n "notCount": false,\n "maxResults": 5,\n "disabled": false,\n "firstResult": 0\n}',
},
{
query_path: {
path: '/default_value',
Expand Down

0 comments on commit 254f0e1

Please sign in to comment.