feat(marketing-pages): add Page + SiteSettings entities + errors
This commit is contained in:
11
packages/marketing-pages/src/entities/errors.ts
Normal file
11
packages/marketing-pages/src/entities/errors.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export class PageNotFoundError extends Error {
|
||||
constructor(message = "Page not found", options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
}
|
||||
}
|
||||
|
||||
export class InputParseError extends Error {
|
||||
constructor(message: string, options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
}
|
||||
}
|
||||
58
packages/marketing-pages/src/entities/page.test.ts
Normal file
58
packages/marketing-pages/src/entities/page.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { pageSchema, pageStatusSchema } from "./page";
|
||||
|
||||
describe("pageSchema", () => {
|
||||
it("accepts a minimal valid page", () => {
|
||||
const result = pageSchema.parse({
|
||||
id: "p1",
|
||||
title: "About",
|
||||
slug: "about",
|
||||
hero: { heading: "About us" },
|
||||
layout: [],
|
||||
seo: { title: "About — My App" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
expect(result.status).toBe("draft");
|
||||
expect(result.publishedAt).toBeNull();
|
||||
});
|
||||
|
||||
it("accepts a published page with publishedAt", () => {
|
||||
const result = pageSchema.parse({
|
||||
id: "p1",
|
||||
title: "About",
|
||||
slug: "about",
|
||||
hero: { heading: "About us" },
|
||||
layout: [],
|
||||
status: "published",
|
||||
publishedAt: new Date(),
|
||||
seo: { title: "About" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
expect(result.status).toBe("published");
|
||||
expect(result.publishedAt).toBeInstanceOf(Date);
|
||||
});
|
||||
|
||||
it("rejects empty title", () => {
|
||||
expect(() =>
|
||||
pageSchema.parse({
|
||||
id: "p1",
|
||||
title: "",
|
||||
slug: "about",
|
||||
hero: { heading: "h" },
|
||||
layout: [],
|
||||
seo: { title: "x" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("pageStatusSchema", () => {
|
||||
it("accepts draft and published", () => {
|
||||
expect(pageStatusSchema.parse("draft")).toBe("draft");
|
||||
expect(pageStatusSchema.parse("published")).toBe("published");
|
||||
});
|
||||
});
|
||||
29
packages/marketing-pages/src/entities/page.ts
Normal file
29
packages/marketing-pages/src/entities/page.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const pageStatusSchema = z.enum(["draft", "published"]);
|
||||
|
||||
export const heroSchema = z.object({
|
||||
heading: z.string().min(1).max(255),
|
||||
subheading: z.string().optional(),
|
||||
imageId: z.string().optional(),
|
||||
});
|
||||
|
||||
export const pageSchema = z.object({
|
||||
id: z.string(),
|
||||
title: z.string().min(1).max(255),
|
||||
slug: z.string().min(1).max(255),
|
||||
hero: heroSchema,
|
||||
layout: z.array(z.unknown()),
|
||||
status: pageStatusSchema.default("draft"),
|
||||
publishedAt: z.date().nullable().default(null),
|
||||
seo: z.object({
|
||||
title: z.string().min(1),
|
||||
description: z.string().optional(),
|
||||
}),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
});
|
||||
|
||||
export type Page = z.infer<typeof pageSchema>;
|
||||
export type PageStatus = z.infer<typeof pageStatusSchema>;
|
||||
export type Hero = z.infer<typeof heroSchema>;
|
||||
8
packages/marketing-pages/src/entities/site-settings.ts
Normal file
8
packages/marketing-pages/src/entities/site-settings.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const siteSettingsSchema = z.object({
|
||||
siteName: z.string().min(1),
|
||||
siteDescription: z.string().optional(),
|
||||
});
|
||||
|
||||
export type SiteSettings = z.infer<typeof siteSettingsSchema>;
|
||||
Reference in New Issue
Block a user