Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)
* feat: scoped container for modules
This commit is contained in:
committed by
GitHub
parent
8e78c533c4
commit
77d46220c2
@@ -24,6 +24,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"awilix": "^8.0.0",
|
||||
"glob": "7.1.6",
|
||||
"medusa-core-utils": "^1.1.39",
|
||||
"medusa-telemetry": "^0.0.16",
|
||||
"resolve-cwd": "^3.0.0"
|
||||
},
|
||||
|
||||
@@ -8,6 +8,7 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
label: "StockLocationService",
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
dependencies: ["eventBusService"],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
@@ -20,6 +21,7 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
label: "InventoryService",
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
dependencies: ["eventBusService"],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
export * from "./types"
|
||||
export * from "./loaders"
|
||||
|
||||
export * from "./module-helper"
|
||||
|
||||
export * from "./definitions"
|
||||
export * from "./loaders"
|
||||
export * from "./module-helper"
|
||||
export * from "./types"
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
import { EOL } from "os"
|
||||
import { AwilixContainer, ClassOrFunctionReturning, Resolver } from "awilix"
|
||||
import { createMedusaContainer } from "medusa-core-utils"
|
||||
import {
|
||||
asFunction,
|
||||
asValue,
|
||||
AwilixContainer,
|
||||
ClassOrFunctionReturning,
|
||||
createContainer,
|
||||
Resolver,
|
||||
} from "awilix"
|
||||
import {
|
||||
ConfigModule,
|
||||
MedusaContainer,
|
||||
ModuleResolution,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
@@ -28,46 +21,9 @@ function asArray(
|
||||
|
||||
const logger = {
|
||||
warn: jest.fn(),
|
||||
error: jest.fn(),
|
||||
} as any
|
||||
|
||||
const buildConfigModule = (
|
||||
configParts: Partial<ConfigModule>
|
||||
): ConfigModule => {
|
||||
return {
|
||||
modules: {},
|
||||
moduleResolutions: {},
|
||||
...configParts,
|
||||
}
|
||||
}
|
||||
|
||||
const buildContainer = () => {
|
||||
const container = createContainer() as MedusaContainer
|
||||
|
||||
container.registerAdd = function (
|
||||
this: MedusaContainer,
|
||||
name: string,
|
||||
registration: typeof asFunction | typeof asValue
|
||||
): MedusaContainer {
|
||||
const storeKey = name + "_STORE"
|
||||
|
||||
if (this.registrations[storeKey] === undefined) {
|
||||
this.register(storeKey, asValue([] as Resolver<unknown>[]))
|
||||
}
|
||||
const store = this.resolve(storeKey) as (
|
||||
| ClassOrFunctionReturning<unknown>
|
||||
| Resolver<unknown>
|
||||
)[]
|
||||
|
||||
if (this.registrations[name] === undefined) {
|
||||
this.register(name, asArray(store))
|
||||
}
|
||||
store.unshift(registration)
|
||||
|
||||
return this
|
||||
}.bind(container)
|
||||
|
||||
return container
|
||||
}
|
||||
describe("modules loader", () => {
|
||||
let container
|
||||
|
||||
@@ -76,7 +32,7 @@ describe("modules loader", () => {
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
container = buildContainer()
|
||||
container = createMedusaContainer()
|
||||
})
|
||||
|
||||
it("registers service as undefined in container when no resolution path is given", async () => {
|
||||
@@ -100,10 +56,7 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
const configModule = buildConfigModule({
|
||||
moduleResolutions,
|
||||
})
|
||||
await moduleLoader({ container, configModule, logger })
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
|
||||
const testService = container.resolve(
|
||||
moduleResolutions.testService.definition.key
|
||||
@@ -132,11 +85,7 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
const configModule = buildConfigModule({
|
||||
moduleResolutions,
|
||||
})
|
||||
|
||||
await moduleLoader({ container, configModule, logger })
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
|
||||
const testService = container.resolve(
|
||||
moduleResolutions.testService.definition.key,
|
||||
@@ -175,14 +124,10 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
const configModule = buildConfigModule({
|
||||
moduleResolutions,
|
||||
})
|
||||
|
||||
await moduleLoader({ container, configModule, logger })
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
"Could not resolve module: TestService. Error: Loaders for module TestService failed: loader"
|
||||
`Could not resolve module: TestService. Error: Loaders for module TestService failed: loader${EOL}`
|
||||
)
|
||||
})
|
||||
|
||||
@@ -207,14 +152,10 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
const configModule = buildConfigModule({
|
||||
moduleResolutions,
|
||||
})
|
||||
|
||||
await moduleLoader({ container, configModule, logger })
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
"Could not resolve module: TestService. Error: No service found in module. Make sure that your module exports a service."
|
||||
`Could not resolve module: TestService. Error: No service found in module. Make sure your module exports at least one service.${EOL}`
|
||||
)
|
||||
})
|
||||
|
||||
@@ -241,14 +182,11 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
const configModule = buildConfigModule({
|
||||
moduleResolutions,
|
||||
})
|
||||
try {
|
||||
await moduleLoader({ container, configModule, logger })
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
} catch (err) {
|
||||
expect(err.message).toEqual(
|
||||
"No service found in module. Make sure that your module exports a service."
|
||||
"No service found in module. Make sure your module exports at least one service."
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -276,11 +214,8 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
const configModule = buildConfigModule({
|
||||
moduleResolutions,
|
||||
})
|
||||
try {
|
||||
await moduleLoader({ container, configModule, logger })
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
} catch (err) {
|
||||
expect(err.message).toEqual(
|
||||
"The module TestService has to define its scope (internal | external)"
|
||||
@@ -308,14 +243,11 @@ describe("modules loader", () => {
|
||||
moduleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
},
|
||||
},
|
||||
} as any,
|
||||
}
|
||||
|
||||
const configModule = buildConfigModule({
|
||||
moduleResolutions,
|
||||
})
|
||||
try {
|
||||
await moduleLoader({ container, configModule, logger })
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
} catch (err) {
|
||||
expect(err.message).toEqual(
|
||||
"The module TestService is missing its resources config"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import {
|
||||
ConfigModule,
|
||||
InternalModuleDeclaration,
|
||||
ModuleDefinition,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "../../types"
|
||||
import { registerModules } from "../module-definition"
|
||||
import { registerModules } from "../register-modules"
|
||||
import MODULE_DEFINITIONS from "../../definitions"
|
||||
|
||||
const RESOLVED_PACKAGE = "@medusajs/test-service-resolved"
|
||||
@@ -35,17 +35,19 @@ describe("module definitions loader", () => {
|
||||
it("Resolves module with default definition given empty config", () => {
|
||||
MODULE_DEFINITIONS.push({ ...defaultDefinition })
|
||||
|
||||
const res = registerModules({ modules: {} } as ConfigModule)
|
||||
const res = registerModules({ modules: {} })
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual({
|
||||
resolutionPath: defaultDefinition.defaultPackage,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
expect(res[defaultDefinition.key]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolutionPath: defaultDefinition.defaultPackage,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe("boolean config", () => {
|
||||
@@ -54,13 +56,15 @@ describe("module definitions loader", () => {
|
||||
|
||||
const res = registerModules({
|
||||
modules: { [defaultDefinition.key]: false },
|
||||
} as ConfigModule)
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual({
|
||||
resolutionPath: false,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
})
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolutionPath: false,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("Fails to resolve module with no resolution path when given false for a required module", () => {
|
||||
@@ -70,7 +74,7 @@ describe("module definitions loader", () => {
|
||||
try {
|
||||
registerModules({
|
||||
modules: { [defaultDefinition.key]: false },
|
||||
} as ConfigModule)
|
||||
})
|
||||
} catch (err) {
|
||||
expect(err.message).toEqual(
|
||||
`Module: ${defaultDefinition.label} is required`
|
||||
@@ -88,17 +92,19 @@ describe("module definitions loader", () => {
|
||||
|
||||
const res = registerModules({
|
||||
modules: {},
|
||||
} as ConfigModule)
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual({
|
||||
resolutionPath: false,
|
||||
definition: definition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolutionPath: false,
|
||||
definition: definition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -110,17 +116,19 @@ describe("module definitions loader", () => {
|
||||
modules: {
|
||||
[defaultDefinition.key]: defaultDefinition.defaultPackage,
|
||||
},
|
||||
} as ConfigModule)
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual({
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -131,22 +139,25 @@ describe("module definitions loader", () => {
|
||||
const res = registerModules({
|
||||
modules: {
|
||||
[defaultDefinition.key]: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
},
|
||||
},
|
||||
} as ConfigModule)
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual({
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "isolated",
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
} as InternalModuleDeclaration,
|
||||
},
|
||||
})
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "isolated",
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("Resolves default resolution path and provides options when only options are provided", () => {
|
||||
@@ -158,18 +169,20 @@ describe("module definitions loader", () => {
|
||||
options: { test: 123 },
|
||||
},
|
||||
},
|
||||
} as unknown as ConfigModule)
|
||||
} as any)
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual({
|
||||
resolutionPath: defaultDefinition.defaultPackage,
|
||||
definition: defaultDefinition,
|
||||
options: { test: 123 },
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
expect(res[defaultDefinition.key]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolutionPath: defaultDefinition.defaultPackage,
|
||||
definition: defaultDefinition,
|
||||
options: { test: 123 },
|
||||
},
|
||||
})
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
options: { test: 123 },
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("Resolves resolution path and provides options when only options are provided", () => {
|
||||
@@ -184,19 +197,21 @@ describe("module definitions loader", () => {
|
||||
resources: "isolated",
|
||||
},
|
||||
},
|
||||
} as unknown as ConfigModule)
|
||||
} as any)
|
||||
|
||||
expect(res[defaultDefinition.key]).toEqual({
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: { test: 123 },
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "isolated",
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
expect(res[defaultDefinition.key]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: { test: 123 },
|
||||
},
|
||||
})
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "isolated",
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
options: { test: 123 },
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,2 @@
|
||||
export * from "./module-loader"
|
||||
|
||||
export * from "./module-definition"
|
||||
export * from "./register-modules"
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import resolveCwd from "resolve-cwd"
|
||||
|
||||
import { ConfigModule, ModuleResolution } from "../types"
|
||||
import MODULE_DEFINITIONS from "../definitions"
|
||||
|
||||
export const registerModules = ({ modules }: ConfigModule) => {
|
||||
const moduleResolutions = {} as Record<string, ModuleResolution>
|
||||
const projectModules = modules ?? {}
|
||||
|
||||
for (const definition of MODULE_DEFINITIONS) {
|
||||
let resolutionPath = definition.defaultPackage
|
||||
|
||||
const moduleConfiguration = projectModules[definition.key]
|
||||
|
||||
if (typeof moduleConfiguration === "boolean") {
|
||||
if (!moduleConfiguration && definition.isRequired) {
|
||||
throw new Error(`Module: ${definition.label} is required`)
|
||||
}
|
||||
if (!moduleConfiguration) {
|
||||
moduleResolutions[definition.key] = {
|
||||
resolutionPath: false,
|
||||
definition,
|
||||
options: {},
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// If user added a module and it's overridable, we resolve that instead
|
||||
if (
|
||||
definition.canOverride &&
|
||||
(typeof moduleConfiguration === "string" ||
|
||||
(typeof moduleConfiguration === "object" &&
|
||||
moduleConfiguration.resolve))
|
||||
) {
|
||||
resolutionPath = resolveCwd(
|
||||
typeof moduleConfiguration === "string"
|
||||
? moduleConfiguration
|
||||
: (moduleConfiguration.resolve as string)
|
||||
)
|
||||
}
|
||||
|
||||
const moduleDeclaration =
|
||||
typeof moduleConfiguration === "object" ? moduleConfiguration : {}
|
||||
|
||||
moduleResolutions[definition.key] = {
|
||||
resolutionPath,
|
||||
definition,
|
||||
moduleDeclaration: {
|
||||
...definition.defaultModuleDeclaration,
|
||||
...moduleDeclaration,
|
||||
},
|
||||
options:
|
||||
typeof moduleConfiguration === "object"
|
||||
? moduleConfiguration.options ?? {}
|
||||
: {},
|
||||
}
|
||||
}
|
||||
|
||||
return moduleResolutions
|
||||
}
|
||||
@@ -1,38 +1,41 @@
|
||||
import { asFunction, asValue } from "awilix"
|
||||
import { trackInstallation } from "medusa-telemetry"
|
||||
import { asValue } from "awilix"
|
||||
import { EOL } from "os"
|
||||
import { loadInternalModule } from "./utils"
|
||||
|
||||
import {
|
||||
ClassConstructor,
|
||||
ConfigModule,
|
||||
LoaderOptions,
|
||||
Logger,
|
||||
MedusaContainer,
|
||||
ModuleExports,
|
||||
ModuleResolution,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "../types/module"
|
||||
} from "../types"
|
||||
|
||||
import { ModulesHelper } from "../module-helper"
|
||||
|
||||
export const moduleHelper = new ModulesHelper()
|
||||
|
||||
const registerModule = async (
|
||||
async function loadModule(
|
||||
container: MedusaContainer,
|
||||
resolution: ModuleResolution,
|
||||
configModule: ConfigModule,
|
||||
logger: Logger
|
||||
): Promise<{ error?: Error } | void> => {
|
||||
const constainerName = resolution.definition.registrationName
|
||||
): Promise<{ error?: Error } | void> {
|
||||
const registrationName = resolution.definition.registrationName
|
||||
|
||||
const { scope, resources } = resolution.moduleDeclaration ?? ({} as any)
|
||||
|
||||
if (scope === MODULE_SCOPE.EXTERNAL) {
|
||||
// TODO: implement external Resolvers
|
||||
// return loadExternalModule(...)
|
||||
throw new Error("External Modules are not supported yet.")
|
||||
}
|
||||
|
||||
const { scope, resources } = resolution.moduleDeclaration ?? {}
|
||||
if (!scope || (scope === MODULE_SCOPE.INTERNAL && !resources)) {
|
||||
let message = `The module ${resolution.definition.label} has to define its scope (internal | external)`
|
||||
if (scope && !resources) {
|
||||
if (scope === MODULE_SCOPE.INTERNAL && !resources) {
|
||||
message = `The module ${resolution.definition.label} is missing its resources config`
|
||||
}
|
||||
|
||||
container.register({
|
||||
[constainerName]: asValue(undefined),
|
||||
[registrationName]: asValue(undefined),
|
||||
})
|
||||
|
||||
return {
|
||||
@@ -42,107 +45,38 @@ const registerModule = async (
|
||||
|
||||
if (!resolution.resolutionPath) {
|
||||
container.register({
|
||||
[constainerName]: asValue(undefined),
|
||||
[registrationName]: asValue(undefined),
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
let loadedModule: ModuleExports
|
||||
try {
|
||||
loadedModule = (await import(resolution.resolutionPath!)).default
|
||||
} catch (error) {
|
||||
return { error }
|
||||
}
|
||||
|
||||
const moduleService = loadedModule?.service || null
|
||||
|
||||
if (!moduleService) {
|
||||
return {
|
||||
error: new Error(
|
||||
"No service found in module. Make sure that your module exports a service."
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
scope === MODULE_SCOPE.INTERNAL &&
|
||||
resources === MODULE_RESOURCE_TYPE.SHARED
|
||||
) {
|
||||
const moduleModels = loadedModule?.models || null
|
||||
if (moduleModels) {
|
||||
moduleModels.map((val: ClassConstructor<unknown>) => {
|
||||
container.registerAdd("db_entities", asValue(val))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: "cradle" should only contain dependent Modules and the EntityManager if module scope is shared
|
||||
container.register({
|
||||
[constainerName]: asFunction((cradle) => {
|
||||
return new moduleService(
|
||||
cradle,
|
||||
resolution.options,
|
||||
resolution.moduleDeclaration
|
||||
)
|
||||
}).singleton(),
|
||||
})
|
||||
|
||||
const moduleLoaders = loadedModule?.loaders || []
|
||||
try {
|
||||
for (const loader of moduleLoaders) {
|
||||
await loader(
|
||||
{
|
||||
container,
|
||||
configModule,
|
||||
logger,
|
||||
options: resolution.options,
|
||||
},
|
||||
resolution.moduleDeclaration
|
||||
)
|
||||
}
|
||||
} catch (err) {
|
||||
return {
|
||||
error: new Error(
|
||||
`Loaders for module ${resolution.definition.label} failed: ${err.message}`
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
trackInstallation(
|
||||
{
|
||||
module: resolution.definition.key,
|
||||
resolution: resolution.resolutionPath,
|
||||
},
|
||||
"module"
|
||||
)
|
||||
return await loadInternalModule(container, resolution, logger)
|
||||
}
|
||||
|
||||
export const moduleLoader = async ({
|
||||
container,
|
||||
configModule,
|
||||
moduleResolutions,
|
||||
logger,
|
||||
}: LoaderOptions): Promise<void> => {
|
||||
const moduleResolutions = configModule?.moduleResolutions ?? {}
|
||||
}: {
|
||||
container: MedusaContainer
|
||||
moduleResolutions: Record<string, ModuleResolution>
|
||||
logger: Logger
|
||||
}): Promise<void> => {
|
||||
for (const resolution of Object.values(moduleResolutions ?? {})) {
|
||||
const registrationResult = await loadModule(container, resolution, logger!)
|
||||
|
||||
for (const resolution of Object.values(moduleResolutions)) {
|
||||
const registrationResult = await registerModule(
|
||||
container,
|
||||
resolution,
|
||||
configModule,
|
||||
logger!
|
||||
)
|
||||
if (registrationResult?.error) {
|
||||
const { error } = registrationResult
|
||||
if (resolution.definition.isRequired) {
|
||||
logger?.warn(
|
||||
`Could not resolve required module: ${resolution.definition.label}. Error: ${error.message}`
|
||||
logger?.error(
|
||||
`Could not resolve required module: ${resolution.definition.label}. Error: ${error.message}${EOL}`
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
logger?.warn(
|
||||
`Could not resolve module: ${resolution.definition.label}. Error: ${error.message}`
|
||||
`Could not resolve module: ${resolution.definition.label}. Error: ${error.message}${EOL}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
85
packages/modules-sdk/src/loaders/register-modules.ts
Normal file
85
packages/modules-sdk/src/loaders/register-modules.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import resolveCwd from "resolve-cwd"
|
||||
|
||||
import {
|
||||
MedusaModuleConfig,
|
||||
InternalModuleDeclaration,
|
||||
ModuleDefinition,
|
||||
ModuleResolution,
|
||||
MODULE_SCOPE,
|
||||
} from "../types"
|
||||
import MODULE_DEFINITIONS from "../definitions"
|
||||
|
||||
export const registerModules = ({
|
||||
modules,
|
||||
}: MedusaModuleConfig): Record<string, ModuleResolution> => {
|
||||
const moduleResolutions = {} as Record<string, ModuleResolution>
|
||||
const projectModules = modules ?? {}
|
||||
|
||||
for (const definition of MODULE_DEFINITIONS) {
|
||||
const customConfig = projectModules[definition.key]
|
||||
const isObj = typeof customConfig === "object"
|
||||
|
||||
if (isObj && customConfig.scope === MODULE_SCOPE.EXTERNAL) {
|
||||
// TODO: getExternalModuleResolution(...)
|
||||
throw new Error("External Modules are not supported yet.")
|
||||
}
|
||||
|
||||
moduleResolutions[definition.key] = getInternalModuleResolution(
|
||||
definition,
|
||||
projectModules[definition.key] as
|
||||
| InternalModuleDeclaration
|
||||
| false
|
||||
| string
|
||||
)
|
||||
}
|
||||
|
||||
return moduleResolutions
|
||||
}
|
||||
|
||||
function getInternalModuleResolution(
|
||||
definition: ModuleDefinition,
|
||||
moduleConfig: InternalModuleDeclaration | false | string
|
||||
): ModuleResolution {
|
||||
if (typeof moduleConfig === "boolean") {
|
||||
if (!moduleConfig && definition.isRequired) {
|
||||
throw new Error(`Module: ${definition.label} is required`)
|
||||
}
|
||||
if (!moduleConfig) {
|
||||
return {
|
||||
resolutionPath: false,
|
||||
definition,
|
||||
dependencies: [],
|
||||
options: {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const isObj = typeof moduleConfig === "object"
|
||||
let resolutionPath = definition.defaultPackage
|
||||
|
||||
// If user added a module and it's overridable, we resolve that instead
|
||||
const isString = typeof moduleConfig === "string"
|
||||
if (definition.canOverride && (isString || (isObj && moduleConfig.resolve))) {
|
||||
resolutionPath = resolveCwd(
|
||||
isString ? moduleConfig : (moduleConfig.resolve as string)
|
||||
)
|
||||
}
|
||||
|
||||
const moduleDeclaration = isObj ? moduleConfig : {}
|
||||
const additionalDependencies = isObj ? moduleConfig.dependencies || [] : []
|
||||
|
||||
return {
|
||||
resolutionPath,
|
||||
definition,
|
||||
dependencies: [
|
||||
...new Set(
|
||||
(definition.dependencies || []).concat(additionalDependencies)
|
||||
),
|
||||
],
|
||||
moduleDeclaration: {
|
||||
...definition.defaultModuleDeclaration,
|
||||
...moduleDeclaration,
|
||||
},
|
||||
options: isObj ? moduleConfig.options ?? {} : {},
|
||||
}
|
||||
}
|
||||
1
packages/modules-sdk/src/loaders/utils/index.ts
Normal file
1
packages/modules-sdk/src/loaders/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./load-internal"
|
||||
114
packages/modules-sdk/src/loaders/utils/load-internal.ts
Normal file
114
packages/modules-sdk/src/loaders/utils/load-internal.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { asFunction, asValue } from "awilix"
|
||||
import { createMedusaContainer } from "medusa-core-utils"
|
||||
import { trackInstallation } from "medusa-telemetry"
|
||||
import {
|
||||
Constructor,
|
||||
InternalModuleDeclaration,
|
||||
Logger,
|
||||
MedusaContainer,
|
||||
ModuleExports,
|
||||
ModuleResolution,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "../../types"
|
||||
|
||||
export async function loadInternalModule(
|
||||
container: MedusaContainer,
|
||||
resolution: ModuleResolution,
|
||||
logger: Logger
|
||||
): Promise<{ error?: Error } | void> {
|
||||
const registrationName = resolution.definition.registrationName
|
||||
|
||||
const { scope, resources } =
|
||||
resolution.moduleDeclaration as InternalModuleDeclaration
|
||||
|
||||
let loadedModule: ModuleExports
|
||||
try {
|
||||
loadedModule = (await import(resolution.resolutionPath as string)).default
|
||||
} catch (error) {
|
||||
return { error }
|
||||
}
|
||||
|
||||
if (!loadedModule?.service) {
|
||||
container.register({
|
||||
[registrationName]: asValue(undefined),
|
||||
})
|
||||
|
||||
return {
|
||||
error: new Error(
|
||||
"No service found in module. Make sure your module exports at least one service."
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
scope === MODULE_SCOPE.INTERNAL &&
|
||||
resources === MODULE_RESOURCE_TYPE.SHARED
|
||||
) {
|
||||
const moduleModels = loadedModule?.models || null
|
||||
if (moduleModels) {
|
||||
moduleModels.map((val: Constructor<unknown>) => {
|
||||
container.registerAdd("db_entities", asValue(val))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const localContainer =
|
||||
resources === MODULE_RESOURCE_TYPE.ISOLATED
|
||||
? createMedusaContainer()
|
||||
: (container.createScope() as MedusaContainer)
|
||||
|
||||
if (resources === MODULE_RESOURCE_TYPE.ISOLATED) {
|
||||
const moduleDependencies = resolution?.dependencies ?? []
|
||||
|
||||
for (const dependency of moduleDependencies) {
|
||||
localContainer.register(
|
||||
dependency,
|
||||
asFunction(() => container.resolve(dependency))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const moduleLoaders = loadedModule?.loaders ?? []
|
||||
try {
|
||||
for (const loader of moduleLoaders) {
|
||||
await loader(
|
||||
{
|
||||
container: localContainer,
|
||||
logger,
|
||||
options: resolution.options,
|
||||
},
|
||||
resolution.moduleDeclaration as InternalModuleDeclaration
|
||||
)
|
||||
}
|
||||
} catch (err) {
|
||||
container.register({
|
||||
[registrationName]: asValue(undefined),
|
||||
})
|
||||
|
||||
return {
|
||||
error: new Error(
|
||||
`Loaders for module ${resolution.definition.label} failed: ${err.message}`
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
const moduleService = loadedModule.service
|
||||
container.register({
|
||||
[registrationName]: asFunction((cradle) => {
|
||||
return new moduleService(
|
||||
localContainer.cradle,
|
||||
resolution.options,
|
||||
resolution.moduleDeclaration
|
||||
)
|
||||
}).singleton(),
|
||||
})
|
||||
|
||||
trackInstallation(
|
||||
{
|
||||
module: resolution.definition.key,
|
||||
resolution: resolution.resolutionPath,
|
||||
},
|
||||
"module"
|
||||
)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ModuleResolution, ModulesResponse } from "./types/module"
|
||||
import { ModuleResolution, ModulesResponse } from "./types"
|
||||
|
||||
export class ModulesHelper {
|
||||
private modules_: Record<string, ModuleResolution> = {}
|
||||
|
||||
@@ -1 +1,101 @@
|
||||
export * from "./module"
|
||||
import { MedusaContainer as coreMedusaContainer } from "medusa-core-utils"
|
||||
import { Logger as _Logger } from "winston"
|
||||
|
||||
export type MedusaContainer = coreMedusaContainer
|
||||
export type Constructor<T> = new (...args: any[]) => T
|
||||
|
||||
export type LogLevel =
|
||||
| "query"
|
||||
| "schema"
|
||||
| "error"
|
||||
| "warn"
|
||||
| "info"
|
||||
| "log"
|
||||
| "migration"
|
||||
export type LoggerOptions = boolean | "all" | LogLevel[]
|
||||
|
||||
export type Logger = _Logger & {
|
||||
progress: (activityId: string, msg: string) => void
|
||||
info: (msg: string) => void
|
||||
warn: (msg: string) => void
|
||||
}
|
||||
|
||||
export enum MODULE_SCOPE {
|
||||
INTERNAL = "internal",
|
||||
EXTERNAL = "external",
|
||||
}
|
||||
|
||||
export enum MODULE_RESOURCE_TYPE {
|
||||
SHARED = "shared",
|
||||
ISOLATED = "isolated",
|
||||
}
|
||||
|
||||
export type InternalModuleDeclaration = {
|
||||
scope: MODULE_SCOPE.INTERNAL
|
||||
resources: MODULE_RESOURCE_TYPE
|
||||
dependencies?: string[]
|
||||
resolve?: string
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export type ExternalModuleDeclaration = {
|
||||
scope: MODULE_SCOPE.EXTERNAL
|
||||
server: {
|
||||
type: "http"
|
||||
url: string
|
||||
keepAlive: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export type ModuleResolution = {
|
||||
resolutionPath: string | false
|
||||
definition: ModuleDefinition
|
||||
options?: Record<string, unknown>
|
||||
dependencies?: string[]
|
||||
moduleDeclaration?: InternalModuleDeclaration | ExternalModuleDeclaration
|
||||
}
|
||||
|
||||
export type ModuleDefinition = {
|
||||
key: string
|
||||
registrationName: string
|
||||
defaultPackage: string | false
|
||||
label: string
|
||||
canOverride?: boolean
|
||||
isRequired?: boolean
|
||||
dependencies?: string[]
|
||||
defaultModuleDeclaration:
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration
|
||||
}
|
||||
|
||||
export type LoaderOptions = {
|
||||
container: MedusaContainer
|
||||
options?: Record<string, unknown>
|
||||
logger?: Logger
|
||||
}
|
||||
|
||||
export type ModuleLoaderFunction = (
|
||||
options: LoaderOptions,
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
) => Promise<void>
|
||||
|
||||
export type ModulesResponse = {
|
||||
module: string
|
||||
resolution: string | false
|
||||
}[]
|
||||
|
||||
export type ModuleExports = {
|
||||
service: Constructor<any>
|
||||
loaders?: ModuleLoaderFunction[]
|
||||
migrations?: any[]
|
||||
models?: Constructor<any>[]
|
||||
}
|
||||
|
||||
export type MedusaModuleConfig = {
|
||||
modules?: Record<
|
||||
string,
|
||||
| false
|
||||
| string
|
||||
| Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
|
||||
>
|
||||
}
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
import { AwilixContainer } from "awilix"
|
||||
import { Logger as _Logger } from "winston"
|
||||
|
||||
export type LogLevel =
|
||||
| "query"
|
||||
| "schema"
|
||||
| "error"
|
||||
| "warn"
|
||||
| "info"
|
||||
| "log"
|
||||
| "migration"
|
||||
export type LoggerOptions = boolean | "all" | LogLevel[]
|
||||
|
||||
export type ClassConstructor<T> = {
|
||||
new (...args: unknown[]): T
|
||||
}
|
||||
|
||||
export type MedusaContainer = AwilixContainer & {
|
||||
registerAdd: <T>(name: string, registration: T) => MedusaContainer
|
||||
}
|
||||
|
||||
export type Logger = _Logger & {
|
||||
progress: (activityId: string, msg: string) => void
|
||||
info: (msg: string) => void
|
||||
warn: (msg: string) => void
|
||||
}
|
||||
|
||||
export enum MODULE_SCOPE {
|
||||
INTERNAL = "internal",
|
||||
EXTERNAL = "external",
|
||||
}
|
||||
|
||||
export enum MODULE_RESOURCE_TYPE {
|
||||
SHARED = "shared",
|
||||
ISOLATED = "isolated",
|
||||
}
|
||||
|
||||
export type ConfigurableModuleDeclaration = {
|
||||
scope: MODULE_SCOPE.INTERNAL
|
||||
resources: MODULE_RESOURCE_TYPE
|
||||
resolve?: string
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
/*
|
||||
| {
|
||||
scope: MODULE_SCOPE.external
|
||||
server: {
|
||||
type: "built-in" | "rest" | "tsrpc" | "grpc" | "gql"
|
||||
url: string
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
export type ModuleResolution = {
|
||||
resolutionPath: string | false
|
||||
definition: ModuleDefinition
|
||||
options?: Record<string, unknown>
|
||||
moduleDeclaration?: ConfigurableModuleDeclaration
|
||||
}
|
||||
|
||||
export type ModuleDefinition = {
|
||||
key: string
|
||||
registrationName: string
|
||||
defaultPackage: string | false
|
||||
label: string
|
||||
canOverride?: boolean
|
||||
isRequired?: boolean
|
||||
defaultModuleDeclaration: ConfigurableModuleDeclaration
|
||||
}
|
||||
|
||||
export type LoaderOptions = {
|
||||
container: MedusaContainer
|
||||
configModule: ConfigModule
|
||||
options?: Record<string, unknown>
|
||||
logger?: Logger
|
||||
}
|
||||
|
||||
export type Constructor<T> = new (...args: any[]) => T
|
||||
|
||||
export type ModuleExports = {
|
||||
loaders: ((
|
||||
options: LoaderOptions,
|
||||
moduleDeclaration?: ConfigurableModuleDeclaration
|
||||
) => Promise<void>)[]
|
||||
service: Constructor<any>
|
||||
migrations?: any[]
|
||||
models?: Constructor<any>[]
|
||||
}
|
||||
|
||||
export type ConfigModule = {
|
||||
options?: Record<string, any>
|
||||
modules?: Record<
|
||||
string,
|
||||
false | string | Partial<ConfigurableModuleDeclaration>
|
||||
>
|
||||
moduleResolutions?: Record<string, ModuleResolution>
|
||||
}
|
||||
|
||||
export type ModulesResponse = {
|
||||
module: string
|
||||
resolution: string | false
|
||||
}[]
|
||||
@@ -1,11 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": [
|
||||
"es5",
|
||||
"es6",
|
||||
"es2019"
|
||||
],
|
||||
"target": "es5",
|
||||
"lib": ["es2020"],
|
||||
"target": "es2020",
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"declaration": true,
|
||||
@@ -19,14 +15,13 @@
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"downlevelIteration": true // to use ES5 specific tooling
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["./src/**/*", "index.d.ts"],
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
"./dist/**/*",
|
||||
"dist",
|
||||
"./src/**/__tests__",
|
||||
"./src/**/__mocks__",
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user