feat(modules-sdk): Module as singleton instances (#4065)
This commit is contained in:
committed by
GitHub
parent
ed382f2ee5
commit
e73c3e51c9
61
packages/modules-sdk/src/loaders/__tests__/medusa-module.ts
Normal file
61
packages/modules-sdk/src/loaders/__tests__/medusa-module.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
InternalModuleDeclaration,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "@medusajs/types"
|
||||
import { MedusaModule } from "../../medusa-module"
|
||||
|
||||
const mockRegisterMedusaModule = jest
|
||||
.fn()
|
||||
.mockImplementation(() => Promise.resolve([]))
|
||||
const mockModuleLoader = jest.fn().mockImplementation(() => Promise.resolve({}))
|
||||
|
||||
jest.mock("./../../loaders", () => ({
|
||||
registerMedusaModule: jest
|
||||
.fn()
|
||||
.mockImplementation((...args) => mockRegisterMedusaModule()),
|
||||
moduleLoader: jest.fn().mockImplementation((...args) => mockModuleLoader()),
|
||||
}))
|
||||
|
||||
describe("Medusa Module", () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules()
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("MedusaModule bootstrap - Singleton instances", async () => {
|
||||
await MedusaModule.bootstrap(
|
||||
"moduleKey",
|
||||
"@path",
|
||||
{
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
{}
|
||||
)
|
||||
|
||||
expect(mockRegisterMedusaModule).toBeCalledTimes(1)
|
||||
expect(mockModuleLoader).toBeCalledTimes(1)
|
||||
|
||||
await MedusaModule.bootstrap(
|
||||
"moduleKey",
|
||||
"@path",
|
||||
{
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
{}
|
||||
)
|
||||
|
||||
expect(mockRegisterMedusaModule).toBeCalledTimes(1)
|
||||
expect(mockModuleLoader).toBeCalledTimes(1)
|
||||
})
|
||||
})
|
||||
@@ -1,22 +1,12 @@
|
||||
import {
|
||||
ModuleResolution,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
ModuleResolution,
|
||||
} from "@medusajs/types"
|
||||
import { AwilixContainer, ClassOrFunctionReturning, Resolver } from "awilix"
|
||||
import { createMedusaContainer } from "medusa-core-utils"
|
||||
import { EOL } from "os"
|
||||
import { moduleLoader } from "../module-loader"
|
||||
import { trackInstallation } from "../__mocks__/medusa-telemetry"
|
||||
|
||||
function asArray(
|
||||
resolvers: (ClassOrFunctionReturning<unknown> | Resolver<unknown>)[]
|
||||
): { resolve: (container: AwilixContainer) => unknown[] } {
|
||||
return {
|
||||
resolve: (container: AwilixContainer): unknown[] =>
|
||||
resolvers.map((resolver) => container.build(resolver)),
|
||||
}
|
||||
}
|
||||
import { moduleLoader } from "../module-loader"
|
||||
|
||||
const logger = {
|
||||
warn: jest.fn(),
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MODULE_SCOPE,
|
||||
ModuleDefinition,
|
||||
ModuleResolution,
|
||||
MODULE_SCOPE,
|
||||
} from "@medusajs/types"
|
||||
import resolveCwd from "resolve-cwd"
|
||||
import MODULE_DEFINITIONS from "../definitions"
|
||||
@@ -49,7 +49,7 @@ export const registerMedusaModule = (
|
||||
}
|
||||
|
||||
if (moduleDeclaration.scope === MODULE_SCOPE.EXTERNAL) {
|
||||
// TODO: getExternalModuleResolution(...)a
|
||||
// TODO: getExternalModuleResolution(...)
|
||||
throw new Error("External Modules are not supported yet.")
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ const logger: any = {
|
||||
}
|
||||
|
||||
export class MedusaModule {
|
||||
private static instances_: Map<string, any> = new Map()
|
||||
public static async bootstrap(
|
||||
moduleKey: string,
|
||||
defaultPath: string,
|
||||
@@ -25,6 +26,10 @@ export class MedusaModule {
|
||||
): Promise<{
|
||||
[key: string]: any
|
||||
}> {
|
||||
if (MedusaModule.instances_.has(moduleKey)) {
|
||||
return MedusaModule.instances_.get(moduleKey)
|
||||
}
|
||||
|
||||
let modDeclaration = declaration
|
||||
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) {
|
||||
modDeclaration = {
|
||||
@@ -56,6 +61,8 @@ export class MedusaModule {
|
||||
services[keyName] = container.resolve(registrationName)
|
||||
}
|
||||
|
||||
MedusaModule.instances_.set(moduleKey, services)
|
||||
|
||||
return services
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user