fix #250 add ref to request params in mock API, for details please check Wiki update of user manual

add-license-1
Bosn 7 years ago
parent 9686872c90
commit 9ed9395e2e

@ -12,6 +12,8 @@ RAP2是在RAP1基础上重做的新项目它包含两个组件(对应两个Gi
* [Official Site 官网: rap2.taobao.org](http://rap2.taobao.org)
* 钉钉群ID: 11789704
* [热心网友提供的部署文档,供参考](https://github.com/thx/rap2-delos/issues/119)
* [用户手册](https://github.com/thx/rap2-delos/wiki/user_manual)
* [常见问题](https://github.com/thx/rap2-delos/wiki/faq)
## 部署

@ -19,9 +19,9 @@ let config: IConfigOptions = {
pool: {
max: 5,
min: 0,
idle: 10000
idle: 10000,
},
logging: true
logging: false,
},
redis: {
// isRedisCluster: true,

@ -113,7 +113,6 @@ const REG_URL_METHOD = /^\/?(get|post|delete|put)/i
router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
let app: any = ctx.app
app.counter.mock++
let { repositoryId, url } = ctx.params
let method = ctx.request.method
repositoryId = +repositoryId

@ -1,13 +1,13 @@
import { Property } from "../../models";
import * as _ from 'underscore'
const vm = require('vm')
const _ = require('underscore')
import * as Mock from 'mockjs'
const { RE_KEY } = require('mockjs/src/mock/constant')
export default class Tree {
public static ArrayToTree (list: Property[]) {
public static ArrayToTree(list: Property[]) {
let result: any = {
name: 'root',
children: [],
@ -17,7 +17,7 @@ export default class Tree {
let mapped: any = {}
list.forEach(item => { mapped[item.id] = item })
function _parseChildren (parentId: any, children: any, depth: any) {
function _parseChildren(parentId: any, children: any, depth: any) {
for (let id in mapped) {
let item = mapped[id]
if (typeof parentId === 'function' ? parentId(item.parentId) : item.parentId === parentId) {
@ -43,8 +43,8 @@ export default class Tree {
}
// TODO 2.x 和前端重复了
public static TreeToTemplate (tree: any) {
function parse (item: any, result: any) {
public static TreeToTemplate(tree: any) {
function parse(item: any, result: any) {
let rule = item.rule ? ('|' + item.rule) : ''
let value = item.value
switch (item.type) {
@ -110,7 +110,7 @@ export default class Tree {
return result
}
public static TemplateToData (template: any) {
public static TemplateToData(template: any) {
// 数据模板 template 中可能含有攻击代码,例如死循环,所以在沙箱中生成最终数据
// https://nodejs.org/dist/latest-v7.x/docs/api/vm.html
const sandbox = { Mock, template, data: {} }
@ -137,9 +137,8 @@ export default class Tree {
public static ArrayToTreeToTemplateToData(list: Property[], extra?: any) {
let tree = Tree.ArrayToTree(list)
let template = Tree.TreeToTemplate(tree)
let template: { [key: string]: any } = Tree.TreeToTemplate(tree)
let data
if (extra) {
// DONE 2.2 支持引用请求参数
let keys = Object.keys(template).map(item => item.replace(RE_KEY, '$1'))
@ -147,6 +146,17 @@ export default class Tree {
let scopedData = Tree.TemplateToData(
Object.assign({}, _.pick(extra, extraKeys), template),
)
for (const key in scopedData) {
if (!scopedData.hasOwnProperty(key)) continue
let data = scopedData[key]
for (const eKey in extra) {
if (!extra.hasOwnProperty(eKey)) continue
const pattern = new RegExp(`\\$${eKey}\\$`, 'g')
if (data && pattern.test(data)) {
data = scopedData[key] = data.replace(pattern, extra[eKey])
}
}
}
data = _.pick(scopedData, keys)
} else {
data = Tree.TemplateToData(template)

@ -1,13 +1,12 @@
import * as redis from 'redis'
import * as ioredis from 'ioredis'
import config from '../config'
import * as ioredis from 'ioredis'
export enum CACHE_KEY {
REPOSITORY_GET = 'REPOSITORY_GET',
}
export default class RedisService {
// 判断 是否使用cluster集群
private static client: redis.RedisClient = config.redis && config.redis.isRedisCluster ? new ioredis.Cluster(config.redis.nodes, {redisOptions: config.redis.redisOptions}) : redis.createClient(config.redis)
private static getCacheKey(key: CACHE_KEY, entityId?: number): string {

@ -1,5 +1,4 @@
/// <reference path="custom-typings.d.ts" />
import { PoolOptions } from "sequelize";
import { ISequelizeConfig } from "sequelize-typescript";
import { RedisOptions } from "koa-redis";
@ -10,6 +9,7 @@ declare interface RedisAndClusterOptions extends RedisOptions {
redisOptions?: any;
}
declare interface IConfigOptions {
version: string,
serve: {
@ -22,4 +22,4 @@ declare interface IConfigOptions {
keycenter?: string | boolean,
db: ISequelizeConfig,
redis: RedisAndClusterOptions
}
}
Loading…
Cancel
Save