feat(modules-sdk): Module provider plugin loader (#6286)
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { IPaymentModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { initialize } from "../../../../src/initialize"
|
||||
import { DB_URL, MikroOrmWrapper } from "../../../utils"
|
||||
import { createPaymentCollections } from "../../../__fixtures__/payment-collection"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { initialize } from "../../../../src/initialize"
|
||||
import { createPaymentCollections } from "../../../__fixtures__/payment-collection"
|
||||
import { DB_URL, MikroOrmWrapper } from "../../../utils"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
export * from "./connection"
|
||||
export * from "./container"
|
||||
export * from "./providers"
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { moduleProviderLoader } from "@medusajs/modules-sdk"
|
||||
|
||||
import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { Lifetime, asFunction } from "awilix"
|
||||
|
||||
const registrationFn = async (klass, container, pluginOptions) => {
|
||||
container.register({
|
||||
[`payment_provider_${klass.prototype}`]: asFunction(
|
||||
(cradle) => new klass(cradle, pluginOptions),
|
||||
{
|
||||
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
|
||||
}
|
||||
),
|
||||
})
|
||||
|
||||
container.registerAdd(
|
||||
"payment_providers",
|
||||
asFunction((cradle) => new klass(cradle, pluginOptions), {
|
||||
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export default async ({
|
||||
container,
|
||||
options,
|
||||
}: LoaderOptions<
|
||||
(
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
) & { providers: ModuleProvider[] }
|
||||
>): Promise<void> => {
|
||||
const pluginProviders =
|
||||
options?.providers?.filter((provider) => provider.resolve) || []
|
||||
|
||||
await moduleProviderLoader({
|
||||
container,
|
||||
providers: pluginProviders,
|
||||
registerServiceFn: registrationFn,
|
||||
})
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { PaymentModuleService } from "@services"
|
||||
|
||||
import loadConnection from "./loaders/connection"
|
||||
import loadContainer from "./loaders/container"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
@@ -24,7 +25,7 @@ export const revertMigration = ModulesSdkUtils.buildRevertMigrationScript(
|
||||
)
|
||||
|
||||
const service = PaymentModuleService
|
||||
const loaders = [loadContainer, loadConnection] as any
|
||||
const loaders = [loadContainer, loadConnection, loadProviders] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
|
||||
Reference in New Issue
Block a user