drapcode-utility 1.9.6 → 1.9.78
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/build/encryption/utility.d.ts +1 -2
- package/build/encryption/utility.js +4 -39
- package/build/format-fields/index.js +1 -1
- package/build/index.d.ts +3 -1
- package/build/index.js +3 -1
- package/build/utils/check-error.js +7 -7
- package/build/utils/common-util.d.ts +78 -0
- package/build/utils/common-util.js +219 -0
- package/build/utils/date-util.js +0 -13
- package/build/utils/file-util.d.ts +7 -0
- package/build/utils/file-util.js +118 -0
- package/build/utils/prepare-query.d.ts +0 -148
- package/build/utils/prepare-query.js +1 -100
- package/build/utils/project-util.d.ts +1 -0
- package/build/utils/project-util.js +31 -49
- package/build/utils/{query-paser-new.js → query-parser-new.js} +24 -174
- package/build/utils/query-parser.d.ts +0 -12
- package/build/utils/query-parser.js +16 -280
- package/build/utils/query-utils.d.ts +20 -0
- package/build/utils/query-utils.js +254 -0
- package/build/utils/rest-client.d.ts +6 -0
- package/build/utils/rest-client.js +342 -0
- package/build/utils/util.d.ts +77 -3
- package/build/utils/util.js +511 -32
- package/package.json +10 -5
- /package/build/utils/{query-paser-new.d.ts → query-parser-new.d.ts} +0 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getValueOfProjectConstant = exports.isFixedValueQuery = exports.getMinMaxValue = exports.checkDate = exports.modifyQuery = exports.mongoSelectQuery = exports.getSearchObjQuery = void 0;
|
|
13
|
+
var drapcode_constant_1 = require("drapcode-constant");
|
|
14
|
+
var date_util_1 = require("./date-util");
|
|
15
|
+
var util_1 = require("./util");
|
|
16
|
+
var getSearchObjQuery = function (searchObj, searchQueryTypeObj, timezone) {
|
|
17
|
+
var searchAggregateQuery = [];
|
|
18
|
+
var likeQuery = [];
|
|
19
|
+
Object.entries(searchObj).forEach(function (_a) {
|
|
20
|
+
var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
|
21
|
+
var key = _a[0], value = _a[1];
|
|
22
|
+
if (value) {
|
|
23
|
+
var searchQueryTypeKey = searchQueryTypeObj[key];
|
|
24
|
+
if (searchQueryTypeKey) {
|
|
25
|
+
if (searchQueryTypeKey === "boolean") {
|
|
26
|
+
// if input is string and db field type is boolean
|
|
27
|
+
searchAggregateQuery.push({
|
|
28
|
+
$addFields: (_b = {},
|
|
29
|
+
_b["_".concat(key)] = "$".concat(key),
|
|
30
|
+
_b),
|
|
31
|
+
});
|
|
32
|
+
likeQuery.push((_c = {},
|
|
33
|
+
_c["_".concat(key)] = toBoolean(value),
|
|
34
|
+
_c));
|
|
35
|
+
}
|
|
36
|
+
else if (__spreadArray(__spreadArray([], drapcode_constant_1.OptionTypeFields, true), drapcode_constant_1.BelongsToReferenceField, true).includes(searchQueryTypeKey)) {
|
|
37
|
+
// if input is string and db field type is array
|
|
38
|
+
searchAggregateQuery.push({
|
|
39
|
+
$addFields: (_d = {},
|
|
40
|
+
_d["_".concat(key)] = "$".concat(key),
|
|
41
|
+
_d),
|
|
42
|
+
});
|
|
43
|
+
likeQuery.push((_e = {},
|
|
44
|
+
_e["_".concat(key)] = { $all: [value] },
|
|
45
|
+
_e));
|
|
46
|
+
}
|
|
47
|
+
else if (searchQueryTypeKey === "tel") {
|
|
48
|
+
// handling value when db field type is tel
|
|
49
|
+
searchAggregateQuery.push({
|
|
50
|
+
$addFields: (_f = {},
|
|
51
|
+
_f["_".concat(key)] = { $toString: "$".concat(key) },
|
|
52
|
+
_f),
|
|
53
|
+
});
|
|
54
|
+
likeQuery.push((_g = {},
|
|
55
|
+
_g["_".concat(key)] = {
|
|
56
|
+
$regex: value.startsWith("+") ? value.replace("+", "") : value,
|
|
57
|
+
$options: "i",
|
|
58
|
+
},
|
|
59
|
+
_g));
|
|
60
|
+
}
|
|
61
|
+
else if (searchQueryTypeKey === "number") {
|
|
62
|
+
if (key.startsWith("min_") || key.startsWith("max_")) {
|
|
63
|
+
var fieldKey = key.replace("min_", "");
|
|
64
|
+
fieldKey = fieldKey.replace("max_", "");
|
|
65
|
+
var numericValue = Number(value);
|
|
66
|
+
if (key.startsWith("min_")) {
|
|
67
|
+
likeQuery.push((_h = {}, _h["".concat(fieldKey)] = { $gte: numericValue }, _h));
|
|
68
|
+
}
|
|
69
|
+
else if (key.startsWith("max_")) {
|
|
70
|
+
likeQuery.push((_j = {}, _j["".concat(fieldKey)] = { $lte: numericValue }, _j));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// handling value when db field type is Double/Int
|
|
75
|
+
searchAggregateQuery.push({
|
|
76
|
+
$addFields: (_k = {},
|
|
77
|
+
_k["_".concat(key)] = { $toString: { $toLong: "$".concat(key) } },
|
|
78
|
+
_k),
|
|
79
|
+
});
|
|
80
|
+
likeQuery.push((_l = {},
|
|
81
|
+
_l["_".concat(key)] = {
|
|
82
|
+
$regex: value,
|
|
83
|
+
$options: "i",
|
|
84
|
+
},
|
|
85
|
+
_l));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (__spreadArray(__spreadArray([
|
|
89
|
+
drapcode_constant_1.FieldTypes.date.id
|
|
90
|
+
], drapcode_constant_1.systemDateField, true), [
|
|
91
|
+
drapcode_constant_1.FieldTypes.time_slot.id,
|
|
92
|
+
], false).includes(searchQueryTypeKey)) {
|
|
93
|
+
if (key.startsWith("start_") || key.startsWith("end_")) {
|
|
94
|
+
var fieldKey = key.replace("start_", "");
|
|
95
|
+
fieldKey = fieldKey.replace("end_", "");
|
|
96
|
+
searchAggregateQuery.push({
|
|
97
|
+
$addFields: (_m = {},
|
|
98
|
+
_m["_".concat(key)] = { $toString: "$".concat(fieldKey) },
|
|
99
|
+
_m),
|
|
100
|
+
});
|
|
101
|
+
if (key.startsWith("start_")) {
|
|
102
|
+
likeQuery.push((_o = {},
|
|
103
|
+
_o["_".concat(key)] = {
|
|
104
|
+
$gte: (0, date_util_1.timezoneDateParse)(value, true, true),
|
|
105
|
+
},
|
|
106
|
+
_o));
|
|
107
|
+
}
|
|
108
|
+
else if (key.startsWith("end_")) {
|
|
109
|
+
likeQuery.push((_p = {},
|
|
110
|
+
_p["_".concat(key)] = {
|
|
111
|
+
$lt: (0, date_util_1.timezoneDateParse)(value, true),
|
|
112
|
+
},
|
|
113
|
+
_p));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
// if input is string and db field type is number
|
|
119
|
+
// converting db field to string first
|
|
120
|
+
searchAggregateQuery.push({
|
|
121
|
+
$addFields: (_q = {},
|
|
122
|
+
_q["_".concat(key)] = { $toString: "$".concat(key) },
|
|
123
|
+
_q),
|
|
124
|
+
});
|
|
125
|
+
likeQuery.push((_r = {},
|
|
126
|
+
_r["_".concat(key)] = {
|
|
127
|
+
$regex: value,
|
|
128
|
+
$options: "i",
|
|
129
|
+
},
|
|
130
|
+
_r));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
likeQuery.push((_s = {},
|
|
135
|
+
_s[key] = {
|
|
136
|
+
$regex: value,
|
|
137
|
+
$options: "i",
|
|
138
|
+
},
|
|
139
|
+
_s));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
console.log("==> searchAggregateQuery", JSON.stringify(searchAggregateQuery));
|
|
144
|
+
console.log("==> likeQuery", JSON.stringify(likeQuery));
|
|
145
|
+
return { searchAggregateQuery: searchAggregateQuery, likeQuery: likeQuery };
|
|
146
|
+
};
|
|
147
|
+
exports.getSearchObjQuery = getSearchObjQuery;
|
|
148
|
+
var mongoSelectQuery = function (query) {
|
|
149
|
+
if (query === void 0) { query = {}; }
|
|
150
|
+
var projection = {};
|
|
151
|
+
var fieldsInclude = query.fieldsInclude, fieldsExclude = query.fieldsExclude, finder = query.finder;
|
|
152
|
+
if (!fieldsExclude)
|
|
153
|
+
fieldsExclude = [];
|
|
154
|
+
if (fieldsInclude[0] === "*") {
|
|
155
|
+
fieldsExclude.map(function (field) {
|
|
156
|
+
if (field)
|
|
157
|
+
return (projection["".concat(field)] = 0);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
if (["FIND_ALL", "FIND"].includes(finder))
|
|
162
|
+
fieldsInclude = (0, util_1.processFieldsInclude)(fieldsInclude);
|
|
163
|
+
fieldsInclude.map(function (field) {
|
|
164
|
+
if (field)
|
|
165
|
+
return (projection["".concat(field)] = 1);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return projection;
|
|
169
|
+
};
|
|
170
|
+
exports.mongoSelectQuery = mongoSelectQuery;
|
|
171
|
+
var modifyQuery = function (obj, hasAndQueryConjunction) {
|
|
172
|
+
if (obj === void 0) { obj = {}; }
|
|
173
|
+
if (hasAndQueryConjunction === void 0) { hasAndQueryConjunction = false; }
|
|
174
|
+
var arr = [];
|
|
175
|
+
Object.entries(obj).forEach(function (_a) {
|
|
176
|
+
var key = _a[0], value = _a[1];
|
|
177
|
+
if (hasAndQueryConjunction) {
|
|
178
|
+
arr.push({ $and: value });
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
arr.push({ $or: value });
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return arr;
|
|
185
|
+
};
|
|
186
|
+
exports.modifyQuery = modifyQuery;
|
|
187
|
+
var toBoolean = function (value) {
|
|
188
|
+
if (!value) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
switch (value.toLocaleLowerCase()) {
|
|
192
|
+
case "true":
|
|
193
|
+
case "1":
|
|
194
|
+
case "on":
|
|
195
|
+
case "yes":
|
|
196
|
+
return true;
|
|
197
|
+
default:
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
var checkDate = function (queryValue) {
|
|
202
|
+
return ("" + queryValue).match(date_util_1.DATE_REGEX);
|
|
203
|
+
};
|
|
204
|
+
exports.checkDate = checkDate;
|
|
205
|
+
var getMinMaxValue = function (value) {
|
|
206
|
+
var isFixedValue = (0, exports.isFixedValueQuery)(value);
|
|
207
|
+
if (isFixedValue)
|
|
208
|
+
value = value.replace("FIXED_VALUE::", "");
|
|
209
|
+
var isValid = value && value.includes("---");
|
|
210
|
+
if (isValid) {
|
|
211
|
+
var isInteger = value.includes("^");
|
|
212
|
+
var result = value.split("---");
|
|
213
|
+
var endValue = result[1];
|
|
214
|
+
if (isInteger && result.length)
|
|
215
|
+
endValue = endValue.slice(0, -1);
|
|
216
|
+
return { startValue: result[0], endValue: endValue, isInteger: isInteger, isFixedValue: isFixedValue };
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
return { startValue: "", endValue: "", isFixedValue: isFixedValue };
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
exports.getMinMaxValue = getMinMaxValue;
|
|
223
|
+
// TODO: can be refactor with entity condition
|
|
224
|
+
var isFixedValueQuery = function (value) {
|
|
225
|
+
var isFixeValue = false;
|
|
226
|
+
if (value && Array.isArray(value)) {
|
|
227
|
+
value.forEach(function (val) {
|
|
228
|
+
if (typeof val === "string" || val instanceof String)
|
|
229
|
+
isFixeValue = val.startsWith("FIXED_VALUE::");
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
if (value && (typeof value === "string" || value instanceof String))
|
|
234
|
+
isFixeValue = value.startsWith("FIXED_VALUE::");
|
|
235
|
+
}
|
|
236
|
+
return isFixeValue;
|
|
237
|
+
};
|
|
238
|
+
exports.isFixedValueQuery = isFixedValueQuery;
|
|
239
|
+
var getValueOfProjectConstant = function (value, currentUser, timezone) {
|
|
240
|
+
if (!timezone)
|
|
241
|
+
timezone = "(GMT+5:30)";
|
|
242
|
+
timezone = timezone.substring(4, 10);
|
|
243
|
+
var result;
|
|
244
|
+
if (value === drapcode_constant_1.CURRENT_USER)
|
|
245
|
+
return currentUser["uuid"];
|
|
246
|
+
if (value === drapcode_constant_1.CURRENT_DATE)
|
|
247
|
+
result = (0, date_util_1.createLoggerDateFormat)(timezone);
|
|
248
|
+
if ([drapcode_constant_1.CURRENT_TIME, drapcode_constant_1.CURRENT_DATE_TIME].includes(value))
|
|
249
|
+
result = (0, date_util_1.createLogsDateFormat)(timezone);
|
|
250
|
+
if (drapcode_constant_1.DateTimeUnit.some(function (cnst) { return value.includes(cnst); }))
|
|
251
|
+
result = (0, date_util_1.getDateValue)(value, timezone);
|
|
252
|
+
return result;
|
|
253
|
+
};
|
|
254
|
+
exports.getValueOfProjectConstant = getValueOfProjectConstant;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const callCurlRequest: (setting: any, user?: any, tenant?: any, userSetting?: any, subTenant?: any, data?: any, timeout?: number, timeoutMessage?: string, envConstants?: {}, downloadBytes?: boolean, requestDataType?: string, wrapJsonDataInArray?: boolean, projectId?: string, dataTransferObject?: any, isRawJSON?: boolean, browserStorageData?: any) => Promise<{
|
|
2
|
+
data: any;
|
|
3
|
+
status: number;
|
|
4
|
+
success: boolean;
|
|
5
|
+
headers: {};
|
|
6
|
+
}>;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
exports.callCurlRequest = void 0;
|
|
54
|
+
var axios_1 = __importDefault(require("axios"));
|
|
55
|
+
var util_1 = require("./util");
|
|
56
|
+
var drapcode_logger_1 = require("drapcode-logger");
|
|
57
|
+
var FormData = require("form-data");
|
|
58
|
+
var fs = require("fs");
|
|
59
|
+
var qs = require("qs");
|
|
60
|
+
var callCurlRequest = function (setting, user, tenant, userSetting, subTenant, data, timeout, timeoutMessage, envConstants, downloadBytes, requestDataType, wrapJsonDataInArray, projectId, dataTransferObject, isRawJSON, browserStorageData) {
|
|
61
|
+
if (user === void 0) { user = {}; }
|
|
62
|
+
if (tenant === void 0) { tenant = {}; }
|
|
63
|
+
if (userSetting === void 0) { userSetting = {}; }
|
|
64
|
+
if (subTenant === void 0) { subTenant = {}; }
|
|
65
|
+
if (data === void 0) { data = {}; }
|
|
66
|
+
if (timeout === void 0) { timeout = 0; }
|
|
67
|
+
if (timeoutMessage === void 0) { timeoutMessage = ""; }
|
|
68
|
+
if (envConstants === void 0) { envConstants = {}; }
|
|
69
|
+
if (downloadBytes === void 0) { downloadBytes = false; }
|
|
70
|
+
if (requestDataType === void 0) { requestDataType = ""; }
|
|
71
|
+
if (wrapJsonDataInArray === void 0) { wrapJsonDataInArray = false; }
|
|
72
|
+
if (projectId === void 0) { projectId = ""; }
|
|
73
|
+
if (dataTransferObject === void 0) { dataTransferObject = {}; }
|
|
74
|
+
if (isRawJSON === void 0) { isRawJSON = false; }
|
|
75
|
+
if (browserStorageData === void 0) { browserStorageData = {}; }
|
|
76
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
77
|
+
var url, params, methodType, authType, requestHeader, accessToken, _a, headers, isMultipart, files, _b, awsSignPluginConfig, _c, sessionValue, sessionFormValue, sessionStorageValue, localStorageValue, cookiesValue, headerValue, requestParams, toDeleteFiles, objectArray, settingsHeaderObjectArray, dataHeaderObjectArray, mergedHeaders_1, settingsHasContentType_1, settingsContentTypeKey_1, headerContentTypeUrlEncoded, isUrlEncoded, result, errorMessage, error_1, err, errData, errStatus;
|
|
78
|
+
return __generator(this, function (_d) {
|
|
79
|
+
switch (_d.label) {
|
|
80
|
+
case 0:
|
|
81
|
+
url = setting.url, params = setting.params, methodType = setting.methodType, authType = setting.authType, requestHeader = setting.requestHeader, accessToken = setting.accessToken, _a = setting.headers, headers = _a === void 0 ? [] : _a, isMultipart = setting.isMultipart, files = setting.files, _b = setting.awsSignPluginConfig, awsSignPluginConfig = _b === void 0 ? null : _b;
|
|
82
|
+
_c = browserStorageData || {}, sessionValue = _c.sessionValue, sessionFormValue = _c.sessionFormValue, sessionStorageValue = _c.sessionStorageData, localStorageValue = _c.localStorageData, cookiesValue = _c.cookiesData;
|
|
83
|
+
drapcode_logger_1.logger.info("STEP 1 Replacing placeholder key values...", {
|
|
84
|
+
label: projectId,
|
|
85
|
+
});
|
|
86
|
+
// Uncomment below only when needed for debugging
|
|
87
|
+
// logger.info(
|
|
88
|
+
// `STEP 1 Replacing URL Session Value: ${sessionValue ? JSON.stringify(sessionValue) : ""
|
|
89
|
+
// }, Session Form Value: ${sessionFormValue ? JSON.stringify(sessionFormValue) : ""
|
|
90
|
+
// }, Browser Storage Data: ${browserStorageData ? JSON.stringify(browserStorageData) : ""
|
|
91
|
+
// }, Session Storage: ${sessionStorageValue ? JSON.stringify(sessionStorageValue) : ""
|
|
92
|
+
// }, Local Storage: ${localStorageValue ? JSON.stringify(localStorageValue) : ""
|
|
93
|
+
// }, Session Cookies: ${cookiesValue ? JSON.stringify(cookiesValue) : ""
|
|
94
|
+
// }, Headers: ${headers ? JSON.stringify(headers) : ""}`,
|
|
95
|
+
// { label: projectId }
|
|
96
|
+
// );
|
|
97
|
+
if (url.includes("{{current_user.") &&
|
|
98
|
+
(!user || Object.keys(user).length == 0)) {
|
|
99
|
+
drapcode_logger_1.logger.info("**2 Sending back", { label: projectId });
|
|
100
|
+
return [2 /*return*/, prepareReturnObject("User is not logged in. URL is wrong")];
|
|
101
|
+
}
|
|
102
|
+
if (data || user || tenant || envConstants || userSetting || subTenant) {
|
|
103
|
+
url = (0, util_1.replaceDataValueIntoExpression)(url, data, user, tenant, userSetting, subTenant, sessionValue, envConstants, sessionFormValue, localStorageValue, cookiesValue);
|
|
104
|
+
}
|
|
105
|
+
if (!url) {
|
|
106
|
+
drapcode_logger_1.logger.info("**5 url not found", { label: projectId });
|
|
107
|
+
return [2 /*return*/, prepareReturnObject("URL is wrong")];
|
|
108
|
+
}
|
|
109
|
+
drapcode_logger_1.logger.info("STEP 2: URL replaced and replacing header", {
|
|
110
|
+
label: projectId,
|
|
111
|
+
});
|
|
112
|
+
headerValue = (0, util_1.mergeObjects)(headers, data, user, tenant, userSetting, subTenant, sessionValue, envConstants, sessionFormValue, dataTransferObject, localStorageValue, cookiesValue);
|
|
113
|
+
headerValue = {
|
|
114
|
+
headers: __assign({}, headerValue),
|
|
115
|
+
};
|
|
116
|
+
drapcode_logger_1.logger.info("STEP 3: Replacing Header Auth", { label: projectId });
|
|
117
|
+
drapcode_logger_1.logger.info("accessToken: ".concat(accessToken, " authType: ").concat(authType), {
|
|
118
|
+
label: projectId,
|
|
119
|
+
});
|
|
120
|
+
if (accessToken) {
|
|
121
|
+
headerValue.headers[requestHeader] = authType
|
|
122
|
+
? "".concat(authType, " ").concat(accessToken)
|
|
123
|
+
: accessToken;
|
|
124
|
+
}
|
|
125
|
+
if (params && params.length) {
|
|
126
|
+
requestParams = (0, util_1.mergeObjects)(params, data, user, tenant, userSetting, subTenant, sessionValue, envConstants, sessionFormValue, dataTransferObject, localStorageValue, cookiesValue);
|
|
127
|
+
headerValue.params = requestParams;
|
|
128
|
+
}
|
|
129
|
+
//unflat data before api call
|
|
130
|
+
data = data ? (0, util_1.unflattenObject)(data) : data;
|
|
131
|
+
drapcode_logger_1.logger.info("Before headers: ".concat(JSON.stringify(headerValue.headers)), {
|
|
132
|
+
label: projectId,
|
|
133
|
+
});
|
|
134
|
+
drapcode_logger_1.logger.info("isMultipart: ".concat(isMultipart), { label: projectId });
|
|
135
|
+
toDeleteFiles = [];
|
|
136
|
+
if (isMultipart || (requestDataType && requestDataType === "FORM_DATA")) {
|
|
137
|
+
objectArray = Object.entries(data);
|
|
138
|
+
data = new FormData();
|
|
139
|
+
objectArray.forEach(function (_a) {
|
|
140
|
+
var key = _a[0], value = _a[1];
|
|
141
|
+
if (files && files.includes(key)) {
|
|
142
|
+
drapcode_logger_1.logger.info("".concat(key, " files.includes(key): ").concat(files.includes(key)), {
|
|
143
|
+
label: projectId,
|
|
144
|
+
});
|
|
145
|
+
drapcode_logger_1.logger.info("value: ".concat(value), { label: projectId });
|
|
146
|
+
data.append(key, fs.createReadStream(value));
|
|
147
|
+
toDeleteFiles.push(value);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
data.append(key, value);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
settingsHeaderObjectArray = Object.entries(headerValue.headers);
|
|
154
|
+
dataHeaderObjectArray = Object.entries(data.getHeaders());
|
|
155
|
+
mergedHeaders_1 = {};
|
|
156
|
+
settingsHasContentType_1 = false;
|
|
157
|
+
settingsContentTypeKey_1 = "";
|
|
158
|
+
settingsHeaderObjectArray.forEach(function (_a) {
|
|
159
|
+
var key = _a[0], value = _a[1];
|
|
160
|
+
if (key && key.toLowerCase() === "content-type") {
|
|
161
|
+
settingsHasContentType_1 = true;
|
|
162
|
+
settingsContentTypeKey_1 = key;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
mergedHeaders_1[key] = value;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
dataHeaderObjectArray.forEach(function (_a) {
|
|
169
|
+
var key = _a[0], value = _a[1];
|
|
170
|
+
if (key && key === "content-type" && settingsHasContentType_1) {
|
|
171
|
+
mergedHeaders_1[settingsContentTypeKey_1] = value;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
mergedHeaders_1[key] = value;
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
drapcode_logger_1.logger.info("*** merged headers :>> ".concat(mergedHeaders_1), {
|
|
178
|
+
label: projectId,
|
|
179
|
+
});
|
|
180
|
+
headerValue.headers = __assign({}, mergedHeaders_1);
|
|
181
|
+
}
|
|
182
|
+
drapcode_logger_1.logger.info("After headers ".concat(JSON.stringify(headerValue.headers)), {
|
|
183
|
+
label: projectId,
|
|
184
|
+
});
|
|
185
|
+
drapcode_logger_1.logger.info("** App Environment: ".concat(process.env.APP_ENV), {
|
|
186
|
+
label: projectId,
|
|
187
|
+
});
|
|
188
|
+
headerContentTypeUrlEncoded = setting.headers.find(function (head) {
|
|
189
|
+
if (head === void 0) { head = {}; }
|
|
190
|
+
return head.key &&
|
|
191
|
+
head.key.toLowerCase() === "content-type" &&
|
|
192
|
+
head.value === "application/x-www-form-urlencoded";
|
|
193
|
+
});
|
|
194
|
+
isUrlEncoded = !!headerContentTypeUrlEncoded;
|
|
195
|
+
drapcode_logger_1.logger.info("** isUrlEncoded: ".concat(isUrlEncoded), { label: projectId });
|
|
196
|
+
//Add Environment in Header for Lambda USE
|
|
197
|
+
headerValue.headers["x-project-env"] = process.env.APP_ENV;
|
|
198
|
+
if (downloadBytes) {
|
|
199
|
+
headerValue["responseType"] = "stream";
|
|
200
|
+
}
|
|
201
|
+
_d.label = 1;
|
|
202
|
+
case 1:
|
|
203
|
+
_d.trys.push([1, 3, , 4]);
|
|
204
|
+
result = null;
|
|
205
|
+
if (!isRawJSON) {
|
|
206
|
+
if (requestDataType === "FORM_URL_ENCODED" || isUrlEncoded) {
|
|
207
|
+
data = qs.stringify(data);
|
|
208
|
+
drapcode_logger_1.logger.info("**17 Encoded data:", data, { label: projectId });
|
|
209
|
+
}
|
|
210
|
+
if (requestDataType === "NO_BODY_DATA")
|
|
211
|
+
data = {};
|
|
212
|
+
}
|
|
213
|
+
return [4 /*yield*/, makeAxiosCall(projectId, methodType, url, headerValue, data, timeout, wrapJsonDataInArray, awsSignPluginConfig)];
|
|
214
|
+
case 2:
|
|
215
|
+
result = _d.sent();
|
|
216
|
+
drapcode_logger_1.logger.info("result.headers: ".concat(result === null || result === void 0 ? void 0 : result.headers), { label: projectId });
|
|
217
|
+
if (!result) {
|
|
218
|
+
errorMessage = "No Response from Server";
|
|
219
|
+
return [2 /*return*/, prepareReturnObject(errorMessage)];
|
|
220
|
+
}
|
|
221
|
+
drapcode_logger_1.logger.info("result.status: ".concat(result === null || result === void 0 ? void 0 : result.status), { label: projectId });
|
|
222
|
+
deleteLocalFiles(isMultipart, toDeleteFiles);
|
|
223
|
+
return [2 /*return*/, prepareReturnObject(result.data, result.status, true, result === null || result === void 0 ? void 0 : result.headers)];
|
|
224
|
+
case 3:
|
|
225
|
+
error_1 = _d.sent();
|
|
226
|
+
deleteLocalFiles(isMultipart, toDeleteFiles);
|
|
227
|
+
err = error_1;
|
|
228
|
+
errData = null, errStatus = 400;
|
|
229
|
+
if (err.response) {
|
|
230
|
+
drapcode_logger_1.logger.info("err.response.data: ".concat(err.response.data ? JSON.stringify(err.response.data) : ""), {
|
|
231
|
+
label: projectId,
|
|
232
|
+
});
|
|
233
|
+
drapcode_logger_1.logger.info("err.response.status: ".concat(err.response.status), {
|
|
234
|
+
label: projectId,
|
|
235
|
+
});
|
|
236
|
+
drapcode_logger_1.logger.info("err.response.headers: ".concat(err.response.headers ? JSON.stringify(err.response.headers) : ""), {
|
|
237
|
+
label: projectId,
|
|
238
|
+
});
|
|
239
|
+
errData = err.response.data;
|
|
240
|
+
errStatus = err.response.status;
|
|
241
|
+
}
|
|
242
|
+
else if (err.request) {
|
|
243
|
+
errData = "No Response Received from the API";
|
|
244
|
+
drapcode_logger_1.logger.info("err.request: ".concat(err.request), { label: projectId });
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
if (err.message.includes("ECONNABORTED")) {
|
|
248
|
+
errData = timeoutMessage || "No response received from the REST API";
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
errData = err.message;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return [2 /*return*/, prepareReturnObject(errData, errStatus)];
|
|
255
|
+
case 4: return [2 /*return*/];
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
};
|
|
260
|
+
exports.callCurlRequest = callCurlRequest;
|
|
261
|
+
var deleteLocalFiles = function (isMultipart, toDeleteFiles) {
|
|
262
|
+
if (isMultipart && toDeleteFiles) {
|
|
263
|
+
toDeleteFiles.forEach(function (filePath) {
|
|
264
|
+
console.log("==> TO DELETE FILES ARRAY filePath :>> ", filePath);
|
|
265
|
+
fs.exists(filePath, function (exists) {
|
|
266
|
+
if (exists) {
|
|
267
|
+
console.log("File exists. Deleting now ...");
|
|
268
|
+
fs.unlinkSync(filePath);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
console.log("File not found, so not deleting.");
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
var prepareReturnObject = function (data, status, success, headers) {
|
|
278
|
+
if (status === void 0) { status = 400; }
|
|
279
|
+
if (success === void 0) { success = false; }
|
|
280
|
+
if (headers === void 0) { headers = {}; }
|
|
281
|
+
return {
|
|
282
|
+
data: data,
|
|
283
|
+
status: status,
|
|
284
|
+
success: success,
|
|
285
|
+
headers: headers,
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
var makeAxiosCall = function (projectId, methodType, url, axiosConfig, data, timeout, wrapJsonDataInArray, awsSignPluginConfig) { return __awaiter(void 0, void 0, void 0, function () {
|
|
289
|
+
var result, instance, dataArray_1, awsRes;
|
|
290
|
+
return __generator(this, function (_a) {
|
|
291
|
+
switch (_a.label) {
|
|
292
|
+
case 0:
|
|
293
|
+
axiosConfig = __assign({ url: url, method: methodType }, axiosConfig);
|
|
294
|
+
result = "";
|
|
295
|
+
instance = axios_1.default.create();
|
|
296
|
+
instance.defaults.timeout = timeout * 1000;
|
|
297
|
+
switch (methodType) {
|
|
298
|
+
case "GET":
|
|
299
|
+
axiosConfig["data"] = data ? data : {};
|
|
300
|
+
break;
|
|
301
|
+
case "POST":
|
|
302
|
+
case "PUT":
|
|
303
|
+
case "PATCH":
|
|
304
|
+
if (wrapJsonDataInArray) {
|
|
305
|
+
dataArray_1 = [];
|
|
306
|
+
if (Object.keys(data) && Object.keys(data).length) {
|
|
307
|
+
if (Object.keys(data)[0] === "0") {
|
|
308
|
+
Object.keys(data).map(function (dataKey) {
|
|
309
|
+
dataArray_1.push(data[dataKey]);
|
|
310
|
+
});
|
|
311
|
+
data = dataArray_1;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
axiosConfig["data"] = data;
|
|
316
|
+
break;
|
|
317
|
+
case "DELETE":
|
|
318
|
+
//Sending data object via axiosConfig
|
|
319
|
+
if (data && Object.entries(data).length) {
|
|
320
|
+
axiosConfig["data"] = __assign({}, data);
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
default:
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
if (!awsSignPluginConfig) return [3 /*break*/, 2];
|
|
327
|
+
return [4 /*yield*/, (0, util_1.getAwsSignature)(axiosConfig, awsSignPluginConfig)];
|
|
328
|
+
case 1:
|
|
329
|
+
awsRes = _a.sent();
|
|
330
|
+
if (awsRes.error)
|
|
331
|
+
return [2 /*return*/, awsRes.error];
|
|
332
|
+
axiosConfig = __assign(__assign({}, awsRes.data), { url: url, data: data });
|
|
333
|
+
_a.label = 2;
|
|
334
|
+
case 2:
|
|
335
|
+
console.log("\n ==> Final axiosConfig for projectID: ".concat(projectId, ":>> "), axiosConfig);
|
|
336
|
+
return [4 /*yield*/, (0, axios_1.default)(__assign({}, axiosConfig))];
|
|
337
|
+
case 3:
|
|
338
|
+
result = _a.sent();
|
|
339
|
+
return [2 /*return*/, result];
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}); };
|