-
Notifications
You must be signed in to change notification settings - Fork 762
/
parameter-builders.js
126 lines (114 loc) · 3.07 KB
/
parameter-builders.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
121
122
123
124
125
126
import stylize from './style-serializer'
export default {
path,
query,
header,
cookie
}
function path({req, value, parameter}) {
const {name, style, explode} = parameter
const styledValue = stylize({
key: parameter.name,
value,
style: style || 'simple',
explode: explode || false,
escape: !parameter.allowReserved,
})
req.url = req.url.replace(`{${name}}`, styledValue)
}
function query({req, value, parameter}) {
req.query = req.query || {}
if (value === false) {
value = 'false'
}
if (value === 0) {
value = '0'
}
if (value) {
const type = typeof value
if (parameter.style === 'deepObject') {
const valueKeys = Object.keys(value)
valueKeys.forEach((k) => {
const v = value[k]
req.query[`${parameter.name}[${k}]`] = {
value: stylize({
key: k,
value: v,
style: 'deepObject',
escape: !parameter.allowReserved,
}),
skipEncoding: true
}
})
}
else if (
type === 'object' &&
!Array.isArray(value) &&
(parameter.style === 'form' || !parameter.style) &&
(parameter.explode || parameter.explode === undefined)
) {
// form explode needs to be handled here,
// since we aren't assigning to `req.query[parameter.name]`
// like we usually do.
const valueKeys = Object.keys(value)
valueKeys.forEach((k) => {
const v = value[k]
req.query[k] = {
value: stylize({
key: k,
value: v,
style: parameter.style || 'form',
escape: !parameter.allowReserved,
})
}
})
}
else {
req.query[parameter.name] = {
value: stylize({
key: parameter.name,
value,
style: parameter.style || 'form',
explode: typeof parameter.explode === 'undefined' ? true : parameter.explode,
escape: !parameter.allowReserved,
}),
skipEncoding: true
}
}
}
else if (parameter.allowEmptyValue) {
const paramName = parameter.name
req.query[paramName] = req.query[paramName] || {}
req.query[paramName].allowEmptyValue = true
}
}
function header({req, parameter, value}) {
req.headers = req.headers || {}
if (typeof value !== 'undefined') {
req.headers[parameter.name] = stylize({
key: parameter.name,
value,
style: parameter.style || 'simple',
explode: typeof parameter.explode === 'undefined' ? false : parameter.explode,
escape: !parameter.allowReserved,
})
}
}
function cookie({req, parameter, value}) {
req.headers = req.headers || {}
const type = typeof value
if (type !== 'undefined') {
const prefix = (
type === 'object' &&
!Array.isArray(value) &&
parameter.explode
) ? '' : `${parameter.name}=`
req.headers.Cookie = prefix + stylize({
key: parameter.name,
value,
escape: false,
style: parameter.style || 'form',
explode: typeof parameter.explode === 'undefined' ? false : parameter.explode
})
}
}