feat: Add support for providers to validate their options at loading time (#8853)
* feat: Add support for providers to validate their options at loading time * fix missing removal * fix integration tests * add tests
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
const service = class TestService {
|
||||
static validateOptions(options: Record<any, any>) {
|
||||
throw new Error("Wrong options")
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
services: [service],
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { Lifetime, asFunction } from "awilix"
|
||||
import { asFunction, Lifetime } from "awilix"
|
||||
import { moduleProviderLoader } from "../module-provider-loader"
|
||||
|
||||
const logger = {
|
||||
@@ -34,6 +34,24 @@ describe("modules loader", () => {
|
||||
expect(testService.constructor.name).toEqual("TestService")
|
||||
})
|
||||
|
||||
it("should fail to register the provider service", async () => {
|
||||
const moduleProviders = [
|
||||
{
|
||||
resolve: "@providers/default-with-fail-validation",
|
||||
id: "default",
|
||||
options: {},
|
||||
},
|
||||
]
|
||||
|
||||
const err = await moduleProviderLoader({
|
||||
container,
|
||||
providers: moduleProviders,
|
||||
}).catch((e) => e)
|
||||
|
||||
expect(err).toBeTruthy()
|
||||
expect(err.message).toBe("Wrong options")
|
||||
})
|
||||
|
||||
it("should register the provider service with custom register fn", async () => {
|
||||
const fn = async (klass, container, details) => {
|
||||
container.register({
|
||||
@@ -64,6 +82,35 @@ describe("modules loader", () => {
|
||||
expect(testService.constructor.name).toEqual("TestService")
|
||||
})
|
||||
|
||||
it("should fail to register the provider service with custom register fn", async () => {
|
||||
const fn = async (klass, container, details) => {
|
||||
container.register({
|
||||
[`testServiceCustomRegistration`]: asFunction(
|
||||
(cradle) => new klass(cradle, details.options),
|
||||
{
|
||||
lifetime: Lifetime.SINGLETON,
|
||||
}
|
||||
),
|
||||
})
|
||||
}
|
||||
const moduleProviders = [
|
||||
{
|
||||
resolve: "@providers/default-with-fail-validation",
|
||||
id: "default",
|
||||
options: {},
|
||||
},
|
||||
]
|
||||
|
||||
const err = await moduleProviderLoader({
|
||||
container,
|
||||
providers: moduleProviders,
|
||||
registerServiceFn: fn,
|
||||
}).catch((e) => e)
|
||||
|
||||
expect(err).toBeTruthy()
|
||||
expect(err.message).toBe("Wrong options")
|
||||
})
|
||||
|
||||
it("should log the errors if no service is defined", async () => {
|
||||
const moduleProviders = [
|
||||
{
|
||||
|
||||
@@ -60,8 +60,11 @@ export async function loadModuleProvider(
|
||||
)
|
||||
}
|
||||
|
||||
const services = await promiseAll(
|
||||
return await promiseAll(
|
||||
loadedProvider.services.map(async (service) => {
|
||||
// Ask the provider to validate its options
|
||||
await service.validateOptions?.(provider.options)
|
||||
|
||||
const name = lowerCaseFirst(service.name)
|
||||
if (registerServiceFn) {
|
||||
// Used to register the specific type of service in the provider
|
||||
@@ -83,6 +86,4 @@ export async function loadModuleProvider(
|
||||
return service
|
||||
})
|
||||
)
|
||||
|
||||
return services
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user