fix(medusa, modules-sdk, modules): Module loading missing dependencies + remote query reference issue (#5468)
This commit is contained in:
committed by
GitHub
parent
725ba9bb9b
commit
a45da9215d
15
.changeset/good-mails-hunt.md
Normal file
15
.changeset/good-mails-hunt.md
Normal file
@@ -0,0 +1,15 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
"@medusajs/cache-inmemory": patch
|
||||
"@medusajs/cache-redis": patch
|
||||
"@medusajs/event-bus-local": patch
|
||||
"@medusajs/event-bus-redis": patch
|
||||
"@medusajs/inventory": patch
|
||||
"@medusajs/link-modules": patch
|
||||
"@medusajs/modules-sdk": patch
|
||||
"@medusajs/pricing": patch
|
||||
"@medusajs/product": patch
|
||||
"@medusajs/stock-location": patch
|
||||
---
|
||||
|
||||
fix(medusa, modules-sdk, modules): Module loading was missing the expected dependencies and remote query reference fix
|
||||
@@ -11,12 +11,13 @@ export const initialize = async (
|
||||
options?: InMemoryCacheModuleOptions | ExternalModuleDeclaration
|
||||
): Promise<ICacheService> => {
|
||||
const serviceKey = Modules.CACHE
|
||||
const loaded = await MedusaModule.bootstrap<ICacheService>(
|
||||
serviceKey,
|
||||
"@medusajs/cache-inmemory",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
undefined
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<ICacheService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: "@medusajs/cache-inmemory",
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
@@ -11,12 +11,13 @@ export const initialize = async (
|
||||
options?: RedisCacheModuleOptions | ExternalModuleDeclaration
|
||||
): Promise<ICacheService> => {
|
||||
const serviceKey = Modules.CACHE
|
||||
const loaded = await MedusaModule.bootstrap<ICacheService>(
|
||||
serviceKey,
|
||||
"@medusajs/cache-redis",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
undefined
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<ICacheService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: "@medusajs/cache-redis",
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ import { IEventBusService } from "@medusajs/types"
|
||||
|
||||
export const initialize = async (): Promise<IEventBusService> => {
|
||||
const serviceKey = Modules.EVENT_BUS
|
||||
const loaded = await MedusaModule.bootstrap<IEventBusService>(
|
||||
serviceKey,
|
||||
"@medusajs/event-bus-local"
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<IEventBusService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: "@medusajs/event-bus-local",
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
@@ -11,12 +11,13 @@ export const initialize = async (
|
||||
options?: EventBusRedisModuleOptions | ExternalModuleDeclaration
|
||||
): Promise<IEventBusService> => {
|
||||
const serviceKey = Modules.EVENT_BUS
|
||||
const loaded = await MedusaModule.bootstrap<IEventBusService>(
|
||||
serviceKey,
|
||||
"@medusajs/event-bus-redis",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
undefined
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<IEventBusService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: "@medusajs/event-bus-redis",
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
MODULE_PACKAGE_NAMES,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IEventBusService, IInventoryService } from "@medusajs/types"
|
||||
@@ -15,13 +16,15 @@ export const initialize = async (
|
||||
}
|
||||
): Promise<IInventoryService> => {
|
||||
const serviceKey = Modules.INVENTORY
|
||||
const loaded = await MedusaModule.bootstrap<IInventoryService>(
|
||||
serviceKey,
|
||||
"@medusajs/inventory",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
moduleDefinition,
|
||||
injectedDependencies
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<IInventoryService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: MODULE_PACKAGE_NAMES[Modules.INVENTORY],
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
injectedDependencies,
|
||||
moduleExports: moduleDefinition,
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
@@ -120,12 +120,12 @@ export const initialize = async (
|
||||
},
|
||||
}
|
||||
|
||||
const loaded = await MedusaModule.bootstrapLink(
|
||||
linkModuleDefinition,
|
||||
options as InternalModuleDeclaration,
|
||||
moduleDefinition,
|
||||
injectedDependencies
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrapLink({
|
||||
definition: linkModuleDefinition,
|
||||
declaration: options as InternalModuleDeclaration,
|
||||
moduleExports: moduleDefinition,
|
||||
injectedDependencies,
|
||||
})
|
||||
|
||||
allLinks[serviceKey as string] = Object.values(loaded)[0]
|
||||
}
|
||||
|
||||
@@ -234,6 +234,7 @@ export default async ({
|
||||
modulesConfig,
|
||||
servicesConfig: joinerConfig,
|
||||
remoteFetchData: remoteQueryFetchData(container),
|
||||
sharedContainer: container,
|
||||
injectedDependencies: {
|
||||
[ContainerRegistrationKeys.PG_CONNECTION]: container.resolve(
|
||||
ContainerRegistrationKeys.PG_CONNECTION
|
||||
|
||||
@@ -46,35 +46,47 @@ describe("Medusa Modules", () => {
|
||||
})
|
||||
|
||||
it("should create singleton instances", async () => {
|
||||
await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
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)
|
||||
await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
expect(mockRegisterMedusaModule).toBeCalledTimes(2)
|
||||
expect(mockModuleLoader).toBeCalledTimes(2)
|
||||
@@ -85,14 +97,18 @@ describe("Medusa Modules", () => {
|
||||
|
||||
for (let i = 5; i--; ) {
|
||||
load.push(
|
||||
MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
@@ -104,81 +120,109 @@ describe("Medusa Modules", () => {
|
||||
})
|
||||
|
||||
it("getModuleInstance should return the first instance of the module if there is none flagged as 'main'", async () => {
|
||||
const moduleA = await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleA = await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
const moduleB = await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleB = await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
expect(MedusaModule.getModuleInstance("moduleKey")).toEqual(moduleA)
|
||||
})
|
||||
|
||||
it("should return the module flagged as 'main' when multiple instances are available", async () => {
|
||||
const moduleA = await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleA = await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
const moduleB = await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleB = await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
expect(MedusaModule.getModuleInstance("moduleKey")).toEqual(moduleB)
|
||||
})
|
||||
|
||||
it("should retrieve the module by their given alias", async () => {
|
||||
const moduleA = await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "mod_A",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleA = await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "mod_A",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
const moduleB = await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
alias: "mod_B",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleB = await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
alias: "mod_B",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
const moduleC = await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "mod_C",
|
||||
options: {
|
||||
moduleC: true,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleC = await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "mod_C",
|
||||
options: {
|
||||
moduleC: true,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
// main
|
||||
expect(MedusaModule.getModuleInstance("moduleKey")).toEqual(moduleB)
|
||||
@@ -195,37 +239,49 @@ describe("Medusa Modules", () => {
|
||||
})
|
||||
|
||||
it("should prevent two main modules being set as 'main'", async () => {
|
||||
await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "mod_A",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "mod_A",
|
||||
options: {
|
||||
abc: 123,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
alias: "mod_B",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
alias: "mod_B",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
const moduleC = MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
alias: "mod_C",
|
||||
options: {
|
||||
moduleC: true,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleC = MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
main: true,
|
||||
alias: "mod_C",
|
||||
options: {
|
||||
moduleC: true,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
expect(moduleC).rejects.toThrow(
|
||||
"Module moduleKey already have a 'main' registered."
|
||||
@@ -233,25 +289,33 @@ describe("Medusa Modules", () => {
|
||||
})
|
||||
|
||||
it("should prevent the same alias be used for different instances of the same module", async () => {
|
||||
await MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "module_alias",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
await MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "module_alias",
|
||||
options: {
|
||||
different_options: "abc",
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
const moduleC = MedusaModule.bootstrap("moduleKey", "@path", {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "module_alias",
|
||||
options: {
|
||||
moduleC: true,
|
||||
},
|
||||
} as InternalModuleDeclaration)
|
||||
const moduleC = MedusaModule.bootstrap({
|
||||
moduleKey: "moduleKey",
|
||||
defaultPath: "@path",
|
||||
declaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
resolve: "@path",
|
||||
alias: "module_alias",
|
||||
options: {
|
||||
moduleC: true,
|
||||
},
|
||||
} as InternalModuleDeclaration,
|
||||
})
|
||||
|
||||
expect(moduleC).rejects.toThrow(
|
||||
"Module moduleKey already registed as 'module_alias'. Please choose a different alias."
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
InternalModuleDeclaration,
|
||||
LoadedModule,
|
||||
LoaderOptions,
|
||||
MedusaContainer,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
ModuleDefinition,
|
||||
@@ -15,14 +16,20 @@ import {
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
ModulesSdkUtils,
|
||||
createMedusaContainer,
|
||||
isObject,
|
||||
ModulesSdkUtils,
|
||||
} from "@medusajs/utils"
|
||||
import { MODULE_PACKAGE_NAMES, Modules } from "./definitions"
|
||||
import {
|
||||
MODULE_PACKAGE_NAMES,
|
||||
ModuleRegistrationName,
|
||||
Modules,
|
||||
} from "./definitions"
|
||||
import { MedusaModule } from "./medusa-module"
|
||||
import { RemoteLink } from "./remote-link"
|
||||
import { RemoteQuery } from "./remote-query"
|
||||
import { cleanGraphQLSchema } from "./utils"
|
||||
import { asValue } from "awilix"
|
||||
|
||||
const LinkModulePackage = "@medusajs/link-modules"
|
||||
|
||||
@@ -57,7 +64,7 @@ export type SharedResources = {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadModules(modulesConfig, injectedDependencies) {
|
||||
async function loadModules(modulesConfig, sharedContainer) {
|
||||
const allModules = {}
|
||||
|
||||
await Promise.all(
|
||||
@@ -85,14 +92,18 @@ async function loadModules(modulesConfig, injectedDependencies) {
|
||||
declaration.resources = MODULE_RESOURCE_TYPE.SHARED
|
||||
}
|
||||
|
||||
const loaded = (await MedusaModule.bootstrap(
|
||||
moduleName,
|
||||
path,
|
||||
const loaded = (await MedusaModule.bootstrap({
|
||||
moduleKey: moduleName,
|
||||
defaultPath: path,
|
||||
declaration,
|
||||
undefined,
|
||||
injectedDependencies,
|
||||
definition
|
||||
)) as LoadedModule
|
||||
sharedContainer,
|
||||
moduleDefinition: definition,
|
||||
})) as LoadedModule
|
||||
|
||||
const service = loaded[moduleName]
|
||||
sharedContainer.register({
|
||||
[service.__definition.registrationName]: asValue(service),
|
||||
})
|
||||
|
||||
if (allModules[moduleName] && !Array.isArray(allModules[moduleName])) {
|
||||
allModules[moduleName] = []
|
||||
@@ -153,6 +164,7 @@ function registerCustomJoinerConfigs(servicesConfig: ModuleJoinerConfig[]) {
|
||||
|
||||
export async function MedusaApp(
|
||||
{
|
||||
sharedContainer,
|
||||
sharedResourcesConfig,
|
||||
servicesConfig,
|
||||
modulesConfigPath,
|
||||
@@ -162,6 +174,7 @@ export async function MedusaApp(
|
||||
remoteFetchData,
|
||||
injectedDependencies,
|
||||
}: {
|
||||
sharedContainer?: MedusaContainer
|
||||
sharedResourcesConfig?: SharedResources
|
||||
loadedModules?: LoadedModule[]
|
||||
servicesConfig?: ModuleJoinerConfig[]
|
||||
@@ -185,6 +198,8 @@ export async function MedusaApp(
|
||||
notFound?: Record<string, Record<string, string>>
|
||||
runMigrations: RunMigrationFn
|
||||
}> {
|
||||
const sharedContainer_ = createMedusaContainer({}, sharedContainer)
|
||||
|
||||
const modules: MedusaModuleConfig =
|
||||
modulesConfig ??
|
||||
(
|
||||
@@ -222,7 +237,20 @@ export async function MedusaApp(
|
||||
linkModuleOptions = linkModule
|
||||
}
|
||||
|
||||
const allModules = await loadModules(modules, injectedDependencies)
|
||||
for (const injectedDependency of Object.keys(injectedDependencies)) {
|
||||
sharedContainer_.register({
|
||||
[injectedDependency]: asValue(injectedDependencies[injectedDependency]),
|
||||
})
|
||||
}
|
||||
|
||||
const allModules = await loadModules(modules, sharedContainer_)
|
||||
|
||||
// Share Event bus with link modules
|
||||
injectedDependencies[ModuleRegistrationName.EVENT_BUS] =
|
||||
sharedContainer_.resolve(ModuleRegistrationName.EVENT_BUS, {
|
||||
allowUnregistered: true,
|
||||
})
|
||||
|
||||
const {
|
||||
remoteLink,
|
||||
linkResolution,
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
InternalModuleDeclaration,
|
||||
LinkModuleDefinition,
|
||||
LoadedModule,
|
||||
MedusaContainer,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
ModuleDefinition,
|
||||
@@ -48,6 +49,23 @@ type ModuleAlias = {
|
||||
main?: boolean
|
||||
}
|
||||
|
||||
export type ModuleBootstrapOptions = {
|
||||
moduleKey: string
|
||||
defaultPath: string
|
||||
declaration?: InternalModuleDeclaration | ExternalModuleDeclaration
|
||||
moduleExports?: ModuleExports
|
||||
sharedContainer?: MedusaContainer
|
||||
moduleDefinition?: ModuleDefinition
|
||||
injectedDependencies?: Record<string, any>
|
||||
}
|
||||
|
||||
export type LinkModuleBootstrapOptions = {
|
||||
definition: LinkModuleDefinition
|
||||
declaration?: InternalModuleDeclaration
|
||||
moduleExports?: ModuleExports
|
||||
injectedDependencies?: Record<string, any>
|
||||
}
|
||||
|
||||
export class MedusaModule {
|
||||
private static instances_: Map<string, any> = new Map()
|
||||
private static modules_: Map<string, ModuleAlias[]> = new Map()
|
||||
@@ -146,14 +164,15 @@ export class MedusaModule {
|
||||
MedusaModule.modules_.set(moduleKey, modules!)
|
||||
}
|
||||
|
||||
public static async bootstrap<T>(
|
||||
moduleKey: string,
|
||||
defaultPath: string,
|
||||
declaration?: InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
moduleExports?: ModuleExports,
|
||||
injectedDependencies?: Record<string, any>,
|
||||
moduleDefinition?: ModuleDefinition
|
||||
): Promise<{
|
||||
public static async bootstrap<T>({
|
||||
moduleKey,
|
||||
defaultPath,
|
||||
declaration,
|
||||
moduleExports,
|
||||
sharedContainer,
|
||||
moduleDefinition,
|
||||
injectedDependencies,
|
||||
}: ModuleBootstrapOptions): Promise<{
|
||||
[key: string]: T
|
||||
}> {
|
||||
const hashKey = simpleHash(
|
||||
@@ -193,11 +212,14 @@ export class MedusaModule {
|
||||
}
|
||||
}
|
||||
|
||||
const container = createMedusaContainer()
|
||||
const container = createMedusaContainer({}, sharedContainer)
|
||||
|
||||
if (injectedDependencies) {
|
||||
for (const service in injectedDependencies) {
|
||||
container.register(service, asValue(injectedDependencies[service]))
|
||||
if (!container.hasRegistration(service)) {
|
||||
container.register(service, asValue(injectedDependencies[service]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,19 +277,19 @@ export class MedusaModule {
|
||||
return services
|
||||
}
|
||||
|
||||
public static async bootstrapLink(
|
||||
definition: LinkModuleDefinition,
|
||||
declaration?: InternalModuleDeclaration,
|
||||
moduleExports?: ModuleExports,
|
||||
injectedDependencies?: Record<string, any>
|
||||
): Promise<{
|
||||
public static async bootstrapLink({
|
||||
definition,
|
||||
declaration,
|
||||
moduleExports,
|
||||
injectedDependencies,
|
||||
}: LinkModuleBootstrapOptions): Promise<{
|
||||
[key: string]: unknown
|
||||
}> {
|
||||
const moduleKey = definition.key
|
||||
const hashKey = simpleHash(stringifyCircular({ moduleKey, declaration }))
|
||||
|
||||
if (MedusaModule.instances_.has(hashKey)) {
|
||||
return MedusaModule.instances_.get(hashKey)
|
||||
return { [moduleKey]: MedusaModule.instances_.get(hashKey) }
|
||||
}
|
||||
|
||||
if (MedusaModule.loading_.has(hashKey)) {
|
||||
|
||||
@@ -34,6 +34,8 @@ export class RemoteQuery {
|
||||
)
|
||||
}
|
||||
|
||||
const servicesConfig_ = [...servicesConfig]
|
||||
|
||||
for (const mod of modulesLoaded) {
|
||||
if (!mod.__definition.isQueryable) {
|
||||
continue
|
||||
@@ -48,12 +50,12 @@ export class RemoteQuery {
|
||||
}
|
||||
|
||||
this.modulesMap.set(serviceName, mod)
|
||||
servicesConfig!.push(mod.__joinerConfig)
|
||||
servicesConfig_!.push(mod.__joinerConfig)
|
||||
}
|
||||
|
||||
this.customRemoteFetchData = customRemoteFetchData
|
||||
this.remoteJoiner = new RemoteJoiner(
|
||||
servicesConfig as JoinerServiceConfig[],
|
||||
servicesConfig_ as JoinerServiceConfig[],
|
||||
this.remoteFetchData.bind(this)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -19,13 +19,15 @@ export const initialize = async (
|
||||
): Promise<IPricingModuleService> => {
|
||||
const serviceKey = Modules.PRICING
|
||||
|
||||
const loaded = await MedusaModule.bootstrap<IPricingModuleService>(
|
||||
serviceKey,
|
||||
MODULE_PACKAGE_NAMES[Modules.PRICING],
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
moduleDefinition,
|
||||
injectedDependencies
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<IPricingModuleService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: MODULE_PACKAGE_NAMES[Modules.PRICING],
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
injectedDependencies,
|
||||
moduleExports: moduleDefinition,
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MODULE_PACKAGE_NAMES,
|
||||
MedusaModule,
|
||||
MODULE_PACKAGE_NAMES,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IProductModuleService, ModulesSdkTypes } from "@medusajs/types"
|
||||
@@ -20,13 +20,15 @@ export const initialize = async (
|
||||
): Promise<IProductModuleService> => {
|
||||
const serviceKey = Modules.PRODUCT
|
||||
|
||||
const loaded = await MedusaModule.bootstrap<IProductModuleService>(
|
||||
serviceKey,
|
||||
MODULE_PACKAGE_NAMES[Modules.PRODUCT],
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
moduleDefinition,
|
||||
injectedDependencies
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<IProductModuleService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: MODULE_PACKAGE_NAMES[Modules.PRODUCT],
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
injectedDependencies,
|
||||
moduleExports: moduleDefinition,
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
MODULE_PACKAGE_NAMES,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IEventBusService, IStockLocationService } from "@medusajs/types"
|
||||
@@ -15,13 +16,15 @@ export const initialize = async (
|
||||
}
|
||||
): Promise<IStockLocationService> => {
|
||||
const serviceKey = Modules.STOCK_LOCATION
|
||||
const loaded = await MedusaModule.bootstrap<IStockLocationService>(
|
||||
serviceKey,
|
||||
"@medusajs/stock-location",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
moduleDefinition,
|
||||
injectedDependencies
|
||||
)
|
||||
const loaded = await MedusaModule.bootstrap<IStockLocationService>({
|
||||
moduleKey: serviceKey,
|
||||
defaultPath: MODULE_PACKAGE_NAMES[Modules.STOCK_LOCATION],
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration,
|
||||
injectedDependencies,
|
||||
moduleExports: moduleDefinition,
|
||||
})
|
||||
|
||||
return loaded[serviceKey]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user