-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow for direct ref to plugin export
- Loading branch information
1 parent
18f4696
commit f6a9815
Showing
3 changed files
with
88 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from "./module-loader" | ||
export * from "./module-provider-loader" | ||
export * from "./register-modules" | ||
export * from "./register-providers" | ||
|
87 changes: 87 additions & 0 deletions
87
packages/modules-sdk/src/loaders/module-provider-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Constructor, MedusaContainer } from "@medusajs/types" | ||
import { isString, promiseAll } from "@medusajs/utils" | ||
import { Lifetime, asFunction } from "awilix" | ||
|
||
type ModuleProviderExports = { | ||
services: Constructor<any>[] | ||
} | ||
|
||
type ModuleProvider = { | ||
resolve: string | ModuleProviderExports | ||
provider_name?: string | ||
options: Record<string, unknown> | ||
} | ||
|
||
export async function moduleProviderLoader({ | ||
container, | ||
providers, | ||
registerServiceFn, | ||
}) { | ||
if (!providers?.length) { | ||
return | ||
} | ||
|
||
await promiseAll( | ||
providers.map(async (pluginDetails) => { | ||
await loadModuleProvider(container, pluginDetails, registerServiceFn) | ||
}) | ||
) | ||
} | ||
|
||
export async function loadModuleProvider( | ||
container: MedusaContainer, | ||
provider: ModuleProvider, | ||
registerServiceFn?: (klass, container, pluginDetails) => Promise<void> | ||
) { | ||
let loadedProvider: any | ||
|
||
const pluginName = provider.resolve ?? provider.provider_name ?? "" | ||
|
||
try { | ||
if (isString(provider.resolve)) { | ||
loadedProvider = await import(provider.resolve) | ||
} else { | ||
loadedProvider = provider.resolve | ||
} | ||
} catch (error) { | ||
throw new Error( | ||
`Unable to find plugin ${pluginName} -- perhaps you need to install its package?` | ||
) | ||
} | ||
|
||
loadedProvider = (loadedProvider as any).default | ||
|
||
if (!loadedProvider) { | ||
throw new Error( | ||
`No default export found in plugin ${pluginName} -- make sure your plugin exports a service.` | ||
) | ||
} | ||
|
||
if (!loadedProvider?.services) { | ||
throw new Error( | ||
`No services found in plugin ${provider.resolve} -- make sure your plugin exports a service.` | ||
) | ||
} | ||
|
||
const services = await promiseAll( | ||
loadedProvider.services.map(async (service) => { | ||
if (registerServiceFn) { | ||
// Used to register the specific type of service in the provider | ||
await registerServiceFn(service, container, provider.options) | ||
} else { | ||
container.register({ | ||
[service.name]: asFunction( | ||
(cradle) => new service(cradle, provider.options), | ||
{ | ||
lifetime: service.LIFE_TIME || Lifetime.SCOPED, | ||
} | ||
), | ||
}) | ||
} | ||
|
||
return service | ||
}) | ||
) | ||
|
||
return services | ||
} |
This file was deleted.
Oops, something went wrong.