-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (27 loc) · 984 Bytes
/
index.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
const qs = require('qs');
module.exports = function (context) {
context.toFetch = () => {
const padLength = 4;
const pad1 = ''.padStart(padLength, ' ');
const pad2 = ''.padStart(padLength * 2, ' ');
const qsRaw = qs.stringify(context.qs);
const parts = [
`fetch("${context.url}${qsRaw ? `?${qsRaw}` : ''}", {`
];
if (context._header && Object.keys(context._header).length) {
parts.push(`${pad1}"headers": {\n${Object.entries(context._header)
.map(([name, value]) => `${pad2}"${name}": "${value}"`)
.join(',\n')}
${pad1}},`)
}
if (context._data) {
parts.push(`${pad1}"body": "${JSON.stringify(context._data)}",`)
} else {
parts.push(`${pad1}"body": null,`)
}
parts.push(`${pad1}"method": "${context.method}"`)
parts.push(`});`)
return parts.join(`\n`);
}
return context;
};