refactor(media): unify use-case I/O schemas + presenter + feature error map
Per Plan 9 (spec R1-R28): - Use cases: input + output schemas (getMedia, listMedia); deleteMedia has input schema only (void output, R12 — no presenter). - Controllers: unknown input + identity presenter on getMedia/listMedia; Promise<void> on deleteMedia. - New integrations/api/procedures.ts with mediaProcedure ([InputParseError → BAD_REQUEST], [MediaNotFoundError → NOT_FOUND]). - Router uses mediaProcedure + .input(xInputSchema). - src/index.ts exports schemas + types; src/ui/index.ts placeholder (media has no queries today); package.json adds ./ui subpath. - R25 + R26 tests added. Refactor log: §1, §2, §3.1, §3.2, §3.3, §5.1, §5.2, §6.1, §6.2 Spec: R1–R6, R8–R15, R18–R20, R22–R26 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
12
packages/media/src/integrations/api/procedures.ts
Normal file
12
packages/media/src/integrations/api/procedures.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { t } from "@repo/core-shared/trpc/init";
|
||||
import { defineErrorMiddleware } from "@repo/core-shared/trpc/define-error-middleware";
|
||||
|
||||
import { MediaNotFoundError } from "../../entities/errors/media";
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
|
||||
export const mediaProcedure = t.procedure.use(
|
||||
defineErrorMiddleware([
|
||||
[InputParseError, "BAD_REQUEST"],
|
||||
[MediaNotFoundError, "NOT_FOUND"],
|
||||
]),
|
||||
);
|
||||
138
packages/media/src/integrations/api/router.test.ts
Normal file
138
packages/media/src/integrations/api/router.test.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { injectable } from "inversify";
|
||||
import { mediaContainer } from "@/di/container";
|
||||
import { MediaModule } from "@/di/module";
|
||||
import { MEDIA_SYMBOLS } from "@/di/symbols";
|
||||
import { getMediaUseCase } from "@/application/use-cases/get-media.use-case";
|
||||
import { listMediaUseCase } from "@/application/use-cases/list-media.use-case";
|
||||
import { deleteMediaUseCase } from "@/application/use-cases/delete-media.use-case";
|
||||
import { getMediaController } from "@/interface-adapters/controllers/get-media.controller";
|
||||
import { listMediaController } from "@/interface-adapters/controllers/list-media.controller";
|
||||
import { deleteMediaController } from "@/interface-adapters/controllers/delete-media.controller";
|
||||
import { mediaRouter } from "@/integrations/api/router";
|
||||
|
||||
describe("mediaRouter", () => {
|
||||
beforeEach(() => {
|
||||
mediaContainer.unbindAll();
|
||||
mediaContainer.load(MediaModule);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mediaContainer.unbindAll();
|
||||
});
|
||||
|
||||
it("exposes getMedia, listMedia, deleteMedia procedures", () => {
|
||||
const names = Object.keys(mediaRouter._def.procedures);
|
||||
expect(names).toContain("getMedia");
|
||||
expect(names).toContain("listMedia");
|
||||
expect(names).toContain("deleteMedia");
|
||||
});
|
||||
|
||||
it("listMedia returns empty array by default", async () => {
|
||||
const caller = mediaRouter.createCaller({});
|
||||
const result = await caller.listMedia({});
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mediaRouter (R26 error mapping)", () => {
|
||||
beforeEach(() => {
|
||||
mediaContainer.unbindAll();
|
||||
mediaContainer.load(MediaModule);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mediaContainer.unbindAll();
|
||||
});
|
||||
|
||||
it("translates MediaNotFoundError → NOT_FOUND when id is missing", async () => {
|
||||
const caller = mediaRouter.createCaller({});
|
||||
try {
|
||||
await caller.getMedia({ id: "nonexistent-id" });
|
||||
throw new Error("expected throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(TRPCError);
|
||||
expect((e as TRPCError).code).toBe("NOT_FOUND");
|
||||
}
|
||||
});
|
||||
|
||||
it("translates zod parse failure → BAD_REQUEST on getMedia with empty id", async () => {
|
||||
const caller = mediaRouter.createCaller({});
|
||||
try {
|
||||
await caller.getMedia({} as unknown as { id: string });
|
||||
throw new Error("expected throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(TRPCError);
|
||||
expect((e as TRPCError).code).toBe("BAD_REQUEST");
|
||||
}
|
||||
});
|
||||
|
||||
it("translates MediaNotFoundError → NOT_FOUND on deleteMedia with missing id", async () => {
|
||||
const caller = mediaRouter.createCaller({});
|
||||
try {
|
||||
await caller.deleteMedia({ id: "nonexistent-id" });
|
||||
throw new Error("expected throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(TRPCError);
|
||||
expect((e as TRPCError).code).toBe("NOT_FOUND");
|
||||
}
|
||||
});
|
||||
|
||||
it("translates MediaNotFoundError → NOT_FOUND via NullMediaRepository", async () => {
|
||||
@injectable()
|
||||
class NullMediaRepository {
|
||||
async getMedia() {
|
||||
return undefined as never;
|
||||
}
|
||||
async listMedia() {
|
||||
return [] as never;
|
||||
}
|
||||
async deleteMedia() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mediaContainer.unbindAll();
|
||||
mediaContainer.bind(MEDIA_SYMBOLS.IMediaRepository).to(NullMediaRepository);
|
||||
mediaContainer
|
||||
.bind(MEDIA_SYMBOLS.IGetMediaUseCase)
|
||||
.toDynamicValue((ctx) =>
|
||||
getMediaUseCase(ctx.container.get(MEDIA_SYMBOLS.IMediaRepository)),
|
||||
);
|
||||
mediaContainer
|
||||
.bind(MEDIA_SYMBOLS.IListMediaUseCase)
|
||||
.toDynamicValue((ctx) =>
|
||||
listMediaUseCase(ctx.container.get(MEDIA_SYMBOLS.IMediaRepository)),
|
||||
);
|
||||
mediaContainer
|
||||
.bind(MEDIA_SYMBOLS.IDeleteMediaUseCase)
|
||||
.toDynamicValue((ctx) =>
|
||||
deleteMediaUseCase(ctx.container.get(MEDIA_SYMBOLS.IMediaRepository)),
|
||||
);
|
||||
mediaContainer
|
||||
.bind(MEDIA_SYMBOLS.IGetMediaController)
|
||||
.toDynamicValue((ctx) =>
|
||||
getMediaController(ctx.container.get(MEDIA_SYMBOLS.IGetMediaUseCase)),
|
||||
);
|
||||
mediaContainer
|
||||
.bind(MEDIA_SYMBOLS.IListMediaController)
|
||||
.toDynamicValue((ctx) =>
|
||||
listMediaController(ctx.container.get(MEDIA_SYMBOLS.IListMediaUseCase)),
|
||||
);
|
||||
mediaContainer
|
||||
.bind(MEDIA_SYMBOLS.IDeleteMediaController)
|
||||
.toDynamicValue((ctx) =>
|
||||
deleteMediaController(ctx.container.get(MEDIA_SYMBOLS.IDeleteMediaUseCase)),
|
||||
);
|
||||
|
||||
const caller = mediaRouter.createCaller({});
|
||||
try {
|
||||
await caller.getMedia({ id: "any-id" });
|
||||
throw new Error("expected throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(TRPCError);
|
||||
expect((e as TRPCError).code).toBe("NOT_FOUND");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,45 +1,31 @@
|
||||
import { z } from "zod";
|
||||
import { router, publicProcedure } from "@repo/core-shared/trpc/init";
|
||||
import { router } from "@repo/core-shared/trpc/init";
|
||||
|
||||
import { mediaContainer } from "../../di/container";
|
||||
import { MEDIA_SYMBOLS } from "../../di/symbols";
|
||||
|
||||
import { getMediaInputSchema } from "../../application/use-cases/get-media.use-case";
|
||||
import { listMediaInputSchema } from "../../application/use-cases/list-media.use-case";
|
||||
import { deleteMediaInputSchema } from "../../application/use-cases/delete-media.use-case";
|
||||
|
||||
import type { IGetMediaController } from "../../interface-adapters/controllers/get-media.controller";
|
||||
import type { IListMediaController } from "../../interface-adapters/controllers/list-media.controller";
|
||||
import type { IDeleteMediaController } from "../../interface-adapters/controllers/delete-media.controller";
|
||||
|
||||
import { mediaProcedure } from "./procedures";
|
||||
|
||||
export const mediaRouter = router({
|
||||
getMedia: publicProcedure
|
||||
.input(z.object({ id: z.string().min(1) }))
|
||||
.query(({ input }) => {
|
||||
const ctrl = mediaContainer.get<IGetMediaController>(
|
||||
MEDIA_SYMBOLS.IGetMediaController,
|
||||
);
|
||||
return ctrl(input);
|
||||
}),
|
||||
|
||||
listMedia: publicProcedure
|
||||
.input(
|
||||
z
|
||||
.object({
|
||||
limit: z.number().optional(),
|
||||
offset: z.number().optional(),
|
||||
})
|
||||
.optional(),
|
||||
)
|
||||
.query(({ input }) => {
|
||||
const ctrl = mediaContainer.get<IListMediaController>(
|
||||
MEDIA_SYMBOLS.IListMediaController,
|
||||
);
|
||||
return ctrl(input ?? {});
|
||||
}),
|
||||
|
||||
deleteMedia: publicProcedure
|
||||
.input(z.object({ id: z.string().min(1) }))
|
||||
.mutation(({ input }) => {
|
||||
const ctrl = mediaContainer.get<IDeleteMediaController>(
|
||||
MEDIA_SYMBOLS.IDeleteMediaController,
|
||||
);
|
||||
return ctrl(input);
|
||||
}),
|
||||
getMedia: mediaProcedure.input(getMediaInputSchema).query(({ input }) => {
|
||||
const ctrl = mediaContainer.get<IGetMediaController>(MEDIA_SYMBOLS.IGetMediaController);
|
||||
return ctrl(input);
|
||||
}),
|
||||
listMedia: mediaProcedure.input(listMediaInputSchema).query(({ input }) => {
|
||||
const ctrl = mediaContainer.get<IListMediaController>(MEDIA_SYMBOLS.IListMediaController);
|
||||
return ctrl(input);
|
||||
}),
|
||||
deleteMedia: mediaProcedure.input(deleteMediaInputSchema).mutation(({ input }) => {
|
||||
const ctrl = mediaContainer.get<IDeleteMediaController>(MEDIA_SYMBOLS.IDeleteMediaController);
|
||||
return ctrl(input);
|
||||
}),
|
||||
});
|
||||
|
||||
export type MediaRouter = typeof mediaRouter;
|
||||
|
||||
Reference in New Issue
Block a user