feat: Flatten the provider config for all modules (#7930)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-07-03 16:57:12 +02:00
1 parent b6e4435c23
commit 012a624ee4
24 files changed
+168 -292

No files matched your search

+6 -21
View File
@@ -14,20 +14,12 @@ const customPaymentProvider = {
resolve: { resolve: {
services: [require("@medusajs/payment/dist/providers/system").default], services: [require("@medusajs/payment/dist/providers/system").default],
}, },
options: { id: "default_2",
config: {
default_2: {},
},
},
} }
const customFulfillmentProvider = { const customFulfillmentProvider = {
resolve: "@medusajs/fulfillment-manual", resolve: "@medusajs/fulfillment-manual",
options: { id: "test-provider",
config: {
"test-provider": {},
},
},
} }
module.exports = { module.exports = {
@@ -74,11 +66,7 @@ module.exports = {
providers: [ providers: [
{ {
resolve: "@medusajs/file-local-next", resolve: "@medusajs/file-local-next",
options: { id: "local",
config: {
local: {},
},
},
}, },
], ],
}, },
@@ -116,13 +104,10 @@ module.exports = {
providers: [ providers: [
{ {
resolve: "@medusajs/notification-local", resolve: "@medusajs/notification-local",
id: "local-notification-provider",
options: { options: {
config: { name: "Local Notification Provider",
"local-notification-provider": { channels: ["log", "email"],
name: "Local Notification Provider",
channels: ["log", "email"],
},
},
}, },
}, },
], ],
+4 -9
View File
@@ -12,11 +12,7 @@ process.env.LOG_LEVEL = "error"
const customFulfillmentProvider = { const customFulfillmentProvider = {
resolve: "@medusajs/fulfillment-manual", resolve: "@medusajs/fulfillment-manual",
options: { id: "test-provider",
config: {
"test-provider": {},
},
},
} }
module.exports = defineConfig({ module.exports = defineConfig({
@@ -41,11 +37,10 @@ module.exports = defineConfig({
providers: [ providers: [
{ {
resolve: "@medusajs/file-local-next", resolve: "@medusajs/file-local-next",
id: "local",
options: { options: {
config: { // This is the directory where we can reliably write in CI environments
// This is the directory where we can reliably write in CI environments upload_dir: path.join(os.tmpdir(), "uploads"),
local: { upload_dir: path.join(os.tmpdir(), "uploads") },
},
}, },
}, },
], ],
+5 -16
View File
@@ -14,20 +14,12 @@ const customPaymentProvider = {
resolve: { resolve: {
services: [require("@medusajs/payment/dist/providers/system").default], services: [require("@medusajs/payment/dist/providers/system").default],
}, },
options: { id: "default_2",
config: {
default_2: {},
},
},
} }
const customFulfillmentProvider = { const customFulfillmentProvider = {
resolve: "@medusajs/fulfillment-manual", resolve: "@medusajs/fulfillment-manual",
options: { id: "test-provider",
config: {
"test-provider": {},
},
},
} }
module.exports = { module.exports = {
@@ -101,13 +93,10 @@ module.exports = {
providers: [ providers: [
{ {
resolve: "@medusajs/notification-local", resolve: "@medusajs/notification-local",
id: "local-notification-provider",
options: { options: {
config: { name: "Local Notification Provider",
"local-notification-provider": { channels: ["log", "email"],
name: "Local Notification Provider",
channels: ["log", "email"],
},
},
}, },
}, },
], ],
@@ -22,6 +22,7 @@ describe("modules loader", () => {
const moduleProviders = [ const moduleProviders = [
{ {
resolve: "@providers/default", resolve: "@providers/default",
id: "default",
options: {}, options: {},
}, },
] ]
@@ -47,6 +48,7 @@ describe("modules loader", () => {
const moduleProviders = [ const moduleProviders = [
{ {
resolve: "@providers/default", resolve: "@providers/default",
id: "default",
options: {}, options: {},
}, },
] ]
@@ -66,6 +68,7 @@ describe("modules loader", () => {
const moduleProviders = [ const moduleProviders = [
{ {
resolve: "@providers/no-service", resolve: "@providers/no-service",
id: "default",
options: {}, options: {},
}, },
] ]
@@ -83,6 +86,7 @@ describe("modules loader", () => {
const moduleProviders = [ const moduleProviders = [
{ {
resolve: "@providers/no-default", resolve: "@providers/no-default",
id: "default",
options: {}, options: {},
}, },
] ]
@@ -32,8 +32,7 @@ export async function loadModuleProvider(
registerServiceFn?: (klass, container, moduleDetails) => Promise<void> registerServiceFn?: (klass, container, moduleDetails) => Promise<void>
) { ) {
let loadedProvider: any let loadedProvider: any
const moduleName = provider.resolve ?? ""
const moduleName = provider.resolve ?? provider.provider_name ?? ""
try { try {
loadedProvider = provider.resolve loadedProvider = provider.resolve
@@ -60,7 +59,10 @@ export async function loadModuleProvider(
const name = lowerCaseFirst(service.name) const name = lowerCaseFirst(service.name)
if (registerServiceFn) { if (registerServiceFn) {
// Used to register the specific type of service in the provider // 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 { } else {
container.register({ container.register({
[name]: asFunction( [name]: asFunction(
@@ -6,6 +6,6 @@ export type ModuleProviderExports = {
export type ModuleProvider = { export type ModuleProvider = {
resolve: string | ModuleProviderExports resolve: string | ModuleProviderExports
provider_name?: string id: string
options: Record<string, unknown> options?: Record<string, unknown>
} }
@@ -21,11 +21,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/file-local-next", "resolve": "@medusajs/file-local-next",
}, },
], ],
@@ -36,11 +32,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "manual",
"config": {
"manual": {},
},
},
"resolve": "@medusajs/fulfillment-manual", "resolve": "@medusajs/fulfillment-manual",
}, },
], ],
@@ -52,11 +44,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/notification-local", "resolve": "@medusajs/notification-local",
}, },
], ],
@@ -123,11 +111,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/file-local-next", "resolve": "@medusajs/file-local-next",
}, },
], ],
@@ -138,11 +122,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "manual",
"config": {
"manual": {},
},
},
"resolve": "@medusajs/fulfillment-manual", "resolve": "@medusajs/fulfillment-manual",
}, },
], ],
@@ -157,11 +137,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/notification-local", "resolve": "@medusajs/notification-local",
}, },
], ],
@@ -228,11 +204,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/file-local-next", "resolve": "@medusajs/file-local-next",
}, },
], ],
@@ -243,11 +215,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "manual",
"config": {
"manual": {},
},
},
"resolve": "@medusajs/fulfillment-manual", "resolve": "@medusajs/fulfillment-manual",
}, },
], ],
@@ -259,11 +227,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/notification-local", "resolve": "@medusajs/notification-local",
}, },
], ],
@@ -332,11 +296,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/file-local-next", "resolve": "@medusajs/file-local-next",
}, },
], ],
@@ -347,11 +307,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "manual",
"config": {
"manual": {},
},
},
"resolve": "@medusajs/fulfillment-manual", "resolve": "@medusajs/fulfillment-manual",
}, },
], ],
@@ -363,11 +319,7 @@ describe("defineConfig", function () {
"options": { "options": {
"providers": [ "providers": [
{ {
"options": { "id": "local",
"config": {
"local": {},
},
},
"resolve": "@medusajs/notification-local", "resolve": "@medusajs/notification-local",
}, },
], ],
@@ -90,11 +90,7 @@ export function defineConfig(config: Partial<ConfigModule> = {}): ConfigModule {
providers: [ providers: [
{ {
resolve: "@medusajs/file-local-next", resolve: "@medusajs/file-local-next",
options: { id: "local",
config: {
local: {},
},
},
}, },
], ],
}, },
@@ -105,11 +101,7 @@ export function defineConfig(config: Partial<ConfigModule> = {}): ConfigModule {
providers: [ providers: [
{ {
resolve: "@medusajs/fulfillment-manual", resolve: "@medusajs/fulfillment-manual",
options: { id: "manual",
config: {
manual: {},
},
},
}, },
], ],
}, },
@@ -120,11 +112,7 @@ export function defineConfig(config: Partial<ConfigModule> = {}): ConfigModule {
providers: [ providers: [
{ {
resolve: "@medusajs/notification-local", resolve: "@medusajs/notification-local",
options: { id: "local",
config: {
local: {},
},
},
}, },
], ],
}, },
@@ -10,11 +10,7 @@ let moduleOptions = {
process.cwd() + process.cwd() +
"/integration-tests/__fixtures__/providers/default-provider" "/integration-tests/__fixtures__/providers/default-provider"
), ),
options: { id: "plaintextpass",
config: {
plaintextpass: {},
},
},
}, },
], ],
} }
+14 -21
View File
@@ -9,18 +9,19 @@ import {
} from "@types" } from "@types"
const registrationFn = async (klass, container, pluginOptions) => { const registrationFn = async (klass, container, pluginOptions) => {
Object.entries(pluginOptions.config || []).map(([name, config]) => { container.register({
container.register({ [AuthProviderRegistrationPrefix + pluginOptions.id]: asFunction(
[AuthProviderRegistrationPrefix + name]: asFunction( (cradle) => new klass(cradle, pluginOptions.options ?? {}),
(cradle) => new klass(cradle, config), {
{ lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, }
} ),
),
})
container.registerAdd(AuthIdentifiersRegistrationName, asValue(name))
}) })
container.registerAdd(
AuthIdentifiersRegistrationName,
asValue(pluginOptions.id)
)
} }
export default async ({ export default async ({
@@ -32,21 +33,13 @@ export default async ({
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
) & { providers: ModuleProvider[] } ) & { providers: ModuleProvider[] }
>): Promise<void> => { >): Promise<void> => {
// 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 // Note: For now we want to inject some providers out of the box
const providerConfig = [ const providerConfig = [
{ {
resolve: EmailPassProvider, resolve: EmailPassProvider,
options: { id: "emailpass",
config: {
emailpass: {},
},
},
}, },
...(isLegacyOptions ? [] : options?.providers ?? []), ...(options?.providers ?? []),
] ]
await moduleProviderLoader({ await moduleProviderLoader({
+8 -6
View File
@@ -19,11 +19,13 @@ export type AuthModuleOptions = Partial<ModuleServiceInitializeOptions> & {
* The module provider to be registered * The module provider to be registered
*/ */
resolve: string | ModuleProviderExports resolve: string | ModuleProviderExports
options: { /**
/** * The id of the provider
* key value pair of the provider name and the configuration to be passed to the provider constructor */
*/ id: string
config: Record<string, unknown> /**
} * key value pair of the configuration to be passed to the provider constructor
*/
options?: Record<string, unknown>
}[] }[]
} }
@@ -20,11 +20,7 @@ const moduleOptions = {
process.cwd() + process.cwd() +
"/integration-tests/__fixtures__/providers/default-provider" "/integration-tests/__fixtures__/providers/default-provider"
), ),
options: { id: "default-provider",
config: {
"default-provider": {},
},
},
}, },
], ],
} }
+13 -12
View File
@@ -8,20 +8,21 @@ import {
import { Lifetime, asFunction, asValue } from "awilix" import { Lifetime, asFunction, asValue } from "awilix"
const registrationFn = async (klass, container, pluginOptions) => { const registrationFn = async (klass, container, pluginOptions) => {
Object.entries(pluginOptions.config || []).map(([name, config]) => { const key = FileProviderService.getRegistrationIdentifier(
const key = FileProviderService.getRegistrationIdentifier(klass, name) klass,
pluginOptions.id
)
container.register({ container.register({
[FileProviderRegistrationPrefix + key]: asFunction( [FileProviderRegistrationPrefix + key]: asFunction(
(cradle) => new klass(cradle, config), (cradle) => new klass(cradle, pluginOptions.options ?? {}),
{ {
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
} }
), ),
})
container.registerAdd(FileProviderIdentifierRegistrationName, asValue(key))
}) })
container.registerAdd(FileProviderIdentifierRegistrationName, asValue(key))
} }
export default async ({ export default async ({
+8 -6
View File
@@ -17,11 +17,13 @@ export type FileModuleOptions = Partial<ModuleServiceInitializeOptions> & {
* The module provider to be registered * The module provider to be registered
*/ */
resolve: string | ModuleProviderExports resolve: string | ModuleProviderExports
options: { /**
/** * The id of the provider
* key value pair of the provider name and the configuration to be passed to the provider constructor */
*/ id: string
config: Record<string, unknown> /**
} * key value pair of the configuration to be passed to the provider constructor
*/
options?: Record<string, unknown>
} }
} }
@@ -23,11 +23,7 @@ const moduleOptions = {
process.cwd() + process.cwd() +
"/integration-tests/__fixtures__/providers/default-provider" "/integration-tests/__fixtures__/providers/default-provider"
), ),
options: { id: "test-provider",
config: {
"test-provider": {},
},
},
}, },
], ],
} }
@@ -18,11 +18,7 @@ let moduleOptions = {
process.cwd() + process.cwd() +
"/integration-tests/__fixtures__/providers/default-provider" "/integration-tests/__fixtures__/providers/default-provider"
), ),
options: { id: "test-provider",
config: {
"test-provider": {},
},
},
}, },
], ],
} }
@@ -125,17 +121,13 @@ moduleIntegrationTestRunner({
definition: ModulesDefinition[Modules.FULFILLMENT], definition: ModulesDefinition[Modules.FULFILLMENT],
options: { options: {
databaseConfig, databaseConfig,
providers: [ providers: Object.keys(providersConfig).map((id) => ({
{ resolve: resolve(
resolve: resolve( process.cwd() +
process.cwd() + "/integration-tests/__fixtures__/providers/default-provider"
"/integration-tests/__fixtures__/providers/default-provider" ),
), id,
options: { })),
config: providersConfig,
},
},
],
}, },
}, },
}, },
@@ -179,17 +171,13 @@ moduleIntegrationTestRunner({
definition: ModulesDefinition[Modules.FULFILLMENT], definition: ModulesDefinition[Modules.FULFILLMENT],
options: { options: {
databaseConfig, databaseConfig,
providers: [ providers: Object.keys(providersConfig2).map((id) => ({
{ resolve: resolve(
resolve: resolve( process.cwd() +
process.cwd() + "/integration-tests/__fixtures__/providers/default-provider"
"/integration-tests/__fixtures__/providers/default-provider" ),
), id,
options: { })),
config: providersConfig2,
},
},
],
}, },
}, },
}, },
@@ -25,11 +25,7 @@ const moduleOptions = {
process.cwd() + process.cwd() +
"/integration-tests/__fixtures__/providers/default-provider" "/integration-tests/__fixtures__/providers/default-provider"
), ),
options: { id: "test-provider",
config: {
"test-provider": {},
},
},
}, },
], ],
} }
@@ -10,20 +10,21 @@ import { FulfillmentIdentifiersRegistrationName } from "@types"
import { Lifetime, asFunction, asValue } from "awilix" import { Lifetime, asFunction, asValue } from "awilix"
const registrationFn = async (klass, container, pluginOptions) => { const registrationFn = async (klass, container, pluginOptions) => {
Object.entries(pluginOptions.config || []).map(([name, config]) => { const key = FulfillmentProviderService.getRegistrationIdentifier(
const key = FulfillmentProviderService.getRegistrationIdentifier( klass,
klass, pluginOptions.id
name )
)
container.register({ container.register({
["fp_" + key]: asFunction((cradle) => new klass(cradle, config), { ["fp_" + key]: asFunction(
(cradle) => new klass(cradle, pluginOptions.options),
{
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
}), }
}) ),
container.registerAdd(FulfillmentIdentifiersRegistrationName, asValue(key))
}) })
container.registerAdd(FulfillmentIdentifiersRegistrationName, asValue(key))
} }
export default async ({ export default async ({
@@ -23,11 +23,13 @@ export type FulfillmentModuleOptions =
* The module provider to be registered * The module provider to be registered
*/ */
resolve: string | ModuleProviderExports resolve: string | ModuleProviderExports
options: { /**
/** * The id of the provider
* key value pair of the provider name and the configuration to be passed to the provider constructor */
*/ id: string
config: Record<string, unknown> /**
} * key value pair of the configuration to be passed to the provider constructor
*/
options?: Record<string, unknown>
}[] }[]
} }
@@ -10,13 +10,10 @@ let moduleOptions = {
process.cwd() + process.cwd() +
"/integration-tests/__fixtures__/providers/default-provider" "/integration-tests/__fixtures__/providers/default-provider"
), ),
id: "test-provider",
options: { options: {
config: { name: "Test provider",
"test-provider": { channels: ["email"],
name: "Test provider",
channels: ["email"],
},
},
}, },
}, },
], ],
@@ -1,11 +1,5 @@
import { moduleProviderLoader } from "@medusajs/modules-sdk" import { moduleProviderLoader } from "@medusajs/modules-sdk"
import { import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@medusajs/types"
DAL,
InferEntityType,
LoaderOptions,
ModuleProvider,
ModulesSdkTypes,
} from "@medusajs/types"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
lowerCaseFirst, lowerCaseFirst,
@@ -20,21 +14,19 @@ import {
import { Lifetime, asFunction, asValue } from "awilix" import { Lifetime, asFunction, asValue } from "awilix"
const registrationFn = async (klass, container, pluginOptions) => { const registrationFn = async (klass, container, pluginOptions) => {
Object.entries(pluginOptions.config || []).map(([name, config]) => { container.register({
container.register({ [NotificationProviderRegistrationPrefix + pluginOptions.id]: asFunction(
[NotificationProviderRegistrationPrefix + name]: asFunction( (cradle) => new klass(cradle, pluginOptions.options ?? {}),
(cradle) => new klass(cradle, config), {
{ lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, }
} ),
),
})
container.registerAdd(
NotificationIdentifiersRegistrationName,
asValue(name)
)
}) })
container.registerAdd(
NotificationIdentifiersRegistrationName,
asValue(pluginOptions.id)
)
} }
export default async ({ export default async ({
@@ -74,20 +66,17 @@ async function syncDatabaseProviders({
const logger = container.resolve(ContainerRegistrationKeys.LOGGER) ?? console const logger = container.resolve(ContainerRegistrationKeys.LOGGER) ?? console
const normalizedProviders = providers.map((provider) => { const normalizedProviders = providers.map((provider) => {
const [name, config] = Object.entries( if (!provider.id) {
provider.options?.config as any
)?.[0] as any
if (!name) {
throw new Error( throw new Error(
"An entry in the provider config is required to initialize notification providers" "An entry in the provider config is required to initialize notification providers"
) )
} }
const id = name const config = provider.options as { channels: string[] }
return { return {
id, id: provider.id,
handle: name, handle: provider.id,
name: config?.name ?? name, name: provider.id,
is_enabled: true, is_enabled: true,
channels: config?.channels ?? [], channels: config?.channels ?? [],
} }
@@ -23,11 +23,13 @@ export type NotificationModuleOptions =
* The module provider to be registered * The module provider to be registered
*/ */
resolve: string | ModuleProviderExports resolve: string | ModuleProviderExports
options: { /**
/** * The id of the provider
* key value pair of the provider name and the configuration to be passed to the provider constructor */
*/ id: string
config: Record<string, unknown> /**
} * key value pair of the configuration to be passed to the provider constructor
*/
options?: Record<string, unknown>
}[] }[]
} }
@@ -11,17 +11,15 @@ import * as providers from "../providers"
import { PaymentProviderService } from "@services" import { PaymentProviderService } from "@services"
const registrationFn = async (klass, container, pluginOptions) => { const registrationFn = async (klass, container, pluginOptions) => {
Object.entries(pluginOptions.config || []).map(([name, config]) => { const key = `pp_${klass.PROVIDER}_${pluginOptions.id}`
const key = `pp_${klass.PROVIDER}_${name}`
container.register({ container.register({
[key]: asFunction((cradle) => new klass(cradle, config), { [key]: asFunction((cradle) => new klass(cradle, pluginOptions.options), {
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
}), }),
})
container.registerAdd("payment_providers", asValue(key))
}) })
container.registerAdd("payment_providers", asValue(key))
} }
export default async ({ export default async ({
@@ -35,7 +33,7 @@ export default async ({
>): Promise<void> => { >): Promise<void> => {
// Local providers // Local providers
for (const provider of Object.values(providers)) { for (const provider of Object.values(providers)) {
await registrationFn(provider, container, { config: { default: {} } }) await registrationFn(provider, container, { id: "default" })
} }
await moduleProviderLoader({ await moduleProviderLoader({
+8 -6
View File
@@ -17,11 +17,13 @@ export type PaymentModuleOptions = Partial<ModuleServiceInitializeOptions> & {
* The module provider to be registered * The module provider to be registered
*/ */
resolve: string | ModuleProviderExports resolve: string | ModuleProviderExports
options: { /**
/** * The id of the provider
* key value pair of the provider name and the configuration to be passed to the provider constructor */
*/ id: string
config: Record<string, unknown> /**
} * key value pair of the configuration to be passed to the provider constructor
*/
options?: Record<string, unknown>
}[] }[]
} }