-
Notifications
You must be signed in to change notification settings - Fork 6
/
create-query.js
120 lines (108 loc) · 2.76 KB
/
create-query.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
const jsonpath = require('jsonpath')
const utils = require('./utils')
const RE_VAR = /^\${1,4}/
/**
* Create query function to query linked data
*
* @params {Object} unitsData - all units req and res data
* @params {Object} configExports - data from config
* @params {Unit} unit
*
* linked-data query pattern:
* - $auth.signup1.res.body.token - cross module (unitsData)
* - $$signup1.res.body.token - current module (unitsData)
* - $$$res.body.id - current unit (unitsData)
* - $$$$a.b.c - everywhere (configExports)
*
*
* @returns {function}
*/
module.exports = function createQuery(unitsData, configExports, unit) {
let currentData = selectUnitsData(unitsData, unit)
return (value, single = true) => {
if (!isLink(value)) return value
let jPath = toJPath(value, unit)
let data
if (isLinkConfigData(value)) {
data = configExports
} else {
let module = unit.module()
currentData[module] = unitsData[module]
data = currentData
}
return query(data, jPath, single)
}
}
/**
* Whether path is actually a link.
* A link is string and start with 1-4 dollar mark, respent a pointer to unitsData or configExports
*
* @param {string} path - path to locate the data
*
* @returns {boolean}
*/
function isLink(path) {
return utils.isTypeOf(path, 'string') && RE_VAR.test(path)
}
/**
* How many dollar mark the path has
* @param {string} path - path to locate the data
*/
function count$(path) {
if (path[0] !== '$') return 0
return count$(path.slice(1)) + 1
}
/**
* Whether path is actually a link to configExports.
* @param {string} path - path to locate the data
*
* @returns {boolean}
*/
function isLinkConfigData(path) {
return path.slice(0, 4) === '$$$$'
}
/**
* Wrap the normal path to jsonpath
* @param {string} path - path to locate the data
* @param {Unit} unit
*
* @returns {string} - jsonpath to locate data
*/
function toJPath(path, unit) {
let n = count$(path)
let segs = isLinkConfigData(path) ? ['$'] : ['$', unit.module(), unit.name()].slice(0, n)
let prefix = jsonpath.stringify(segs)
let tail = path.slice(n)
return prefix + delimiter(tail) + tail
}
/**
* Delimiter to concat jsonpath segment
*/
function delimiter(path) {
return path[0] === '[' ? '' : '.'
}
/**
* Use jsonpath to locate the data
*/
function query(source, jp, single) {
let result
try {
result = jsonpath.query(source, jp)
} catch (err) {
result = []
}
return single ? result[0] : result
}
/**
* Only pick the properties of unitsData which match the unit module name or dependencies
*/
function selectUnitsData(unitsData, unit) {
let currentData = {}
unit
.dependencies()
.slice()
.forEach(({ name, module }) => {
currentData[name] = unitsData[module]
})
return currentData
}