import {z} from 'zod' export type GetApiChannelsChannelIdArgs = {channel_id: string}; export type GetApiChannelsChannelIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const channel = z.object({id: z.string().optional(), name: z.string(), projectName: z.string(), branch: z.string().optional(), branchOf: z.string().optional(), externalPreviewEnabled: z.boolean().optional(), externalPreviewToken: z.string().optional(), updateComponentContentOnPageSync: z.boolean().optional(), contentRootPath: z.string(), icon: z.string().optional(), locale: z.string().optional(), devices: z.array(z.string()).optional(), defaultDevice: z.string().optional(), responseHeaders: z.record(z.string(), z.string()).optional(), linkurlPrefix: z.string().optional(), cdnHost: z.string().optional(), remoteHostProtection: z.boolean().optional(), parameters: z.record(z.string(), z.object({})).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 * * GET /channels/{channel_id} */ export const getApiChannelsChannelId = async (args: GetApiChannelsChannelIdArgs, options?: GetApiChannelsChannelIdOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/channels/${encodeURIComponent(String(args.channel_id))}${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(channel, await res.json()) };