feat(medusa): Add Lifetime support on project/plugin services (#3349)

* feat(medusa): Add Lifetime support on project/plugin services

* Create kind-dogs-argue.md

---------

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2023-03-02 16:33:11 +01:00
committed by GitHub
co-authored by Oliver Windall Juhl
parent f5ed47053c
commit f033711ad6
3 changed files with 67 additions and 21 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
feat(medusa): Add Lifetime support on project/plugin services
+21 -7
View File
@@ -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<AbstractPaymentService>,
klass: ClassConstructor<AbstractPaymentService> & {
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<AbstractPaymentProcessor>,
klass: ClassConstructor<AbstractPaymentProcessor> & {
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),
+41 -14
View File
@@ -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,
}
),
})
}