import {z} from 'zod' export type GetApiV1BuilderWebsitesWebsiteIdPagesArgs = {website_id: number, 'x-api-key': string}; export type GetApiV1BuilderWebsitesWebsiteIdPagesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const builderPage = z.object({ID: z.number().int().optional(), title: z.string().optional(), url: z.string().optional(), post_modified: z.string().optional(), post_date: z.string().optional(), twbb_created_with: z.string().optional(), post_type: z.string().optional(), template_type: z.string().optional(), is_edited: z.boolean().optional(), post_status: z.enum(["publish", "draft", "pending", "private"]).optional(), page_on_front: z.boolean().nullable().optional()}); export const getPagesResponse = z.object({msg: z.string().optional(), status: z.number().int().optional(), data: z.array(builderPage).optional()}); 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 }; /** * Get pages for a website * * GET /v1/builder/websites/{website_id}/pages */ export const getApiV1BuilderWebsitesWebsiteIdPages = async (args: GetApiV1BuilderWebsitesWebsiteIdPagesArgs, options?: GetApiV1BuilderWebsitesWebsiteIdPagesOptions) => { const {baseUrl = "https://api.10web.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/builder/websites/${encodeURIComponent(String(args.website_id))}/pages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getPagesResponse, await res.json()) };