@cocreate/lazy-loader 1.19.3 → 1.20.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.20.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.19.4...v1.20.0) (2024-05-19)
2
+
3
+
4
+ ### Features
5
+
6
+ * $crud operator handles an array of objects ([1cd7205](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/1cd7205b8a021cc9e98edb74cc3df7575357fd6e))
7
+
8
+ ## [1.19.4](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.19.3...v1.19.4) (2024-05-10)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * removed environment handling variables as it is database specific ([d63921c](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/d63921cb039c284ab452bf47f0802fc56a66eeb3))
14
+
1
15
  ## [1.19.3](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.19.2...v1.19.3) (2024-05-08)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.19.3",
3
+ "version": "1.20.0",
4
4
  "description": "A simple lazy-loader component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "lazy-loader",
package/src/server.js CHANGED
@@ -150,23 +150,49 @@ class CoCreateLazyLoader {
150
150
  }
151
151
  }
152
152
 
153
+ /**
154
+ * TODO: Implement Enhanced API Configuration Handling
155
+ *
156
+ * Description:
157
+ * - Implement functionality to dynamically handle API configurations, supporting both complete and base URL endpoints with automatic method-based path appending.
158
+ * - Enable dynamic generation of query parameters from a designated object (`stripe` in the examples) when `query` is true.
159
+ *
160
+ * Requirements:
161
+ * 1. Dynamic Endpoint Handling:
162
+ * - Check if the endpoint configuration is a complete URL or a base URL.
163
+ * - If the `method` derived path is not already included in the endpoint, append it dynamically.
164
+ * Example:
165
+ * `{ "method": "stripe.accounts.retrieve", "endpoint": "https://api.stripe.com", "query": true, "stripe": { "acct": "acct_123", "name": "John Doe" } }`
166
+ * `{ "method": "stripe.accounts.retrieve", "endpoint": "https://api.stripe.com/accounts/retrieve", "query": true, "stripe": { "acct": "acct_123", "name": "John Doe" } }`
167
+ * - Develop logic to parse the `method` and check against the endpoint. If necessary, append the appropriate API method segment.
168
+ *
169
+ * 2. Query Parameter Handling:
170
+ * - Dynamically construct and append query parameters from the `stripe` object if `query` is true. Ensure proper URL-encoding of keys and values.
171
+ *
172
+ * 3. Security:
173
+ * - Use the `method` for permission checks, ensuring that each API request complies with security protocols.
174
+ *
175
+ * 4. Testing:
176
+ * - Test both scenarios where the endpoint may or may not include the method path to ensure the dynamic construction works correctly.
177
+ * - Ensure that all query parameters are correctly formatted and appended.
178
+ *
179
+ * Notes:
180
+ * - Consider utility functions for parsing and modifying URLs, as well as for encoding parameters.
181
+ * - Maintain clear and detailed documentation for each part of the implementation to assist future development and troubleshooting.
182
+ */
183
+
153
184
  async api(config, data) {
154
185
  try {
155
186
  const methodPath = data.method.split('.')
156
187
  const name = methodPath.shift()
157
188
 
158
189
  const apis = await this.getApiKey(data, name)
159
- let environment = 'production';
160
-
161
- if (data.environment)
162
- environment = data.environment
163
- else if (data.host.startsWith('dev.') || data.host.startsWith('test.'))
164
- environment = 'test'
165
190
 
166
- const key = apis[environment].key;
191
+ const key = apis.key;
167
192
  if (!key)
168
193
  throw new Error(`Missing ${name} key in organization apis object`);
169
194
 
195
+ // ToDo: if data.endpoint service not required as endpoint will be used
170
196
  const service = require(config.path);
171
197
  let instance
172
198
  if (config.initialize)
@@ -205,31 +231,25 @@ class CoCreateLazyLoader {
205
231
  async webhooks(config, data, name) {
206
232
  try {
207
233
  const apis = await this.getApiKey(data, name)
208
- let environment = 'production';
209
-
210
- if (data.environment)
211
- environment = data.environment
212
- else if (data.host.startsWith('dev.') || data.host.startsWith('test.'))
213
- environment = 'test'
214
234
 
215
- const key = apis[environment].key;
235
+ const key = apis.key;
216
236
  if (!key)
217
237
  throw new Error(`Missing ${name} key in organization apis object`);
218
238
 
219
239
  let webhookName = data.req.url.split('/');
220
240
  webhookName = webhookName[webhookName.length - 1]
221
241
 
222
- const webhook = apis[environment].webhooks[webhookName];
242
+ const webhook = apis.webhooks[webhookName];
223
243
  if (!webhook)
224
244
  throw new Error(`Webhook ${name} ${webhookName} is not defined`);
225
245
 
226
246
  // eventDataKey is used to access the event data
227
- let eventDataKey = webhook.eventDataKey || apis[environment].eventDataKey
247
+ let eventDataKey = webhook.eventDataKey || apis.eventDataKey
228
248
  if (!eventDataKey)
229
249
  throw new Error(`Webhook ${name} eventKey is not defined`);
230
250
 
231
251
  // eventNameKey is used to access the event the event name
232
- let eventNameKey = webhook.eventNameKey || apis[environment].eventNameKey
252
+ let eventNameKey = webhook.eventNameKey || apis.eventNameKey
233
253
  if (!eventNameKey)
234
254
  throw new Error(`Webhook ${name} eventNameKey is not defined`);
235
255
 
@@ -254,15 +274,15 @@ class CoCreateLazyLoader {
254
274
 
255
275
  if (webhook.authenticate && webhook.authenticate.method) {
256
276
  method = webhook.authenticate.method
257
- } else if (apis[environment].authenticate && apis[environment].authenticate.method) {
258
- method = apis[environment].authenticate.method
277
+ } else if (apis.authenticate && apis.authenticate.method) {
278
+ method = apis.authenticate.method
259
279
  } else
260
280
  throw new Error(`Webhook ${name} authenticate method is not defined`);
261
281
 
262
282
  if (webhook.authenticate && webhook.authenticate.parameters) {
263
283
  parameters = webhook.authenticate.parameters
264
- } else if (apis[environment].authenticate && apis[environment].authenticate.parameters) {
265
- parameters = apis[environment].authenticate.parameters
284
+ } else if (apis.authenticate && apis.authenticate.parameters) {
285
+ parameters = apis.authenticate.parameters
266
286
  } else
267
287
  throw new Error(`Webhook ${name} authenticate parameters is not defined`);
268
288
 
@@ -349,11 +369,25 @@ class CoCreateLazyLoader {
349
369
  } else if (operator.startsWith('$rawBody')) {
350
370
  return getValueFromObject(data, operator.substring(1))
351
371
  } else if (operator.startsWith('$crud')) {
352
- context = await this.processOperators(data, event, context);
353
- result = await this.crud.send(context)
354
- if (operator.startsWith('$crud.'))
355
- result = getValueFromObject(operator, operator.substring(6))
356
- return await this.processOperators(data, event, result);
372
+ let results = context
373
+ let isObject = false
374
+ if (!Array.isArray(results)) {
375
+ isObject = true
376
+ results = [results]
377
+ }
378
+
379
+ for (let i = 0; i < results.length; i++) {
380
+ results[i] = await this.processOperators(data, event, results[i]);
381
+ results[i] = await this.crud.send(results[i])
382
+ if (operator.startsWith('$crud.'))
383
+ results[i] = getValueFromObject(operator, operator.substring(6))
384
+ results[i] = await this.processOperators(data, event, results[i])
385
+ }
386
+
387
+ if (isObject)
388
+ results = results[0]
389
+
390
+ return results;
357
391
  } else if (operator.startsWith('$socket')) {
358
392
  context = await this.processOperators(data, event, context);
359
393
  result = await this.socket.send(context)