@cocreate/lazy-loader 1.21.0 → 1.22.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,10 @@
1
+ # [1.22.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.21.0...v1.22.0) (2024-09-21)
2
+
3
+
4
+ ### Features
5
+
6
+ * API initialization accepts an array to handle multi step initialization of third party APIS ([f5b07d0](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/f5b07d07b41652ed84a83ab1acaa97022000ac6f))
7
+
1
8
  # [1.21.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.20.5...v1.21.0) (2024-08-30)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.21.0",
3
+ "version": "1.22.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
@@ -193,22 +193,50 @@ class CoCreateLazyLoader {
193
193
  throw new Error(`Missing ${name} key in organization apis object`);
194
194
 
195
195
  // ToDo: if data.endpoint service not required as endpoint will be used
196
- let instance = require(config.path);
196
+ let instance;
197
197
 
198
- if (config.initialize) {
199
- const initialize = config.initialize.split('.');
200
-
201
- // Traverse the nested structure to reach the correct constructor
202
- for (let i = 0; i < initialize.length; i++) {
203
- if (instance[initialize[i]]) {
204
- instance = instance[initialize[i]];
205
- } else {
206
- throw new Error(`Service path ${config.initialize} is incorrect at ${initialize[i]}`);
207
- }
198
+ // Try using require() first, for CommonJS modules
199
+ try {
200
+ instance = require(config.path); // Attempt to require the module
201
+ } catch (err) {
202
+ if (err.code === 'ERR_REQUIRE_ESM') {
203
+ // If it's an ESM module, fallback to dynamic import()
204
+ instance = await import(config.path);
205
+ } else {
206
+ throw err; // Re-throw other errors
208
207
  }
209
208
  }
210
209
 
211
- instance = new instance(key);
210
+ if (config.initialize) {
211
+ if (Array.isArray(config.initialize)) {
212
+ const initializations = []
213
+ for (let i = 0; i < config.initialize.length; i++) {
214
+ const initialize = config.initialize[i].split('.');
215
+ initializations.push(instance)
216
+ // Traverse the nested structure to reach the correct constructor
217
+ for (let j = 0; j < initialize.length; j++) {
218
+ if (initializations[i][initialize[j]]) {
219
+ initializations[i] = initializations[i][initialize[j]];
220
+ } else {
221
+ throw new Error(`Service path ${config.initialize[i]} is incorrect at ${initialize[j]}`);
222
+ }
223
+ }
224
+ }
225
+ instance = new initializations[1](new initializations[0](key));
226
+ } else {
227
+ const initialize = config.initialize.split('.');
228
+ // Traverse the nested structure to reach the correct constructor
229
+ for (let i = 0; i < initialize.length; i++) {
230
+ if (instance[initialize[i]]) {
231
+ instance = instance[initialize[i]];
232
+ } else {
233
+ throw new Error(`Service path ${config.initialize} is incorrect at ${initialize[i]}`);
234
+ }
235
+ }
236
+ }
237
+ instance = new instance(key);
238
+ } else
239
+ instance = new instance(key);
212
240
 
213
241
  let params = [], mainParam = false
214
242
  for (let i = 0; true; i++) {