chore(medusa, modules-sdk, types): Refactor modules loading from medusa (#5018)
This commit is contained in:
committed by
GitHub
parent
5362bfc348
commit
4fa675ec25
@@ -35,6 +35,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
{
|
||||
[Modules.EVENT_BUS]: {
|
||||
key: Modules.EVENT_BUS,
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.EVENT_BUS,
|
||||
defaultPackage: MODULE_PACKAGE_NAMES[Modules.EVENT_BUS],
|
||||
label: "EventBusModuleService",
|
||||
@@ -48,6 +49,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
},
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
key: Modules.STOCK_LOCATION,
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.STOCK_LOCATION,
|
||||
defaultPackage: false,
|
||||
label: "StockLocationService",
|
||||
@@ -62,6 +64,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
},
|
||||
[Modules.INVENTORY]: {
|
||||
key: Modules.INVENTORY,
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.INVENTORY,
|
||||
defaultPackage: false,
|
||||
label: "InventoryService",
|
||||
@@ -76,6 +79,7 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
},
|
||||
[Modules.CACHE]: {
|
||||
key: Modules.CACHE,
|
||||
isLegacy: true,
|
||||
registrationName: ModuleRegistrationName.CACHE,
|
||||
defaultPackage: MODULE_PACKAGE_NAMES[Modules.CACHE],
|
||||
label: "CacheService",
|
||||
|
||||
@@ -16,6 +16,7 @@ describe("module definitions loader", () => {
|
||||
registrationName: "testService",
|
||||
defaultPackage: "@medusajs/test-service",
|
||||
label: "TestService",
|
||||
isLegacy: true,
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
defaultModuleDeclaration: {
|
||||
|
||||
@@ -11,18 +11,29 @@ import { isObject } from "@medusajs/utils"
|
||||
import resolveCwd from "resolve-cwd"
|
||||
import { MODULE_DEFINITIONS, ModulesDefinition } from "../definitions"
|
||||
|
||||
/**
|
||||
*
|
||||
* @param modules
|
||||
* @param isolatedModules Will be removed once the isolated flag is being removed
|
||||
*/
|
||||
export const registerModules = (
|
||||
modules?: Record<
|
||||
string,
|
||||
| false
|
||||
| string
|
||||
| Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
|
||||
>
|
||||
>,
|
||||
{ loadLegacyOnly } = { loadLegacyOnly: false }
|
||||
): Record<string, ModuleResolution> => {
|
||||
const moduleResolutions = {} as Record<string, ModuleResolution>
|
||||
const projectModules = modules ?? {}
|
||||
|
||||
for (const definition of MODULE_DEFINITIONS) {
|
||||
// Skip non legacy modules
|
||||
if (loadLegacyOnly && !definition.isLegacy) {
|
||||
continue
|
||||
}
|
||||
|
||||
const customConfig = projectModules[definition.key]
|
||||
|
||||
const canSkip =
|
||||
@@ -62,6 +73,12 @@ export const registerMedusaModule = (
|
||||
throw new Error(`Module: ${moduleKey} is not defined.`)
|
||||
}
|
||||
|
||||
if (modDefinition.isLegacy) {
|
||||
throw new Error(
|
||||
`Module: ${moduleKey} is a legacy module. Please use registerModules instead.`
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
isObject(moduleDeclaration) &&
|
||||
moduleDeclaration?.scope === MODULE_SCOPE.EXTERNAL
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { RemoteFetchDataCallback } from "@medusajs/orchestration"
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
LoadedModule,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
ModuleConfig,
|
||||
ModuleDefinition,
|
||||
ModuleJoinerConfig,
|
||||
ModuleServiceInitializeOptions,
|
||||
RemoteJoinerQuery,
|
||||
@@ -18,13 +20,30 @@ import { MedusaModule } from "./medusa-module"
|
||||
import { RemoteLink } from "./remote-link"
|
||||
import { RemoteQuery } from "./remote-query"
|
||||
|
||||
export type MedusaModuleConfig = (Partial<ModuleConfig> | Modules)[]
|
||||
type SharedResources = {
|
||||
database?: ModuleServiceInitializeOptions["database"]
|
||||
export type MedusaModuleConfig = {
|
||||
[key: string | Modules]:
|
||||
| Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
|
||||
| true
|
||||
}
|
||||
|
||||
const isModuleConfig = (obj: any): obj is ModuleConfig => {
|
||||
return isObject(obj)
|
||||
export type SharedResources = {
|
||||
database?: ModuleServiceInitializeOptions["database"] & {
|
||||
/**
|
||||
* {
|
||||
* name?: string
|
||||
* afterCreate?: Function
|
||||
* min?: number
|
||||
* max?: number
|
||||
* refreshIdle?: boolean
|
||||
* idleTimeoutMillis?: number
|
||||
* reapIntervalMillis?: number
|
||||
* returnToHead?: boolean
|
||||
* priorityRange?: number
|
||||
* log?: (message: string, logLevel: string) => void
|
||||
* }
|
||||
*/
|
||||
pool?: Record<string, unknown>
|
||||
}
|
||||
}
|
||||
|
||||
export async function MedusaApp({
|
||||
@@ -83,20 +102,15 @@ export async function MedusaApp({
|
||||
const allModules: Record<string, LoadedModule | LoadedModule[]> = {}
|
||||
|
||||
await Promise.all(
|
||||
modules.map(async (mod: Partial<ModuleConfig> | Modules) => {
|
||||
let key: Modules | string = mod as Modules
|
||||
Object.keys(modules).map(async (moduleName) => {
|
||||
const mod = modules[moduleName] as MedusaModuleConfig
|
||||
|
||||
let path: string
|
||||
let declaration: any = {}
|
||||
|
||||
if (isModuleConfig(mod)) {
|
||||
if (!mod.module) {
|
||||
throw new Error(
|
||||
`Module ${JSON.stringify(mod)} is missing module name.`
|
||||
)
|
||||
}
|
||||
|
||||
key = mod.module
|
||||
path = mod.path ?? MODULE_PACKAGE_NAMES[key]
|
||||
if (isObject(mod)) {
|
||||
const mod_ = mod as unknown as InternalModuleDeclaration
|
||||
path = mod_.resolve ?? MODULE_PACKAGE_NAMES[moduleName]
|
||||
|
||||
declaration = { ...mod }
|
||||
delete declaration.definition
|
||||
@@ -104,10 +118,6 @@ export async function MedusaApp({
|
||||
path = MODULE_PACKAGE_NAMES[mod as Modules]
|
||||
}
|
||||
|
||||
if (!path) {
|
||||
throw new Error(`Module ${key} is missing path.`)
|
||||
}
|
||||
|
||||
declaration.scope ??= MODULE_SCOPE.INTERNAL
|
||||
|
||||
if (
|
||||
@@ -118,22 +128,22 @@ export async function MedusaApp({
|
||||
}
|
||||
|
||||
const loaded = (await MedusaModule.bootstrap(
|
||||
key,
|
||||
moduleName,
|
||||
path,
|
||||
declaration,
|
||||
undefined,
|
||||
injectedDependencies,
|
||||
isModuleConfig(mod) ? mod.definition : undefined
|
||||
(isObject(mod) ? mod.definition : undefined) as ModuleDefinition
|
||||
)) as LoadedModule
|
||||
|
||||
if (allModules[key] && !Array.isArray(allModules[key])) {
|
||||
allModules[key] = []
|
||||
if (allModules[moduleName] && !Array.isArray(allModules[moduleName])) {
|
||||
allModules[moduleName] = []
|
||||
}
|
||||
|
||||
if (allModules[key]) {
|
||||
;(allModules[key] as LoadedModule[]).push(loaded[key])
|
||||
if (allModules[moduleName]) {
|
||||
;(allModules[moduleName] as LoadedModule[]).push(loaded[moduleName])
|
||||
} else {
|
||||
allModules[key] = loaded[key]
|
||||
allModules[moduleName] = loaded[moduleName]
|
||||
}
|
||||
|
||||
return loaded
|
||||
|
||||
Reference in New Issue
Block a user