drapcode-developer-sdk 1.0.4 → 1.0.6

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/README.md CHANGED
@@ -32,7 +32,7 @@ const api = new DrapcodeApis(
32
32
  ### Example:
33
33
 
34
34
  ```
35
- const drapcodeApi = new DrapcodeApis("test-project-7138");
35
+ const drapcodeApi = new DrapcodeApis("test-project-7138",xApiKey, authorization, environment);
36
36
  ```
37
37
 
38
38
  # Methods
@@ -51,6 +51,63 @@ const items = await drapcodeApi.getAllItems("users");
51
51
 
52
52
  Retrieves items from the "users" collection.
53
53
 
54
+ ## Pagination and Search
55
+
56
+ ### getAllItems(collectionName: string, reqQuery: SearchPaginate, query: Query[])
57
+
58
+ Retrieves all items from a specified collection. The items will come under 'data' JSON path.
59
+
60
+ **collectionName:** The name of the collection to retrieve items from. Required
61
+ **reqQuery:** Search and Pagination options. Optional, must pass null, in case of query
62
+
63
+ ```
64
+ sortField:"",
65
+ sortOrder:"",
66
+ searchTerm:"",
67
+ isPagination: true|false
68
+ page: 1, //Greater than 0 and isPagination must be true
69
+ limit: 1 //Greater than 0 and isPagination must be true
70
+ ```
71
+
72
+ **query**: Filter on basis of query. Optional
73
+
74
+ ```
75
+ {
76
+ field: field_name
77
+ condition: QueryCondition
78
+ value: value
79
+ }
80
+ ```
81
+
82
+ ```
83
+ //QueryCondition
84
+ EQUALS,
85
+ IS_NOT_NULL,
86
+ IS_NULL,
87
+ LIKE,
88
+ LESS_THAN_EQUALS_TO,
89
+ GREATER_THAN_EQUALS_TO,
90
+ LESS_THAN,
91
+ GREATER_THAN,
92
+ IN_LIST,
93
+ NOT_IN_LIST
94
+ ```
95
+
96
+ ### Example
97
+
98
+ ```
99
+ // 1
100
+ const reqQuery = {isPaginate: true, page:1, limit: 100}
101
+ const items = await drapcodeApi.getAllItems("users", reqQuery);
102
+ // 2
103
+ const query = {field: "userName", condition: "EQUALS", value: "test@test.com"}
104
+ const queries = [query]
105
+ const items = await drapcodeApi.getAllItems("users", null, queries);
106
+ // 3
107
+ const items = await drapcodeApi.getAllItems("users", reqQuery, queries);
108
+
109
+ ```
110
+
54
111
  ## createItem(collectionName: string, body: JSON)
55
112
 
56
113
  Creates a new item in the specified collection.
@@ -161,3 +218,28 @@ await drapcodeApi.sendEmail("345-678", "support@drapcode.com");
161
218
  Sends an email using the template ID "345-678" to the email address "support@drapcode.com".
162
219
 
163
220
  For better experience, utilize async/await syntax when using these methods.
221
+
222
+ ## Encryption Related
223
+
224
+ We have two methods related to encryption.
225
+
226
+ 1. encryptData
227
+ 2. decryptData
228
+
229
+ ### Encrypt Data
230
+
231
+ ```
232
+ await encryptData(content, publicKey)
233
+ ```
234
+
235
+ **content:** Content/Text you want to encrypt.
236
+ **publicKey:** Public key, which will be used to encrypt data.
237
+
238
+ ### Decrypt Data
239
+
240
+ ```
241
+ await decryptData(content, publicKey)
242
+ ```
243
+
244
+ **content:** Content/Text you want to decrypt.
245
+ **publicKey:** Public key, which was used to decrypt data.
package/build/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Query, SearchPaginate } from "./utils/constants";
1
2
  export declare class DrapcodeApis {
2
3
  private project_seo_name;
3
4
  private xApiKey;
@@ -7,7 +8,7 @@ export declare class DrapcodeApis {
7
8
  constructor(project_seo_name: string, xApiKey?: string, authorization?: string, environment?: string);
8
9
  private getBaseUrl;
9
10
  private getHeaders;
10
- getAllItems(collectionName: string): Promise<{
11
+ getAllItems(collectionName: string, reqQuery?: SearchPaginate | any, query?: Query[] | []): Promise<{
11
12
  code: any;
12
13
  success: boolean;
13
14
  data: any;
@@ -19,18 +20,21 @@ export declare class DrapcodeApis {
19
20
  error: any;
20
21
  message: any;
21
22
  data: string;
23
+ totalItems?: undefined;
24
+ totalPages?: undefined;
22
25
  }>;
23
26
  createItem(collectionName: string, body: any): Promise<{
24
- code: any;
25
27
  success: boolean;
26
- data: any;
28
+ data: string;
27
29
  error: string;
28
30
  message: string;
31
+ code?: undefined;
29
32
  } | {
33
+ code: any;
30
34
  success: boolean;
31
- data: string;
35
+ data: any;
32
36
  error: string;
33
- message: string;
37
+ message: any;
34
38
  }>;
35
39
  getItemsWithFilter(collectionName: string, filterUuid: string): Promise<{
36
40
  code: any;
@@ -44,6 +48,8 @@ export declare class DrapcodeApis {
44
48
  error: any;
45
49
  message: any;
46
50
  data: string;
51
+ totalItems?: undefined;
52
+ totalPages?: undefined;
47
53
  }>;
48
54
  getItemsCountWithFilter(collectionName: string, filterUuid: string): Promise<{
49
55
  code: any;
@@ -57,6 +63,8 @@ export declare class DrapcodeApis {
57
63
  error: any;
58
64
  message: any;
59
65
  data: string;
66
+ totalItems?: undefined;
67
+ totalPages?: undefined;
60
68
  }>;
61
69
  getItemWithUuid(collectionName: string, itemUuid: string): Promise<{
62
70
  code: any;
@@ -70,6 +78,8 @@ export declare class DrapcodeApis {
70
78
  error: any;
71
79
  message: any;
72
80
  data: string;
81
+ totalItems?: undefined;
82
+ totalPages?: undefined;
73
83
  }>;
74
84
  updateItemWithUuid(collectionName: string, itemUuid: string, body: any): Promise<{
75
85
  code: any;
@@ -83,6 +93,8 @@ export declare class DrapcodeApis {
83
93
  error: any;
84
94
  message: any;
85
95
  data: string;
96
+ totalItems?: undefined;
97
+ totalPages?: undefined;
86
98
  }>;
87
99
  deleteItemWithUuid(collectionName: string, itemUuid: string): Promise<{
88
100
  code: any;
@@ -128,3 +140,6 @@ export declare class DrapcodeApis {
128
140
  message: string;
129
141
  }>;
130
142
  }
143
+ export * from "./utils/index";
144
+ export * from "./utils/crypt";
145
+ export * from "./utils/constants";
package/build/index.js CHANGED
@@ -1,4 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
17
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
18
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -50,7 +64,7 @@ var DrapcodeApis = /** @class */ (function () {
50
64
  this.environment = environment;
51
65
  }
52
66
  DrapcodeApis.prototype.getBaseUrl = function () {
53
- switch (this.environment) {
67
+ switch (this.environment.toUpperCase()) {
54
68
  case "PRODUCTION":
55
69
  return "https://".concat(this.project_seo_name, ".api.").concat(this.API_PATH);
56
70
  case "PREVIEW":
@@ -74,12 +88,15 @@ var DrapcodeApis = /** @class */ (function () {
74
88
  if (this.authorization) {
75
89
  headers["Authorization"] = this.authorization;
76
90
  }
91
+ console.log("here is header", headers);
77
92
  return headers;
78
93
  };
79
- DrapcodeApis.prototype.getAllItems = function (collectionName) {
94
+ DrapcodeApis.prototype.getAllItems = function (collectionName, reqQuery, query) {
95
+ if (reqQuery === void 0) { reqQuery = null; }
96
+ if (query === void 0) { query = []; }
80
97
  return __awaiter(this, void 0, void 0, function () {
81
98
  return __generator(this, function (_a) {
82
- return [2 /*return*/, (0, methods_1.getAllItems)(this.getBaseUrl(), this.getHeaders(), collectionName)];
99
+ return [2 /*return*/, (0, methods_1.getAllItems)(this.getBaseUrl(), this.getHeaders(), collectionName, reqQuery, query)];
83
100
  });
84
101
  });
85
102
  };
@@ -149,3 +166,6 @@ var DrapcodeApis = /** @class */ (function () {
149
166
  return DrapcodeApis;
150
167
  }());
151
168
  exports.DrapcodeApis = DrapcodeApis;
169
+ __exportStar(require("./utils/index"), exports);
170
+ __exportStar(require("./utils/crypt"), exports);
171
+ __exportStar(require("./utils/constants"), exports);
@@ -1,4 +1,5 @@
1
- export declare const getAllItems: (baseurl: string, headers: Record<string, string>, collectionName: string) => Promise<{
1
+ import { Query, SearchPaginate } from "../utils/constants";
2
+ export declare const getAllItems: (baseurl: string, headers: Record<string, string>, collectionName: string, reqQuery: SearchPaginate, query: Query[]) => Promise<{
2
3
  code: any;
3
4
  success: boolean;
4
5
  data: any;
@@ -10,18 +11,21 @@ export declare const getAllItems: (baseurl: string, headers: Record<string, stri
10
11
  error: any;
11
12
  message: any;
12
13
  data: string;
14
+ totalItems?: undefined;
15
+ totalPages?: undefined;
13
16
  }>;
14
17
  export declare const createItem: (baseurl: string, headers: Record<string, string>, collectionName: string, body: any) => Promise<{
15
- code: any;
16
18
  success: boolean;
17
- data: any;
19
+ data: string;
18
20
  error: string;
19
21
  message: string;
22
+ code?: undefined;
20
23
  } | {
24
+ code: any;
21
25
  success: boolean;
22
- data: string;
26
+ data: any;
23
27
  error: string;
24
- message: string;
28
+ message: any;
25
29
  }>;
26
30
  export declare const getItemsWithFilter: (baseurl: string, headers: Record<string, string>, collectionName: string, filterUuid: string) => Promise<{
27
31
  code: any;
@@ -35,6 +39,8 @@ export declare const getItemsWithFilter: (baseurl: string, headers: Record<strin
35
39
  error: any;
36
40
  message: any;
37
41
  data: string;
42
+ totalItems?: undefined;
43
+ totalPages?: undefined;
38
44
  }>;
39
45
  export declare const getItemsCountWithFilter: (baseurl: string, headers: Record<string, string>, collectionName: string, filterUuid: string) => Promise<{
40
46
  code: any;
@@ -48,6 +54,8 @@ export declare const getItemsCountWithFilter: (baseurl: string, headers: Record<
48
54
  error: any;
49
55
  message: any;
50
56
  data: string;
57
+ totalItems?: undefined;
58
+ totalPages?: undefined;
51
59
  }>;
52
60
  export declare const getItemWithUuid: (baseurl: string, headers: Record<string, string>, collectionName: string, itemUuid: string) => Promise<{
53
61
  code: any;
@@ -61,6 +69,8 @@ export declare const getItemWithUuid: (baseurl: string, headers: Record<string,
61
69
  error: any;
62
70
  message: any;
63
71
  data: string;
72
+ totalItems?: undefined;
73
+ totalPages?: undefined;
64
74
  }>;
65
75
  export declare const updateItemWithUuid: (baseurl: string, headers: Record<string, string>, collectionName: string, itemUuid: string, body: any) => Promise<{
66
76
  code: any;
@@ -74,6 +84,8 @@ export declare const updateItemWithUuid: (baseurl: string, headers: Record<strin
74
84
  error: any;
75
85
  message: any;
76
86
  data: string;
87
+ totalItems?: undefined;
88
+ totalPages?: undefined;
77
89
  }>;
78
90
  export declare const deleteItemWithUuid: (baseurl: string, headers: Record<string, string>, collectionName: string, itemUuid: string) => Promise<{
79
91
  code: any;
@@ -37,7 +37,9 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.sendEmail = exports.getItemsByids = exports.bulkDeleteItems = exports.deleteItemWithUuid = exports.updateItemWithUuid = exports.getItemWithUuid = exports.getItemsCountWithFilter = exports.getItemsWithFilter = exports.createItem = exports.getAllItems = void 0;
40
+ var constants_1 = require("../utils/constants");
40
41
  var createErrorResponse = function (error) {
42
+ var _a;
41
43
  if (error.response && error.response.status === 404) {
42
44
  var responseData = error.response.data;
43
45
  var finalData = void 0;
@@ -71,7 +73,7 @@ var createErrorResponse = function (error) {
71
73
  else if (error.response && error.response.status === 400) {
72
74
  var responseData = error.response;
73
75
  return {
74
- code: responseData.status,
76
+ code: responseData === null || responseData === void 0 ? void 0 : responseData.status,
75
77
  success: false,
76
78
  data: "Please Check Your Credentials",
77
79
  error: "",
@@ -79,7 +81,7 @@ var createErrorResponse = function (error) {
79
81
  };
80
82
  }
81
83
  return {
82
- code: error.response.code,
84
+ code: (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.code,
83
85
  success: false,
84
86
  data: "",
85
87
  error: "Please check your project name or publish again.",
@@ -87,47 +89,72 @@ var createErrorResponse = function (error) {
87
89
  };
88
90
  };
89
91
  var processResponse = function (result) {
90
- console.log("result", result);
91
- if (result.status === "FAILED") {
92
- if (result.error) {
93
- if (result.error.errStatus === 401) {
94
- return {
95
- code: result.error.errStatus,
96
- success: false,
97
- error: result.error.message,
98
- message: result.error.message,
99
- data: "",
100
- };
101
- }
102
- }
92
+ var _a, _b;
93
+ var defaultMessages = {
94
+ 401: "Unauthorized",
95
+ 404: "Not Found",
96
+ 409: "Conflict",
97
+ 500: "Internal Server Error",
98
+ };
99
+ if ((result === null || result === void 0 ? void 0 : result.status) === "FAILED") {
100
+ var statusCode = ((_a = result === null || result === void 0 ? void 0 : result.error) === null || _a === void 0 ? void 0 : _a.errStatus) || 400;
101
+ var errorMessage = ((_b = result === null || result === void 0 ? void 0 : result.error) === null || _b === void 0 ? void 0 : _b.message) || defaultMessages[statusCode] || "API Failed";
103
102
  return {
104
- code: 400,
103
+ code: statusCode,
105
104
  success: false,
106
- error: "API Failed",
107
- message: "",
108
- data: result,
105
+ error: errorMessage,
106
+ message: errorMessage,
107
+ data: "",
109
108
  };
110
109
  }
111
- else {
112
- if (result.code === 404) {
113
- return {
114
- code: result.code,
115
- success: false,
116
- error: result.data ? result.data : result,
117
- message: result.data ? result.data : result,
118
- data: "",
119
- };
120
- }
121
- return { code: 200, success: true, error: "", message: "", data: result };
110
+ if ((result === null || result === void 0 ? void 0 : result.code) && (result === null || result === void 0 ? void 0 : result.code) !== 200) {
111
+ var errorMessage = (result === null || result === void 0 ? void 0 : result.data) || defaultMessages[result === null || result === void 0 ? void 0 : result.code] || "An error occurred";
112
+ return {
113
+ code: result.code,
114
+ success: false,
115
+ error: errorMessage,
116
+ message: errorMessage,
117
+ data: "",
118
+ };
122
119
  }
120
+ return {
121
+ code: 200,
122
+ success: true,
123
+ error: "",
124
+ message: "",
125
+ data: (result === null || result === void 0 ? void 0 : result.result) || result,
126
+ totalItems: (result === null || result === void 0 ? void 0 : result.totalItems) || 0,
127
+ totalPages: (result === null || result === void 0 ? void 0 : result.totalPages) || 0,
128
+ };
123
129
  };
124
- var getAllItems = function (baseurl, headers, collectionName) { return __awaiter(void 0, void 0, void 0, function () {
125
- var url, response, result, error_1;
130
+ var getAllItems = function (baseurl, headers, collectionName, reqQuery, query) { return __awaiter(void 0, void 0, void 0, function () {
131
+ var queryParams_1, url, response, result, error_1;
126
132
  return __generator(this, function (_a) {
127
133
  switch (_a.label) {
128
134
  case 0:
129
135
  _a.trys.push([0, 3, , 4]);
130
- url = "".concat(baseurl, "/collection/").concat(collectionName, "/items");
136
+ queryParams_1 = new URLSearchParams();
137
+ console.log("headers :>> ", headers);
138
+ console.log("query :>> ", query);
139
+ console.log("reqQuery :>> ", reqQuery);
140
+ if (reqQuery === null || reqQuery === void 0 ? void 0 : reqQuery.sortField)
141
+ queryParams_1.append("sortField", reqQuery.sortField);
142
+ if (reqQuery === null || reqQuery === void 0 ? void 0 : reqQuery.sortOrder)
143
+ queryParams_1.append("sortOrder", reqQuery.sortOrder);
144
+ if (reqQuery === null || reqQuery === void 0 ? void 0 : reqQuery.searchTerm)
145
+ queryParams_1.append("searchTerm", reqQuery.searchTerm);
146
+ if (reqQuery === null || reqQuery === void 0 ? void 0 : reqQuery.isPagination) {
147
+ queryParams_1.append("page", reqQuery.page);
148
+ queryParams_1.append("limit", reqQuery.limit);
149
+ }
150
+ query.map(function (query) {
151
+ var conditionString = constants_1.QueryOperation[query.condition];
152
+ var field = encodeURIComponent(query.field);
153
+ var value = encodeURIComponent(query.value);
154
+ queryParams_1.append("".concat(field, "%3A").concat(conditionString), "".concat(value));
155
+ });
156
+ url = "".concat(baseurl, "/collection/").concat(collectionName, "/items?").concat(queryParams_1.toString());
157
+ console.log("Generated URL:", url);
131
158
  return [4 /*yield*/, fetch(url, { method: "GET", headers: headers })];
132
159
  case 1:
133
160
  response = _a.sent();
@@ -137,6 +164,7 @@ var getAllItems = function (baseurl, headers, collectionName) { return __awaiter
137
164
  return [2 /*return*/, processResponse(result)];
138
165
  case 3:
139
166
  error_1 = _a.sent();
167
+ console.log("error :>> ", error_1);
140
168
  return [2 /*return*/, createErrorResponse(error_1)];
141
169
  case 4: return [2 /*return*/];
142
170
  }
@@ -168,11 +196,11 @@ var createItem = function (baseurl, headers, collectionName, body) { return __aw
168
196
  case 3:
169
197
  result = _a.sent();
170
198
  return [2 /*return*/, {
171
- code: result.code,
199
+ code: result === null || result === void 0 ? void 0 : result.code,
172
200
  success: true,
173
- data: result,
201
+ data: result === null || result === void 0 ? void 0 : result.data,
174
202
  error: "",
175
- message: "",
203
+ message: result.message || "",
176
204
  }];
177
205
  case 4: return [3 /*break*/, 6];
178
206
  case 5:
@@ -310,8 +338,8 @@ var deleteItemWithUuid = function (baseurl, headers, collectionName, itemUuid) {
310
338
  case 2:
311
339
  result = _a.sent();
312
340
  return [2 /*return*/, {
313
- code: result.code,
314
- success: result.code == 200 ? true : false,
341
+ code: result === null || result === void 0 ? void 0 : result.code,
342
+ success: (result === null || result === void 0 ? void 0 : result.code) == 200 ? true : false,
315
343
  data: result.message,
316
344
  error: "",
317
345
  message: "",
@@ -0,0 +1,25 @@
1
+ export declare enum QueryOperation {
2
+ EQUALS = "EQUALS",
3
+ IS_NOT_NULL = "IS_NOT_NULL",
4
+ IS_NULL = "IS_NULL",
5
+ LIKE = "LIKE",
6
+ LESS_THAN_EQUALS_TO = "LESS_THAN_EQUALS_TO",
7
+ GREATER_THAN_EQUALS_TO = "GREATER_THAN_EQUALS_TO",
8
+ LESS_THAN = "LESS_THAN",
9
+ GREATER_THAN = "GREATER_THAN",
10
+ IN_LIST = "IN_LIST",
11
+ NOT_IN_LIST = "NOT_IN_LIST"
12
+ }
13
+ export type Query = {
14
+ field: string;
15
+ condition: QueryOperation;
16
+ value: string;
17
+ };
18
+ export type SearchPaginate = {
19
+ sortField?: string;
20
+ sortOrder?: string;
21
+ searchTerm?: string | any;
22
+ isPagination?: boolean;
23
+ page?: number | string | any;
24
+ limit?: number | string | any;
25
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryOperation = void 0;
4
+ var QueryOperation;
5
+ (function (QueryOperation) {
6
+ QueryOperation["EQUALS"] = "EQUALS";
7
+ QueryOperation["IS_NOT_NULL"] = "IS_NOT_NULL";
8
+ QueryOperation["IS_NULL"] = "IS_NULL";
9
+ QueryOperation["LIKE"] = "LIKE";
10
+ QueryOperation["LESS_THAN_EQUALS_TO"] = "LESS_THAN_EQUALS_TO";
11
+ QueryOperation["GREATER_THAN_EQUALS_TO"] = "GREATER_THAN_EQUALS_TO";
12
+ QueryOperation["LESS_THAN"] = "LESS_THAN";
13
+ QueryOperation["GREATER_THAN"] = "GREATER_THAN";
14
+ QueryOperation["IN_LIST"] = "IN_LIST";
15
+ QueryOperation["NOT_IN_LIST"] = "NOT_IN_LIST";
16
+ })(QueryOperation = exports.QueryOperation || (exports.QueryOperation = {}));
@@ -0,0 +1,2 @@
1
+ export declare const encryptData: (data: string, key: string) => Promise<string>;
2
+ export declare const decryptData: (data: string, key: string) => Promise<string>;
@@ -0,0 +1,99 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.decryptData = exports.encryptData = void 0;
43
+ var crypto_1 = __importDefault(require("crypto"));
44
+ 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
+ }); };
64
+ 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
+ }); };
85
+ exports.decryptData = decryptData;
86
+ var handleExtraString = function (key, append) {
87
+ 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);
97
+ return key;
98
+ }
99
+ };
@@ -0,0 +1,26 @@
1
+ export declare enum MethodTypes {
2
+ POST = "post",
3
+ GET = "get",
4
+ PATCH = "patch",
5
+ PUT = "put",
6
+ DELETE = "delete"
7
+ }
8
+ export type APIData = {
9
+ method: MethodTypes;
10
+ url: string;
11
+ headers: any;
12
+ body: object;
13
+ };
14
+ export declare const makeAPICall: (apiData: APIData) => Promise<{
15
+ status: number;
16
+ error: null;
17
+ headers: Headers;
18
+ data: any;
19
+ message?: undefined;
20
+ } | {
21
+ status: number;
22
+ error: any;
23
+ message: any;
24
+ headers?: undefined;
25
+ data?: undefined;
26
+ }>;
@@ -0,0 +1,112 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.makeAPICall = exports.MethodTypes = void 0;
40
+ var ErrorTypes = {
41
+ INVALID_DATA: "Given data is not valid",
42
+ MISSING_URL: "Provided URL is not valid",
43
+ };
44
+ var MethodTypes;
45
+ (function (MethodTypes) {
46
+ MethodTypes["POST"] = "post";
47
+ MethodTypes["GET"] = "get";
48
+ MethodTypes["PATCH"] = "patch";
49
+ MethodTypes["PUT"] = "put";
50
+ MethodTypes["DELETE"] = "delete";
51
+ })(MethodTypes = exports.MethodTypes || (exports.MethodTypes = {}));
52
+ var makeAPICall = function (apiData) { return __awaiter(void 0, void 0, void 0, function () {
53
+ var method, url, headers, body, response, bodyMethods, extra, error_1;
54
+ var _a;
55
+ return __generator(this, function (_b) {
56
+ switch (_b.label) {
57
+ case 0:
58
+ console.log("API call started");
59
+ _b.label = 1;
60
+ case 1:
61
+ _b.trys.push([1, 4, , 5]);
62
+ if (!apiData) {
63
+ return [2 /*return*/, { status: 402, error: {}, message: ErrorTypes.INVALID_DATA }];
64
+ }
65
+ method = apiData.method, url = apiData.url, headers = apiData.headers, body = apiData.body;
66
+ if (!url) {
67
+ return [2 /*return*/, { status: 402, error: {}, message: ErrorTypes.MISSING_URL }];
68
+ }
69
+ if (!method) {
70
+ method = MethodTypes.GET;
71
+ }
72
+ response = null;
73
+ bodyMethods = [
74
+ MethodTypes.POST,
75
+ MethodTypes.PATCH,
76
+ MethodTypes.PUT,
77
+ ];
78
+ extra = { method: method };
79
+ if (headers) {
80
+ extra.headers = headers;
81
+ }
82
+ if (body) {
83
+ extra.body = body;
84
+ }
85
+ return [4 /*yield*/, fetch(url, extra)];
86
+ case 2:
87
+ response = _b.sent();
88
+ if (!(response === null || response === void 0 ? void 0 : response.ok)) {
89
+ return [2 /*return*/, {
90
+ status: response === null || response === void 0 ? void 0 : response.status,
91
+ error: {},
92
+ message: response.statusText,
93
+ }];
94
+ }
95
+ console.log("API call finished");
96
+ _a = {
97
+ status: response.status,
98
+ error: null,
99
+ headers: response.headers
100
+ };
101
+ return [4 /*yield*/, response.json()];
102
+ case 3: return [2 /*return*/, (_a.data = _b.sent(),
103
+ _a)];
104
+ case 4:
105
+ error_1 = _b.sent();
106
+ console.log("error", error_1);
107
+ return [2 /*return*/, { status: 402, error: error_1, message: error_1.message }];
108
+ case 5: return [2 /*return*/];
109
+ }
110
+ });
111
+ }); };
112
+ exports.makeAPICall = makeAPICall;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drapcode-developer-sdk",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "main": "build/index.js",
5
5
  "types": "build/index.d.ts",
6
6
  "files": [
@@ -22,9 +22,9 @@
22
22
  "url": "https://github.com/Drapcode/developer-sdk.git"
23
23
  },
24
24
  "devDependencies": {
25
+ "@types/node": "^22.12.0",
25
26
  "del-cli": "^5.0.0",
26
27
  "typescript": "^4.0.2"
27
28
  },
28
- "dependencies": {},
29
29
  "description": ""
30
30
  }