import {z} from 'zod' export type ContentPageType = 'INFO' | 'LEGAL' | 'MARKETING'; export type ContentPageHeadingFont = 'FUN' | 'NORMAL'; export type ContentBlockType = 'CARD_SET' | 'LINK_SET' | 'BAG_SELECTION' | 'PRODUCT_SELECTION' | 'REORDER' | 'SOCIAL_PROOF' | 'HTML'; export type ContentBlockHeader = {id: number, modified: string, sortOrder: number}; export type ContentBlockDefinitionProperties = Record; export type ContentBlockDefinition = {type: ContentBlockType, header: ContentBlockHeader, properties: ContentBlockDefinitionProperties}; export type ContentPage = {id: number, type: ContentPageType, uri: string, title: string, subtitle?: (string | null) | undefined, description: string, signupQueryParameters?: ({[key: string]: string} | null) | undefined, previewImageUrl?: (string | null) | undefined, headingFont: ContentPageHeadingFont, isSystem: boolean, isLandingPage: boolean, isPublished: boolean, blockDefinitions: Array}; export type CreateApiContentPagesArgs = {body: ContentPage}; export type CreateApiContentPagesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const contentPageType = z.enum(["INFO", "LEGAL", "MARKETING"]); export const contentPageHeadingFont = z.enum(["FUN", "NORMAL"]); export const contentBlockType = z.enum(["CARD_SET", "LINK_SET", "BAG_SELECTION", "PRODUCT_SELECTION", "REORDER", "SOCIAL_PROOF", "HTML"]); export const contentBlockHeader = z.object({id: z.number().int(), modified: z.string(), sortOrder: z.number().int().gte(0)}); export const contentBlockDefinitionProperties = z.object({}); export const contentBlockDefinition = z.object({type: contentBlockType, header: contentBlockHeader, properties: contentBlockDefinitionProperties}); export const contentPage = z.object({id: z.number().int(), type: contentPageType, uri: z.string().min(1), title: z.string().min(1), subtitle: z.string().nullable().optional(), description: z.string().min(1), signupQueryParameters: z.record(z.string(), z.string()).nullable().optional(), previewImageUrl: z.string().nullable().optional(), headingFont: contentPageHeadingFont, isSystem: z.boolean(), isLandingPage: z.boolean(), isPublished: z.boolean(), blockDefinitions: z.array(contentBlockDefinition)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * POST /content-pages */ export const createApiContentPages = async (args: CreateApiContentPagesArgs, options: CreateApiContentPagesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/content-pages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(contentPage, await res.json()) };