feat(modules-sdk): Module as singleton instances (#4065)
This commit is contained in:
1 parent
ed382f2ee5
commit
e73c3e51c9
5 files changed
+77
-14
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/modules-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore: module as singleton instances
|
||||||
@@ -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 {
|
import {
|
||||||
ModuleResolution,
|
|
||||||
MODULE_RESOURCE_TYPE,
|
MODULE_RESOURCE_TYPE,
|
||||||
MODULE_SCOPE,
|
MODULE_SCOPE,
|
||||||
|
ModuleResolution,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { AwilixContainer, ClassOrFunctionReturning, Resolver } from "awilix"
|
|
||||||
import { createMedusaContainer } from "medusa-core-utils"
|
import { createMedusaContainer } from "medusa-core-utils"
|
||||||
import { EOL } from "os"
|
import { EOL } from "os"
|
||||||
import { moduleLoader } from "../module-loader"
|
|
||||||
import { trackInstallation } from "../__mocks__/medusa-telemetry"
|
import { trackInstallation } from "../__mocks__/medusa-telemetry"
|
||||||
|
import { moduleLoader } from "../module-loader"
|
||||||
function asArray(
|
|
||||||
resolvers: (ClassOrFunctionReturning<unknown> | Resolver<unknown>)[]
|
|
||||||
): { resolve: (container: AwilixContainer) => unknown[] } {
|
|
||||||
return {
|
|
||||||
resolve: (container: AwilixContainer): unknown[] =>
|
|
||||||
resolvers.map((resolver) => container.build(resolver)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const logger = {
|
const logger = {
|
||||||
warn: jest.fn(),
|
warn: jest.fn(),
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
ExternalModuleDeclaration,
|
ExternalModuleDeclaration,
|
||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
|
MODULE_SCOPE,
|
||||||
ModuleDefinition,
|
ModuleDefinition,
|
||||||
ModuleResolution,
|
ModuleResolution,
|
||||||
MODULE_SCOPE,
|
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import resolveCwd from "resolve-cwd"
|
import resolveCwd from "resolve-cwd"
|
||||||
import MODULE_DEFINITIONS from "../definitions"
|
import MODULE_DEFINITIONS from "../definitions"
|
||||||
@@ -49,7 +49,7 @@ export const registerMedusaModule = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (moduleDeclaration.scope === MODULE_SCOPE.EXTERNAL) {
|
if (moduleDeclaration.scope === MODULE_SCOPE.EXTERNAL) {
|
||||||
// TODO: getExternalModuleResolution(...)a
|
// TODO: getExternalModuleResolution(...)
|
||||||
throw new Error("External Modules are not supported yet.")
|
throw new Error("External Modules are not supported yet.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const logger: any = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class MedusaModule {
|
export class MedusaModule {
|
||||||
|
private static instances_: Map<string, any> = new Map()
|
||||||
public static async bootstrap(
|
public static async bootstrap(
|
||||||
moduleKey: string,
|
moduleKey: string,
|
||||||
defaultPath: string,
|
defaultPath: string,
|
||||||
@@ -25,6 +26,10 @@ export class MedusaModule {
|
|||||||
): Promise<{
|
): Promise<{
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
}> {
|
}> {
|
||||||
|
if (MedusaModule.instances_.has(moduleKey)) {
|
||||||
|
return MedusaModule.instances_.get(moduleKey)
|
||||||
|
}
|
||||||
|
|
||||||
let modDeclaration = declaration
|
let modDeclaration = declaration
|
||||||
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) {
|
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) {
|
||||||
modDeclaration = {
|
modDeclaration = {
|
||||||
@@ -56,6 +61,8 @@ export class MedusaModule {
|
|||||||
services[keyName] = container.resolve(registrationName)
|
services[keyName] = container.resolve(registrationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MedusaModule.instances_.set(moduleKey, services)
|
||||||
|
|
||||||
return services
|
return services
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user