playov2-js-utilities 0.3.51 → 0.3.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -13,5 +13,8 @@ module.exports = {
13
13
  MessagePublisher: require('./message_publisher'),
14
14
  middleware: require('./middleware'),
15
15
  profanityFilter: require("./profanityFilter/profanityFilter"),
16
- playoResponseHandler: require("./responseHandler/responseHandler")
16
+ playoResponseHandler: require("./responseHandler/responseHandler"),
17
+ PLAYO_ERROR_HANDLER: require("./playo.utils/playo.error.handler"),
18
+ PLAYO_HTTP_HANDLER: require("./playo.utils/playo.http.handler"),
19
+ PLAYO_RESPONSE_GENERATOR: require("./playo.utils/playo.res.generator"),
17
20
  };
@@ -0,0 +1,24 @@
1
+ const {generatePlayoResponse} = require("./playo.res.generator");
2
+ const LOGGER = require("../logger");
3
+
4
+ const playoError = (error, sourceFunction = "") => {
5
+ return {
6
+ stack: error.stack,
7
+ source: {
8
+ sourceFunction,
9
+ errorType: error.name || "Unknown error"
10
+ },
11
+ type: error.type
12
+ }
13
+ }
14
+
15
+ const badResponseErrorHandler = (error, message = "", requestId) => {
16
+ const {stack, source = {}} = error;
17
+ LOGGER.prepareErrorLog(requestId, {stack, source}, message);
18
+ return generatePlayoResponse("BAD_REQUEST", 0, {message}, "OOPs!! something does not seem right, we are on it ;)");
19
+ }
20
+
21
+ module.exports = {
22
+ badResponseErrorHandler,
23
+ playoError
24
+ }
@@ -0,0 +1,148 @@
1
+ const Axios = require('axios');
2
+ const TIMEOUT_DURATION = 15 * 1000 // seconds, used as default
3
+ const Logger = require('../logger.js');
4
+
5
+
6
+ /**
7
+ * Axios response handler
8
+ * @param {Response} resp
9
+ * resp structure |
10
+ * on success {
11
+ * status: HTTP status code returned by the service
12
+ * data,
13
+ * config: request config
14
+ * }
15
+ * on fail : {
16
+ * code: error code | http errorcode like ECONNREFUSED, ETIMEOUT etc
17
+ * message: error message | description of the error code
18
+ * config: request config
19
+ * }
20
+ * @param {String} requestId
21
+ * @returns
22
+ */
23
+ const playoResponseHandler = (resp, requestId) => {
24
+ if (resp.isAxiosError) {
25
+ //if axios error is triggered then axios throws HTTP code for timeout or other forms of unsuccessful errors
26
+ const {
27
+ code = "UNCAUGHT_ERROR",
28
+ message = "encountered uncaught error",
29
+ config = { url: '' },
30
+ response = { data: { requestStatus: 0 } }
31
+ } = resp;
32
+ const { url } = config;
33
+ const { data = { requestStatus: 0, statusError: "POTENTIAL-BAD-GATEWAY" }} = response;
34
+ data["message"] = message;
35
+ Logger.prepareErrorLog(requestId, { code, message }, `Api call to url to ${url} failed`)
36
+ throw (data);
37
+ }
38
+
39
+ const { status, data, config = { url: '' } } = resp;
40
+ const { url = '' } = config;
41
+ if (status >= 200 && status < 400) {
42
+ const { requestStatus, message = 'success' } = data;
43
+ if (!requestStatus && requestStatus != 0) { //if the service does not provide request status
44
+ data["requestStatus"] = 1;
45
+ }
46
+ Logger.prepareInfoLog(requestId, { status, requestStatus: data.requestStatus, message }, `Api call to url to ${url} succeeded with status ${status}`)
47
+ return data
48
+ }
49
+
50
+ else { // ideally this should not get triggered unless there is fault in calling lib itself
51
+ Logger.prepareErrorLog(requestId, { response: resp }, "axios error");
52
+ throw ({ requestStatus: 0, message: "Failed to handle response" })
53
+ }
54
+ }
55
+
56
+
57
+ /**
58
+ * Makes a get http request - throws error to be handled by calling function
59
+ * @param {String} url
60
+ * @param {Object} headers
61
+ * @param {Object} query
62
+ * @param {String} requestId
63
+ * @param {Boolean} shouldTimeOut - Defaults to false
64
+ * @param {Number} timeout - in seconds, Defaults to 10 seconds
65
+ * @param {Function} responseHandler - Caller should pass it's own http response handler. Defaults to playo's response function handler
66
+ */
67
+ const get = async (url, headers = {}, query = {}, requestId = '', responseHandler = playoResponseHandler, timeout = TIMEOUT_DURATION, apiName = '') => {
68
+ try {
69
+ const resp = await Axios.get(url, { headers, params: query, timeout });
70
+ return responseHandler(resp, requestId, apiName);
71
+ } catch (error) {
72
+ Logger.prepareErrorLog(requestId, { url, headers, query, data: error.response }, `Api call to url to ${url} failed!`);
73
+ return responseHandler(error.response, requestId, apiName);
74
+ }
75
+ };
76
+
77
+ /**
78
+ * Makes a POST http request - throws error to be handled by calling function
79
+ * @param {String} url
80
+ * @param {Object} headers
81
+ * @param {Object} query
82
+ * @param {Object} body
83
+ * @param {String} requestId
84
+ * @param {Boolean} shouldTimeOut - Defaults to false
85
+ * @param {Number} timeout - in seconds, Defaults to 10 seconds
86
+ * @param {Function} responseHandler - Caller should pass it's own http response handler. Defaults to playo's response function handler
87
+ */
88
+ const post = async (url, headers = {}, query = {}, body = {}, requestId = '', responseHandler = playoResponseHandler, timeout = TIMEOUT_DURATION, apiName = '') => {
89
+ try {
90
+ const options = { headers, params: query, timeout };
91
+ const resp = await Axios.post(url, body, options);
92
+ return responseHandler(resp, requestId, apiName);
93
+ } catch (error) {
94
+ Logger.prepareErrorLog(requestId, { url, headers, query, body, data: error.response.data }, `Api call to url to ${url} failed due to ${error.response.message}`);
95
+ return responseHandler(error.response, requestId, apiName);
96
+ }
97
+ };
98
+
99
+ /**
100
+ * Makes a PUT http request - throws error to be handled by calling function
101
+ * @param {String} url
102
+ * @param {Object} headers
103
+ * @param {Object} query
104
+ * @param {Object} body
105
+ * @param {String} requestId
106
+ * @param {Boolean} shouldTimeOut - Defaults to false
107
+ * @param {Number} timeout - in seconds, Defaults to 10 seconds
108
+ * @param {Function} responseHandler - Caller should pass it's own http response handler. Defaults to playo's response function handler
109
+ */
110
+ const put = async (url, headers = {}, query = {}, body = {}, requestId = '', responseHandler = playoResponseHandler, timeout = TIMEOUT_DURATION, apiName = '') => {
111
+ try {
112
+ const options = { headers, params: query, timeout };
113
+ const resp = await Axios.put(url, body, options);
114
+ return responseHandler(resp, requestId, apiName);
115
+ } catch (error) {
116
+ Logger.prepareErrorLog(requestId, { url, headers, query, body, data: error.response.data }, `Api call to url to ${url} failed due to ${error.response.message}`);
117
+ return responseHandler(error.response, requestId, apiName);
118
+ }
119
+ };
120
+
121
+ /**
122
+ * Makes a DELETE http request - throws error to be handled by calling function
123
+ * @param {String} url
124
+ * @param {Object} headers
125
+ * @param {Object} query
126
+ * @param {Object} body
127
+ * @param {String} requestId
128
+ * @param {Boolean} shouldTimeOut - Defaults to false
129
+ * @param {Number} timeout - in seconds, Defaults to 10 seconds
130
+ * @param {Function} responseHandler - Caller should pass it's own http response handler. Defaults to playo's response function handler
131
+ */
132
+ const remove = async (url, headers = {}, query = {}, body, requestId = '', responseHandler = playoResponseHandler, timeout = TIMEOUT_DURATION) => {
133
+ try {
134
+ const options = { headers, params: query, data: body, timeout };
135
+ const resp = await Axios.delete(url, options);
136
+ return responseHandler(resp, requestId);
137
+ } catch (error) {
138
+ Logger.prepareErrorLog(requestId, { url, headers, query, body, data: error.response.data }, `Api call to url to ${url} failed due to ${error.response.message}`);
139
+ return responseHandler(error.response, requestId, apiName);
140
+ }
141
+ };
142
+
143
+ module.exports = {
144
+ get,
145
+ post,
146
+ put,
147
+ remove
148
+ }
@@ -0,0 +1,58 @@
1
+ // these file's functionality should be in utils.js file
2
+ const httpStatusMap = {
3
+ 'OK': 200,
4
+ 'BAD_REQUEST': 400,
5
+ 'URL_NOT_FOUND': 404,
6
+ 'INTERNAL_ERROR': 500,
7
+ };
8
+
9
+ const responseStatusMessageMap = {
10
+ 1: "Success",
11
+ 0: "Failed",
12
+ 2: "Invalid Request",
13
+ 3: "warning",
14
+ }
15
+
16
+ /**
17
+ * Function to generate dynamic playo response
18
+ * @typedef {"OK" | "BAD_REQUEST" | "URL_NOT_FOUND" | "INTERNAL_ERROR" } httpMessage
19
+ * @typedef {0 | 1 | 2 | 3} requestStatus
20
+ * @param {httpMessage} httpMessage ['OK', 'BAD_REQUEST', 'URL_NOT_FOUND', 'INTERNAL_ERROR']
21
+ * @param {requestStatus} requestStatus [0, 1, 2, 3]
22
+ * @param {Object} jsonData
23
+ * @param {String} customMessage
24
+ * @returns {Object} -
25
+ * {
26
+ * httpStatus: 200,
27
+ * httpMessage: 'Ok',
28
+ * playoResponse: {
29
+ * requestStatus: 1,
30
+ * message: "Success",
31
+ * data: {}
32
+ * }
33
+ * }
34
+ */
35
+ const generatePlayoResponse = (httpMessage = 'Ok', requestStatus = 1, jsonData = {}, customMessage = null) => {
36
+
37
+ const httpStatus = httpStatusMap[httpMessage] || 200;
38
+
39
+ const playoResponse = {
40
+ requestStatus,
41
+ message: responseStatusMessageMap[requestStatus] || "Success",
42
+ data: jsonData
43
+ }
44
+
45
+ if(customMessage) {
46
+ playoResponse.message = customMessage;
47
+ }
48
+
49
+ return {
50
+ httpStatus,
51
+ httpMessage,
52
+ playoResponse
53
+ }
54
+ }
55
+
56
+ module.exports = {
57
+ generatePlayoResponse
58
+ }
@@ -27,7 +27,7 @@ const responseHandler = (resp, requestId) => {
27
27
  response = { data: { requestStatus: 0 } }
28
28
  } = resp;
29
29
  const { url } = config;
30
- const { data } = response;
30
+ const { data = { requestStatus: 0, statusError: "POTENTIAL-BAD-GATEWAY" }} = response;
31
31
  data["message"] = message;
32
32
  Logger.prepareErrorLog(requestId, { code, message }, `Api call to url to ${url} failed`)
33
33
  throw (data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playov2-js-utilities",
3
- "version": "0.3.51",
3
+ "version": "0.3.53",
4
4
  "description": "Private package for JS utility functions",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,7 +13,7 @@
13
13
  "keywords": [
14
14
  "Playo"
15
15
  ],
16
- "author": "Saurabh Agrawal",
16
+ "author": "Avish",
17
17
  "license": "ISC",
18
18
  "homepage": "https://bitbucket.org/techmash/playov2-js-utilities#readme",
19
19
  "devDependencies": {