import {z} from 'zod' export type CatalogGroup = {name: string, hidden?: boolean | undefined, system?: boolean | undefined}; export type UpdateApiChannelsChannelIdComponentGroupsGroupNameArgs = {'X-Resource-Version'?: string | undefined, channel_id: string, group_name: string, body: CatalogGroup}; export type UpdateApiChannelsChannelIdComponentGroupsGroupNameOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const catalogGroup = z.object({name: z.string(), hidden: z.boolean().optional(), system: z.boolean().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 }; /** * Create or update a channel component group * * PUT /channels/{channel_id}/component_groups/{group_name} */ export const updateApiChannelsChannelIdComponentGroupsGroupName = async (args: UpdateApiChannelsChannelIdComponentGroupsGroupNameArgs, options?: UpdateApiChannelsChannelIdComponentGroupsGroupNameOptions) => { const {baseUrl = "https://developers.bloomreach.io/delivery/site/v2/documents", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Resource-Version'] != null) headers.set("X-Resource-Version", String(args['X-Resource-Version'])) const url = `${baseUrl.replace(/\/+$/, '')}/channels/${encodeURIComponent(String(args.channel_id))}/component_groups/${encodeURIComponent(String(args.group_name))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(catalogGroup, await res.json()) };