-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (28 loc) · 1.11 KB
/
index.js
File metadata and controls
31 lines (28 loc) · 1.11 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
module.exports.requestHooks = [
context => {
const params = context
.request
.getParameters()
.sort((param1, param2) => param1.name.length - param2.name.length);
const url = new URL(context.request.getUrl());
const body = context.request.getBody();
let path = url.pathname;
let bodyText = body.text;
for (const {name, value} of params) {
if (!name) continue;
const urlParamPattern = `:${name}`;
const bodyParamPattern = '${' + name + '}';
if (path.includes(urlParamPattern)) {
path = path.replaceAll(urlParamPattern, encodeURIComponent(value));
url.pathname = path;
context.request.removeParameter(name);
} else if (bodyText.includes(bodyParamPattern)) {
bodyText = bodyText.replaceAll(bodyParamPattern, value);
context.request.removeParameter(name);
}
}
context.request.setUrl(url.toString());
body.text = bodyText;
context.request.setBody(body);
},
];