From 012a624ee4ced0bb68a5193e222461f61950f220 Mon Sep 17 00:00:00 2001 From: Stevche Radevski Date: Wed, 3 Jul 2024 16:57:12 +0200 Subject: [PATCH] feat: Flatten the provider config for all modules (#7930) --- integration-tests/api/medusa-config.js | 27 ++----- integration-tests/http/medusa-config.js | 13 ++-- integration-tests/modules/medusa-config.js | 21 ++---- .../__tests__/module-provider-loader.ts | 4 ++ .../src/loaders/module-provider-loader.ts | 8 ++- .../types/src/modules-sdk/module-provider.ts | 4 +- .../common/__tests__/define-config.spec.ts | 72 ++++--------------- .../core/utils/src/common/define-config.ts | 18 +---- .../auth-module-service/index.spec.ts | 6 +- .../modules/auth/src/loaders/providers.ts | 35 ++++----- packages/modules/auth/src/types/index.ts | 14 ++-- .../__tests__/module.spec.ts | 6 +- .../modules/file/src/loaders/providers.ts | 25 +++---- packages/modules/file/src/types/index.ts | 14 ++-- .../fulfillment.spec.ts | 6 +- .../fulfillment-module-service/index.spec.ts | 42 ++++------- .../shipping-option.spec.ts | 6 +- .../fulfillment/src/loaders/providers.ts | 23 +++--- .../modules/fulfillment/src/types/index.ts | 14 ++-- .../notification-module-service/index.spec.ts | 9 +-- .../notification/src/loaders/providers.ts | 47 +++++------- .../modules/notification/src/types/index.ts | 14 ++-- .../modules/payment/src/loaders/providers.ts | 18 +++-- packages/modules/payment/src/types/index.ts | 14 ++-- 24 files changed, 168 insertions(+), 292 deletions(-) diff --git a/integration-tests/api/medusa-config.js b/integration-tests/api/medusa-config.js index 738f8534a7..d22ed9574d 100644 --- a/integration-tests/api/medusa-config.js +++ b/integration-tests/api/medusa-config.js @@ -14,20 +14,12 @@ const customPaymentProvider = { resolve: { services: [require("@medusajs/payment/dist/providers/system").default], }, - options: { - config: { - default_2: {}, - }, - }, + id: "default_2", } const customFulfillmentProvider = { resolve: "@medusajs/fulfillment-manual", - options: { - config: { - "test-provider": {}, - }, - }, + id: "test-provider", } module.exports = { @@ -74,11 +66,7 @@ module.exports = { providers: [ { resolve: "@medusajs/file-local-next", - options: { - config: { - local: {}, - }, - }, + id: "local", }, ], }, @@ -116,13 +104,10 @@ module.exports = { providers: [ { resolve: "@medusajs/notification-local", + id: "local-notification-provider", options: { - config: { - "local-notification-provider": { - name: "Local Notification Provider", - channels: ["log", "email"], - }, - }, + name: "Local Notification Provider", + channels: ["log", "email"], }, }, ], diff --git a/integration-tests/http/medusa-config.js b/integration-tests/http/medusa-config.js index 56c5bf92ec..c0d5916cbd 100644 --- a/integration-tests/http/medusa-config.js +++ b/integration-tests/http/medusa-config.js @@ -12,11 +12,7 @@ process.env.LOG_LEVEL = "error" const customFulfillmentProvider = { resolve: "@medusajs/fulfillment-manual", - options: { - config: { - "test-provider": {}, - }, - }, + id: "test-provider", } module.exports = defineConfig({ @@ -41,11 +37,10 @@ module.exports = defineConfig({ providers: [ { resolve: "@medusajs/file-local-next", + id: "local", options: { - config: { - // This is the directory where we can reliably write in CI environments - local: { upload_dir: path.join(os.tmpdir(), "uploads") }, - }, + // This is the directory where we can reliably write in CI environments + upload_dir: path.join(os.tmpdir(), "uploads"), }, }, ], diff --git a/integration-tests/modules/medusa-config.js b/integration-tests/modules/medusa-config.js index 1227e1720b..5240705613 100644 --- a/integration-tests/modules/medusa-config.js +++ b/integration-tests/modules/medusa-config.js @@ -14,20 +14,12 @@ const customPaymentProvider = { resolve: { services: [require("@medusajs/payment/dist/providers/system").default], }, - options: { - config: { - default_2: {}, - }, - }, + id: "default_2", } const customFulfillmentProvider = { resolve: "@medusajs/fulfillment-manual", - options: { - config: { - "test-provider": {}, - }, - }, + id: "test-provider", } module.exports = { @@ -101,13 +93,10 @@ module.exports = { providers: [ { resolve: "@medusajs/notification-local", + id: "local-notification-provider", options: { - config: { - "local-notification-provider": { - name: "Local Notification Provider", - channels: ["log", "email"], - }, - }, + name: "Local Notification Provider", + channels: ["log", "email"], }, }, ], diff --git a/packages/core/modules-sdk/src/loaders/__tests__/module-provider-loader.ts b/packages/core/modules-sdk/src/loaders/__tests__/module-provider-loader.ts index cc383af882..497697b787 100644 --- a/packages/core/modules-sdk/src/loaders/__tests__/module-provider-loader.ts +++ b/packages/core/modules-sdk/src/loaders/__tests__/module-provider-loader.ts @@ -22,6 +22,7 @@ describe("modules loader", () => { const moduleProviders = [ { resolve: "@providers/default", + id: "default", options: {}, }, ] @@ -47,6 +48,7 @@ describe("modules loader", () => { const moduleProviders = [ { resolve: "@providers/default", + id: "default", options: {}, }, ] @@ -66,6 +68,7 @@ describe("modules loader", () => { const moduleProviders = [ { resolve: "@providers/no-service", + id: "default", options: {}, }, ] @@ -83,6 +86,7 @@ describe("modules loader", () => { const moduleProviders = [ { resolve: "@providers/no-default", + id: "default", options: {}, }, ] diff --git a/packages/core/modules-sdk/src/loaders/module-provider-loader.ts b/packages/core/modules-sdk/src/loaders/module-provider-loader.ts index 373c96bb72..b966541eee 100644 --- a/packages/core/modules-sdk/src/loaders/module-provider-loader.ts +++ b/packages/core/modules-sdk/src/loaders/module-provider-loader.ts @@ -32,8 +32,7 @@ export async function loadModuleProvider( registerServiceFn?: (klass, container, moduleDetails) => Promise ) { let loadedProvider: any - - const moduleName = provider.resolve ?? provider.provider_name ?? "" + const moduleName = provider.resolve ?? "" try { loadedProvider = provider.resolve @@ -60,7 +59,10 @@ export async function loadModuleProvider( const name = lowerCaseFirst(service.name) if (registerServiceFn) { // Used to register the specific type of service in the provider - await registerServiceFn(service, container, provider.options) + await registerServiceFn(service, container, { + id: provider.id, + options: provider.options, + }) } else { container.register({ [name]: asFunction( diff --git a/packages/core/types/src/modules-sdk/module-provider.ts b/packages/core/types/src/modules-sdk/module-provider.ts index 5a418c14b3..1eff6ab132 100644 --- a/packages/core/types/src/modules-sdk/module-provider.ts +++ b/packages/core/types/src/modules-sdk/module-provider.ts @@ -6,6 +6,6 @@ export type ModuleProviderExports = { export type ModuleProvider = { resolve: string | ModuleProviderExports - provider_name?: string - options: Record + id: string + options?: Record } diff --git a/packages/core/utils/src/common/__tests__/define-config.spec.ts b/packages/core/utils/src/common/__tests__/define-config.spec.ts index 53a3ad94b2..f312922edf 100644 --- a/packages/core/utils/src/common/__tests__/define-config.spec.ts +++ b/packages/core/utils/src/common/__tests__/define-config.spec.ts @@ -21,11 +21,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/file-local-next", }, ], @@ -36,11 +32,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "manual": {}, - }, - }, + "id": "manual", "resolve": "@medusajs/fulfillment-manual", }, ], @@ -52,11 +44,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/notification-local", }, ], @@ -123,11 +111,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/file-local-next", }, ], @@ -138,11 +122,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "manual": {}, - }, - }, + "id": "manual", "resolve": "@medusajs/fulfillment-manual", }, ], @@ -157,11 +137,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/notification-local", }, ], @@ -228,11 +204,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/file-local-next", }, ], @@ -243,11 +215,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "manual": {}, - }, - }, + "id": "manual", "resolve": "@medusajs/fulfillment-manual", }, ], @@ -259,11 +227,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/notification-local", }, ], @@ -332,11 +296,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/file-local-next", }, ], @@ -347,11 +307,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "manual": {}, - }, - }, + "id": "manual", "resolve": "@medusajs/fulfillment-manual", }, ], @@ -363,11 +319,7 @@ describe("defineConfig", function () { "options": { "providers": [ { - "options": { - "config": { - "local": {}, - }, - }, + "id": "local", "resolve": "@medusajs/notification-local", }, ], diff --git a/packages/core/utils/src/common/define-config.ts b/packages/core/utils/src/common/define-config.ts index 55b9524ad8..87123a94c3 100644 --- a/packages/core/utils/src/common/define-config.ts +++ b/packages/core/utils/src/common/define-config.ts @@ -90,11 +90,7 @@ export function defineConfig(config: Partial = {}): ConfigModule { providers: [ { resolve: "@medusajs/file-local-next", - options: { - config: { - local: {}, - }, - }, + id: "local", }, ], }, @@ -105,11 +101,7 @@ export function defineConfig(config: Partial = {}): ConfigModule { providers: [ { resolve: "@medusajs/fulfillment-manual", - options: { - config: { - manual: {}, - }, - }, + id: "manual", }, ], }, @@ -120,11 +112,7 @@ export function defineConfig(config: Partial = {}): ConfigModule { providers: [ { resolve: "@medusajs/notification-local", - options: { - config: { - local: {}, - }, - }, + id: "local", }, ], }, diff --git a/packages/modules/auth/integration-tests/__tests__/auth-module-service/index.spec.ts b/packages/modules/auth/integration-tests/__tests__/auth-module-service/index.spec.ts index 1bcf63c531..32f3e345f2 100644 --- a/packages/modules/auth/integration-tests/__tests__/auth-module-service/index.spec.ts +++ b/packages/modules/auth/integration-tests/__tests__/auth-module-service/index.spec.ts @@ -10,11 +10,7 @@ let moduleOptions = { process.cwd() + "/integration-tests/__fixtures__/providers/default-provider" ), - options: { - config: { - plaintextpass: {}, - }, - }, + id: "plaintextpass", }, ], } diff --git a/packages/modules/auth/src/loaders/providers.ts b/packages/modules/auth/src/loaders/providers.ts index 28cbdde3d2..83fb613f9e 100644 --- a/packages/modules/auth/src/loaders/providers.ts +++ b/packages/modules/auth/src/loaders/providers.ts @@ -9,18 +9,19 @@ import { } from "@types" const registrationFn = async (klass, container, pluginOptions) => { - Object.entries(pluginOptions.config || []).map(([name, config]) => { - container.register({ - [AuthProviderRegistrationPrefix + name]: asFunction( - (cradle) => new klass(cradle, config), - { - lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, - } - ), - }) - - container.registerAdd(AuthIdentifiersRegistrationName, asValue(name)) + container.register({ + [AuthProviderRegistrationPrefix + pluginOptions.id]: asFunction( + (cradle) => new klass(cradle, pluginOptions.options ?? {}), + { + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, + } + ), }) + + container.registerAdd( + AuthIdentifiersRegistrationName, + asValue(pluginOptions.id) + ) } export default async ({ @@ -32,21 +33,13 @@ export default async ({ | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions ) & { providers: ModuleProvider[] } >): Promise => { - // TODO: Temporary settings used by the starter, remove once the auth module is updated - const isLegacyOptions = - options?.providers?.length && !!(options?.providers[0] as any)?.name - // Note: For now we want to inject some providers out of the box const providerConfig = [ { resolve: EmailPassProvider, - options: { - config: { - emailpass: {}, - }, - }, + id: "emailpass", }, - ...(isLegacyOptions ? [] : options?.providers ?? []), + ...(options?.providers ?? []), ] await moduleProviderLoader({ diff --git a/packages/modules/auth/src/types/index.ts b/packages/modules/auth/src/types/index.ts index 4481d309c2..717b235867 100644 --- a/packages/modules/auth/src/types/index.ts +++ b/packages/modules/auth/src/types/index.ts @@ -19,11 +19,13 @@ export type AuthModuleOptions = Partial & { * The module provider to be registered */ resolve: string | ModuleProviderExports - options: { - /** - * key value pair of the provider name and the configuration to be passed to the provider constructor - */ - config: Record - } + /** + * The id of the provider + */ + id: string + /** + * key value pair of the configuration to be passed to the provider constructor + */ + options?: Record }[] } diff --git a/packages/modules/file/integration-tests/__tests__/module.spec.ts b/packages/modules/file/integration-tests/__tests__/module.spec.ts index 401118a59e..4620ace720 100644 --- a/packages/modules/file/integration-tests/__tests__/module.spec.ts +++ b/packages/modules/file/integration-tests/__tests__/module.spec.ts @@ -20,11 +20,7 @@ const moduleOptions = { process.cwd() + "/integration-tests/__fixtures__/providers/default-provider" ), - options: { - config: { - "default-provider": {}, - }, - }, + id: "default-provider", }, ], } diff --git a/packages/modules/file/src/loaders/providers.ts b/packages/modules/file/src/loaders/providers.ts index 926eafa174..68139366b9 100644 --- a/packages/modules/file/src/loaders/providers.ts +++ b/packages/modules/file/src/loaders/providers.ts @@ -8,20 +8,21 @@ import { import { Lifetime, asFunction, asValue } from "awilix" const registrationFn = async (klass, container, pluginOptions) => { - Object.entries(pluginOptions.config || []).map(([name, config]) => { - const key = FileProviderService.getRegistrationIdentifier(klass, name) + const key = FileProviderService.getRegistrationIdentifier( + klass, + pluginOptions.id + ) - container.register({ - [FileProviderRegistrationPrefix + key]: asFunction( - (cradle) => new klass(cradle, config), - { - lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, - } - ), - }) - - container.registerAdd(FileProviderIdentifierRegistrationName, asValue(key)) + container.register({ + [FileProviderRegistrationPrefix + key]: asFunction( + (cradle) => new klass(cradle, pluginOptions.options ?? {}), + { + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, + } + ), }) + + container.registerAdd(FileProviderIdentifierRegistrationName, asValue(key)) } export default async ({ diff --git a/packages/modules/file/src/types/index.ts b/packages/modules/file/src/types/index.ts index 2f215aba19..f4344c4afa 100644 --- a/packages/modules/file/src/types/index.ts +++ b/packages/modules/file/src/types/index.ts @@ -17,11 +17,13 @@ export type FileModuleOptions = Partial & { * The module provider to be registered */ resolve: string | ModuleProviderExports - options: { - /** - * key value pair of the provider name and the configuration to be passed to the provider constructor - */ - config: Record - } + /** + * The id of the provider + */ + id: string + /** + * key value pair of the configuration to be passed to the provider constructor + */ + options?: Record } } diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts index aed3fc0ca0..4219b6aa07 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts @@ -23,11 +23,7 @@ const moduleOptions = { process.cwd() + "/integration-tests/__fixtures__/providers/default-provider" ), - options: { - config: { - "test-provider": {}, - }, - }, + id: "test-provider", }, ], } diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/index.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/index.spec.ts index fcb6529a8f..30f517825e 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/index.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/index.spec.ts @@ -18,11 +18,7 @@ let moduleOptions = { process.cwd() + "/integration-tests/__fixtures__/providers/default-provider" ), - options: { - config: { - "test-provider": {}, - }, - }, + id: "test-provider", }, ], } @@ -125,17 +121,13 @@ moduleIntegrationTestRunner({ definition: ModulesDefinition[Modules.FULFILLMENT], options: { databaseConfig, - providers: [ - { - resolve: resolve( - process.cwd() + - "/integration-tests/__fixtures__/providers/default-provider" - ), - options: { - config: providersConfig, - }, - }, - ], + providers: Object.keys(providersConfig).map((id) => ({ + resolve: resolve( + process.cwd() + + "/integration-tests/__fixtures__/providers/default-provider" + ), + id, + })), }, }, }, @@ -179,17 +171,13 @@ moduleIntegrationTestRunner({ definition: ModulesDefinition[Modules.FULFILLMENT], options: { databaseConfig, - providers: [ - { - resolve: resolve( - process.cwd() + - "/integration-tests/__fixtures__/providers/default-provider" - ), - options: { - config: providersConfig2, - }, - }, - ], + providers: Object.keys(providersConfig2).map((id) => ({ + resolve: resolve( + process.cwd() + + "/integration-tests/__fixtures__/providers/default-provider" + ), + id, + })), }, }, }, diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts index ecce3cfdcf..27a2e3af0e 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts @@ -25,11 +25,7 @@ const moduleOptions = { process.cwd() + "/integration-tests/__fixtures__/providers/default-provider" ), - options: { - config: { - "test-provider": {}, - }, - }, + id: "test-provider", }, ], } diff --git a/packages/modules/fulfillment/src/loaders/providers.ts b/packages/modules/fulfillment/src/loaders/providers.ts index c844926881..fc6b87b00c 100644 --- a/packages/modules/fulfillment/src/loaders/providers.ts +++ b/packages/modules/fulfillment/src/loaders/providers.ts @@ -10,20 +10,21 @@ import { FulfillmentIdentifiersRegistrationName } from "@types" import { Lifetime, asFunction, asValue } from "awilix" const registrationFn = async (klass, container, pluginOptions) => { - Object.entries(pluginOptions.config || []).map(([name, config]) => { - const key = FulfillmentProviderService.getRegistrationIdentifier( - klass, - name - ) + const key = FulfillmentProviderService.getRegistrationIdentifier( + klass, + pluginOptions.id + ) - container.register({ - ["fp_" + key]: asFunction((cradle) => new klass(cradle, config), { + container.register({ + ["fp_" + key]: asFunction( + (cradle) => new klass(cradle, pluginOptions.options), + { lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, - }), - }) - - container.registerAdd(FulfillmentIdentifiersRegistrationName, asValue(key)) + } + ), }) + + container.registerAdd(FulfillmentIdentifiersRegistrationName, asValue(key)) } export default async ({ diff --git a/packages/modules/fulfillment/src/types/index.ts b/packages/modules/fulfillment/src/types/index.ts index c80d690b5a..88abd95f77 100644 --- a/packages/modules/fulfillment/src/types/index.ts +++ b/packages/modules/fulfillment/src/types/index.ts @@ -23,11 +23,13 @@ export type FulfillmentModuleOptions = * The module provider to be registered */ resolve: string | ModuleProviderExports - options: { - /** - * key value pair of the provider name and the configuration to be passed to the provider constructor - */ - config: Record - } + /** + * The id of the provider + */ + id: string + /** + * key value pair of the configuration to be passed to the provider constructor + */ + options?: Record }[] } diff --git a/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts b/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts index 60ef81287f..13b6d76ae0 100644 --- a/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts +++ b/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts @@ -10,13 +10,10 @@ let moduleOptions = { process.cwd() + "/integration-tests/__fixtures__/providers/default-provider" ), + id: "test-provider", options: { - config: { - "test-provider": { - name: "Test provider", - channels: ["email"], - }, - }, + name: "Test provider", + channels: ["email"], }, }, ], diff --git a/packages/modules/notification/src/loaders/providers.ts b/packages/modules/notification/src/loaders/providers.ts index 3c9adb56d0..3b743c9e05 100644 --- a/packages/modules/notification/src/loaders/providers.ts +++ b/packages/modules/notification/src/loaders/providers.ts @@ -1,11 +1,5 @@ import { moduleProviderLoader } from "@medusajs/modules-sdk" -import { - DAL, - InferEntityType, - LoaderOptions, - ModuleProvider, - ModulesSdkTypes, -} from "@medusajs/types" +import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@medusajs/types" import { ContainerRegistrationKeys, lowerCaseFirst, @@ -20,21 +14,19 @@ import { import { Lifetime, asFunction, asValue } from "awilix" const registrationFn = async (klass, container, pluginOptions) => { - Object.entries(pluginOptions.config || []).map(([name, config]) => { - container.register({ - [NotificationProviderRegistrationPrefix + name]: asFunction( - (cradle) => new klass(cradle, config), - { - lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, - } - ), - }) - - container.registerAdd( - NotificationIdentifiersRegistrationName, - asValue(name) - ) + container.register({ + [NotificationProviderRegistrationPrefix + pluginOptions.id]: asFunction( + (cradle) => new klass(cradle, pluginOptions.options ?? {}), + { + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, + } + ), }) + + container.registerAdd( + NotificationIdentifiersRegistrationName, + asValue(pluginOptions.id) + ) } export default async ({ @@ -74,20 +66,17 @@ async function syncDatabaseProviders({ const logger = container.resolve(ContainerRegistrationKeys.LOGGER) ?? console const normalizedProviders = providers.map((provider) => { - const [name, config] = Object.entries( - provider.options?.config as any - )?.[0] as any - if (!name) { + if (!provider.id) { throw new Error( "An entry in the provider config is required to initialize notification providers" ) } - const id = name + const config = provider.options as { channels: string[] } return { - id, - handle: name, - name: config?.name ?? name, + id: provider.id, + handle: provider.id, + name: provider.id, is_enabled: true, channels: config?.channels ?? [], } diff --git a/packages/modules/notification/src/types/index.ts b/packages/modules/notification/src/types/index.ts index 6d05e33dda..3b8e900bbd 100644 --- a/packages/modules/notification/src/types/index.ts +++ b/packages/modules/notification/src/types/index.ts @@ -23,11 +23,13 @@ export type NotificationModuleOptions = * The module provider to be registered */ resolve: string | ModuleProviderExports - options: { - /** - * key value pair of the provider name and the configuration to be passed to the provider constructor - */ - config: Record - } + /** + * The id of the provider + */ + id: string + /** + * key value pair of the configuration to be passed to the provider constructor + */ + options?: Record }[] } diff --git a/packages/modules/payment/src/loaders/providers.ts b/packages/modules/payment/src/loaders/providers.ts index 2416385965..313804f221 100644 --- a/packages/modules/payment/src/loaders/providers.ts +++ b/packages/modules/payment/src/loaders/providers.ts @@ -11,17 +11,15 @@ import * as providers from "../providers" import { PaymentProviderService } from "@services" const registrationFn = async (klass, container, pluginOptions) => { - Object.entries(pluginOptions.config || []).map(([name, config]) => { - const key = `pp_${klass.PROVIDER}_${name}` + const key = `pp_${klass.PROVIDER}_${pluginOptions.id}` - container.register({ - [key]: asFunction((cradle) => new klass(cradle, config), { - lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, - }), - }) - - container.registerAdd("payment_providers", asValue(key)) + container.register({ + [key]: asFunction((cradle) => new klass(cradle, pluginOptions.options), { + lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, + }), }) + + container.registerAdd("payment_providers", asValue(key)) } export default async ({ @@ -35,7 +33,7 @@ export default async ({ >): Promise => { // Local providers for (const provider of Object.values(providers)) { - await registrationFn(provider, container, { config: { default: {} } }) + await registrationFn(provider, container, { id: "default" }) } await moduleProviderLoader({ diff --git a/packages/modules/payment/src/types/index.ts b/packages/modules/payment/src/types/index.ts index 5e299a7aff..bee03db2db 100644 --- a/packages/modules/payment/src/types/index.ts +++ b/packages/modules/payment/src/types/index.ts @@ -17,11 +17,13 @@ export type PaymentModuleOptions = Partial & { * The module provider to be registered */ resolve: string | ModuleProviderExports - options: { - /** - * key value pair of the provider name and the configuration to be passed to the provider constructor - */ - config: Record - } + /** + * The id of the provider + */ + id: string + /** + * key value pair of the configuration to be passed to the provider constructor + */ + options?: Record }[] }