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:
@@ -5,6 +5,7 @@
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./ui": "./src/ui/index.ts",
|
||||
"./cms": "./src/integrations/cms/index.ts",
|
||||
"./api": "./src/integrations/api/router.ts",
|
||||
"./di/bind-production": "./src/di/bind-production.ts"
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -3,4 +3,23 @@ export type { SiteSettings } from "./entities/models/site-settings";
|
||||
export type { MarketingPagesRouter } from "./integrations/api/router";
|
||||
export { PageNotFoundError } from "./entities/errors/page";
|
||||
export { InputParseError } from "./entities/errors/common";
|
||||
export { pageBySlugQuery, siteSettingsQuery } from "./ui/query";
|
||||
|
||||
// Use case schemas + types (Plan 9 R18)
|
||||
export {
|
||||
getPageBySlugInputSchema,
|
||||
getPageBySlugOutputSchema,
|
||||
type GetPageBySlugInput,
|
||||
type GetPageBySlugOutput,
|
||||
type IGetPageBySlugUseCase,
|
||||
} from "./application/use-cases/get-page-by-slug.use-case";
|
||||
export {
|
||||
getSiteSettingsInputSchema,
|
||||
getSiteSettingsOutputSchema,
|
||||
type GetSiteSettingsInput,
|
||||
type GetSiteSettingsOutput,
|
||||
type IGetSiteSettingsUseCase,
|
||||
} from "./application/use-cases/get-site-settings.use-case";
|
||||
|
||||
// Controller type aliases
|
||||
export type { IGetPageBySlugController } from "./interface-adapters/controllers/get-page-by-slug.controller";
|
||||
export type { IGetSiteSettingsController } from "./interface-adapters/controllers/get-site-settings.controller";
|
||||
|
||||
12
packages/marketing-pages/src/integrations/api/procedures.ts
Normal file
12
packages/marketing-pages/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 { PageNotFoundError } from "../../entities/errors/page";
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
|
||||
export const marketingPagesProcedure = t.procedure.use(
|
||||
defineErrorMiddleware([
|
||||
[InputParseError, "BAD_REQUEST"],
|
||||
[PageNotFoundError, "NOT_FOUND"],
|
||||
]),
|
||||
);
|
||||
@@ -1,7 +1,19 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { marketingPagesRouter } from "./router";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { marketingPagesContainer } from "@/di/container";
|
||||
import { MarketingPagesModule } from "@/di/module";
|
||||
import { marketingPagesRouter } from "@/integrations/api/router";
|
||||
|
||||
describe("marketingPagesRouter", () => {
|
||||
beforeEach(() => {
|
||||
marketingPagesContainer.unbindAll();
|
||||
marketingPagesContainer.load(MarketingPagesModule);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
marketingPagesContainer.unbindAll();
|
||||
});
|
||||
|
||||
it("exposes pageBySlug + siteSettings procedures", () => {
|
||||
const names = Object.keys(marketingPagesRouter._def.procedures);
|
||||
expect(names).toContain("pageBySlug");
|
||||
@@ -16,7 +28,35 @@ describe("marketingPagesRouter", () => {
|
||||
|
||||
it("siteSettings returns site name", async () => {
|
||||
const caller = marketingPagesRouter.createCaller({});
|
||||
const result = await caller.siteSettings();
|
||||
const result = await caller.siteSettings({});
|
||||
expect(result.siteName).toBe("My App");
|
||||
});
|
||||
});
|
||||
|
||||
describe("marketingPagesRouter (R26 error mapping)", () => {
|
||||
beforeEach(() => {
|
||||
marketingPagesContainer.unbindAll();
|
||||
marketingPagesContainer.load(MarketingPagesModule);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
marketingPagesContainer.unbindAll();
|
||||
});
|
||||
|
||||
it("translates zod parse failure → BAD_REQUEST on pageBySlug", async () => {
|
||||
const caller = marketingPagesRouter.createCaller({});
|
||||
try {
|
||||
await caller.pageBySlug({} as unknown as { slug: string });
|
||||
throw new Error("expected throw");
|
||||
} catch (e) {
|
||||
expect(e).toBeInstanceOf(TRPCError);
|
||||
expect((e as TRPCError).code).toBe("BAD_REQUEST");
|
||||
}
|
||||
});
|
||||
|
||||
it("returns undefined (not NOT_FOUND) for missing slug since use case returns undefined", async () => {
|
||||
const caller = marketingPagesRouter.createCaller({});
|
||||
const result = await caller.pageBySlug({ slug: "does-not-exist" });
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
import { z } from "zod";
|
||||
import { router, publicProcedure } from "@repo/core-shared/trpc/init";
|
||||
import { router } from "@repo/core-shared/trpc/init";
|
||||
|
||||
import { marketingPagesContainer } from "../../di/container";
|
||||
import { MARKETING_PAGES_SYMBOLS } from "../../di/symbols";
|
||||
|
||||
import { getPageBySlugInputSchema } from "../../application/use-cases/get-page-by-slug.use-case";
|
||||
import { getSiteSettingsInputSchema } from "../../application/use-cases/get-site-settings.use-case";
|
||||
|
||||
import type { IGetPageBySlugController } from "../../interface-adapters/controllers/get-page-by-slug.controller";
|
||||
import type { IGetSiteSettingsController } from "../../interface-adapters/controllers/get-site-settings.controller";
|
||||
|
||||
import { marketingPagesProcedure } from "./procedures";
|
||||
|
||||
export const marketingPagesRouter = router({
|
||||
pageBySlug: publicProcedure
|
||||
.input(z.object({ slug: z.string().min(1) }))
|
||||
pageBySlug: marketingPagesProcedure
|
||||
.input(getPageBySlugInputSchema)
|
||||
.query(({ input }) => {
|
||||
const ctrl = marketingPagesContainer.get<IGetPageBySlugController>(
|
||||
MARKETING_PAGES_SYMBOLS.IGetPageBySlugController,
|
||||
@@ -15,12 +21,14 @@ export const marketingPagesRouter = router({
|
||||
return ctrl(input);
|
||||
}),
|
||||
|
||||
siteSettings: publicProcedure.query(() => {
|
||||
const ctrl = marketingPagesContainer.get<IGetSiteSettingsController>(
|
||||
MARKETING_PAGES_SYMBOLS.IGetSiteSettingsController,
|
||||
);
|
||||
return ctrl();
|
||||
}),
|
||||
siteSettings: marketingPagesProcedure
|
||||
.input(getSiteSettingsInputSchema)
|
||||
.query(({ input }) => {
|
||||
const ctrl = marketingPagesContainer.get<IGetSiteSettingsController>(
|
||||
MARKETING_PAGES_SYMBOLS.IGetSiteSettingsController,
|
||||
);
|
||||
return ctrl(input);
|
||||
}),
|
||||
});
|
||||
|
||||
export type MarketingPagesRouter = typeof marketingPagesRouter;
|
||||
|
||||
@@ -20,7 +20,7 @@ describe("getPageBySlugController", () => {
|
||||
const controller = getPageBySlugController(useCase);
|
||||
|
||||
await expect(
|
||||
controller({} as { slug: string }),
|
||||
controller({}),
|
||||
).rejects.toBeInstanceOf(InputParseError);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
import type { Page } from "../../entities/models/page";
|
||||
import type { IGetPageBySlugUseCase } from "../../application/use-cases/get-page-by-slug.use-case";
|
||||
import {
|
||||
getPageBySlugInputSchema,
|
||||
type GetPageBySlugOutput,
|
||||
type IGetPageBySlugUseCase,
|
||||
} from "../../application/use-cases/get-page-by-slug.use-case";
|
||||
|
||||
const inputSchema = z.object({
|
||||
slug: z.string().min(1),
|
||||
});
|
||||
function presenter(value: GetPageBySlugOutput) {
|
||||
return value;
|
||||
}
|
||||
|
||||
export type IGetPageBySlugController = ReturnType<typeof getPageBySlugController>;
|
||||
|
||||
export const getPageBySlugController =
|
||||
(getPageBySlugUseCase: IGetPageBySlugUseCase) =>
|
||||
async (input: Partial<z.infer<typeof inputSchema>>): Promise<Page | undefined> => {
|
||||
const parsed = inputSchema.safeParse(input);
|
||||
async (input: unknown): Promise<ReturnType<typeof presenter> | undefined> => {
|
||||
const parsed = getPageBySlugInputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
throw new InputParseError("Invalid get-page-by-slug input", {
|
||||
cause: parsed.error,
|
||||
});
|
||||
throw new InputParseError("Invalid get-page-by-slug input", { cause: parsed.error });
|
||||
}
|
||||
return getPageBySlugUseCase(parsed.data);
|
||||
const result = await getPageBySlugUseCase(parsed.data);
|
||||
if (result === undefined) return undefined;
|
||||
return presenter(result);
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ describe("getSiteSettingsController", () => {
|
||||
const useCase = getSiteSettingsUseCase(repo);
|
||||
const controller = getSiteSettingsController(useCase);
|
||||
|
||||
const result = await controller();
|
||||
const result = await controller({});
|
||||
expect(result.siteName).toBe("My App");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
import type { SiteSettings } from "../../entities/models/site-settings";
|
||||
import type { IGetSiteSettingsUseCase } from "../../application/use-cases/get-site-settings.use-case";
|
||||
import { InputParseError } from "../../entities/errors/common";
|
||||
import {
|
||||
getSiteSettingsInputSchema,
|
||||
type GetSiteSettingsOutput,
|
||||
type IGetSiteSettingsUseCase,
|
||||
} from "../../application/use-cases/get-site-settings.use-case";
|
||||
|
||||
function presenter(value: GetSiteSettingsOutput) {
|
||||
return value;
|
||||
}
|
||||
|
||||
export type IGetSiteSettingsController = ReturnType<typeof getSiteSettingsController>;
|
||||
|
||||
export const getSiteSettingsController =
|
||||
(getSiteSettingsUseCase: IGetSiteSettingsUseCase) =>
|
||||
async (): Promise<SiteSettings> => {
|
||||
return getSiteSettingsUseCase();
|
||||
async (input: unknown): Promise<ReturnType<typeof presenter>> => {
|
||||
const parsed = getSiteSettingsInputSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
throw new InputParseError("Invalid get-site-settings input", { cause: parsed.error });
|
||||
}
|
||||
const result = await getSiteSettingsUseCase(parsed.data);
|
||||
return presenter(result);
|
||||
};
|
||||
|
||||
1
packages/marketing-pages/src/ui/index.ts
Normal file
1
packages/marketing-pages/src/ui/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { pageBySlugQuery, siteSettingsQuery } from "./query";
|
||||
Reference in New Issue
Block a user