drapcode-developer-sdk 1.0.10-dev → 1.0.11

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.
@@ -1,2 +1,2 @@
1
- export declare const encryptData: (data: string, key: string) => Promise<string>;
2
- export declare const decryptData: (data: string, key: string) => Promise<string>;
1
+ export declare const encryptData: (data: string, key: string, iv?: string) => string;
2
+ export declare const decryptData: (data: string, key: string, iv?: string) => string;
@@ -1,40 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- 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;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
@@ -42,58 +6,57 @@ Object.defineProperty(exports, "__esModule", { value: true });
42
6
  exports.decryptData = exports.encryptData = void 0;
43
7
  var crypto_1 = __importDefault(require("crypto"));
44
8
  var defaultAlgorithm = "aes-256-cbc";
45
- var encryptData = function (data, key) { return __awaiter(void 0, void 0, void 0, function () {
46
- var iv, keyBuffer, cipher, encryptedDataBuffer, result;
47
- return __generator(this, function (_a) {
48
- try {
49
- iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
50
- keyBuffer = Buffer.from(key, "base64");
51
- cipher = crypto_1.default.createCipheriv(defaultAlgorithm, keyBuffer, iv);
52
- encryptedDataBuffer = cipher.update("".concat(data));
53
- encryptedDataBuffer = Buffer.concat([encryptedDataBuffer, cipher.final()]);
54
- result = encryptedDataBuffer.toString("base64");
55
- return [2 /*return*/, handleExtraString(result, true)];
56
- }
57
- catch (error) {
58
- console.error("\n Error: ", error);
59
- return [2 /*return*/, data];
60
- }
61
- return [2 /*return*/];
62
- });
63
- }); };
9
+ var fixedIv = "i4mboZDwaNEC38YCzi77lw==";
10
+ var toBuffer = function (base64) { return Buffer.from(base64, "base64"); };
11
+ var createCipher = function (key, iv) {
12
+ return crypto_1.default.createCipheriv(defaultAlgorithm, toBuffer(key), toBuffer(iv));
13
+ };
14
+ var createDecipher = function (key, iv) {
15
+ return crypto_1.default.createDecipheriv(defaultAlgorithm, toBuffer(key), toBuffer(iv));
16
+ };
17
+ var encryptData = function (data, key, iv) {
18
+ try {
19
+ if (!iv)
20
+ iv = fixedIv;
21
+ var cipher = createCipher(key, iv);
22
+ var encrypted = Buffer.concat([cipher.update("".concat(data)), cipher.final()]);
23
+ return handleExtraString(encrypted.toString("base64"), true);
24
+ }
25
+ catch (error) {
26
+ console.error("\n Error: ", error);
27
+ return data;
28
+ }
29
+ };
64
30
  exports.encryptData = encryptData;
65
- var decryptData = function (data, key) { return __awaiter(void 0, void 0, void 0, function () {
66
- var iv, encryptedData, keyBuffer, decipher, decryptedBuffer;
67
- return __generator(this, function (_a) {
68
- try {
69
- data = handleExtraString(data, false);
70
- iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
71
- encryptedData = Buffer.from(data, "base64");
72
- keyBuffer = Buffer.from(key, "base64");
73
- decipher = crypto_1.default.createDecipheriv(defaultAlgorithm, keyBuffer, iv);
74
- decryptedBuffer = decipher.update(encryptedData);
75
- decryptedBuffer = Buffer.concat([decryptedBuffer, decipher.final()]);
76
- return [2 /*return*/, decryptedBuffer.toString()];
77
- }
78
- catch (error) {
79
- console.error("\n Error: ", error);
80
- return [2 /*return*/, data];
81
- }
82
- return [2 /*return*/];
83
- });
84
- }); };
31
+ var decryptData = function (data, key, iv) {
32
+ try {
33
+ console.log("decryptData: key :>> ", key);
34
+ console.log("decryptData: iv :>> ", iv);
35
+ var cleaned = handleExtraString(data, false);
36
+ var encryptedData = toBuffer(cleaned);
37
+ if (!iv)
38
+ iv = fixedIv;
39
+ var decipher = createDecipher(key, iv);
40
+ var decrypted = Buffer.concat([
41
+ decipher.update(encryptedData),
42
+ decipher.final(),
43
+ ]);
44
+ return decrypted.toString();
45
+ }
46
+ catch (error) {
47
+ console.error("\n Error: ", error);
48
+ return data;
49
+ }
50
+ };
85
51
  exports.decryptData = decryptData;
86
52
  var handleExtraString = function (key, append) {
87
53
  if (append === void 0) { append = true; }
88
- if (append) {
89
- var start = crypto_1.default.randomBytes(2).toString("hex");
90
- var end = crypto_1.default.randomBytes(2).toString("hex");
91
- key = "".concat(start).concat(key).concat(end);
92
- return key;
93
- }
94
- else {
95
- key = key.substring(4, key.length);
96
- key = key.substring(0, key.length - 4);
54
+ if (!key || key === "undefined")
97
55
  return key;
56
+ if (append) {
57
+ var prefix = crypto_1.default.randomBytes(2).toString("hex");
58
+ var suffix = crypto_1.default.randomBytes(2).toString("hex");
59
+ return "".concat(prefix).concat(key).concat(suffix);
98
60
  }
61
+ return key.substring(4, key.length - 4);
99
62
  };
@@ -11,6 +11,31 @@ export declare const createErrorResponse: (error: any) => Promise<{
11
11
  error: any;
12
12
  message: string;
13
13
  }>;
14
+ export declare const processFilterResponse: (response: any) => {
15
+ code: any;
16
+ data: any;
17
+ count: any;
18
+ error: string;
19
+ status: string;
20
+ message: string;
21
+ } | {
22
+ code: any;
23
+ data: never[];
24
+ count: number;
25
+ error: any;
26
+ status: string;
27
+ message: any;
28
+ };
29
+ export declare const processCountFilterResponse: () => void;
30
+ export declare const processCreateItemResponse: (response: any) => {
31
+ code: any;
32
+ data: any;
33
+ status: any;
34
+ error: any;
35
+ message: any;
36
+ } | undefined;
37
+ export declare const processCreateErrorResponse: (error: any) => Promise<any>;
38
+ export declare const processListResponse: () => void;
14
39
  export declare const processResponse: (result: any) => {
15
40
  code: any;
16
41
  success: boolean;
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.processResponse = exports.createErrorResponse = void 0;
39
+ exports.processResponse = exports.processListResponse = exports.processCreateErrorResponse = exports.processCreateItemResponse = exports.processCountFilterResponse = exports.processFilterResponse = exports.createErrorResponse = void 0;
40
40
  var defaultMessages = {
41
41
  401: "Unauthorized",
42
42
  404: "Not Found",
@@ -44,15 +44,29 @@ var defaultMessages = {
44
44
  500: "Internal Server Error",
45
45
  };
46
46
  var createErrorResponse = function (error) { return __awaiter(void 0, void 0, void 0, function () {
47
- var status, responseData, finalMessage;
48
- return __generator(this, function (_a) {
49
- switch (_a.label) {
47
+ var status, cloneError, responseData, _a, finalMessage;
48
+ return __generator(this, function (_b) {
49
+ switch (_b.label) {
50
50
  case 0:
51
51
  status = error === null || error === void 0 ? void 0 : error.status;
52
+ cloneError = error.clone();
52
53
  console.log("status :>> ", status);
53
- return [4 /*yield*/, error.json()];
54
+ _b.label = 1;
54
55
  case 1:
55
- responseData = _a.sent();
56
+ _b.trys.push([1, 3, , 5]);
57
+ return [4 /*yield*/, error.json()];
58
+ case 2:
59
+ // Try parsing as JSON
60
+ responseData = _b.sent();
61
+ return [3 /*break*/, 5];
62
+ case 3:
63
+ _a = _b.sent();
64
+ return [4 /*yield*/, cloneError.text()];
65
+ case 4:
66
+ // If it's not JSON, fallback to text
67
+ responseData = _b.sent();
68
+ return [3 /*break*/, 5];
69
+ case 5:
56
70
  console.log("responseData :>> ", responseData);
57
71
  if (status === 404) {
58
72
  finalMessage = "Not found";
@@ -89,7 +103,7 @@ var createErrorResponse = function (error) { return __awaiter(void 0, void 0, vo
89
103
  return [2 /*return*/, {
90
104
  code: status,
91
105
  success: false,
92
- data: "Please check your credentials",
106
+ data: responseData || "Please check your credentials",
93
107
  error: "",
94
108
  message: "",
95
109
  }];
@@ -105,9 +119,80 @@ var createErrorResponse = function (error) { return __awaiter(void 0, void 0, vo
105
119
  });
106
120
  }); };
107
121
  exports.createErrorResponse = createErrorResponse;
122
+ var processFilterResponse = function (response) {
123
+ console.log("response :>> ", response);
124
+ var code = response.code, message = response.message, result = response.result, count = response.count;
125
+ if (code === 200) {
126
+ return {
127
+ code: code,
128
+ data: result,
129
+ count: count,
130
+ error: "",
131
+ status: "success",
132
+ message: "",
133
+ };
134
+ }
135
+ return {
136
+ code: code,
137
+ data: [],
138
+ count: 0,
139
+ error: message,
140
+ status: "failed",
141
+ message: message,
142
+ };
143
+ };
144
+ exports.processFilterResponse = processFilterResponse;
145
+ var processCountFilterResponse = function () { };
146
+ exports.processCountFilterResponse = processCountFilterResponse;
147
+ var processCreateItemResponse = function (response) {
148
+ console.log("1", response);
149
+ var code = response.code, message = response.message, data = response.data, status = response.status, error = response.error;
150
+ if ([200, 201].includes(code)) {
151
+ return {
152
+ code: code,
153
+ data: data,
154
+ status: status,
155
+ error: error,
156
+ message: message,
157
+ };
158
+ }
159
+ };
160
+ exports.processCreateItemResponse = processCreateItemResponse;
161
+ var processCreateErrorResponse = function (error) { return __awaiter(void 0, void 0, void 0, function () {
162
+ var status, cloneError, responseData, _a;
163
+ return __generator(this, function (_b) {
164
+ switch (_b.label) {
165
+ case 0:
166
+ status = error === null || error === void 0 ? void 0 : error.status;
167
+ cloneError = error.clone();
168
+ console.log("status :>> ", status);
169
+ _b.label = 1;
170
+ case 1:
171
+ _b.trys.push([1, 3, , 5]);
172
+ return [4 /*yield*/, error.json()];
173
+ case 2:
174
+ // Try parsing as JSON
175
+ responseData = _b.sent();
176
+ return [3 /*break*/, 5];
177
+ case 3:
178
+ _a = _b.sent();
179
+ return [4 /*yield*/, cloneError.text()];
180
+ case 4:
181
+ // If it's not JSON, fallback to text
182
+ responseData = _b.sent();
183
+ return [3 /*break*/, 5];
184
+ case 5:
185
+ console.log("responseData :>> ", responseData);
186
+ return [2 /*return*/, responseData];
187
+ }
188
+ });
189
+ }); };
190
+ exports.processCreateErrorResponse = processCreateErrorResponse;
191
+ var processListResponse = function () { };
192
+ exports.processListResponse = processListResponse;
108
193
  var processResponse = function (result) {
109
194
  var _a, _b;
110
- console.log("1");
195
+ console.log("1", result);
111
196
  if ((result === null || result === void 0 ? void 0 : result.status) === "FAILED") {
112
197
  var statusCode = ((_a = result === null || result === void 0 ? void 0 : result.error) === null || _a === void 0 ? void 0 : _a.errStatus) || 400;
113
198
  var errorMessage = ((_b = result === null || result === void 0 ? void 0 : result.error) === null || _b === void 0 ? void 0 : _b.message) || defaultMessages[statusCode] || "API Failed";
@@ -120,7 +205,7 @@ var processResponse = function (result) {
120
205
  data: "",
121
206
  };
122
207
  }
123
- console.log("2");
208
+ console.log("2", result === null || result === void 0 ? void 0 : result.code);
124
209
  if (![200, 201].includes(result === null || result === void 0 ? void 0 : result.code)) {
125
210
  var errorMessage = (result === null || result === void 0 ? void 0 : result.data) || defaultMessages[result === null || result === void 0 ? void 0 : result.code] || "An error occurred";
126
211
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drapcode-developer-sdk",
3
- "version": "1.0.10-dev",
3
+ "version": "1.0.11",
4
4
  "main": "build/index.js",
5
5
  "types": "build/index.d.ts",
6
6
  "files": [