Made ModuleLoader interface

And added 2 default implementations:

 - `NODE_MODULE_LOADER` - uses `import` under the hood
  - `DYNAMIC_MODULE_LOADER` - will be able to load modules needed in other parts of this project. Currently it's just a stub
This commit is contained in:
Kir_Antipov 2023-01-14 13:38:15 +00:00
parent fd2975b30b
commit 491aa11ac5
3 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,3 @@
export const ACTION_MODULE_LOADER = (path: string): Promise<Record<string, unknown>> => {
return Promise.resolve(undefined);
};

View file

@ -0,0 +1,25 @@
import { ACTION_MODULE_LOADER } from "./module-loader.g";
/**
* Represents a function that loads a module by its name.
*/
export interface ModuleLoader {
/**
* Loads a module by its name.
*
* @param name - The name of the module to load.
*
* @returns A promise that resolves with the loaded module.
*/
(name: string): Promise<unknown>;
}
/**
* A module loader implementation that loads modules using Node.js dynamic `import` syntax.
*/
export const NODE_MODULE_LOADER: ModuleLoader = new Function("x", "return import(x).catch(() => undefined)") as (name: string) => Promise<unknown>;
/**
* Represents a dynamic module loader that is capable of loading modules by their source path (e.g., `"utils/string-utils"`).
*/
export const DYNAMIC_MODULE_LOADER: ModuleLoader = ACTION_MODULE_LOADER;

View file

@ -0,0 +1,17 @@
import { NODE_MODULE_LOADER, DYNAMIC_MODULE_LOADER } from "@/utils/reflection/module-loader";
describe("NODE_MODULE_LOADER", () => {
test("returns undefined if a module cannot be loaded", async () => {
const loadedModule = await NODE_MODULE_LOADER("#");
expect(loadedModule).toBeUndefined();
});
});
describe("DYNAMIC_MODULE_LOADER", () => {
test("returns undefined if a module cannot be loaded", async () => {
const loadedModule = await DYNAMIC_MODULE_LOADER("#");
expect(loadedModule).toBeUndefined();
});
});