fix(utils): Fix inferrence of public method name and service registration name based on configuration (#8237)
This commit is contained in:
committed by
GitHub
parent
1b61c9b309
commit
a9abf448c6
@@ -1,4 +1,5 @@
|
||||
import { MedusaService } from "../medusa-service"
|
||||
import { model } from "../../dml"
|
||||
|
||||
const baseRepoMock = {
|
||||
serialize: jest.fn().mockImplementation((item) => item),
|
||||
@@ -52,7 +53,7 @@ describe("Abstract Module Service Factory", () => {
|
||||
})
|
||||
|
||||
describe("Main Model Methods", () => {
|
||||
let instance: medusaService
|
||||
let instance: InstanceType<typeof medusaService>
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
@@ -172,4 +173,109 @@ describe("Abstract Module Service Factory", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Custom configuration using DML", () => {
|
||||
const containerMock = {
|
||||
baseRepository: baseRepoMock,
|
||||
mainModelMockRepository: baseRepoMock,
|
||||
otherModelMock1Repository: baseRepoMock,
|
||||
otherModelMock2Repository: baseRepoMock,
|
||||
houseService: {
|
||||
retrieve: jest.fn().mockResolvedValue({ id: "1", name: "Item" }),
|
||||
list: jest.fn().mockResolvedValue([{ id: "1", name: "Item" }]),
|
||||
delete: jest.fn().mockResolvedValue(undefined),
|
||||
softDelete: jest.fn().mockResolvedValue([[], {}]),
|
||||
restore: jest.fn().mockResolvedValue([[], {}]),
|
||||
},
|
||||
carService: {
|
||||
retrieve: jest.fn().mockResolvedValue({ id: "1", name: "Item" }),
|
||||
list: jest.fn().mockResolvedValue([{ id: "1", name: "Item" }]),
|
||||
delete: jest.fn().mockResolvedValue(undefined),
|
||||
softDelete: jest.fn().mockResolvedValue([[], {}]),
|
||||
restore: jest.fn().mockResolvedValue([[], {}]),
|
||||
},
|
||||
userService: {
|
||||
retrieve: jest.fn().mockResolvedValue({ id: "1", name: "Item" }),
|
||||
list: jest.fn().mockResolvedValue([{ id: "1", name: "Item" }]),
|
||||
delete: jest.fn().mockResolvedValue(undefined),
|
||||
softDelete: jest.fn().mockResolvedValue([[], {}]),
|
||||
restore: jest.fn().mockResolvedValue([[], {}]),
|
||||
},
|
||||
}
|
||||
|
||||
const MockModel = model.define("user", {
|
||||
id: model.id().primaryKey(),
|
||||
})
|
||||
const MockModel2 = model.define("car", {
|
||||
id: model.id().primaryKey(),
|
||||
})
|
||||
const MockModel3 = model.define("house", {
|
||||
id: model.id().primaryKey(),
|
||||
})
|
||||
|
||||
const medusaService = MedusaService({
|
||||
MockModel,
|
||||
MockModel2,
|
||||
MockModel3,
|
||||
})
|
||||
|
||||
let instance: InstanceType<typeof medusaService>
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
instance = new medusaService(containerMock)
|
||||
})
|
||||
|
||||
it("should have the correct methods name while resolving the correct underlying service representation of target models", async () => {
|
||||
const prototype = Object.getPrototypeOf(instance)
|
||||
|
||||
expect(prototype).toHaveProperty("retrieveMockModel")
|
||||
expect(prototype).toHaveProperty("listMockModels")
|
||||
expect(prototype).toHaveProperty("listAndCountMockModels")
|
||||
expect(prototype).toHaveProperty("createMockModels")
|
||||
expect(prototype).toHaveProperty("updateMockModels")
|
||||
expect(prototype).toHaveProperty("deleteMockModels")
|
||||
expect(prototype).toHaveProperty("softDeleteMockModels")
|
||||
|
||||
expect(prototype).toHaveProperty("retrieveMockModel2")
|
||||
expect(prototype).toHaveProperty("listMockModel2s")
|
||||
expect(prototype).toHaveProperty("listAndCountMockModel2s")
|
||||
expect(prototype).toHaveProperty("createMockModel2s")
|
||||
expect(prototype).toHaveProperty("updateMockModel2s")
|
||||
expect(prototype).toHaveProperty("deleteMockModel2s")
|
||||
expect(prototype).toHaveProperty("softDeleteMockModel2s")
|
||||
|
||||
expect(prototype).toHaveProperty("retrieveMockModel3")
|
||||
expect(prototype).toHaveProperty("listMockModel3s")
|
||||
expect(prototype).toHaveProperty("listAndCountMockModel3s")
|
||||
expect(prototype).toHaveProperty("createMockModel3s")
|
||||
expect(prototype).toHaveProperty("updateMockModel3s")
|
||||
expect(prototype).toHaveProperty("deleteMockModel3s")
|
||||
expect(prototype).toHaveProperty("softDeleteMockModel3s")
|
||||
|
||||
let result = await instance.retrieveMockModel("1")
|
||||
expect(result).toEqual({ id: "1", name: "Item" })
|
||||
expect(containerMock.userService.retrieve).toHaveBeenCalledWith(
|
||||
"1",
|
||||
undefined,
|
||||
defaultContext
|
||||
)
|
||||
|
||||
result = await instance.retrieveMockModel2("1")
|
||||
expect(result).toEqual({ id: "1", name: "Item" })
|
||||
expect(containerMock.carService.retrieve).toHaveBeenCalledWith(
|
||||
"1",
|
||||
undefined,
|
||||
defaultContext
|
||||
)
|
||||
|
||||
result = await instance.retrieveMockModel3("1")
|
||||
expect(result).toEqual({ id: "1", name: "Item" })
|
||||
expect(containerMock.houseService.retrieve).toHaveBeenCalledWith(
|
||||
"1",
|
||||
undefined,
|
||||
defaultContext
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
ModelsConfigTemplate,
|
||||
} from "./types/medusa-service"
|
||||
import { buildModelsNameToLinkableKeysMap } from "./joiner-config-builder"
|
||||
import { DmlEntity } from "../dml"
|
||||
|
||||
const readMethods = ["retrieve", "list", "listAndCount"] as BaseMethods[]
|
||||
const writeMethods = [
|
||||
@@ -46,7 +47,7 @@ const methods: BaseMethods[] = [...readMethods, ...writeMethods]
|
||||
* @internal
|
||||
*/
|
||||
function buildMethodNamesFromModel(
|
||||
modelName: string,
|
||||
defaultMethodName: string,
|
||||
model: ModelEntries[keyof ModelEntries]
|
||||
): Record<string, string> {
|
||||
return methods.reduce((acc, method) => {
|
||||
@@ -54,10 +55,14 @@ function buildMethodNamesFromModel(
|
||||
|
||||
if (method === "retrieve") {
|
||||
normalizedModelName =
|
||||
"singular" in model && model.singular ? model.singular : modelName
|
||||
"singular" in model && model.singular
|
||||
? model.singular
|
||||
: defaultMethodName
|
||||
} else {
|
||||
normalizedModelName =
|
||||
"plural" in model && model.plural ? model.plural : pluralize(modelName)
|
||||
"plural" in model && model.plural
|
||||
? model.plural
|
||||
: pluralize(defaultMethodName)
|
||||
}
|
||||
|
||||
const methodName = `${method}${upperCaseFirst(normalizedModelName)}`
|
||||
@@ -406,11 +411,11 @@ export function MedusaService<
|
||||
*/
|
||||
|
||||
const modelsMethods: [
|
||||
string,
|
||||
TModels[keyof TModels],
|
||||
Record<string, string>
|
||||
string, // model name
|
||||
TModels[keyof TModels], // configuration (dml, conf, entity)
|
||||
Record<string, string> // method names
|
||||
][] = Object.entries(models as {}).map(([name, config]) => [
|
||||
name,
|
||||
DmlEntity.isDmlEntity(config) ? config.name : name,
|
||||
config as TModels[keyof TModels],
|
||||
buildMethodNamesFromModel(name, config as TModels[keyof TModels]),
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user