From f1759f0128ab87b3762e323f0b25f863a1e1f680 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Fri, 4 Oct 2024 15:47:06 +0530 Subject: [PATCH] Feat: Move container bindings declaration merging within the framework (#9467) --- packages/core/framework/src/config/index.ts | 2 + packages/core/framework/src/container.ts | 30 +------ packages/core/framework/src/database/index.ts | 1 + .../core/framework/src/feature-flags/index.ts | 2 + packages/core/framework/src/http/index.ts | 2 + .../core/framework/src/types/container.ts | 89 +++++++++++++++++++ packages/core/framework/src/utils/index.ts | 2 + .../core/framework/src/workflows-sdk/index.ts | 2 + .../core/framework/src/workflows/index.ts | 2 + packages/core/modules-sdk/src/medusa-app.ts | 14 --- .../core/utils/src/modules-sdk/definition.ts | 55 ------------ .../src/helper/workflow-export.ts | 19 ++-- .../src/utils/composer/__tests__/compose.ts | 4 +- 13 files changed, 119 insertions(+), 105 deletions(-) create mode 100644 packages/core/framework/src/types/container.ts diff --git a/packages/core/framework/src/config/index.ts b/packages/core/framework/src/config/index.ts index 85b6201418ead..69de72a8a57d3 100644 --- a/packages/core/framework/src/config/index.ts +++ b/packages/core/framework/src/config/index.ts @@ -1,3 +1,5 @@ +import "../types/container" + export * from "./loader" export * from "./config" export * from "./types" diff --git a/packages/core/framework/src/container.ts b/packages/core/framework/src/container.ts index 61120c7df12d5..ffae61c06db72 100644 --- a/packages/core/framework/src/container.ts +++ b/packages/core/framework/src/container.ts @@ -1,32 +1,4 @@ +export * from "./types/container" import { createMedusaContainer } from "@medusajs/utils" -import { AwilixContainer, ResolveOptions } from "awilix" -import { ModuleImplementations } from "@medusajs/types" - -/** - * The Medusa Container extends [Awilix](https://github.com/jeffijoe/awilix) to - * provide dependency injection functionalities. - */ -// export type MedusaContainer> = -export type MedusaContainer = - Omit & { - resolve( - key: K, - resolveOptions?: ResolveOptions - ): Cradle[K] - resolve(key: string, resolveOptions?: ResolveOptions): T - - /** - * @ignore - */ - registerAdd: (name: string, registration: T) => MedusaContainer - /** - * @ignore - */ - createScope: () => MedusaContainer - } - -export type ContainerLike = { - resolve(key: string): T -} export const container = createMedusaContainer() diff --git a/packages/core/framework/src/database/index.ts b/packages/core/framework/src/database/index.ts index 9d7e6b0bb22db..b64e5b9e4914e 100644 --- a/packages/core/framework/src/database/index.ts +++ b/packages/core/framework/src/database/index.ts @@ -1 +1,2 @@ +import "../types/container" export * from "./pg-connection-loader" diff --git a/packages/core/framework/src/feature-flags/index.ts b/packages/core/framework/src/feature-flags/index.ts index 4a05bac969121..bd28b404de93b 100644 --- a/packages/core/framework/src/feature-flags/index.ts +++ b/packages/core/framework/src/feature-flags/index.ts @@ -1,3 +1,5 @@ +import "../types/container" + export * from "./types" export * from "./feature-flag-loader" export * from "./flag-router" diff --git a/packages/core/framework/src/http/index.ts b/packages/core/framework/src/http/index.ts index 57229d120a0d4..fb9843d6dd491 100644 --- a/packages/core/framework/src/http/index.ts +++ b/packages/core/framework/src/http/index.ts @@ -1,3 +1,5 @@ +import "../types/container" + export * from "./express-loader" export * from "./router" export * from "./types" diff --git a/packages/core/framework/src/types/container.ts b/packages/core/framework/src/types/container.ts new file mode 100644 index 0000000000000..5bc1540ebff8c --- /dev/null +++ b/packages/core/framework/src/types/container.ts @@ -0,0 +1,89 @@ +import { Knex } from "@mikro-orm/knex" +import { RemoteLink } from "@medusajs/modules-sdk" +import { AwilixContainer, ResolveOptions } from "awilix" +import { Modules, ContainerRegistrationKeys } from "@medusajs/utils" +import { + Logger, + ConfigModule, + ModuleImplementations, + RemoteQueryFunction, + IAuthModuleService, + ICacheService, + ICartModuleService, + ICustomerModuleService, + IEventBusModuleService, + IInventoryService, + IPaymentModuleService, + IPricingModuleService, + IProductModuleService, + IPromotionModuleService, + ISalesChannelModuleService, + ITaxModuleService, + IFulfillmentModuleService, + IStockLocationService, + IUserModuleService, + IWorkflowEngineService, + IRegionModuleService, + IOrderModuleService, + IApiKeyModuleService, + IStoreModuleService, + ICurrencyModuleService, + IFileModuleService, + INotificationModuleService, +} from "@medusajs/types" + +declare module "@medusajs/types" { + export interface ModuleImplementations { + [ContainerRegistrationKeys.REMOTE_LINK]: RemoteLink + [ContainerRegistrationKeys.CONFIG_MODULE]: ConfigModule + [ContainerRegistrationKeys.PG_CONNECTION]: Knex + [ContainerRegistrationKeys.REMOTE_QUERY]: RemoteQueryFunction + [ContainerRegistrationKeys.QUERY]: Omit + [ContainerRegistrationKeys.LOGGER]: Logger + [Modules.AUTH]: IAuthModuleService + [Modules.CACHE]: ICacheService + [Modules.CART]: ICartModuleService + [Modules.CUSTOMER]: ICustomerModuleService + [Modules.EVENT_BUS]: IEventBusModuleService + [Modules.INVENTORY]: IInventoryService + [Modules.PAYMENT]: IPaymentModuleService + [Modules.PRICING]: IPricingModuleService + [Modules.PRODUCT]: IProductModuleService + [Modules.PROMOTION]: IPromotionModuleService + [Modules.SALES_CHANNEL]: ISalesChannelModuleService + [Modules.TAX]: ITaxModuleService + [Modules.FULFILLMENT]: IFulfillmentModuleService + [Modules.STOCK_LOCATION]: IStockLocationService + [Modules.USER]: IUserModuleService + [Modules.WORKFLOW_ENGINE]: IWorkflowEngineService + [Modules.REGION]: IRegionModuleService + [Modules.ORDER]: IOrderModuleService + [Modules.API_KEY]: IApiKeyModuleService + [Modules.STORE]: IStoreModuleService + [Modules.CURRENCY]: ICurrencyModuleService + [Modules.FILE]: IFileModuleService + [Modules.NOTIFICATION]: INotificationModuleService + } +} + +export type MedusaContainer = + Omit & { + resolve( + key: K, + resolveOptions?: ResolveOptions + ): Cradle[K] + resolve(key: string, resolveOptions?: ResolveOptions): T + + /** + * @ignore + */ + registerAdd: (name: string, registration: T) => MedusaContainer + /** + * @ignore + */ + createScope: () => MedusaContainer + } + +export type ContainerLike = { + resolve(key: string): T +} diff --git a/packages/core/framework/src/utils/index.ts b/packages/core/framework/src/utils/index.ts index e493cec740b04..4f1278757f1b5 100644 --- a/packages/core/framework/src/utils/index.ts +++ b/packages/core/framework/src/utils/index.ts @@ -1 +1,3 @@ +import "../types/container" + export * from "@medusajs/utils" diff --git a/packages/core/framework/src/workflows-sdk/index.ts b/packages/core/framework/src/workflows-sdk/index.ts index a12280906dfda..6f14c2d93e976 100644 --- a/packages/core/framework/src/workflows-sdk/index.ts +++ b/packages/core/framework/src/workflows-sdk/index.ts @@ -1 +1,3 @@ +import "../types/container" + export * from "@medusajs/workflows-sdk" diff --git a/packages/core/framework/src/workflows/index.ts b/packages/core/framework/src/workflows/index.ts index 17ce03811f2e7..8160f53f30954 100644 --- a/packages/core/framework/src/workflows/index.ts +++ b/packages/core/framework/src/workflows/index.ts @@ -1 +1,3 @@ +import "../types/container" + export * from "./workflow-loader" diff --git a/packages/core/modules-sdk/src/medusa-app.ts b/packages/core/modules-sdk/src/medusa-app.ts index 64e26629ca5d3..35bdca46a5cb5 100644 --- a/packages/core/modules-sdk/src/medusa-app.ts +++ b/packages/core/modules-sdk/src/medusa-app.ts @@ -1,11 +1,9 @@ import { RemoteFetchDataCallback } from "@medusajs/orchestration" import { - ConfigModule, ExternalModuleDeclaration, ILinkMigrationsPlanner, InternalModuleDeclaration, LoadedModule, - Logger, MedusaContainer, ModuleBootstrapDeclaration, ModuleDefinition, @@ -26,7 +24,6 @@ import { ModulesSdkUtils, promiseAll, } from "@medusajs/utils" -import type { Knex } from "@mikro-orm/knex" import { asValue } from "awilix" import { MODULE_PACKAGE_NAMES } from "./definitions" import { @@ -41,17 +38,6 @@ import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types" const LinkModulePackage = MODULE_PACKAGE_NAMES[Modules.LINK] -declare module "@medusajs/types" { - export interface ModuleImplementations { - [ContainerRegistrationKeys.REMOTE_LINK]: RemoteLink - [ContainerRegistrationKeys.CONFIG_MODULE]: ConfigModule - [ContainerRegistrationKeys.PG_CONNECTION]: Knex - [ContainerRegistrationKeys.REMOTE_QUERY]: RemoteQueryFunction - [ContainerRegistrationKeys.QUERY]: Omit - [ContainerRegistrationKeys.LOGGER]: Logger - } -} - export type RunMigrationFn = () => Promise export type RevertMigrationFn = (moduleNames: string[]) => Promise export type GenerateMigrations = (moduleNames: string[]) => Promise diff --git a/packages/core/utils/src/modules-sdk/definition.ts b/packages/core/utils/src/modules-sdk/definition.ts index 4219e7df563bc..fd59d85d696c2 100644 --- a/packages/core/utils/src/modules-sdk/definition.ts +++ b/packages/core/utils/src/modules-sdk/definition.ts @@ -1,29 +1,3 @@ -import type { - IApiKeyModuleService, - IAuthModuleService, - ICacheService, - ICartModuleService, - ICurrencyModuleService, - ICustomerModuleService, - IEventBusModuleService, - IFileModuleService, - IFulfillmentModuleService, - IInventoryService, - INotificationModuleService, - IOrderModuleService, - IPaymentModuleService, - IPricingModuleService, - IProductModuleService, - IPromotionModuleService, - IRegionModuleService, - ISalesChannelModuleService, - IStockLocationService, - IStoreModuleService, - ITaxModuleService, - IUserModuleService, - IWorkflowEngineService, -} from "@medusajs/types" - export enum Modules { AUTH = "Auth", CACHE = "Cache", @@ -53,32 +27,3 @@ export enum Modules { } export const ModuleRegistrationName = Modules - -declare module "@medusajs/types" { - export interface ModuleImplementations { - [Modules.AUTH]: IAuthModuleService - [Modules.CACHE]: ICacheService - [Modules.CART]: ICartModuleService - [Modules.CUSTOMER]: ICustomerModuleService - [Modules.EVENT_BUS]: IEventBusModuleService - [Modules.INVENTORY]: IInventoryService - [Modules.PAYMENT]: IPaymentModuleService - [Modules.PRICING]: IPricingModuleService - [Modules.PRODUCT]: IProductModuleService - [Modules.PROMOTION]: IPromotionModuleService - [Modules.SALES_CHANNEL]: ISalesChannelModuleService - [Modules.TAX]: ITaxModuleService - [Modules.FULFILLMENT]: IFulfillmentModuleService - [Modules.STOCK_LOCATION]: IStockLocationService - [Modules.USER]: IUserModuleService - [Modules.WORKFLOW_ENGINE]: IWorkflowEngineService - [Modules.REGION]: IRegionModuleService - [Modules.ORDER]: IOrderModuleService - [Modules.API_KEY]: IApiKeyModuleService - [Modules.STORE]: IStoreModuleService - [Modules.CURRENCY]: ICurrencyModuleService - [Modules.FILE]: IFileModuleService - [Modules.NOTIFICATION]: INotificationModuleService - [Modules.INDEX]: any // TODO: define index module interface - } -} diff --git a/packages/core/workflows-sdk/src/helper/workflow-export.ts b/packages/core/workflows-sdk/src/helper/workflow-export.ts index 502b4e13be3f6..5c7a13dbe536a 100644 --- a/packages/core/workflows-sdk/src/helper/workflow-export.ts +++ b/packages/core/workflows-sdk/src/helper/workflow-export.ts @@ -6,7 +6,13 @@ import { TransactionHandlerType, TransactionState, } from "@medusajs/orchestration" -import { Context, LoadedModule, MedusaContainer } from "@medusajs/types" +import { + Context, + IEventBusModuleService, + LoadedModule, + Logger, + MedusaContainer, +} from "@medusajs/types" import { ContainerRegistrationKeys, isPresent, @@ -514,7 +520,7 @@ function attachOnFinishReleaseEvents( const flowEventGroupId = transaction.getFlow().metadata?.eventGroupId const logger = - (flow.container as MedusaContainer).resolve( + (flow.container as MedusaContainer).resolve( ContainerRegistrationKeys.LOGGER, { allowUnregistered: true } ) || console @@ -539,10 +545,11 @@ function attachOnFinishReleaseEvents( await onFinish?.(args) - const eventBusService = (flow.container as MedusaContainer).resolve( - Modules.EVENT_BUS, - { allowUnregistered: true } - ) + const eventBusService = ( + flow.container as MedusaContainer + ).resolve(Modules.EVENT_BUS, { + allowUnregistered: true, + }) if (!eventBusService || !flowEventGroupId) { return diff --git a/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts b/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts index ab041f66618c8..f5e7fca486a9c 100644 --- a/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts +++ b/packages/core/workflows-sdk/src/utils/composer/__tests__/compose.ts @@ -2216,7 +2216,9 @@ describe("Workflow composer", function () { throwOnError: false, }) - const eventBusMock = container.resolve(Modules.EVENT_BUS) + const eventBusMock = container.resolve( + Modules.EVENT_BUS + ) expect(eventBusMock.emit).toHaveBeenCalledTimes(1) expect(eventBusMock.releaseGroupedEvents).toHaveBeenCalledTimes(0)