import {z} from 'zod' export type TemplateRequest = {name?: (string | null) | undefined, group_id?: (string | null) | undefined, tags?: (string | null) | undefined, script?: (string | null) | undefined, back_color?: (string | null) | undefined, width?: (number | null) | undefined, height?: (number | null) | undefined, orientation?: (number | null) | undefined}; export type UpdateApiTemplatesIdArgs = {id: string, body: TemplateRequest}; export type UpdateApiTemplatesIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const moduleOption = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), value: z.string().nullable().optional()}); export const module = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), type: z.string().nullable().optional(), left: z.number().int().nullable().optional(), top: z.number().int().nullable().optional(), width: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), playlist_id: z.string().nullable().optional(), sequence: z.number().int().nullable().optional(), options: z.array(moduleOption).nullable().optional()}); export const template = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), group_name: z.string().nullable().optional(), group_id: z.string().nullable().optional(), tags: z.string().nullable().optional(), created_on: z.string().nullable().optional(), created_by_id: z.string().nullable().optional(), modified_on: z.string().nullable().optional(), modified_by_id: z.string().nullable().optional(), script: z.string().nullable().optional(), back_color: z.string().nullable().optional(), width: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), orientation: z.number().int().nullable().optional(), modules: z.array(module).nullable().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 }; /** * Update a template using PUT. Full resource update. * * PUT /templates/{id} * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const updateApiTemplatesId = async (args: UpdateApiTemplatesIdArgs, options?: UpdateApiTemplatesIdOptions) => { const {baseUrl = "https://api.reveldigital.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/templates/${encodeURIComponent(String(args.id))}${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(template, await res.json()) };