@cocreate/lazy-loader 1.20.5 → 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,17 @@
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
+
8
+ # [1.21.0](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.20.5...v1.21.0) (2024-08-30)
9
+
10
+
11
+ ### Features
12
+
13
+ * handle nested initailization function ([c46b449](https://github.com/CoCreate-app/CoCreate-lazy-loader/commit/c46b4497936e61d4c932de664be70bbbf6e72f05))
14
+
1
15
  ## [1.20.5](https://github.com/CoCreate-app/CoCreate-lazy-loader/compare/v1.20.4...v1.20.5) (2024-08-24)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/lazy-loader",
3
- "version": "1.20.5",
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,12 +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
- const service = require(config.path);
197
- let instance
198
- if (config.initialize)
199
- instance = new service[config.initialize](key);
200
- else
201
- instance = new service(key);
196
+ let instance;
197
+
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
207
+ }
208
+ }
209
+
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);
202
240
 
203
241
  let params = [], mainParam = false
204
242
  for (let i = 0; true; i++) {