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:
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user