@63klabs/cache-data 1.3.9 → 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.
@@ -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 APIRequest class.
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 exmaple, get(connection, data)
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 get = async (connection, data = null) => {
25
- * return (new Endpoint(connection).get());
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
- "use strict";
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 GET request to a remote endpoint with the specified connection configuration.
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.get(
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.get(
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.get({
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.get({
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 get = async (connection, query = {}) => {
111
+ const send = async (connection, query = {}) => {
114
112
  if (query === null) { query = {} };
115
- return (new Endpoint(connection, query).get());
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.get()`. However, it can be extended to add custom pre/post-processing logic.
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.get();
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 get() {
152
+ * async send() {
138
153
  * // Custom pre-processing
139
- * const response = await super.get();
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 APIRequest
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 get() function
260
+ * // Basic usage through the send() function
239
261
  * const endpoint = new Endpoint({ host: "api.example.com", path: "/data" });
240
- * const response = await endpoint.get();
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.get();
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 get() {
277
+ async send() {
256
278
 
257
279
  if (this.response === null) {
258
280
 
259
281
  // send the call
260
282
  try {
261
283
 
262
- tools.DebugAndLog.debug("Sending call", this.request);
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
- tools.DebugAndLog.debug("This isn't JSON so we'll keep as text and do nothing. This isn't a true error.");
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
- tools.DebugAndLog.error(`Error in call to remote endpoint (${this.request.note}): ${error.message}`, error.stack);
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 APIRequest class.
312
+ * Internal method that makes the actual HTTP request using the ApiRequest class.
291
313
  *
292
- * This method creates an APIRequest instance with the configured request settings
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 get() method.
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 APIRequest
297
- * @throws {Error} Throws an error if the APIRequest instantiation or send operation fails
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 tools.APIRequest(this.request);
331
+ var apiRequest = new ApiRequest(this.request);
309
332
  response = await apiRequest.send();
310
333
 
311
334
  } catch (error) {
312
- tools.DebugAndLog.error(`Error in call (${this.request.note}): ${error.message}`, error.stack);
313
- response = tools.APIRequest.responseFormat(false, 500, "Error in call()");
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
- getDataDirectFromURI: get, // deprecated alias
324
- get
350
+ /** @deprecated Use send() instead */
351
+ getDataDirectFromURI,
352
+ /** @deprecated Use send() instead */
353
+ get,
354
+ send
325
355
  };