refactor(marketing-pages): unify use-case I/O schemas + presenter + feature error map
Per Plan 9 (spec R1-R28):
- Use cases: input + output schemas (getPageBySlug, getSiteSettings).
Site-settings input is z.object({}).strict() per R5 (uniform input).
- Controllers: unknown input + identity presenter; void output not
applicable (both use cases return data).
- New integrations/api/procedures.ts with marketingPagesProcedure
([InputParseError → BAD_REQUEST], [PageNotFoundError → NOT_FOUND]).
- Router uses marketingPagesProcedure + .input(xInputSchema).
- src/index.ts: remove pageBySlugQuery/siteSettingsQuery; export
schemas + types + IUseCase/IController aliases.
- src/ui/index.ts (NEW); package.json adds ./ui subpath.
- R25 output-validation tests + R26 router error-mapping test.
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
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { ZodError } from "zod";
|
||||
import { getPageBySlugUseCase } from "@/application/use-cases/get-page-by-slug.use-case";
|
||||
import { MockPagesRepository } from "@/infrastructure/repositories/pages.repository.mock";
|
||||
|
||||
@@ -16,4 +17,23 @@ describe("getPageBySlugUseCase", () => {
|
||||
const result = await useCase({ slug: "missing-page" });
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("(R25) throws ZodError when repository returns malformed page data", async () => {
|
||||
const repo = new MockPagesRepository([
|
||||
{
|
||||
id: "p-bad",
|
||||
title: "",
|
||||
slug: "bad-page",
|
||||
hero: { heading: "" },
|
||||
layout: [],
|
||||
status: "published",
|
||||
publishedAt: null,
|
||||
seo: { title: "" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
} as never,
|
||||
]);
|
||||
const useCase = getPageBySlugUseCase(repo);
|
||||
await expect(useCase({ slug: "bad-page" })).rejects.toBeInstanceOf(ZodError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
import type { Page } from "../../entities/models/page";
|
||||
import { z } from "zod";
|
||||
|
||||
import { pageSchema } from "../../entities/models/page";
|
||||
import type { IPagesRepository } from "../repositories/pages.repository.interface";
|
||||
|
||||
// ── Input ────────────────────────────────────────────────────────────────
|
||||
export const getPageBySlugInputSchema = z.object({ slug: z.string().min(1) }).strict();
|
||||
export type GetPageBySlugInput = z.infer<typeof getPageBySlugInputSchema>;
|
||||
|
||||
// ── Output ───────────────────────────────────────────────────────────────
|
||||
export const getPageBySlugOutputSchema = pageSchema;
|
||||
export type GetPageBySlugOutput = z.infer<typeof getPageBySlugOutputSchema>;
|
||||
|
||||
// ── Use case ─────────────────────────────────────────────────────────────
|
||||
export type IGetPageBySlugUseCase = ReturnType<typeof getPageBySlugUseCase>;
|
||||
|
||||
export const getPageBySlugUseCase =
|
||||
(pagesRepository: IPagesRepository) =>
|
||||
async (input: { slug: string }): Promise<Page | undefined> => {
|
||||
return pagesRepository.getPageBySlug(input.slug);
|
||||
async (input: GetPageBySlugInput): Promise<GetPageBySlugOutput | undefined> => {
|
||||
const page = await pagesRepository.getPageBySlug(input.slug);
|
||||
if (!page) return undefined;
|
||||
return getPageBySlugOutputSchema.parse(page);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { ZodError } from "zod";
|
||||
import { getSiteSettingsUseCase } from "@/application/use-cases/get-site-settings.use-case";
|
||||
import { MockSiteSettingsRepository } from "@/infrastructure/repositories/site-settings.repository.mock";
|
||||
|
||||
@@ -6,7 +7,15 @@ describe("getSiteSettingsUseCase", () => {
|
||||
it("returns the seeded site settings", async () => {
|
||||
const repo = new MockSiteSettingsRepository();
|
||||
const useCase = getSiteSettingsUseCase(repo);
|
||||
const result = await useCase();
|
||||
const result = await useCase({});
|
||||
expect(result.siteName).toBe("My App");
|
||||
});
|
||||
|
||||
it("(R25) throws ZodError when repository returns malformed site settings", async () => {
|
||||
const malformedRepo = {
|
||||
getSiteSettings: async () => ({ siteName: "" }),
|
||||
};
|
||||
const useCase = getSiteSettingsUseCase(malformedRepo);
|
||||
await expect(useCase({})).rejects.toBeInstanceOf(ZodError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
import type { SiteSettings } from "../../entities/models/site-settings";
|
||||
import { z } from "zod";
|
||||
|
||||
import { siteSettingsSchema } from "../../entities/models/site-settings";
|
||||
import type { ISiteSettingsRepository } from "../repositories/site-settings.repository.interface";
|
||||
|
||||
// ── Input ────────────────────────────────────────────────────────────────
|
||||
export const getSiteSettingsInputSchema = z.object({}).strict();
|
||||
export type GetSiteSettingsInput = z.infer<typeof getSiteSettingsInputSchema>;
|
||||
|
||||
// ── Output ───────────────────────────────────────────────────────────────
|
||||
export const getSiteSettingsOutputSchema = siteSettingsSchema;
|
||||
export type GetSiteSettingsOutput = z.infer<typeof getSiteSettingsOutputSchema>;
|
||||
|
||||
// ── Use case ─────────────────────────────────────────────────────────────
|
||||
export type IGetSiteSettingsUseCase = ReturnType<typeof getSiteSettingsUseCase>;
|
||||
|
||||
export const getSiteSettingsUseCase =
|
||||
(siteSettingsRepository: ISiteSettingsRepository) =>
|
||||
async (): Promise<SiteSettings> => {
|
||||
return siteSettingsRepository.getSiteSettings();
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async (_input: GetSiteSettingsInput): Promise<GetSiteSettingsOutput> => {
|
||||
const result = await siteSettingsRepository.getSiteSettings();
|
||||
return getSiteSettingsOutputSchema.parse(result);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user