b23-lib 1.1.0
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 +0 -0
- package/dist/index.d.mts +279 -0
- package/dist/index.d.ts +279 -0
- package/dist/index.js +569 -0
- package/dist/index.mjs +542 -0
- package/package.json +33 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
// src/Dynamodb/index.ts
|
|
2
|
+
import {
|
|
3
|
+
BatchGetItemCommand,
|
|
4
|
+
DeleteItemCommand,
|
|
5
|
+
DynamoDBClient,
|
|
6
|
+
ExecuteStatementCommand,
|
|
7
|
+
GetItemCommand,
|
|
8
|
+
PutItemCommand,
|
|
9
|
+
QueryCommand,
|
|
10
|
+
ScanCommand,
|
|
11
|
+
TransactWriteItemsCommand,
|
|
12
|
+
UpdateItemCommand,
|
|
13
|
+
ReturnConsumedCapacity,
|
|
14
|
+
ReturnValue,
|
|
15
|
+
ReturnValuesOnConditionCheckFailure,
|
|
16
|
+
ReturnItemCollectionMetrics
|
|
17
|
+
} from "@aws-sdk/client-dynamodb";
|
|
18
|
+
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
|
|
19
|
+
var DynamoDBUtility = class {
|
|
20
|
+
client;
|
|
21
|
+
returnItemCollectionMetrics;
|
|
22
|
+
logCapacity;
|
|
23
|
+
region;
|
|
24
|
+
marshall = marshall;
|
|
25
|
+
unmarshall = unmarshall;
|
|
26
|
+
ReturnValue = ReturnValue;
|
|
27
|
+
ReturnItemCollectionMetrics = ReturnItemCollectionMetrics;
|
|
28
|
+
ReturnValuesOnConditionCheckFailure = ReturnValuesOnConditionCheckFailure;
|
|
29
|
+
constructor({ region, returnItemCollectionMetrics = ReturnItemCollectionMetrics.NONE, logCapacity = false }) {
|
|
30
|
+
this.region = region;
|
|
31
|
+
this.returnItemCollectionMetrics = returnItemCollectionMetrics;
|
|
32
|
+
this.logCapacity = logCapacity;
|
|
33
|
+
this.client = new DynamoDBClient({ region: this.region });
|
|
34
|
+
}
|
|
35
|
+
log(message, capacity, size) {
|
|
36
|
+
if (this.logCapacity) {
|
|
37
|
+
console.log(message, "Capacity:", capacity, "Size:", size);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async putItem(TableName, item, condition, attributeName, attributeValue, ReturnValues = ReturnValue.NONE, ReturnValuesOnFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD) {
|
|
41
|
+
const input = {
|
|
42
|
+
TableName,
|
|
43
|
+
Item: marshall(item, {
|
|
44
|
+
removeUndefinedValues: true,
|
|
45
|
+
convertClassInstanceToMap: true
|
|
46
|
+
}),
|
|
47
|
+
ConditionExpression: condition,
|
|
48
|
+
ExpressionAttributeNames: attributeName,
|
|
49
|
+
ExpressionAttributeValues: attributeValue,
|
|
50
|
+
ReturnValues,
|
|
51
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.INDEXES,
|
|
52
|
+
ReturnValuesOnConditionCheckFailure: ReturnValuesOnFailure,
|
|
53
|
+
ReturnItemCollectionMetrics: this.returnItemCollectionMetrics
|
|
54
|
+
};
|
|
55
|
+
const command = new PutItemCommand(input);
|
|
56
|
+
const result = await this.client.send(command);
|
|
57
|
+
this.log("Put", result.ConsumedCapacity, result.ItemCollectionMetrics);
|
|
58
|
+
return unmarshall(result.Attributes || {});
|
|
59
|
+
}
|
|
60
|
+
async transactWriteItems(transactItems) {
|
|
61
|
+
const input = {
|
|
62
|
+
TransactItems: transactItems.map((item) => {
|
|
63
|
+
if (item.Put) {
|
|
64
|
+
item.Put.Item = marshall(item.Put.Item, {
|
|
65
|
+
removeUndefinedValues: true,
|
|
66
|
+
convertClassInstanceToMap: true
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (item.Update) {
|
|
70
|
+
item.Update.Key = marshall(item.Update.Key);
|
|
71
|
+
}
|
|
72
|
+
if (item.Delete) {
|
|
73
|
+
item.Delete.Key = marshall(item.Delete.Key);
|
|
74
|
+
}
|
|
75
|
+
return item;
|
|
76
|
+
}),
|
|
77
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.INDEXES,
|
|
78
|
+
ReturnItemCollectionMetrics: this.returnItemCollectionMetrics
|
|
79
|
+
};
|
|
80
|
+
const command = new TransactWriteItemsCommand(input);
|
|
81
|
+
const result = await this.client.send(command);
|
|
82
|
+
this.log("Transaction", result.ConsumedCapacity, result.ItemCollectionMetrics);
|
|
83
|
+
}
|
|
84
|
+
async getItem(TableName, key, consistent = false, projection, attributeName) {
|
|
85
|
+
const input = {
|
|
86
|
+
TableName,
|
|
87
|
+
Key: marshall(key),
|
|
88
|
+
ConsistentRead: consistent,
|
|
89
|
+
ProjectionExpression: projection,
|
|
90
|
+
ExpressionAttributeNames: attributeName,
|
|
91
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.TOTAL
|
|
92
|
+
};
|
|
93
|
+
const command = new GetItemCommand(input);
|
|
94
|
+
const result = await this.client.send(command);
|
|
95
|
+
this.log("Read", result.ConsumedCapacity);
|
|
96
|
+
return unmarshall(result.Item || {});
|
|
97
|
+
}
|
|
98
|
+
async batchGetItem(TableName, keys, consistent = false, projection, attributeName) {
|
|
99
|
+
const input = {
|
|
100
|
+
RequestItems: {
|
|
101
|
+
[TableName]: {
|
|
102
|
+
Keys: keys.map((key) => marshall(key)),
|
|
103
|
+
ConsistentRead: consistent,
|
|
104
|
+
ProjectionExpression: projection,
|
|
105
|
+
ExpressionAttributeNames: attributeName
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.TOTAL
|
|
109
|
+
};
|
|
110
|
+
const command = new BatchGetItemCommand(input);
|
|
111
|
+
const result = await this.client.send(command);
|
|
112
|
+
this.log("BatchRead", result.ConsumedCapacity);
|
|
113
|
+
return result.Responses?.[TableName]?.map((item) => unmarshall(item)) || [];
|
|
114
|
+
}
|
|
115
|
+
async queryItems(TableName, keyCondition, consistent = false, projection, attributeName, attributeValue, lastEvaluatedKey) {
|
|
116
|
+
const input = {
|
|
117
|
+
TableName,
|
|
118
|
+
KeyConditionExpression: keyCondition,
|
|
119
|
+
ExpressionAttributeValues: attributeValue,
|
|
120
|
+
ConsistentRead: consistent,
|
|
121
|
+
ProjectionExpression: projection,
|
|
122
|
+
ExpressionAttributeNames: attributeName,
|
|
123
|
+
ExclusiveStartKey: lastEvaluatedKey,
|
|
124
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.TOTAL
|
|
125
|
+
};
|
|
126
|
+
const command = new QueryCommand(input);
|
|
127
|
+
const result = await this.client.send(command);
|
|
128
|
+
this.log("Query", result.ConsumedCapacity);
|
|
129
|
+
return {
|
|
130
|
+
items: result.Items?.map((item) => unmarshall(item)) || [],
|
|
131
|
+
lastEvaluatedKey: result.LastEvaluatedKey
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async scanItems(TableName, filterExpression, consistent = false, projection, attributeName, attributeValue, lastEvaluatedKey) {
|
|
135
|
+
const input = {
|
|
136
|
+
TableName,
|
|
137
|
+
FilterExpression: filterExpression,
|
|
138
|
+
ExpressionAttributeValues: attributeValue,
|
|
139
|
+
ConsistentRead: consistent,
|
|
140
|
+
ProjectionExpression: projection,
|
|
141
|
+
ExpressionAttributeNames: attributeName,
|
|
142
|
+
ExclusiveStartKey: lastEvaluatedKey,
|
|
143
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.TOTAL
|
|
144
|
+
};
|
|
145
|
+
const command = new ScanCommand(input);
|
|
146
|
+
const result = await this.client.send(command);
|
|
147
|
+
this.log("Scan", result.ConsumedCapacity);
|
|
148
|
+
return {
|
|
149
|
+
items: result.Items?.map((item) => unmarshall(item)) || [],
|
|
150
|
+
lastEvaluatedKey: result.LastEvaluatedKey
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
async partiQL(statement, parameter = [], nextToken, consistent = false) {
|
|
154
|
+
const input = {
|
|
155
|
+
Statement: statement,
|
|
156
|
+
Parameters: parameter,
|
|
157
|
+
ConsistentRead: consistent,
|
|
158
|
+
NextToken: nextToken,
|
|
159
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.INDEXES
|
|
160
|
+
};
|
|
161
|
+
const command = new ExecuteStatementCommand(input);
|
|
162
|
+
const result = await this.client.send(command);
|
|
163
|
+
this.log("PartiQL", result.ConsumedCapacity);
|
|
164
|
+
return {
|
|
165
|
+
Items: result.Items?.map((item) => unmarshall(item)) || [],
|
|
166
|
+
nextToken: result.NextToken,
|
|
167
|
+
lastEvaluatedKey: result.LastEvaluatedKey
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
async updateItem(TableName, key, condition, update, attributeName, attributeValue, ReturnValues = ReturnValue.UPDATED_NEW, ReturnValuesOnFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD) {
|
|
171
|
+
const input = {
|
|
172
|
+
TableName,
|
|
173
|
+
Key: marshall(key),
|
|
174
|
+
ConditionExpression: condition,
|
|
175
|
+
UpdateExpression: update,
|
|
176
|
+
ExpressionAttributeNames: attributeName,
|
|
177
|
+
ExpressionAttributeValues: attributeValue,
|
|
178
|
+
ReturnValues,
|
|
179
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.INDEXES,
|
|
180
|
+
ReturnValuesOnConditionCheckFailure: ReturnValuesOnFailure,
|
|
181
|
+
ReturnItemCollectionMetrics: this.returnItemCollectionMetrics
|
|
182
|
+
};
|
|
183
|
+
const command = new UpdateItemCommand(input);
|
|
184
|
+
const result = await this.client.send(command);
|
|
185
|
+
this.log("Update", result.ConsumedCapacity, result.ItemCollectionMetrics);
|
|
186
|
+
return unmarshall(result.Attributes || {});
|
|
187
|
+
}
|
|
188
|
+
async deleteItem(TableName, key, condition, attributeName, attributeValue, ReturnValues = ReturnValue.ALL_OLD, ReturnValuesOnFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD) {
|
|
189
|
+
const input = {
|
|
190
|
+
TableName,
|
|
191
|
+
Key: marshall(key),
|
|
192
|
+
ConditionExpression: condition,
|
|
193
|
+
ExpressionAttributeNames: attributeName,
|
|
194
|
+
ExpressionAttributeValues: attributeValue,
|
|
195
|
+
ReturnValues,
|
|
196
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.INDEXES,
|
|
197
|
+
ReturnValuesOnConditionCheckFailure: ReturnValuesOnFailure,
|
|
198
|
+
ReturnItemCollectionMetrics: this.returnItemCollectionMetrics
|
|
199
|
+
};
|
|
200
|
+
const command = new DeleteItemCommand(input);
|
|
201
|
+
const result = await this.client.send(command);
|
|
202
|
+
this.log("Delete", result.ConsumedCapacity, result.ItemCollectionMetrics);
|
|
203
|
+
return unmarshall(result.Attributes || {});
|
|
204
|
+
}
|
|
205
|
+
async getItemByIndex(TableName, index, keyCondition, consistent = false, projection, attributeName, attributeValue) {
|
|
206
|
+
const input = {
|
|
207
|
+
TableName,
|
|
208
|
+
IndexName: index,
|
|
209
|
+
KeyConditionExpression: keyCondition,
|
|
210
|
+
ExpressionAttributeValues: attributeValue,
|
|
211
|
+
ConsistentRead: consistent,
|
|
212
|
+
ProjectionExpression: projection,
|
|
213
|
+
ExpressionAttributeNames: attributeName,
|
|
214
|
+
ReturnConsumedCapacity: ReturnConsumedCapacity.INDEXES
|
|
215
|
+
};
|
|
216
|
+
const command = new QueryCommand(input);
|
|
217
|
+
const result = await this.client.send(command);
|
|
218
|
+
this.log("Query", result.ConsumedCapacity);
|
|
219
|
+
return { Items: result.Items?.map((item) => unmarshall(item)) || [] };
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var Dynamodb_default = DynamoDBUtility;
|
|
223
|
+
|
|
224
|
+
// src/Schema/definition.json
|
|
225
|
+
var definition_default = {
|
|
226
|
+
$id: "standards",
|
|
227
|
+
definitions: {
|
|
228
|
+
lowercaseText: {
|
|
229
|
+
type: "string",
|
|
230
|
+
pattern: "^(?!\\s)(?!.*\\s$)[a-z]*$"
|
|
231
|
+
},
|
|
232
|
+
lowercaseText10: {
|
|
233
|
+
type: "string",
|
|
234
|
+
pattern: "^(?!\\s)(?!.*\\s$)[a-z]{0,10}$"
|
|
235
|
+
},
|
|
236
|
+
lowercaseText16: {
|
|
237
|
+
type: "string",
|
|
238
|
+
pattern: "^(?!\\s)(?!.*\\s$)[a-z]{0,16}$"
|
|
239
|
+
},
|
|
240
|
+
lowercaseText30: {
|
|
241
|
+
type: "string",
|
|
242
|
+
pattern: "^(?!\\s)(?!.*\\s$)[a-z]{0,30}$"
|
|
243
|
+
},
|
|
244
|
+
lowercaseText50: {
|
|
245
|
+
type: "string",
|
|
246
|
+
pattern: "^(?!\\s)(?!.*\\s$)[a-z]{0,50}$"
|
|
247
|
+
},
|
|
248
|
+
lowercaseText256: {
|
|
249
|
+
type: "string",
|
|
250
|
+
pattern: "^(?!\\s)(?!.*\\s$)[a-z]{0,256}$"
|
|
251
|
+
},
|
|
252
|
+
text: {
|
|
253
|
+
type: "string",
|
|
254
|
+
pattern: "^(?!\\s)(?!.*\\s$).*$"
|
|
255
|
+
},
|
|
256
|
+
text10: {
|
|
257
|
+
type: "string",
|
|
258
|
+
pattern: "^(?!\\s)(?!.*\\s$).{0,10}$"
|
|
259
|
+
},
|
|
260
|
+
text16: {
|
|
261
|
+
type: "string",
|
|
262
|
+
pattern: "^(?!\\s)(?!.*\\s$).{0,16}$"
|
|
263
|
+
},
|
|
264
|
+
text30: {
|
|
265
|
+
type: "string",
|
|
266
|
+
pattern: "^(?!\\s)(?!.*\\s$).{0,30}$"
|
|
267
|
+
},
|
|
268
|
+
text50: {
|
|
269
|
+
type: "string",
|
|
270
|
+
pattern: "^(?!\\s)(?!.*\\s$).{0,50}$"
|
|
271
|
+
},
|
|
272
|
+
text256: {
|
|
273
|
+
type: "string",
|
|
274
|
+
pattern: "^(?!\\s)(?!.*\\s$).{0,256}$"
|
|
275
|
+
},
|
|
276
|
+
requiredText: {
|
|
277
|
+
type: "string",
|
|
278
|
+
pattern: "^(?!\\s)(?!.*\\s$).+$"
|
|
279
|
+
},
|
|
280
|
+
requiredText10: {
|
|
281
|
+
type: "string",
|
|
282
|
+
pattern: "^(?!\\s)(?!.*\\s$).{1,10}$"
|
|
283
|
+
},
|
|
284
|
+
requiredText16: {
|
|
285
|
+
type: "string",
|
|
286
|
+
pattern: "^(?!\\s)(?!.*\\s$).{1,16}$"
|
|
287
|
+
},
|
|
288
|
+
requiredText30: {
|
|
289
|
+
type: "string",
|
|
290
|
+
pattern: "^(?!\\s)(?!.*\\s$).{1,30}$"
|
|
291
|
+
},
|
|
292
|
+
requiredText50: {
|
|
293
|
+
type: "string",
|
|
294
|
+
pattern: "^(?!\\s)(?!.*\\s$).{1,50}$"
|
|
295
|
+
},
|
|
296
|
+
requiredText256: {
|
|
297
|
+
type: "string",
|
|
298
|
+
pattern: "^(?!\\s)(?!.*\\s$).{1,256}$"
|
|
299
|
+
},
|
|
300
|
+
url: {
|
|
301
|
+
type: "string",
|
|
302
|
+
pattern: "^https://[^\\s/$.?#].[^\\s]*$",
|
|
303
|
+
maxLength: 2048
|
|
304
|
+
},
|
|
305
|
+
uuid: {
|
|
306
|
+
type: "string",
|
|
307
|
+
minLength: 1,
|
|
308
|
+
pattern: "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
|
|
309
|
+
},
|
|
310
|
+
productKey: {
|
|
311
|
+
type: "string",
|
|
312
|
+
pattern: "^(?!\\s)(?!.*\\s$)[A-Z0-9-]{4,16}$"
|
|
313
|
+
},
|
|
314
|
+
variantId: {
|
|
315
|
+
type: "string",
|
|
316
|
+
pattern: "^(?!\\s)(?!.*\\s$)[A-Z0-9-]{4,16}$"
|
|
317
|
+
},
|
|
318
|
+
firstName: { $ref: "#/definitions/requiredText30" },
|
|
319
|
+
lastName: { $ref: "#/definitions/text30" },
|
|
320
|
+
phone: {
|
|
321
|
+
type: "string",
|
|
322
|
+
pattern: "^[0-9]{10}$"
|
|
323
|
+
},
|
|
324
|
+
email: {
|
|
325
|
+
type: "string",
|
|
326
|
+
pattern: "^[^\\s]+@[^\\s]+\\.[^\\s]+$"
|
|
327
|
+
},
|
|
328
|
+
addressLine1: { $ref: "#/definitions/requiredText50" },
|
|
329
|
+
addressLine2: { $ref: "#/definitions/text50" },
|
|
330
|
+
city: { $ref: "#/definitions/requiredText30" },
|
|
331
|
+
postalCode: { $ref: "#/definitions/requiredText16" },
|
|
332
|
+
state: {
|
|
333
|
+
type: "string",
|
|
334
|
+
enum: ["AP", "AR", "AS", "BR", "CT", "GA", "GJ", "HR", "HP", "JH", "KA", "KL", "MP", "MH", "MN", "ML", "MZ", "NL", "OR", "PB", "RJ", "SK", "TN", "TG", "TR", "UP", "UT", "WB", "AN", "CH", "DH", "LD", "DL", "PY", "LA", "JK"]
|
|
335
|
+
},
|
|
336
|
+
country: {
|
|
337
|
+
type: "string",
|
|
338
|
+
enum: [
|
|
339
|
+
"IN"
|
|
340
|
+
]
|
|
341
|
+
},
|
|
342
|
+
currency: {
|
|
343
|
+
type: "string",
|
|
344
|
+
enum: [
|
|
345
|
+
"INR"
|
|
346
|
+
]
|
|
347
|
+
},
|
|
348
|
+
locale: {
|
|
349
|
+
type: "string",
|
|
350
|
+
enum: [
|
|
351
|
+
"en-IN"
|
|
352
|
+
]
|
|
353
|
+
},
|
|
354
|
+
addressType: {
|
|
355
|
+
type: "string",
|
|
356
|
+
enum: ["shipping", "billing", "shipping&billing"]
|
|
357
|
+
},
|
|
358
|
+
address: {
|
|
359
|
+
type: "object",
|
|
360
|
+
properties: {
|
|
361
|
+
firstName: { $ref: "standards#/definitions/firstName" },
|
|
362
|
+
lastName: { $ref: "standards#/definitions/lastName" },
|
|
363
|
+
phone: { $ref: "standards#/definitions/phone" },
|
|
364
|
+
email: { $ref: "standards#/definitions/email" },
|
|
365
|
+
addressLine1: { $ref: "standards#/definitions/addressLine1" },
|
|
366
|
+
addressLine2: { $ref: "standards#/definitions/addressLine2" },
|
|
367
|
+
city: { $ref: "standards#/definitions/city" },
|
|
368
|
+
postalCode: { $ref: "standards#/definitions/postalCode" },
|
|
369
|
+
state: { $ref: "standards#/definitions/state" },
|
|
370
|
+
country: { $ref: "standards#/definitions/country" }
|
|
371
|
+
},
|
|
372
|
+
required: ["firstName", "lastName", "phone", "email", "addressLine1", "postalCode", "state", "country"]
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
// src/Schema/index.ts
|
|
378
|
+
var Schema = {
|
|
379
|
+
getStandardSchemaDefinition() {
|
|
380
|
+
return definition_default;
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
var Schema_default = Schema;
|
|
384
|
+
|
|
385
|
+
// src/Auth/index.ts
|
|
386
|
+
import { EncryptJWT, jwtDecrypt } from "jose";
|
|
387
|
+
import util2 from "util";
|
|
388
|
+
|
|
389
|
+
// src/enums/ErrorTypes.ts
|
|
390
|
+
var ErrorTypes_default = Object.freeze({
|
|
391
|
+
INVALID_TOKEN: "Invalid Token",
|
|
392
|
+
TOKEN_EXPIRED: "Token Expired",
|
|
393
|
+
INTERNAL_SERVER_ERROR: "Internal Server Error"
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
// src/Logger/index.ts
|
|
397
|
+
import util from "util";
|
|
398
|
+
var Logger = {
|
|
399
|
+
logException: (functionName, error) => {
|
|
400
|
+
console.log(`Exception Occurred in Function: ${functionName}, Error: ${util.inspect(error)}`);
|
|
401
|
+
},
|
|
402
|
+
logError: (functionName, errorMessage) => {
|
|
403
|
+
console.log(`Error Occurred in Function: ${functionName}, Error: ${util.inspect(errorMessage)}`);
|
|
404
|
+
},
|
|
405
|
+
logMessage: (functionName, message) => {
|
|
406
|
+
console.log(`Message in Function: ${functionName}, Error: ${util.inspect(message)}`);
|
|
407
|
+
},
|
|
408
|
+
logInvalidPayload: (functionName, errorMessage) => {
|
|
409
|
+
console.log(`Invalid Payload received for Function: ${functionName}, Error: ${errorMessage}`);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
var Logger_default = Logger;
|
|
413
|
+
|
|
414
|
+
// src/Utils/response.ts
|
|
415
|
+
var ResponseUtility = {
|
|
416
|
+
handleException: (functionName, error, res) => {
|
|
417
|
+
if (error.knownError) {
|
|
418
|
+
error.logError && Logger_default.logError(functionName, error);
|
|
419
|
+
res.status(error.status).json({
|
|
420
|
+
status: error.status,
|
|
421
|
+
error: error.error
|
|
422
|
+
});
|
|
423
|
+
} else {
|
|
424
|
+
Logger_default.logException(functionName, error);
|
|
425
|
+
res.status(500).json({
|
|
426
|
+
status: 500,
|
|
427
|
+
error: ErrorTypes_default.INTERNAL_SERVER_ERROR
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
generateResponse: (status, data, error) => {
|
|
432
|
+
return {
|
|
433
|
+
status,
|
|
434
|
+
data,
|
|
435
|
+
error
|
|
436
|
+
};
|
|
437
|
+
},
|
|
438
|
+
generateError: (status, error, knownError = true, logError = false) => {
|
|
439
|
+
return {
|
|
440
|
+
status,
|
|
441
|
+
error,
|
|
442
|
+
knownError,
|
|
443
|
+
logError
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
var response_default = ResponseUtility;
|
|
448
|
+
|
|
449
|
+
// src/Auth/index.ts
|
|
450
|
+
var AuthUtility = class {
|
|
451
|
+
secretToken;
|
|
452
|
+
maxTokenAge;
|
|
453
|
+
constructor({ secret, maxTokenAge = "30 days" }) {
|
|
454
|
+
this.secretToken = secret;
|
|
455
|
+
this.maxTokenAge = maxTokenAge;
|
|
456
|
+
}
|
|
457
|
+
async createToken(id, additionalData) {
|
|
458
|
+
const payload = {
|
|
459
|
+
id,
|
|
460
|
+
...additionalData
|
|
461
|
+
};
|
|
462
|
+
const secretKey = Buffer.from(this.secretToken, "hex");
|
|
463
|
+
const token = await new EncryptJWT(payload).setExpirationTime(this.maxTokenAge).setIssuedAt().setProtectedHeader({ alg: "dir", enc: "A256CBC-HS512" }).encrypt(secretKey);
|
|
464
|
+
return token;
|
|
465
|
+
}
|
|
466
|
+
async verifyToken(token) {
|
|
467
|
+
const secretKey = Buffer.from(this.secretToken, "hex");
|
|
468
|
+
const jwt = await jwtDecrypt(token, secretKey, { maxTokenAge: this.maxTokenAge });
|
|
469
|
+
return jwt.payload;
|
|
470
|
+
}
|
|
471
|
+
AuthMiddleware() {
|
|
472
|
+
return async (req, res, next) => {
|
|
473
|
+
try {
|
|
474
|
+
const token = req.get("Authorization")?.split(" ")?.[1];
|
|
475
|
+
if (!token) {
|
|
476
|
+
throw new Error(ErrorTypes_default.INVALID_TOKEN);
|
|
477
|
+
}
|
|
478
|
+
const payload = await this.verifyToken(token);
|
|
479
|
+
res.locals.auth = payload;
|
|
480
|
+
next();
|
|
481
|
+
} catch (error) {
|
|
482
|
+
Logger_default.logError("AuthMiddleware", util2.inspect(error));
|
|
483
|
+
response_default.handleException("AuthMiddleware", response_default.generateError(401, ErrorTypes_default.TOKEN_EXPIRED), res);
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
var Auth_default = AuthUtility;
|
|
489
|
+
|
|
490
|
+
// src/Utils/fetch.ts
|
|
491
|
+
var Fetch = async (baseURL, endpoint, method = "GET", headers = {}, payload) => {
|
|
492
|
+
const options = {
|
|
493
|
+
method,
|
|
494
|
+
headers: {
|
|
495
|
+
"Content-Type": "application/json",
|
|
496
|
+
...headers
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
if (method !== "GET" && payload) {
|
|
500
|
+
options.body = JSON.stringify(payload);
|
|
501
|
+
}
|
|
502
|
+
try {
|
|
503
|
+
const response = await fetch(`${baseURL}/${endpoint}`, options);
|
|
504
|
+
if (!response.ok) {
|
|
505
|
+
const errorBody = await response.json().catch(() => response.text());
|
|
506
|
+
throw {
|
|
507
|
+
status: response.status,
|
|
508
|
+
statusText: response.statusText,
|
|
509
|
+
error: errorBody ? errorBody : {
|
|
510
|
+
status: response.status,
|
|
511
|
+
error: response.statusText
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
const body = await response.json();
|
|
516
|
+
Logger_default.logMessage("Fetch", `API call successful: URL-${baseURL}/${endpoint}, Status- ${response.status}}`);
|
|
517
|
+
return {
|
|
518
|
+
status: response.status,
|
|
519
|
+
statusText: response.statusText,
|
|
520
|
+
data: body.data
|
|
521
|
+
};
|
|
522
|
+
} catch (err) {
|
|
523
|
+
Logger_default.logError("Fetch", `API call failed: URL-${baseURL}/${endpoint}, Status- ${err.status || 500}, Error- ${JSON.stringify(err.error, null, 2)}`);
|
|
524
|
+
throw {
|
|
525
|
+
status: err.status || 500,
|
|
526
|
+
statusText: err.statusText || "Internal Server Error",
|
|
527
|
+
error: err.error || {
|
|
528
|
+
status: err.status || 500,
|
|
529
|
+
error: err.statusText || "Something went wrong"
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
var fetch_default = Fetch;
|
|
535
|
+
export {
|
|
536
|
+
Auth_default as Auth,
|
|
537
|
+
Dynamodb_default as DynamoDB,
|
|
538
|
+
fetch_default as Fetch,
|
|
539
|
+
Logger_default as Logger,
|
|
540
|
+
response_default as ResponseUtility,
|
|
541
|
+
Schema_default as Schema
|
|
542
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "b23-lib",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "This repo hold common classes, type and util functiona",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"repository": {
|
|
9
|
+
"url": "https://github.com/MyWebsite-B23/util-lib.git"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"b23-lib"
|
|
16
|
+
],
|
|
17
|
+
"author": "Bharath Gowda B",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@aws-sdk/client-dynamodb": "^3.620.0",
|
|
21
|
+
"@aws-sdk/util-dynamodb": "^3.620.0",
|
|
22
|
+
"jose": "^5.9.6",
|
|
23
|
+
"util": "^0.12.5"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^22.10.1",
|
|
27
|
+
"tsup": "^8.3.5",
|
|
28
|
+
"typescript": "^5.7.2"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
]
|
|
33
|
+
}
|