-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
64 lines (54 loc) · 1.34 KB
/
server.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
const pick = require('lodash.pick')
const omit = require('lodash.omit')
const mapValues = require('lodash.mapvalues')
const Trailpack = require('./')
const footprintOptions = [
'populate',
'limit',
'offset',
'sort'
]
/**
* Web Server Trailpack
*
* Web Servers should inherit from this Trailpack in order to provide consistent
* API for all web servers.
*/
module.exports = class ServerTrailpack extends Trailpack {
_parseQuery(data) {
return mapValues(data, value => {
if (value === 'true' || value === 'false') {
value = value === 'true'
}
if (value === '%00' || value === 'null') {
value = null
}
const parseValue = parseFloat(value)
if (!isNaN(parseValue) && isFinite(value)) {
value = parseValue
}
return value
})
}
/**
* Extract options from request query and return the object subset.
*/
getOptionsFromQuery(query) {
return this._parseQuery(pick(query, footprintOptions))
}
/**
* Extract the criteria from the query
*/
getCriteriaFromQuery(query) {
return this._parseQuery(omit(query, footprintOptions))
}
constructor (app, config) {
if (!config) {
throw new Error('ServerTrailpack must be subclassed. Do not load it directly.')
}
super(app, config)
}
static get type () {
return 'server'
}
}