chore: Update modules providers configuration with 'identifier' and 'PROVIDER' (#9636)

* chore: Update modules providers configuration with 'identifier' and 'PROVIDER'

* update check

* fix tests

* type

* normalize auth provider

* emailpass

* providers

---------

Co-authored-by: Carlos R. L. Rodrigues <rodrigolr@gmail.com>
Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-10-18 09:24:15 +02:00
committed by GitHub
parent 902ac12f73
commit 876d8072e7
22 changed files with 131 additions and 67 deletions
@@ -1 +1,3 @@
export class ModuleProvider2Service {}
export class ModuleProvider2Service {
static identifier = "provider-2"
}
@@ -1,5 +1,9 @@
import { IModuleService, ModuleResolution } from "@medusajs/types"
import { createMedusaContainer, upperCaseFirst } from "@medusajs/utils"
import {
createMedusaContainer,
getProviderRegistrationKey,
upperCaseFirst,
} from "@medusajs/utils"
import { join } from "path"
import {
ModuleWithDmlMixedWithoutJoinerConfigFixtures,
@@ -10,11 +14,7 @@ import {
import { ModuleService as ModuleServiceWithProvider } from "../__fixtures__/module-with-providers"
import { ModuleProviderService as ModuleServiceWithProviderProvider1 } from "../__fixtures__/module-with-providers/provider-1"
import { ModuleProvider2Service as ModuleServiceWithProviderProvider2 } from "../__fixtures__/module-with-providers/provider-2"
import {
getProviderRegistrationKey,
loadInternalModule,
loadResources,
} from "../load-internal"
import { loadInternalModule, loadResources } from "../load-internal"
describe("load internal", () => {
describe("loadResources", () => {
@@ -376,9 +376,10 @@ describe("load internal", () => {
const moduleService = container.resolve(moduleResolution.definition.key)
const provider = (moduleService as any).container[
getProviderRegistrationKey(
ModuleServiceWithProviderProvider1.identifier
)
getProviderRegistrationKey({
providerId: moduleResolution.options!.providers![0].id,
providerIdentifier: ModuleServiceWithProviderProvider1.identifier,
})
]
expect(moduleService).toBeInstanceOf(ModuleServiceWithProvider)
@@ -427,7 +428,10 @@ describe("load internal", () => {
const moduleService = container.resolve(moduleResolution.definition.key)
const provider = (moduleService as any).container[
getProviderRegistrationKey(moduleResolution.options!.providers![0].id)
getProviderRegistrationKey({
providerId: moduleResolution.options!.providers![0].id,
providerIdentifier: ModuleServiceWithProviderProvider2.identifier,
})
]
expect(moduleService).toBeInstanceOf(ModuleServiceWithProvider)
@@ -18,6 +18,7 @@ import {
defineJoinerConfig,
DmlEntity,
dynamicImport,
getProviderRegistrationKey,
isString,
MedusaModuleProviderType,
MedusaModuleType,
@@ -51,17 +52,6 @@ type ResolvedModuleProvider = ModuleProviderExports & {
discoveryPath: string
}
export const moduleProviderRegistrationKeyPrefix = "__providers__"
/**
* Return the key used to register a module provider in the container
* @param {string} moduleKey
* @return {string}
*/
export function getProviderRegistrationKey(moduleKey: string): string {
return moduleProviderRegistrationKeyPrefix + moduleKey
}
export async function resolveModuleExports({
resolution,
}: {
@@ -140,7 +130,7 @@ async function loadInternalProvider(
moduleExports: !isString(providerRes) ? providerRes : undefined,
definition: {
...resolution.definition,
key: provider.id,
key: provider.id!,
},
resolutionPath: isString(provider.resolve)
? require.resolve(provider.resolve, {
@@ -321,13 +311,39 @@ export async function loadInternalModule(args: {
for (const moduleProviderService of moduleProviderServices) {
const modProvider_ = moduleProviderService as any
modProvider_.identifier ??= keyName
modProvider_.__type = MedusaModuleProviderType
const registrationKey = getProviderRegistrationKey(
modProvider_.identifier
const originalIdentifier = modProvider_.identifier as string
const providerId = keyName
if (!originalIdentifier) {
const providerResolutionName =
modProvider_.DISPLAY_NAME ?? resolution.resolutionPath
throw new Error(
`Module provider ${providerResolutionName} does not have a static "identifier" property on its service class.`
)
}
const alreadyRegisteredProvider = container.hasRegistration(
getProviderRegistrationKey({
providerId,
providerIdentifier: originalIdentifier,
})
)
if (alreadyRegisteredProvider) {
throw new Error(
`Module provider ${originalIdentifier} has already been registered. Please provide a different "id" in the provider options.`
)
}
modProvider_.__type = MedusaModuleProviderType
const registrationKey = getProviderRegistrationKey({
providerId,
providerIdentifier: originalIdentifier,
})
container.register({
[registrationKey]: asFunction((cradle) => {
[registrationKey]: asFunction(() => {
;(moduleProviderService as any).__type = MedusaModuleType
return new moduleProviderService(
localContainer.cradle,