|
| 1 | +/* eslint-disable no-use-before-define */ |
1 | 2 | const request = require('request');
|
2 | 3 | const qs = require('querystring');
|
3 |
| -const messages = require('elasticio-node').messages; |
| 4 | +const { messages } = require('elasticio-node'); |
4 | 5 |
|
5 | 6 | exports.putOrPost = function putOrPost(method, msg, conf) {
|
6 |
| - const uri = conf.uri; |
7 |
| - const body = msg.body; |
| 7 | + const { uri } = conf; |
| 8 | + const { body } = msg; |
8 | 9 |
|
9 |
| - this.logger.info('Request body: %j', body); |
| 10 | + this.logger.info('Request body: %j', body); |
10 | 11 |
|
11 |
| - const requestSettings = buildRequestSettings(method, uri, conf.secret); |
12 |
| - requestSettings.body = JSON.stringify(body); |
13 |
| - requestSettings.headers['Content-Type'] = 'application/json;charset=UTF-8'; |
| 12 | + const requestSettings = buildRequestSettings(method, uri, conf.secret); |
| 13 | + requestSettings.body = JSON.stringify(body); |
| 14 | + requestSettings.headers['Content-Type'] = 'application/json;charset=UTF-8'; |
14 | 15 |
|
15 |
| - request(requestSettings, callback.bind(this)); |
| 16 | + request(requestSettings, callback.bind(this)); |
16 | 17 | };
|
17 | 18 |
|
18 | 19 | exports.get = function get(msg, conf) {
|
19 |
| - let uri = conf.uri; |
| 20 | + let { uri } = conf; |
20 | 21 |
|
21 |
| - // Check if URI ends in ? If it doesn't add one. |
22 |
| - if (uri.charAt(uri.length - 1) !== '?') { |
23 |
| - uri += '?'; |
24 |
| - } |
| 22 | + // Check if URI ends in ? If it doesn't add one. |
| 23 | + if (uri.charAt(uri.length - 1) !== '?') { |
| 24 | + uri += '?'; |
| 25 | + } |
25 | 26 |
|
26 |
| - uri += qs.stringify(msg.body); |
| 27 | + uri += qs.stringify(msg.body); |
27 | 28 |
|
28 |
| - const requestSettings = buildRequestSettings('GET', uri, conf.secret); |
29 |
| - request(requestSettings, callback.bind(this)); |
| 29 | + const requestSettings = buildRequestSettings('GET', uri, conf.secret); |
| 30 | + request(requestSettings, callback.bind(this)); |
30 | 31 | };
|
31 | 32 |
|
32 | 33 | function buildRequestSettings(method, uri, secret) {
|
33 |
| - const requestSettings = { |
34 |
| - uri: uri, |
35 |
| - method: method, |
36 |
| - headers: {} |
37 |
| - }; |
| 34 | + const requestSettings = { |
| 35 | + uri, |
| 36 | + method, |
| 37 | + headers: {}, |
| 38 | + }; |
38 | 39 |
|
39 |
| - if (secret) { |
40 |
| - requestSettings.headers['X-Api-Secret'] = secret; |
41 |
| - } |
| 40 | + if (secret) { |
| 41 | + requestSettings.headers['X-Api-Secret'] = secret; |
| 42 | + } |
42 | 43 |
|
43 |
| - return requestSettings; |
| 44 | + return requestSettings; |
44 | 45 | }
|
45 | 46 |
|
46 | 47 |
|
47 | 48 | function callback(err, response, body) {
|
48 |
| - if (err) { |
49 |
| - this.emit('error', err); |
50 |
| - this.emit('end'); |
51 |
| - return; |
52 |
| - } |
53 |
| - |
54 |
| - const sc = response.statusCode; |
55 |
| - |
56 |
| - if (sc >= 200 && sc <= 206) { |
57 |
| - this.emit('data', newMessage(response, body)); |
58 |
| - } else { |
59 |
| - this.emit('error', new Error('Endpoint responds with ' + sc)); |
60 |
| - } |
| 49 | + if (err) { |
| 50 | + this.emit('error', err); |
61 | 51 | this.emit('end');
|
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const sc = response.statusCode; |
| 56 | + |
| 57 | + if (sc >= 200 && sc <= 206) { |
| 58 | + this.emit('data', newMessage(response, body)); |
| 59 | + } else { |
| 60 | + this.emit('error', new Error(`Endpoint responds with ${sc}`)); |
| 61 | + } |
| 62 | + this.emit('end'); |
62 | 63 | }
|
63 | 64 |
|
64 | 65 | function newMessage(response, body) {
|
65 |
| - const headers = response.headers; |
66 |
| - const contentType = headers['content-type']; |
67 |
| - const msgBody = getJSONBody(contentType, body); |
68 |
| - const msg = messages.newMessageWithBody(msgBody); |
69 |
| - msg.headers = headers; |
| 66 | + const { headers } = response; |
| 67 | + const contentType = headers['content-type']; |
| 68 | + const msgBody = getJSONBody(contentType, body); |
| 69 | + const msg = messages.newMessageWithBody(msgBody); |
| 70 | + msg.headers = headers; |
70 | 71 |
|
71 |
| - return msg; |
| 72 | + return msg; |
72 | 73 | }
|
73 | 74 |
|
74 | 75 | function getJSONBody(contentType, body) {
|
75 |
| - if (contentType && contentType.indexOf('application/json') === 0) { |
76 |
| - return JSON.parse(body); |
77 |
| - } |
| 76 | + if (contentType && contentType.indexOf('application/json') === 0) { |
| 77 | + return JSON.parse(body); |
| 78 | + } |
78 | 79 |
|
79 |
| - return { |
80 |
| - responseBody: body |
81 |
| - }; |
| 80 | + return { |
| 81 | + responseBody: body, |
| 82 | + }; |
82 | 83 | }
|
0 commit comments