Skip to content

Commit

Permalink
Feat: Move container bindings declaration merging within the framework (
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored and panalgin committed Oct 7, 2024
1 parent 58f9b2e commit f1759f0
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 105 deletions.
2 changes: 2 additions & 0 deletions packages/core/framework/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "../types/container"

export * from "./loader"
export * from "./config"
export * from "./types"
30 changes: 1 addition & 29 deletions packages/core/framework/src/container.ts
Original file line number Diff line number Diff line change
@@ -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<Cradle extends object = TransformObjectMethodToAsync<ModuleImplementations>> =
export type MedusaContainer<Cradle extends object = ModuleImplementations> =
Omit<AwilixContainer, "resolve"> & {
resolve<K extends keyof Cradle>(
key: K,
resolveOptions?: ResolveOptions
): Cradle[K]
resolve<T>(key: string, resolveOptions?: ResolveOptions): T

/**
* @ignore
*/
registerAdd: <T>(name: string, registration: T) => MedusaContainer
/**
* @ignore
*/
createScope: () => MedusaContainer
}

export type ContainerLike = {
resolve<T = unknown>(key: string): T
}

export const container = createMedusaContainer()
1 change: 1 addition & 0 deletions packages/core/framework/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import "../types/container"
export * from "./pg-connection-loader"
2 changes: 2 additions & 0 deletions packages/core/framework/src/feature-flags/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "../types/container"

export * from "./types"
export * from "./feature-flag-loader"
export * from "./flag-router"
2 changes: 2 additions & 0 deletions packages/core/framework/src/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "../types/container"

export * from "./express-loader"
export * from "./router"
export * from "./types"
Expand Down
89 changes: 89 additions & 0 deletions packages/core/framework/src/types/container.ts
Original file line number Diff line number Diff line change
@@ -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<any>
[ContainerRegistrationKeys.REMOTE_QUERY]: RemoteQueryFunction
[ContainerRegistrationKeys.QUERY]: Omit<RemoteQueryFunction, symbol>
[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<Cradle extends object = ModuleImplementations> =
Omit<AwilixContainer, "resolve"> & {
resolve<K extends keyof Cradle>(
key: K,
resolveOptions?: ResolveOptions
): Cradle[K]
resolve<T>(key: string, resolveOptions?: ResolveOptions): T

/**
* @ignore
*/
registerAdd: <T>(name: string, registration: T) => MedusaContainer
/**
* @ignore
*/
createScope: () => MedusaContainer
}

export type ContainerLike = {
resolve<T = unknown>(key: string): T
}
2 changes: 2 additions & 0 deletions packages/core/framework/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "../types/container"

export * from "@medusajs/utils"
2 changes: 2 additions & 0 deletions packages/core/framework/src/workflows-sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "../types/container"

export * from "@medusajs/workflows-sdk"
2 changes: 2 additions & 0 deletions packages/core/framework/src/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "../types/container"

export * from "./workflow-loader"
14 changes: 0 additions & 14 deletions packages/core/modules-sdk/src/medusa-app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { RemoteFetchDataCallback } from "@medusajs/orchestration"
import {
ConfigModule,
ExternalModuleDeclaration,
ILinkMigrationsPlanner,
InternalModuleDeclaration,
LoadedModule,
Logger,
MedusaContainer,
ModuleBootstrapDeclaration,
ModuleDefinition,
Expand All @@ -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 {
Expand All @@ -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<any>
[ContainerRegistrationKeys.REMOTE_QUERY]: RemoteQueryFunction
[ContainerRegistrationKeys.QUERY]: Omit<RemoteQueryFunction, symbol>
[ContainerRegistrationKeys.LOGGER]: Logger
}
}

export type RunMigrationFn = () => Promise<void>
export type RevertMigrationFn = (moduleNames: string[]) => Promise<void>
export type GenerateMigrations = (moduleNames: string[]) => Promise<void>
Expand Down
55 changes: 0 additions & 55 deletions packages/core/utils/src/modules-sdk/definition.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
}
}
19 changes: 13 additions & 6 deletions packages/core/workflows-sdk/src/helper/workflow-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -514,7 +520,7 @@ function attachOnFinishReleaseEvents(
const flowEventGroupId = transaction.getFlow().metadata?.eventGroupId

const logger =
(flow.container as MedusaContainer).resolve(
(flow.container as MedusaContainer).resolve<Logger>(
ContainerRegistrationKeys.LOGGER,
{ allowUnregistered: true }
) || console
Expand All @@ -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<IEventBusModuleService>(Modules.EVENT_BUS, {
allowUnregistered: true,
})

if (!eventBusService || !flowEventGroupId) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,9 @@ describe("Workflow composer", function () {
throwOnError: false,
})

const eventBusMock = container.resolve(Modules.EVENT_BUS)
const eventBusMock = container.resolve<IEventBusModuleService>(
Modules.EVENT_BUS
)

expect(eventBusMock.emit).toHaveBeenCalledTimes(1)
expect(eventBusMock.releaseGroupedEvents).toHaveBeenCalledTimes(0)
Expand Down

0 comments on commit f1759f0

Please sign in to comment.