1 | const client = require('./client');
|
2 |
|
3 | module.exports = {
|
4 | get: async (params, options) => _http('GET', params, false, true, options),
|
5 | head: async (params, options) => _http('HEAD', params, false, false, options),
|
6 | put: async (params, options) => _http('PUT', params, true, true, options),
|
7 | post: async (params, options) => _http('POST', params, true, true, options),
|
8 | patch: async (params, options) => _http('PATCH', params, true, true, options),
|
9 | delete: async (params, options) => _http('DELETE', params, false, true, options),
|
10 | options: async (params, options) => _http('OPTIONS', params, false, true, options)
|
11 | };
|
12 |
|
13 | async function _http(verb, params, sendBody, receiveBody, { pluginConfig, setOutput }) {
|
14 | const headers = params.headers || {};
|
15 | const options = {
|
16 | followRedirects: (params.followRedirect !== undefined)
|
17 | ? params.followRedirect : true,
|
18 | maxRedirects: params.maxRedirects || 10,
|
19 | insecureSSL: !!params.insecure,
|
20 | decode: false,
|
21 |
|
22 |
|
23 | encoding: (params.encoding && params.encoding.toLowerCase() === 'binary')
|
24 | ? null : params.encoding
|
25 | };
|
26 |
|
27 | const reqBody = (params.hasOwnProperty('body') && sendBody) ? params.body : undefined;
|
28 | if (pluginConfig.proxy) {
|
29 | options.proxy = pluginConfig.proxy;
|
30 | }
|
31 | const { response, body }
|
32 | = await client.invoke(verb, params.url, reqBody, headers, options);
|
33 |
|
34 | const statusType = (response.status / 100) | 0;
|
35 | if (statusType) {
|
36 | const resp = {
|
37 | status: response.status,
|
38 | headers: response.headers
|
39 | };
|
40 |
|
41 | if (receiveBody) {
|
42 | resp.body = body;
|
43 | }
|
44 | return setOutput(`${statusType}xx`, resp);
|
45 | } else {
|
46 | throw new Error(response.error);
|
47 | }
|
48 | }
|