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:
1 parent
f5ed47053c
commit
f033711ad6
3 files changed
+67
-21
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): Add Lifetime support on project/plugin services
|
||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
isPaymentProcessor,
|
isPaymentProcessor,
|
||||||
isPaymentService,
|
isPaymentService,
|
||||||
} from "../../interfaces"
|
} from "../../interfaces"
|
||||||
import { aliasTo, asFunction } from "awilix"
|
import { aliasTo, asFunction, Lifetime, LifetimeType } from "awilix"
|
||||||
|
|
||||||
type Context = {
|
type Context = {
|
||||||
container: MedusaContainer
|
container: MedusaContainer
|
||||||
@@ -14,7 +14,9 @@ type Context = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function registerPaymentServiceFromClass(
|
export function registerPaymentServiceFromClass(
|
||||||
klass: ClassConstructor<AbstractPaymentService>,
|
klass: ClassConstructor<AbstractPaymentService> & {
|
||||||
|
LIFE_TIME?: LifetimeType
|
||||||
|
},
|
||||||
context: Context
|
context: Context
|
||||||
): void {
|
): void {
|
||||||
if (!isPaymentService(klass.prototype)) {
|
if (!isPaymentService(klass.prototype)) {
|
||||||
@@ -25,12 +27,17 @@ export function registerPaymentServiceFromClass(
|
|||||||
|
|
||||||
container.registerAdd(
|
container.registerAdd(
|
||||||
"paymentProviders",
|
"paymentProviders",
|
||||||
asFunction((cradle) => new klass(cradle, pluginDetails.options))
|
asFunction((cradle) => new klass(cradle, pluginDetails.options), {
|
||||||
|
lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT,
|
||||||
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
container.register({
|
container.register({
|
||||||
[registrationName]: asFunction(
|
[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}`]:
|
[`pp_${(klass as unknown as typeof AbstractPaymentService).identifier}`]:
|
||||||
aliasTo(registrationName),
|
aliasTo(registrationName),
|
||||||
@@ -38,7 +45,9 @@ export function registerPaymentServiceFromClass(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function registerPaymentProcessorFromClass(
|
export function registerPaymentProcessorFromClass(
|
||||||
klass: ClassConstructor<AbstractPaymentProcessor>,
|
klass: ClassConstructor<AbstractPaymentProcessor> & {
|
||||||
|
LIFE_TIME?: LifetimeType
|
||||||
|
},
|
||||||
context: Context
|
context: Context
|
||||||
): void {
|
): void {
|
||||||
if (!isPaymentProcessor(klass.prototype)) {
|
if (!isPaymentProcessor(klass.prototype)) {
|
||||||
@@ -49,12 +58,17 @@ export function registerPaymentProcessorFromClass(
|
|||||||
|
|
||||||
container.registerAdd(
|
container.registerAdd(
|
||||||
"paymentProviders",
|
"paymentProviders",
|
||||||
asFunction((cradle) => new klass(cradle, pluginDetails.options))
|
asFunction((cradle) => new klass(cradle, pluginDetails.options), {
|
||||||
|
lifetime: klass.LIFE_TIME || Lifetime.TRANSIENT,
|
||||||
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
container.register({
|
container.register({
|
||||||
[registrationName]: asFunction(
|
[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}`]:
|
[`pp_${(klass as unknown as typeof AbstractPaymentProcessor).identifier}`]:
|
||||||
aliasTo(registrationName),
|
aliasTo(registrationName),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { aliasTo, asFunction, asValue } from "awilix"
|
import { aliasTo, asFunction, asValue, Lifetime } from "awilix"
|
||||||
import { Express } from "express"
|
import { Express } from "express"
|
||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import { sync as existsSync } from "fs-exists-cached"
|
import { sync as existsSync } from "fs-exists-cached"
|
||||||
@@ -377,36 +377,49 @@ export async function registerServices(
|
|||||||
const name = appDetails.application_name
|
const name = appDetails.application_name
|
||||||
container.register({
|
container.register({
|
||||||
[`${name}Oauth`]: asFunction(
|
[`${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) {
|
} else if (loaded.prototype instanceof FulfillmentService) {
|
||||||
// Register our payment providers to paymentProviders
|
// Register our payment providers to paymentProviders
|
||||||
container.registerAdd(
|
container.registerAdd(
|
||||||
"fulfillmentProviders",
|
"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
|
// Add the service directly to the container in order to make simple
|
||||||
// resolution if we already know which fulfillment provider we need to use
|
// resolution if we already know which fulfillment provider we need to use
|
||||||
container.register({
|
container.register({
|
||||||
[name]: asFunction(
|
[name]: asFunction(
|
||||||
(cradle) => new loaded(cradle, pluginDetails.options)
|
(cradle) => new loaded(cradle, pluginDetails.options),
|
||||||
).singleton(),
|
{
|
||||||
|
lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON,
|
||||||
|
}
|
||||||
|
),
|
||||||
[`fp_${loaded.identifier}`]: aliasTo(name),
|
[`fp_${loaded.identifier}`]: aliasTo(name),
|
||||||
})
|
})
|
||||||
} else if (isNotificationService(loaded.prototype)) {
|
} else if (isNotificationService(loaded.prototype)) {
|
||||||
container.registerAdd(
|
container.registerAdd(
|
||||||
"notificationProviders",
|
"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
|
// Add the service directly to the container in order to make simple
|
||||||
// resolution if we already know which notification provider we need to use
|
// resolution if we already know which notification provider we need to use
|
||||||
container.register({
|
container.register({
|
||||||
[name]: asFunction(
|
[name]: asFunction(
|
||||||
(cradle) => new loaded(cradle, pluginDetails.options)
|
(cradle) => new loaded(cradle, pluginDetails.options),
|
||||||
).singleton(),
|
{
|
||||||
|
lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON,
|
||||||
|
}
|
||||||
|
),
|
||||||
[`noti_${loaded.identifier}`]: aliasTo(name),
|
[`noti_${loaded.identifier}`]: aliasTo(name),
|
||||||
})
|
})
|
||||||
} else if (
|
} else if (
|
||||||
@@ -417,7 +430,10 @@ export async function registerServices(
|
|||||||
// resolution if we already know which file storage provider we need to use
|
// resolution if we already know which file storage provider we need to use
|
||||||
container.register({
|
container.register({
|
||||||
[name]: asFunction(
|
[name]: asFunction(
|
||||||
(cradle) => new loaded(cradle, pluginDetails.options)
|
(cradle) => new loaded(cradle, pluginDetails.options),
|
||||||
|
{
|
||||||
|
lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT,
|
||||||
|
}
|
||||||
),
|
),
|
||||||
[`fileService`]: aliasTo(name),
|
[`fileService`]: aliasTo(name),
|
||||||
})
|
})
|
||||||
@@ -426,7 +442,10 @@ export async function registerServices(
|
|||||||
// resolution if we already know which search provider we need to use
|
// resolution if we already know which search provider we need to use
|
||||||
container.register({
|
container.register({
|
||||||
[name]: asFunction(
|
[name]: asFunction(
|
||||||
(cradle) => new loaded(cradle, pluginDetails.options)
|
(cradle) => new loaded(cradle, pluginDetails.options),
|
||||||
|
{
|
||||||
|
lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT,
|
||||||
|
}
|
||||||
),
|
),
|
||||||
[`searchService`]: aliasTo(name),
|
[`searchService`]: aliasTo(name),
|
||||||
})
|
})
|
||||||
@@ -435,19 +454,27 @@ export async function registerServices(
|
|||||||
} else if (loaded.prototype instanceof AbstractTaxService) {
|
} else if (loaded.prototype instanceof AbstractTaxService) {
|
||||||
container.registerAdd(
|
container.registerAdd(
|
||||||
"taxProviders",
|
"taxProviders",
|
||||||
asFunction((cradle) => new loaded(cradle, pluginDetails.options))
|
asFunction((cradle) => new loaded(cradle, pluginDetails.options), {
|
||||||
|
lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT,
|
||||||
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
container.register({
|
container.register({
|
||||||
[name]: asFunction(
|
[name]: asFunction(
|
||||||
(cradle) => new loaded(cradle, pluginDetails.options)
|
(cradle) => new loaded(cradle, pluginDetails.options),
|
||||||
).singleton(),
|
{
|
||||||
|
lifetime: loaded.LIFE_TIME || Lifetime.SINGLETON,
|
||||||
|
}
|
||||||
|
),
|
||||||
[`tp_${loaded.identifier}`]: aliasTo(name),
|
[`tp_${loaded.identifier}`]: aliasTo(name),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
container.register({
|
container.register({
|
||||||
[name]: asFunction(
|
[name]: asFunction(
|
||||||
(cradle) => new loaded(cradle, pluginDetails.options)
|
(cradle) => new loaded(cradle, pluginDetails.options),
|
||||||
|
{
|
||||||
|
lifetime: loaded.LIFE_TIME || Lifetime.TRANSIENT,
|
||||||
|
}
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user