import {z} from 'zod' export type GetApiV1WebsitePagesArgs = {sort?: string | undefined, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined, 'fields[website_pages]'?: string | undefined}; export type GetApiV1WebsitePagesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const websitePagesAttributes = z.object({title: z.string().optional(), url: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const resourceIdentifier = z.object({id: z.string(), type: z.string()}); export const websitePagesIndexResponse = z.object({data: z.array(z.object({id: z.string().optional(), type: z.literal("website_pages").optional(), attributes: websitePagesAttributes.optional(), relationships: z.object({site: z.object({data: resourceIdentifier.optional()}).optional()}).optional()})).optional(), links: z.object({self: z.string().optional(), current: z.string().optional()}).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 }; /** * List website pages * * GET /v1/website_pages */ export const getApiV1WebsitePages = async (args: GetApiV1WebsitePagesArgs, options?: GetApiV1WebsitePagesOptions) => { const {baseUrl = "https://api.kajabi.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.sort != null) query.append("sort", String(args.sort)) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['fields[website_pages]'] != null) query.append("fields[website_pages]", String(args['fields[website_pages]'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/website_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(websitePagesIndexResponse, await res.json()) };