@twin.org/modules 0.0.2-next.5 → 0.0.2-next.6

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.
@@ -14,6 +14,13 @@ class ModuleHelper {
14
14
  * Runtime name for the class.
15
15
  */
16
16
  static CLASS_NAME = "ModuleHelper";
17
+ /**
18
+ * Override the import function for modules.
19
+ * @param overrideImport The override import function.
20
+ */
21
+ static overrideImport(overrideImport) {
22
+ core.SharedStore.set("overrideImport", overrideImport);
23
+ }
17
24
  /**
18
25
  * Get the module entry.
19
26
  * @param module The module.
@@ -24,7 +31,16 @@ class ModuleHelper {
24
31
  static async getModuleEntry(module, entry) {
25
32
  let moduleInstance;
26
33
  try {
27
- moduleInstance = await import(module);
34
+ let useDefault = true;
35
+ const overrideImport = core.SharedStore.get("overrideImport");
36
+ if (!core.Is.empty(overrideImport)) {
37
+ const overrideResult = await overrideImport(module);
38
+ moduleInstance = overrideResult.module;
39
+ useDefault = overrideResult.useDefault;
40
+ }
41
+ if (useDefault) {
42
+ moduleInstance = await import(module);
43
+ }
28
44
  }
29
45
  catch (err) {
30
46
  throw new core.GeneralError(ModuleHelper.CLASS_NAME, "moduleNotFound", {
@@ -32,7 +48,7 @@ class ModuleHelper {
32
48
  entry
33
49
  }, core.BaseError.fromError(err));
34
50
  }
35
- const moduleEntry = moduleInstance[entry];
51
+ const moduleEntry = moduleInstance?.[entry];
36
52
  if (core.Is.empty(moduleEntry)) {
37
53
  throw new core.GeneralError(ModuleHelper.CLASS_NAME, "entryNotFound", {
38
54
  module,
@@ -159,6 +175,22 @@ class ModuleHelper {
159
175
  });
160
176
  });
161
177
  }
178
+ /**
179
+ * Check if a module is a local module.
180
+ * @param name The name of the module.
181
+ * @returns True if the module is local, false otherwise.
182
+ */
183
+ static isLocalModule(name) {
184
+ return name.startsWith(".") || name.startsWith("/");
185
+ }
186
+ /**
187
+ * Check if a module is a relative module.
188
+ * @param name The name of the module.
189
+ * @returns True if the module is relative, false otherwise.
190
+ */
191
+ static isRelativeModule(name) {
192
+ return name.startsWith(".");
193
+ }
162
194
  }
163
195
 
164
196
  exports.ModuleHelper = ModuleHelper;
@@ -1,5 +1,5 @@
1
1
  import { Worker } from 'node:worker_threads';
2
- import { GeneralError, BaseError, Is } from '@twin.org/core';
2
+ import { SharedStore, Is, GeneralError, BaseError } from '@twin.org/core';
3
3
 
4
4
  // Copyright 2024 IOTA Stiftung.
5
5
  // SPDX-License-Identifier: Apache-2.0.
@@ -12,6 +12,13 @@ class ModuleHelper {
12
12
  * Runtime name for the class.
13
13
  */
14
14
  static CLASS_NAME = "ModuleHelper";
15
+ /**
16
+ * Override the import function for modules.
17
+ * @param overrideImport The override import function.
18
+ */
19
+ static overrideImport(overrideImport) {
20
+ SharedStore.set("overrideImport", overrideImport);
21
+ }
15
22
  /**
16
23
  * Get the module entry.
17
24
  * @param module The module.
@@ -22,7 +29,16 @@ class ModuleHelper {
22
29
  static async getModuleEntry(module, entry) {
23
30
  let moduleInstance;
24
31
  try {
25
- moduleInstance = await import(module);
32
+ let useDefault = true;
33
+ const overrideImport = SharedStore.get("overrideImport");
34
+ if (!Is.empty(overrideImport)) {
35
+ const overrideResult = await overrideImport(module);
36
+ moduleInstance = overrideResult.module;
37
+ useDefault = overrideResult.useDefault;
38
+ }
39
+ if (useDefault) {
40
+ moduleInstance = await import(module);
41
+ }
26
42
  }
27
43
  catch (err) {
28
44
  throw new GeneralError(ModuleHelper.CLASS_NAME, "moduleNotFound", {
@@ -30,7 +46,7 @@ class ModuleHelper {
30
46
  entry
31
47
  }, BaseError.fromError(err));
32
48
  }
33
- const moduleEntry = moduleInstance[entry];
49
+ const moduleEntry = moduleInstance?.[entry];
34
50
  if (Is.empty(moduleEntry)) {
35
51
  throw new GeneralError(ModuleHelper.CLASS_NAME, "entryNotFound", {
36
52
  module,
@@ -157,6 +173,22 @@ class ModuleHelper {
157
173
  });
158
174
  });
159
175
  }
176
+ /**
177
+ * Check if a module is a local module.
178
+ * @param name The name of the module.
179
+ * @returns True if the module is local, false otherwise.
180
+ */
181
+ static isLocalModule(name) {
182
+ return name.startsWith(".") || name.startsWith("/");
183
+ }
184
+ /**
185
+ * Check if a module is a relative module.
186
+ * @param name The name of the module.
187
+ * @returns True if the module is relative, false otherwise.
188
+ */
189
+ static isRelativeModule(name) {
190
+ return name.startsWith(".");
191
+ }
160
192
  }
161
193
 
162
194
  export { ModuleHelper };
@@ -6,6 +6,14 @@ export declare class ModuleHelper {
6
6
  * Runtime name for the class.
7
7
  */
8
8
  static readonly CLASS_NAME: string;
9
+ /**
10
+ * Override the import function for modules.
11
+ * @param overrideImport The override import function.
12
+ */
13
+ static overrideImport(overrideImport: (moduleName: string) => Promise<{
14
+ module?: unknown;
15
+ useDefault: boolean;
16
+ }>): void;
9
17
  /**
10
18
  * Get the module entry.
11
19
  * @param module The module.
@@ -40,4 +48,16 @@ export declare class ModuleHelper {
40
48
  * @throws GeneralError if executing the module entry failed.
41
49
  */
42
50
  static execModuleMethodThread<T>(module: string, method: string, args?: unknown[]): Promise<T>;
51
+ /**
52
+ * Check if a module is a local module.
53
+ * @param name The name of the module.
54
+ * @returns True if the module is local, false otherwise.
55
+ */
56
+ static isLocalModule(name: string): boolean;
57
+ /**
58
+ * Check if a module is a relative module.
59
+ * @param name The name of the module.
60
+ * @returns True if the module is relative, false otherwise.
61
+ */
62
+ static isRelativeModule(name: string): boolean;
43
63
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @twin.org/modules - Changelog
2
2
 
3
+ ## [0.0.2-next.6](https://github.com/twinfoundation/framework/compare/modules-v0.0.2-next.5...modules-v0.0.2-next.6) (2025-08-27)
4
+
5
+
6
+ ### Features
7
+
8
+ * provide module helper override ([e998a64](https://github.com/twinfoundation/framework/commit/e998a64842cfd18693a14444be33b084fef2bb90))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/core bumped from 0.0.2-next.5 to 0.0.2-next.6
16
+ * @twin.org/nameof bumped from 0.0.2-next.5 to 0.0.2-next.6
17
+ * devDependencies
18
+ * @twin.org/nameof-transformer bumped from 0.0.2-next.5 to 0.0.2-next.6
19
+ * @twin.org/nameof-vitest-plugin bumped from 0.0.2-next.5 to 0.0.2-next.6
20
+
3
21
  ## [0.0.2-next.5](https://github.com/twinfoundation/framework/compare/modules-v0.0.2-next.4...modules-v0.0.2-next.5) (2025-08-19)
4
22
 
5
23
 
@@ -22,6 +22,26 @@ Runtime name for the class.
22
22
 
23
23
  ## Methods
24
24
 
25
+ ### overrideImport()
26
+
27
+ > `static` **overrideImport**(`overrideImport`): `void`
28
+
29
+ Override the import function for modules.
30
+
31
+ #### Parameters
32
+
33
+ ##### overrideImport
34
+
35
+ (`moduleName`) => `Promise`\<\{ `module?`: `unknown`; `useDefault`: `boolean`; \}\>
36
+
37
+ The override import function.
38
+
39
+ #### Returns
40
+
41
+ `void`
42
+
43
+ ***
44
+
25
45
  ### getModuleEntry()
26
46
 
27
47
  > `static` **getModuleEntry**\<`T`\>(`module`, `entry`): `Promise`\<`T`\>
@@ -183,3 +203,47 @@ The result of the method execution.
183
203
  #### Throws
184
204
 
185
205
  GeneralError if executing the module entry failed.
206
+
207
+ ***
208
+
209
+ ### isLocalModule()
210
+
211
+ > `static` **isLocalModule**(`name`): `boolean`
212
+
213
+ Check if a module is a local module.
214
+
215
+ #### Parameters
216
+
217
+ ##### name
218
+
219
+ `string`
220
+
221
+ The name of the module.
222
+
223
+ #### Returns
224
+
225
+ `boolean`
226
+
227
+ True if the module is local, false otherwise.
228
+
229
+ ***
230
+
231
+ ### isRelativeModule()
232
+
233
+ > `static` **isRelativeModule**(`name`): `boolean`
234
+
235
+ Check if a module is a relative module.
236
+
237
+ #### Parameters
238
+
239
+ ##### name
240
+
241
+ `string`
242
+
243
+ The name of the module.
244
+
245
+ #### Returns
246
+
247
+ `boolean`
248
+
249
+ True if the module is relative, false otherwise.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/modules",
3
- "version": "0.0.2-next.5",
3
+ "version": "0.0.2-next.6",
4
4
  "description": "Helper classes for loading and executing from modules",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,8 +14,8 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "0.0.2-next.5",
18
- "@twin.org/nameof": "0.0.2-next.5"
17
+ "@twin.org/core": "0.0.2-next.6",
18
+ "@twin.org/nameof": "0.0.2-next.6"
19
19
  },
20
20
  "main": "./dist/cjs/index.cjs",
21
21
  "module": "./dist/esm/index.mjs",