@63klabs/cache-data 1.3.8 → 1.3.10
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/CHANGELOG.md +109 -5
- package/CONTRIBUTING.md +58 -5
- package/README.md +20 -34
- package/package.json +16 -27
- package/src/lib/dao-cache.js +55 -47
- package/src/lib/dao-endpoint.js +68 -38
- package/src/lib/tools/AWS.classes.js +58 -5
- package/src/lib/tools/{APIRequest.class.js → ApiRequest.class.js} +164 -51
- package/src/lib/tools/CachedParametersSecrets.classes.js +10 -9
- package/src/lib/tools/ClientRequest.class.js +987 -44
- package/src/lib/tools/Connections.classes.js +5 -5
- package/src/lib/tools/Response.class.js +25 -0
- package/src/lib/tools/generic.response.html.js +8 -113
- package/src/lib/tools/generic.response.js +73 -0
- package/src/lib/tools/generic.response.json.js +5 -135
- package/src/lib/tools/generic.response.rss.js +10 -114
- package/src/lib/tools/generic.response.text.js +5 -115
- package/src/lib/tools/generic.response.xml.js +10 -114
- package/src/lib/tools/index.js +113 -40
- package/src/lib/utils/ValidationExecutor.class.js +70 -0
- package/src/lib/utils/ValidationMatcher.class.js +417 -0
- package/AGENTS.md +0 -1012
package/src/lib/dao-endpoint.js
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
*
|
|
9
9
|
* The class can be used as a template and modified to provide additional
|
|
10
10
|
* logic, query, filtering, and manipulation before/after data is sent/received
|
|
11
|
-
* via the
|
|
11
|
+
* via the ApiRequest class.
|
|
12
12
|
*
|
|
13
13
|
* The class itself is not exposed, instead various functions can be used
|
|
14
|
-
* to access the class. For
|
|
14
|
+
* to access the class. For example, send(connection, data)
|
|
15
15
|
*
|
|
16
16
|
* The connection parameter is used to pass connection information to the
|
|
17
17
|
* API (host, path, query, etc).
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* // access function that utilizes the class
|
|
24
|
-
* const
|
|
25
|
-
* return (new Endpoint(connection).
|
|
24
|
+
* const send = async (connection, data = null) => {
|
|
25
|
+
* return (new Endpoint(connection).send());
|
|
26
26
|
* };
|
|
27
27
|
*/
|
|
28
28
|
|
|
@@ -53,12 +53,11 @@
|
|
|
53
53
|
* -----------------------------------------------------------------------------
|
|
54
54
|
*/
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const tools = require("./tools/index.js");
|
|
56
|
+
const DebugAndLog = require("./tools/DebugAndLog.class.js");
|
|
57
|
+
const ApiRequest = require("./tools/ApiRequest.class.js");
|
|
59
58
|
|
|
60
59
|
/**
|
|
61
|
-
* Makes a
|
|
60
|
+
* Makes a request to a remote endpoint with the specified connection configuration.
|
|
62
61
|
*
|
|
63
62
|
* This function provides a simple interface for making HTTP requests to external APIs
|
|
64
63
|
* or services. It supports both URI-based and component-based (protocol/host/path)
|
|
@@ -70,12 +69,11 @@ const tools = require("./tools/index.js");
|
|
|
70
69
|
* @param {ConnectionObject} connection - Connection configuration object specifying the endpoint details
|
|
71
70
|
* @param {Object} [query={}] - Additional query data to merge with connection parameters. If query.parameters is provided, it will be merged with connection.parameters
|
|
72
71
|
* @returns {Promise<{success: boolean, statusCode: number, body: Object|string|null, headers: Object}>} Response object containing success status, HTTP status code, parsed body, and response headers
|
|
73
|
-
* @throws {Error} Throws an error if the request fails due to network issues or invalid configuration
|
|
74
72
|
*
|
|
75
73
|
* @example
|
|
76
74
|
* // Using separate host and path
|
|
77
75
|
* const { endpoint } = require("@63klabs/cache-data");
|
|
78
|
-
* const response = await endpoint.
|
|
76
|
+
* const response = await endpoint.send(
|
|
79
77
|
* { host: "api.example.com", path: "/data" },
|
|
80
78
|
* { parameters: { q: "Chicago" } }
|
|
81
79
|
* );
|
|
@@ -84,7 +82,7 @@ const tools = require("./tools/index.js");
|
|
|
84
82
|
* @example
|
|
85
83
|
* // Using complete URI
|
|
86
84
|
* const { endpoint } = require("@63klabs/cache-data");
|
|
87
|
-
* const response = await endpoint.
|
|
85
|
+
* const response = await endpoint.send(
|
|
88
86
|
* { uri: "https://api.example.com/data" },
|
|
89
87
|
* { parameters: { q: "Chicago" } }
|
|
90
88
|
* );
|
|
@@ -93,7 +91,7 @@ const tools = require("./tools/index.js");
|
|
|
93
91
|
* @example
|
|
94
92
|
* // With custom headers and timeout
|
|
95
93
|
* const { endpoint } = require("@63klabs/cache-data");
|
|
96
|
-
* const response = await endpoint.
|
|
94
|
+
* const response = await endpoint.send({
|
|
97
95
|
* host: "api.example.com",
|
|
98
96
|
* path: "/secure/data",
|
|
99
97
|
* headers: { "Authorization": "Bearer token123" },
|
|
@@ -103,19 +101,36 @@ const tools = require("./tools/index.js");
|
|
|
103
101
|
* @example
|
|
104
102
|
* // POST request with body
|
|
105
103
|
* const { endpoint } = require("@63klabs/cache-data");
|
|
106
|
-
* const response = await endpoint.
|
|
104
|
+
* const response = await endpoint.send({
|
|
107
105
|
* method: "POST",
|
|
108
106
|
* uri: "https://api.example.com/submit",
|
|
109
107
|
* body: JSON.stringify({ name: "John", age: 30 }),
|
|
110
108
|
* headers: { "Content-Type": "application/json" }
|
|
111
109
|
* });
|
|
112
110
|
*/
|
|
113
|
-
const
|
|
111
|
+
const send = async (connection, query = {}) => {
|
|
114
112
|
if (query === null) { query = {} };
|
|
115
|
-
return (new Endpoint(connection, query).
|
|
113
|
+
return (new Endpoint(connection, query).send());
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @deprecated Use send() instead. This function is maintained for backwards compatibility.
|
|
118
|
+
* @see send
|
|
119
|
+
*/
|
|
120
|
+
const get = async (connection, query = {}) => {
|
|
121
|
+
return send(connection, query);
|
|
116
122
|
};
|
|
117
123
|
|
|
118
124
|
/**
|
|
125
|
+
* @deprecated Use send() instead. This function is maintained for backwards compatibility.
|
|
126
|
+
* @see send
|
|
127
|
+
*/
|
|
128
|
+
const getDataDirectFromURI = async (connection, query = {}) => {
|
|
129
|
+
return send(connection, query);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @class Endpoint
|
|
119
134
|
* Endpoint request class for making HTTP requests to remote APIs.
|
|
120
135
|
*
|
|
121
136
|
* This class provides a bare-bones implementation for making API requests and can be used
|
|
@@ -124,19 +139,19 @@ const get = async (connection, query = {}) => {
|
|
|
124
139
|
* merging, and automatic JSON parsing of responses.
|
|
125
140
|
*
|
|
126
141
|
* The class is typically not instantiated directly but accessed through convenience functions
|
|
127
|
-
* like `endpoint.
|
|
142
|
+
* like `endpoint.send()`. However, it can be extended to add custom pre/post-processing logic.
|
|
128
143
|
*
|
|
129
144
|
* @example
|
|
130
145
|
* // Direct instantiation (advanced usage)
|
|
131
146
|
* const endpoint = new Endpoint({ host: "api.example.com", path: "/data" });
|
|
132
|
-
* const response = await endpoint.
|
|
147
|
+
* const response = await endpoint.send();
|
|
133
148
|
*
|
|
134
149
|
* @example
|
|
135
150
|
* // Extending for custom logic
|
|
136
151
|
* class CustomEndpoint extends Endpoint {
|
|
137
|
-
* async
|
|
152
|
+
* async send() {
|
|
138
153
|
* // Custom pre-processing
|
|
139
|
-
* const response = await super.
|
|
154
|
+
* const response = await super.send();
|
|
140
155
|
* // Custom post-processing
|
|
141
156
|
* return response;
|
|
142
157
|
* }
|
|
@@ -167,6 +182,9 @@ class Endpoint {
|
|
|
167
182
|
*/
|
|
168
183
|
constructor(connection, query = {}) {
|
|
169
184
|
|
|
185
|
+
/**
|
|
186
|
+
* @property {Object|null} response - Configured response object
|
|
187
|
+
*/
|
|
170
188
|
this.response = null;
|
|
171
189
|
|
|
172
190
|
// if query has parameters property then we will combine with connection parameters
|
|
@@ -180,6 +198,9 @@ class Endpoint {
|
|
|
180
198
|
}
|
|
181
199
|
}
|
|
182
200
|
|
|
201
|
+
/**
|
|
202
|
+
* @property {Object} request - Configured request object
|
|
203
|
+
*/
|
|
183
204
|
this.request = {
|
|
184
205
|
method: this._setRequestSetting(connection, "method", "GET"),
|
|
185
206
|
uri: this._setRequestSetting(connection, "uri", ""),
|
|
@@ -202,6 +223,8 @@ class Endpoint {
|
|
|
202
223
|
* default value and returns that default. This ensures all request settings
|
|
203
224
|
* have valid values.
|
|
204
225
|
*
|
|
226
|
+
* @private
|
|
227
|
+
*
|
|
205
228
|
* @param {ConnectionObject} connection - The connection object to check for the key
|
|
206
229
|
* @param {string} key - The property key to check for and retrieve
|
|
207
230
|
* @param {*} defaultValue - The default value to use if the key is not found
|
|
@@ -223,7 +246,7 @@ class Endpoint {
|
|
|
223
246
|
/**
|
|
224
247
|
* Executes the HTTP request and returns the response.
|
|
225
248
|
*
|
|
226
|
-
* This method sends the configured request to the remote endpoint using the
|
|
249
|
+
* This method sends the configured request to the remote endpoint using the ApiRequest
|
|
227
250
|
* class. It automatically attempts to parse the response body as JSON. If the body is
|
|
228
251
|
* not valid JSON, it is kept as text. The method caches the response so subsequent
|
|
229
252
|
* calls return the same result without making additional requests.
|
|
@@ -232,12 +255,11 @@ class Endpoint {
|
|
|
232
255
|
* request) or post-processing (after the response) logic.
|
|
233
256
|
*
|
|
234
257
|
* @returns {Promise<{success: boolean, statusCode: number, body: Object|string|null, headers: Object}>} Response object with success status, HTTP status code, parsed body, and headers
|
|
235
|
-
* @throws {Error} Throws an error if the request fails due to network issues, timeout, or invalid configuration
|
|
236
258
|
*
|
|
237
259
|
* @example
|
|
238
|
-
* // Basic usage through the
|
|
260
|
+
* // Basic usage through the send() function
|
|
239
261
|
* const endpoint = new Endpoint({ host: "api.example.com", path: "/data" });
|
|
240
|
-
* const response = await endpoint.
|
|
262
|
+
* const response = await endpoint.send();
|
|
241
263
|
* if (response.success) {
|
|
242
264
|
* console.log(response.body);
|
|
243
265
|
* }
|
|
@@ -246,20 +268,20 @@ class Endpoint {
|
|
|
246
268
|
* // Handling errors
|
|
247
269
|
* try {
|
|
248
270
|
* const endpoint = new Endpoint({ uri: "https://api.example.com/data" });
|
|
249
|
-
* const response = await endpoint.
|
|
271
|
+
* const response = await endpoint.send();
|
|
250
272
|
* console.log(response.statusCode, response.body);
|
|
251
273
|
* } catch (error) {
|
|
252
274
|
* console.error("Request failed:", error.message);
|
|
253
275
|
* }
|
|
254
276
|
*/
|
|
255
|
-
async
|
|
277
|
+
async send() {
|
|
256
278
|
|
|
257
279
|
if (this.response === null) {
|
|
258
280
|
|
|
259
281
|
// send the call
|
|
260
282
|
try {
|
|
261
283
|
|
|
262
|
-
|
|
284
|
+
DebugAndLog.debug("Sending call", this.request);
|
|
263
285
|
this.response = await this._call();
|
|
264
286
|
|
|
265
287
|
// if it is not JSON we don't convert
|
|
@@ -274,11 +296,11 @@ class Endpoint {
|
|
|
274
296
|
this.response.body = body;
|
|
275
297
|
|
|
276
298
|
} catch (error) {
|
|
277
|
-
|
|
299
|
+
DebugAndLog.debug("This isn't JSON so we'll keep as text and do nothing. This isn't a true error.");
|
|
278
300
|
}
|
|
279
301
|
|
|
280
302
|
} catch (error) {
|
|
281
|
-
|
|
303
|
+
DebugAndLog.error(`Error in call to remote endpoint (${this.request.note}): ${error.message}`, error.stack);
|
|
282
304
|
}
|
|
283
305
|
|
|
284
306
|
}
|
|
@@ -287,14 +309,15 @@ class Endpoint {
|
|
|
287
309
|
}
|
|
288
310
|
|
|
289
311
|
/**
|
|
290
|
-
* Internal method that makes the actual HTTP request using the
|
|
312
|
+
* Internal method that makes the actual HTTP request using the ApiRequest class.
|
|
291
313
|
*
|
|
292
|
-
* This method creates an
|
|
314
|
+
* This method creates an ApiRequest instance with the configured request settings
|
|
293
315
|
* and sends the request. It handles errors by logging them and returning a formatted
|
|
294
|
-
* error response. This method is called internally by the
|
|
316
|
+
* error response. This method is called internally by the send() method.
|
|
295
317
|
*
|
|
296
|
-
* @returns {Promise<{success: boolean, statusCode: number, body: Object|string|null, headers: Object}>} Response object from the
|
|
297
|
-
*
|
|
318
|
+
* @returns {Promise<{success: boolean, statusCode: number, body: Object|string|null, headers: Object}>} Response object from the ApiRequest
|
|
319
|
+
*
|
|
320
|
+
* @private
|
|
298
321
|
*
|
|
299
322
|
* @example
|
|
300
323
|
* // Internal usage (not typically called directly)
|
|
@@ -305,12 +328,12 @@ class Endpoint {
|
|
|
305
328
|
var response = null;
|
|
306
329
|
|
|
307
330
|
try {
|
|
308
|
-
var apiRequest = new
|
|
331
|
+
var apiRequest = new ApiRequest(this.request);
|
|
309
332
|
response = await apiRequest.send();
|
|
310
333
|
|
|
311
334
|
} catch (error) {
|
|
312
|
-
|
|
313
|
-
response =
|
|
335
|
+
DebugAndLog.error(`Error in call (${this.request.note}): ${error.message}`, error.stack);
|
|
336
|
+
response = ApiRequest.responseFormat(false, 500, "Error in call()");
|
|
314
337
|
}
|
|
315
338
|
|
|
316
339
|
return response;
|
|
@@ -319,7 +342,14 @@ class Endpoint {
|
|
|
319
342
|
|
|
320
343
|
};
|
|
321
344
|
|
|
345
|
+
/**
|
|
346
|
+
* @module dao-endpoint
|
|
347
|
+
* @description Provides endpoint request functionality for making HTTP requests
|
|
348
|
+
*/
|
|
322
349
|
module.exports = {
|
|
323
|
-
|
|
324
|
-
|
|
350
|
+
/** @deprecated Use send() instead */
|
|
351
|
+
getDataDirectFromURI,
|
|
352
|
+
/** @deprecated Use send() instead */
|
|
353
|
+
get,
|
|
354
|
+
send
|
|
325
355
|
};
|
|
@@ -17,7 +17,17 @@ const isTrue = (value) => {
|
|
|
17
17
|
*/
|
|
18
18
|
const USE_XRAY = isTrue(process.env?.CacheData_AWSXRayOn) || isTrue(process.env?.CACHE_DATA_AWS_X_RAY_ON);
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* X-Ray object if it is enabled
|
|
22
|
+
* @returns {object|null} The AWSXRay object if enabled and initialized, null otherwise
|
|
23
|
+
*/
|
|
20
24
|
let AWSXRay = null;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Whether or not X-Ray is initialized
|
|
28
|
+
* @private
|
|
29
|
+
* @returns {object|null} The AWSXRay object if enabled and initialized, null otherwise
|
|
30
|
+
*/
|
|
21
31
|
let xrayInitialized = false;
|
|
22
32
|
|
|
23
33
|
const initializeXRay = () => {
|
|
@@ -278,9 +288,25 @@ class AWS {
|
|
|
278
288
|
}
|
|
279
289
|
}
|
|
280
290
|
)();
|
|
281
|
-
|
|
291
|
+
|
|
282
292
|
/**
|
|
283
|
-
*
|
|
293
|
+
* Provides a DynamoDB client and helper functions.
|
|
294
|
+
*
|
|
295
|
+
* @returns {{
|
|
296
|
+
* client: object,
|
|
297
|
+
* put: {(params:Object) => Promise<any>},
|
|
298
|
+
* get: {(params:Object) => Promise<any>},
|
|
299
|
+
* scan: {(params:Object) => Promise<any>},
|
|
300
|
+
* delete: {(params:Object) => Promise<any>},
|
|
301
|
+
* update: {(params:Object) => Promise<any>},
|
|
302
|
+
* sdk: {
|
|
303
|
+
* DynamoDB: object,
|
|
304
|
+
* DynamoDBClient: object,
|
|
305
|
+
* DynamoDBDocumentClient: object,
|
|
306
|
+
* GetCommand: object,
|
|
307
|
+
* PutCommand: object
|
|
308
|
+
* }
|
|
309
|
+
* }}
|
|
284
310
|
*/
|
|
285
311
|
static get dynamo() {
|
|
286
312
|
return {
|
|
@@ -295,7 +321,18 @@ class AWS {
|
|
|
295
321
|
}
|
|
296
322
|
|
|
297
323
|
/**
|
|
298
|
-
*
|
|
324
|
+
* Provides a S3 client and helper functions.
|
|
325
|
+
*
|
|
326
|
+
* @returns {{
|
|
327
|
+
* client: object,
|
|
328
|
+
* put: {(params:Object) => Promise<any>},
|
|
329
|
+
* get: {(params:Object) => Promise<any>},
|
|
330
|
+
* sdk: {
|
|
331
|
+
* S3: object,
|
|
332
|
+
* GetObjectCommand: object,
|
|
333
|
+
* PutObjectCommand: object
|
|
334
|
+
* }
|
|
335
|
+
* }}
|
|
299
336
|
*/
|
|
300
337
|
static get s3() {
|
|
301
338
|
return {
|
|
@@ -307,7 +344,18 @@ class AWS {
|
|
|
307
344
|
}
|
|
308
345
|
|
|
309
346
|
/**
|
|
310
|
-
*
|
|
347
|
+
* Provides a SSM client and helper functions.
|
|
348
|
+
*
|
|
349
|
+
* @returns {{
|
|
350
|
+
* client: object,
|
|
351
|
+
* getByName: {(query:Object) => Promise<any>},
|
|
352
|
+
* getByPath: {(query:Object) => Promise<any>},
|
|
353
|
+
* sdk: {
|
|
354
|
+
* SSMClient: object,
|
|
355
|
+
* GetParametersByPathCommand: object,
|
|
356
|
+
* GetParametersCommand: object
|
|
357
|
+
* }
|
|
358
|
+
* }}
|
|
311
359
|
*/
|
|
312
360
|
static get ssm() {
|
|
313
361
|
return {
|
|
@@ -327,4 +375,9 @@ class AWS {
|
|
|
327
375
|
|
|
328
376
|
};
|
|
329
377
|
|
|
330
|
-
module.exports = {
|
|
378
|
+
module.exports = {
|
|
379
|
+
AWS,
|
|
380
|
+
Aws: AWS, // alias
|
|
381
|
+
AWSXRay,
|
|
382
|
+
AwsXRay: AWSXRay, // alias
|
|
383
|
+
};
|