-
Notifications
You must be signed in to change notification settings - Fork 31
/
postData.js
122 lines (111 loc) · 2.88 KB
/
postData.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
const params = require('./params')
const {
empty,
emptyObject,
seralizeURLSearchParams,
getContentTypeValue,
} = require('../aid')
const { InvalidArchiveError } = require('../error')
const { createPostDataPath } = require('./utils/path')
const { createEntriesIndexes } = require('./utils/indexes')
/*
* mimeType: required string
* params: optional array
* text: optional string
* comment: optional string
*
* max 1 nonempty params|text
* structured data type application/x-www-form-urlencoded|multipart/form-data
*/
function postData(node, i, assay) {
if (!emptyObject(node)) {
validate(node, i)
if (node.params) {
params(node.params, i, assay)
}
}
}
function validate(node, i) {
const mimeType = empty(node.mimeType) ? '' : node.mimeType
if (mimeType === '') {
console.warn(
`[WARN] Post data MIME type is missing and will be inferred from the content (${i})`
)
}
if (typeof mimeType !== 'string') {
throw new InvalidArchiveError(
createErrorParams({
name: 'InvalidPostDataType',
index: i,
path: 'mimeType',
}),
`Post data MIME type must be a string`
)
}
if (node.params && !Array.isArray(node.params)) {
throw new InvalidArchiveError(
createErrorParams({
name: 'InvalidPostDataParams',
index: i,
path: 'params',
}),
`Post data params are invalid, must be an array`
)
}
if (node.text && typeof node.text !== 'string') {
throw new InvalidArchiveError(
createErrorParams({
name: 'InvalidPostDataText',
index: i,
path: 'text',
}),
`Post data text must be a string`
)
}
if (node.comment && typeof node.comment !== 'string') {
throw new InvalidArchiveError(
createErrorParams({
name: 'InvalidPostDataComment',
index: i,
path: 'comment',
}),
`Post data comment must be a string`
)
}
if (
node.params &&
node.params.length &&
node.text &&
node.mimeType.includes('application/x-www-form-urlencoded')
) {
if (seralizeURLSearchParams(node.params) !== node.text) {
console.warn(
`[WARN] Post data conflict (${i}): Both params and text are provided but the values do not match.`
)
}
}
if (
node.params &&
node.params.length &&
!['application/x-www-form-urlencoded', 'multipart/form-data'].includes(
getContentTypeValue(node.mimeType)
)
) {
throw new InvalidArchiveError(
createErrorParams({
name: 'InvalidPostDataType',
index: i,
path: 'mimeType',
}),
`Post data MIME type must be one of "multipart/form-data", "application/x-www-form-urlencoded"`
)
}
}
function createErrorParams({ name, index, path = '' }) {
return {
name,
path: createPostDataPath(index, path),
indexes: createEntriesIndexes(index),
}
}
module.exports = postData