Repository navigation
Expand file tree
/
Copy pathrequest-builder.js
More file actions
45 lines (40 loc) · 1.66 KB
/
Copy pathrequest-builder.js
File metadata and controls
45 lines (40 loc) · 1.66 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
const BODYLESS_METHODS = ['GET', 'DELETE', 'HEAD'];
const hasBody = (operation) => !BODYLESS_METHODS.includes(operation.method);
const belongsInBody = (operation, name) => hasBody(operation) && (operation.body.includes(name) || !operation.query.includes(name));
function fillPath(operation, params) {
let path = operation.path;
for (const name of operation.path_params) {
if (params[name] === undefined || params[name] === null) {
throw new Error(`Missing path parameter '${name}' for ${operation.key}`);
}
path = path.replace(`{${name}}`, encodeURIComponent(String(params[name])));
delete params[name];
}
return path;
}
/** Splits flat params into path, query and body according to the operation. */
export function buildRequest(operation, params) {
const remaining = { ...params };
const path = fillPath(operation, remaining);
const query = {};
const body = {};
for (const [name, value] of Object.entries(remaining)) {
if (value === undefined || value === null) continue;
(belongsInBody(operation, name) ? body : query)[name] = value;
}
return {
method: operation.method,
path,
query: Object.keys(query).length ? query : undefined,
body: hasBody(operation) && Object.keys(body).length ? body : undefined,
};
}
export function encodeQuery(params) {
const out = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
if (value === undefined || value === null) continue;
if (Array.isArray(value)) value.forEach((item) => out.append(`${key}[]`, String(item)));
else out.append(key, typeof value === 'boolean' ? (value ? 'true' : 'false') : String(value));
}
return out.toString();
}