Skip to content

Commit 3c51709

Browse files
authored
feat: map container types for core services to interface (medusajs#11295)
1 parent 70b3e16 commit 3c51709

File tree

5 files changed

+101
-15
lines changed

5 files changed

+101
-15
lines changed

.changeset/witty-lies-burn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/types": patch
3+
"@medusajs/utils": patch
4+
---
5+
6+
feat: map container types for core services to interface

packages/core/types/src/common/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface FindConfig<Entity> {
6666
/**
6767
* An array of strings, each being attribute names of the entity to retrieve in the result.
6868
*/
69-
select?: (keyof Entity | string)[]
69+
select?: (keyof Entity | (string & {}))[]
7070

7171
/**
7272
* A number indicating the number of records to skip before retrieving the results.

packages/core/utils/src/modules-sdk/__tests__/modules-to-container-types.spec.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ describe("generateContainerTypes", function () {
1414
{
1515
cache: {
1616
__definition: {
17-
key: "cache",
17+
key: "foo-cache",
1818
label: "Cache",
19-
defaultPackage: "@medusajs/foo",
20-
resolvePath: "@medusajs/foo",
19+
defaultPackage: "@medusajs/foo-cache",
20+
resolvePath: "@medusajs/foo-cache",
2121
defaultModuleDeclaration: {
2222
scope: "internal",
2323
},
@@ -34,23 +34,58 @@ describe("generateContainerTypes", function () {
3434
expect(await fileSystem.exists("modules-bindings.d.ts")).toBeTruthy()
3535
expect(await fileSystem.contents("modules-bindings.d.ts"))
3636
.toMatchInlineSnapshot(`
37-
"import type Cache from '@medusajs/foo'
37+
"import type FooCache from '@medusajs/foo-cache'
3838
3939
declare module '@medusajs/framework/types' {
4040
interface ModulesImplementations {
41-
cache: InstanceType<(typeof Cache)['service']>
41+
'foo-cache': InstanceType<(typeof FooCache)['service']>
4242
}
4343
}"
4444
`)
4545
})
4646

47-
it("should normalize module path pointing to a relative file", async function () {
47+
it("point inbuilt packages to their interfaces", async function () {
4848
await generateContainerTypes(
4949
{
5050
cache: {
5151
__definition: {
5252
key: "cache",
5353
label: "Cache",
54+
defaultPackage: "@medusajs/foo-cache",
55+
resolvePath: "@medusajs/foo-cache",
56+
defaultModuleDeclaration: {
57+
scope: "internal",
58+
},
59+
},
60+
__joinerConfig: {},
61+
},
62+
},
63+
{
64+
outputDir: fileSystem.basePath,
65+
interfaceName: "ModulesImplementations",
66+
}
67+
)
68+
69+
expect(await fileSystem.exists("modules-bindings.d.ts")).toBeTruthy()
70+
expect(await fileSystem.contents("modules-bindings.d.ts"))
71+
.toMatchInlineSnapshot(`
72+
"import type { ICacheService } from '@medusajs/framework/types'
73+
74+
declare module '@medusajs/framework/types' {
75+
interface ModulesImplementations {
76+
'cache': ICacheService
77+
}
78+
}"
79+
`)
80+
})
81+
82+
it("should normalize module path pointing to a relative file", async function () {
83+
await generateContainerTypes(
84+
{
85+
bar: {
86+
__definition: {
87+
key: "bar",
88+
label: "Bar",
5489
defaultPackage: "./foo/bar",
5590
resolvePath: "./foo/bar",
5691
defaultModuleDeclaration: {
@@ -69,11 +104,11 @@ describe("generateContainerTypes", function () {
69104
expect(await fileSystem.exists("modules-bindings.d.ts")).toBeTruthy()
70105
expect(await fileSystem.contents("modules-bindings.d.ts"))
71106
.toMatchInlineSnapshot(`
72-
"import type Cache from '../../foo/bar'
107+
"import type Bar from '../../foo/bar'
73108
74109
declare module '@medusajs/framework/types' {
75110
interface ModulesImplementations {
76-
cache: InstanceType<(typeof Cache)['service']>
111+
'bar': InstanceType<(typeof Bar)['service']>
77112
}
78113
}"
79114
`)

packages/core/utils/src/modules-sdk/modules-to-container-types.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
11
import { join } from "path"
2+
import { Modules } from "./definition"
23
import type { LoadedModule } from "@medusajs/types"
34
import { FileSystem } from "../common/file-system"
45
import { toCamelCase } from "../common/to-camel-case"
56
import { upperCaseFirst } from "../common/upper-case-first"
67

8+
/**
9+
* For known services that has interfaces, we will set the container
10+
* type to the interface than the actual service implementation.
11+
*
12+
* The idea is to provide more precise types.
13+
*/
14+
const SERVICES_INTERFACES = {
15+
[Modules.AUTH]: "IAuthModuleService",
16+
[Modules.CACHE]: "ICacheService",
17+
[Modules.CART]: "ICartModuleService",
18+
[Modules.CUSTOMER]: "ICustomerModuleService",
19+
[Modules.EVENT_BUS]: "IEventBusModuleService",
20+
[Modules.INVENTORY]: "IInventoryService",
21+
[Modules.PAYMENT]: "IPaymentModuleService",
22+
[Modules.PRICING]: "IPricingModuleService",
23+
[Modules.PRODUCT]: "IProductModuleService",
24+
[Modules.PROMOTION]: "IPromotionModuleService",
25+
[Modules.SALES_CHANNEL]: "ISalesChannelModuleService",
26+
[Modules.TAX]: "ITaxModuleService",
27+
[Modules.FULFILLMENT]: "IFulfillmentModuleService",
28+
[Modules.STOCK_LOCATION]: "IStockLocationService",
29+
[Modules.USER]: "IUserModuleService",
30+
[Modules.WORKFLOW_ENGINE]: "IWorkflowEngineService",
31+
[Modules.REGION]: "IRegionModuleService",
32+
[Modules.ORDER]: "IOrderModuleService",
33+
[Modules.API_KEY]: "IApiKeyModuleService",
34+
[Modules.STORE]: "IStoreModuleService",
35+
[Modules.CURRENCY]: "ICurrencyModuleService",
36+
[Modules.FILE]: "IFileModuleService",
37+
[Modules.NOTIFICATION]: "INotificationModuleService",
38+
[Modules.LOCKING]: "ILockingModule",
39+
}
40+
741
/**
842
* Modules registered inside the config file points to one
943
* of the following paths.
@@ -55,6 +89,15 @@ export async function generateContainerTypes(
5589
* Key registered within the container
5690
*/
5791
const key = service.__definition.key
92+
const interfaceKey = `'${key}'`
93+
94+
if (SERVICES_INTERFACES[key]) {
95+
result.imports.push(
96+
`import type { ${SERVICES_INTERFACES[key]} } from '@medusajs/framework/types'`
97+
)
98+
result.mappings.push(`${interfaceKey}: ${SERVICES_INTERFACES[key]}`)
99+
return
100+
}
58101

59102
/**
60103
* @todo. The property should exist on "LoadedModule"
@@ -71,7 +114,7 @@ export async function generateContainerTypes(
71114

72115
result.imports.push(`import type ${serviceName} from '${servicePath}'`)
73116
result.mappings.push(
74-
`${key}: InstanceType<(typeof ${serviceName})['service']>`
117+
`${interfaceKey}: InstanceType<(typeof ${serviceName})['service']>`
75118
)
76119
})
77120
return result

packages/core/utils/src/modules-sdk/types/medusa-service.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export type AbstractModuleService<
120120
TModelName
121121
>}`]: (
122122
id: string,
123-
config?: FindConfig<any>,
123+
config?: FindConfig<TModelsDtoConfig[TModelName]["dto"]>,
124124
sharedContext?: Context
125125
) => Promise<TModelsDtoConfig[TModelName]["dto"]>
126126
} & {
@@ -129,17 +129,19 @@ export type AbstractModuleService<
129129
TModelName
130130
>}`]: (
131131
filters?: any,
132-
config?: FindConfig<any>,
132+
config?: FindConfig<TModelsDtoConfig[TModelName]["dto"]>,
133133
sharedContext?: Context
134134
) => Promise<TModelsDtoConfig[TModelName]["dto"][]>
135135
} & {
136136
[TModelName in keyof TModelsDtoConfig as `listAndCount${ExtractPluralName<
137137
TModelsDtoConfig,
138138
TModelName
139139
>}`]: {
140-
(filters?: any, config?: FindConfig<any>, sharedContext?: Context): Promise<
141-
[TModelsDtoConfig[TModelName]["dto"][], number]
142-
>
140+
(
141+
filters?: any,
142+
config?: FindConfig<TModelsDtoConfig[TModelName]["dto"]>,
143+
sharedContext?: Context
144+
): Promise<[TModelsDtoConfig[TModelName]["dto"][], number]>
143145
}
144146
} & {
145147
[TModelName in keyof TModelsDtoConfig as `delete${ExtractPluralName<

0 commit comments

Comments
 (0)