-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-compile.js
More file actions
107 lines (88 loc) · 2.65 KB
/
test-compile.js
File metadata and controls
107 lines (88 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const ApiAdapter = require("./index")
const _ = require("underscore")
const ObjectPath = require("object-path");
const merge = require('deepmerge')
const config = {
name: "home1",
host: "https://b2ads.ad5track.com",
path: "/ad_products/top",
method: "GET",
request: [
{ from: "headers.referrer", to: "query.referrer" },
{ from: "query.size" , to: "headers.limit"},
{ from: "query.userId" , to: "query.user_id" , middleware: [ "parseInt" ] },
{ value: "json" , to: "query.t" },
{ from: "query.terms", to: "query.q", middleware: [ "threeFirstWords" ] },
{ from: "query.type", to: "query.type", required: true }
]
}
const request = {
headers: {
referrer: "http://www.americanas.com"
},
query: {
size: 10,
userId: "30",
terms: "notebook asus 16gb blabla bla",
type: "supplier"
}
}
const request2 = {
headers: {
referrer: "http://www.shoptime.com"
},
query: {
size: 23,
userId: "30",
terms: "iphone 16gb blabla bla",
type: "seller"
}
}
const allMiddlewares = {
threeFirstWords: (terms, data) => {
return terms.split(" ").slice(0, 3).join(" ")
}
}
function generateFunction(config, middlewares) {
function applyMiddlewares(middlewares, valueAssign) {
const [ left, right ] = valueAssign;
const rightWithMiddleware = middlewares.reduce((prev, state) =>
`${state}(${right}, data)`
, right)
return [ left , rightWithMiddleware ].join(" = ")
}
function defineMiddlewares(middlewares) {
return _(middlewares).keys().map(middlewareName =>
`const ${middlewareName} = ${middlewares[middlewareName].toString()}`
).join(";\n")
}
function getProperties(config) {
return config.map(conf => {
const { to, from, value, middleware = [] } = conf
const resultValue = value ? `"${value}"` : `data.${from}`;
const valueAssign = [ `output.${to}`, resultValue ]
return applyMiddlewares(middleware, valueAssign)
}).join(";\n")
}
function genObjectsByKeys(dotKeys, obj = {}) {
const [ head, ...tail ] = dotKeys.split(".")
if(!obj[head])
obj[head] = {}
if (tail.length > 0)
obj[head] = genObjectsByKeys(tail.join("."), obj[head])
return obj
}
function getBaseKeys(config) {
return merge.all( config.map(conf => genObjectsByKeys(conf.to)))
}
return new Function("data",`
${defineMiddlewares(middlewares)}
const output = ${JSON.stringify(getBaseKeys(config),"", 2)};
${getProperties(config)}
return output;
`)
}
const callback2 = generateFunction(config.request, allMiddlewares)
console.log(callback2.toString())
console.log(callback2(request))
console.log(callback2(request2))