forked from rudderlabs/rudder-transformer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
routerUtils.js
61 lines (55 loc) · 1.63 KB
/
routerUtils.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
const logger = require("./logger");
const { proxyRequest } = require("./adapters/network");
const { nodeSysErrorToStatus } = require("./adapters/utils/networkUtils");
let areFunctionsEnabled = -1;
const functionsEnabled = () => {
if (areFunctionsEnabled === -1) {
areFunctionsEnabled = process.env.ENABLE_FUNCTIONS === "false" ? 0 : 1;
}
return areFunctionsEnabled === 1;
};
const userTransformHandler = () => {
if (functionsEnabled()) {
return require("./util/customTransformer").userTransformHandler;
}
throw new Error("Functions are not enabled");
};
async function sendToDestination(destination, payload) {
let parsedResponse;
logger.info("Request recieved for destination", destination);
const resp = await proxyRequest(payload);
if (resp.success) {
const { response } = resp;
parsedResponse = {
headers: response.headers,
response: response.data,
status: response.status
};
return parsedResponse;
}
const { response: error } = resp;
// handling axios error case
if (!error.response && error.code) {
const nodeSysErr = nodeSysErrorToStatus(error.code);
parsedResponse = {
headers: null,
networkFailure: true,
response: nodeSysErr.message,
status: nodeSysErr.status
};
} else {
parsedResponse = {
headers: error.response.headers,
status: error.response.status,
response:
error.response.data || "Error occurred while processing payload."
};
}
return parsedResponse;
}
module.exports = {
sendToDestination,
userTransformHandler
};