diff --git a/.changeset/kind-dogs-argue.md b/.changeset/kind-dogs-argue.md new file mode 100644 index 0000000000..fab700bdac --- /dev/null +++ b/.changeset/kind-dogs-argue.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +feat(medusa): Add Lifetime support on project/plugin services diff --git a/packages/medusa/src/loaders/helpers/plugins.ts b/packages/medusa/src/loaders/helpers/plugins.ts index 539b1d412f..830e2556e8 100644 --- a/packages/medusa/src/loaders/helpers/plugins.ts +++ b/packages/medusa/src/loaders/helpers/plugins.ts @@ -5,7 +5,7 @@ import { isPaymentProcessor, isPaymentService, } from "../../interfaces" -import { aliasTo, asFunction } from "awilix" +import { aliasTo, asFunction, Lifetime, LifetimeType } from "awilix" type Context = { container: MedusaContainer @@ -14,7 +14,9 @@ type Context = { } export function registerPaymentServiceFromClass( - klass: ClassConstructor, + klass: ClassConstructor & { + LIFE_TIME?: LifetimeType + }, context: Context ): void { if (!isPaymentService(klass.prototype)) { @@ -25,12 +27,17 @@ export function registerPaymentServiceFromClass( container.registerAdd( "paymentProviders", - asFunction((cradle) => new klass(cradle, pluginDetails.options)) + asFunction((cradle) => new klass(cradle, pluginDetails.options), { + lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + }) ) container.register({ [registrationName]: asFunction( - (cradle) => new klass(cradle, pluginDetails.options) + (cradle) => new klass(cradle, pluginDetails.options), + { + lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + } ), [`pp_${(klass as unknown as typeof AbstractPaymentService).identifier}`]: aliasTo(registrationName), @@ -38,7 +45,9 @@ export function registerPaymentServiceFromClass( } export function registerPaymentProcessorFromClass( - klass: ClassConstructor, + klass: ClassConstructor & { + LIFE_TIME?: LifetimeType + }, context: Context ): void { if (!isPaymentProcessor(klass.prototype)) { @@ -49,12 +58,17 @@ export function registerPaymentProcessorFromClass( container.registerAdd( "paymentProviders", - asFunction((cradle) => new klass(cradle, pluginDetails.options)) + asFunction((cradle) => new klass(cradle, pluginDetails.options), { + lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + }) ) container.register({ [registrationName]: asFunction( - (cradle) => new klass(cradle, pluginDetails.options) + (cradle) => new klass(cradle, pluginDetails.options), + { + lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT, + } ), [`pp_${(klass as unknown as typeof AbstractPaymentProcessor).identifier}`]: aliasTo(registrationName), diff --git a/packages/medusa/src/loaders/plugins.ts b/packages/medusa/src/loaders/plugins.ts index 28ac45cf77..8de56b2ed4 100644 --- a/packages/medusa/src/loaders/plugins.ts +++ b/packages/medusa/src/loaders/plugins.ts @@ -1,4 +1,4 @@ -import { aliasTo, asFunction, asValue } from "awilix" +import { aliasTo, asFunction, asValue, Lifetime } from "awilix" import { Express } from "express" import fs from "fs" import { sync as existsSync } from "fs-exists-cached" @@ -377,36 +377,49 @@ export async function registerServices( const name = appDetails.application_name container.register({ [`${name}Oauth`]: asFunction( - (cradle) => new loaded(cradle, pluginDetails.options) + (cradle) => new loaded(cradle, pluginDetails.options), + { + lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + } ), }) } else if (loaded.prototype instanceof FulfillmentService) { // Register our payment providers to paymentProviders container.registerAdd( "fulfillmentProviders", - asFunction((cradle) => new loaded(cradle, pluginDetails.options)) + asFunction((cradle) => new loaded(cradle, pluginDetails.options), { + lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + }) ) // Add the service directly to the container in order to make simple // resolution if we already know which fulfillment provider we need to use container.register({ [name]: asFunction( - (cradle) => new loaded(cradle, pluginDetails.options) - ).singleton(), + (cradle) => new loaded(cradle, pluginDetails.options), + { + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, + } + ), [`fp_${loaded.identifier}`]: aliasTo(name), }) } else if (isNotificationService(loaded.prototype)) { container.registerAdd( "notificationProviders", - asFunction((cradle) => new loaded(cradle, pluginDetails.options)) + asFunction((cradle) => new loaded(cradle, pluginDetails.options), { + lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + }) ) // Add the service directly to the container in order to make simple // resolution if we already know which notification provider we need to use container.register({ [name]: asFunction( - (cradle) => new loaded(cradle, pluginDetails.options) - ).singleton(), + (cradle) => new loaded(cradle, pluginDetails.options), + { + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, + } + ), [`noti_${loaded.identifier}`]: aliasTo(name), }) } else if ( @@ -417,7 +430,10 @@ export async function registerServices( // resolution if we already know which file storage provider we need to use container.register({ [name]: asFunction( - (cradle) => new loaded(cradle, pluginDetails.options) + (cradle) => new loaded(cradle, pluginDetails.options), + { + lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + } ), [`fileService`]: aliasTo(name), }) @@ -426,7 +442,10 @@ export async function registerServices( // resolution if we already know which search provider we need to use container.register({ [name]: asFunction( - (cradle) => new loaded(cradle, pluginDetails.options) + (cradle) => new loaded(cradle, pluginDetails.options), + { + lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + } ), [`searchService`]: aliasTo(name), }) @@ -435,19 +454,27 @@ export async function registerServices( } else if (loaded.prototype instanceof AbstractTaxService) { container.registerAdd( "taxProviders", - asFunction((cradle) => new loaded(cradle, pluginDetails.options)) + asFunction((cradle) => new loaded(cradle, pluginDetails.options), { + lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + }) ) container.register({ [name]: asFunction( - (cradle) => new loaded(cradle, pluginDetails.options) - ).singleton(), + (cradle) => new loaded(cradle, pluginDetails.options), + { + lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON, + } + ), [`tp_${loaded.identifier}`]: aliasTo(name), }) } else { container.register({ [name]: asFunction( - (cradle) => new loaded(cradle, pluginDetails.options) + (cradle) => new loaded(cradle, pluginDetails.options), + { + lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT, + } ), }) }