import {z} from 'zod' export type GetApiChannelsChannelIdLayoutsLayoutNameArgs = {channel_id: string, layout_name: string, resolved?: boolean | undefined}; export type GetApiChannelsChannelIdLayoutsLayoutNameOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const abstractComponent = z.object({name: z.string(), label: z.string().optional(), description: z.string().optional(), parameters: z.record(z.string(), z.string()).optional(), xtype: z.enum(["hst.vbox", "hst.unorderedlist", "hst.orderedlist", "hst.span", "hst.nomarkup"]).optional(), type: z.enum(["static", "managed"])}); export const layout = z.object({name: z.string(), label: z.string().optional(), description: z.string().optional(), parameters: z.record(z.string(), z.string()).optional(), type: z.enum(["abstract", "page", "xpage"]), extends: z.string().optional(), components: z.array(abstractComponent).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 a channel layout * * GET /channels/{channel_id}/layouts/{layout_name} */ export const getApiChannelsChannelIdLayoutsLayoutName = async (args: GetApiChannelsChannelIdLayoutsLayoutNameArgs, options?: GetApiChannelsChannelIdLayoutsLayoutNameOptions) => { const {baseUrl = "https://developers.bloomreach.io/delivery/site/v2/documents", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.resolved != null) query.append("resolved", String(args.resolved)) const url = `${baseUrl.replace(/\/+$/, '')}/channels/${encodeURIComponent(String(args.channel_id))}/layouts/${encodeURIComponent(String(args.layout_name))}${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(layout, await res.json()) };