From 1eeb1e9de3e0b571735437b00968ee96e4aabad5 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 30 Apr 2024 09:05:55 -0300 Subject: [PATCH] fix(modules-sdk): load custom modules options (#7176) --- .changeset/cold-birds-judge.md | 5 +++ .../src/loaders/__tests__/register-modules.ts | 26 +++++++++++ .../src/loaders/register-modules.ts | 43 +++++++++++-------- 3 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 .changeset/cold-birds-judge.md diff --git a/.changeset/cold-birds-judge.md b/.changeset/cold-birds-judge.md new file mode 100644 index 0000000000..9b1366ada4 --- /dev/null +++ b/.changeset/cold-birds-judge.md @@ -0,0 +1,5 @@ +--- +"@medusajs/modules-sdk": patch +--- + +Load custom modules config diff --git a/packages/modules-sdk/src/loaders/__tests__/register-modules.ts b/packages/modules-sdk/src/loaders/__tests__/register-modules.ts index a0d61bfc6f..0eec63aacc 100644 --- a/packages/modules-sdk/src/loaders/__tests__/register-modules.ts +++ b/packages/modules-sdk/src/loaders/__tests__/register-modules.ts @@ -55,6 +55,32 @@ describe("module definitions loader", () => { ) }) + it("Resolves a custom module without pre-defined definition", () => { + const res = registerMedusaModule("customModulesABC", { + options: { + test: 123, + }, + }) + + expect(res).toEqual({ + customModulesABC: expect.objectContaining({ + resolutionPath: "@medusajs/test-service-resolved", + definition: expect.objectContaining({ + key: "customModulesABC", + label: "Custom: customModulesABC", + registrationName: "customModulesABC", + }), + moduleDeclaration: { + resources: "shared", + scope: "internal", + }, + options: { + test: 123, + }, + }), + }) + }) + describe("boolean config", () => { it("Resolves module with no resolution path when given false", () => { Object.assign(ModulesDefinition, { diff --git a/packages/modules-sdk/src/loaders/register-modules.ts b/packages/modules-sdk/src/loaders/register-modules.ts index dee02d61bb..7dfaf6a273 100644 --- a/packages/modules-sdk/src/loaders/register-modules.ts +++ b/packages/modules-sdk/src/loaders/register-modules.ts @@ -8,7 +8,7 @@ import { ModuleResolution, } from "@medusajs/types" -import { isObject } from "@medusajs/utils" +import { isObject, isString } from "@medusajs/utils" import resolveCwd from "resolve-cwd" import { ModulesDefinition } from "../definitions" @@ -25,14 +25,6 @@ export const registerMedusaModule = ( const modDefinition = definition ?? ModulesDefinition[moduleKey] - if (modDefinition === undefined) { - moduleResolutions[moduleKey] = getCustomModuleResolution( - moduleKey, - moduleDeclaration as InternalModuleDeclaration - ) - return moduleResolutions - } - const modDeclaration = moduleDeclaration ?? (modDefinition?.defaultModuleDeclaration as InternalModuleDeclaration) @@ -49,6 +41,14 @@ export const registerMedusaModule = ( throw new Error("External Modules are not supported yet.") } + if (modDefinition === undefined) { + moduleResolutions[moduleKey] = getCustomModuleResolution( + moduleKey, + moduleDeclaration as InternalModuleDeclaration + ) + return moduleResolutions + } + moduleResolutions[moduleKey] = getInternalModuleResolution( modDefinition, moduleDeclaration as InternalModuleDeclaration, @@ -62,11 +62,16 @@ function getCustomModuleResolution( key: string, moduleConfig: InternalModuleDeclaration | string ): ModuleResolution { - const isString = typeof moduleConfig === "string" const resolutionPath = resolveCwd( - isString ? moduleConfig : (moduleConfig.resolve as string) + isString(moduleConfig) ? moduleConfig : (moduleConfig.resolve as string) ) + const conf = isObject(moduleConfig) + ? moduleConfig + : ({} as InternalModuleDeclaration) + + const dependencies = conf?.dependencies ?? [] + return { resolutionPath, definition: { @@ -74,7 +79,7 @@ function getCustomModuleResolution( label: `Custom: ${key}`, isRequired: false, defaultPackage: "", - dependencies: [], + dependencies, registrationName: key, defaultModuleDeclaration: { resources: MODULE_RESOURCE_TYPE.SHARED, @@ -82,11 +87,11 @@ function getCustomModuleResolution( }, }, moduleDeclaration: { - resources: MODULE_RESOURCE_TYPE.SHARED, + resources: conf?.resources ?? MODULE_RESOURCE_TYPE.SHARED, scope: MODULE_SCOPE.INTERNAL, }, - dependencies: [], - options: {}, + dependencies, + options: conf?.options ?? {}, } } @@ -126,14 +131,14 @@ function getInternalModuleResolution( } } - const isObj = typeof moduleConfig === "object" + const isObj = isObject(moduleConfig) let resolutionPath = definition.defaultPackage // If user added a module and it's overridable, we resolve that instead - const isString = typeof moduleConfig === "string" - if (isString || (isObj && moduleConfig.resolve)) { + const isStr = isString(moduleConfig) + if (isStr || (isObj && moduleConfig.resolve)) { resolutionPath = !moduleExports - ? resolveCwd(isString ? moduleConfig : (moduleConfig.resolve as string)) + ? resolveCwd(isStr ? moduleConfig : (moduleConfig.resolve as string)) : // Explicitly assign an empty string, later, we will check if the value is exactly false. // This allows to continue the module loading while using the module exports instead of re importing the module itself during the process. ""