import {z} from 'zod' export type OperationType = 'Add' | 'Remove' | 'Replace' | 'Move' | 'Copy' | 'Test' | 'Invalid'; export type TemplateRequestOperation = {operationType?: OperationType | undefined, path?: (string | null) | undefined, op?: (string | null) | undefined, from?: (string | null) | undefined, value: unknown}; export type IContractResolver = Record; export type TemplateRequestJsonPatchDocument = {operations?: (Array | null) | undefined, contractResolver?: IContractResolver | undefined}; export type PatchApiTemplatesIdArgs = {id: string, body: TemplateRequestJsonPatchDocument}; export type PatchApiTemplatesIdOptions = {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 }; /** * Patch a template. Patch allows partial updates by targeting specific properties of the template. * Uses the JSON Patch syntax (http://jsonpatch.com) * * PATCH /templates/{id} * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const patchApiTemplatesId = async (args: PatchApiTemplatesIdArgs, options?: PatchApiTemplatesIdOptions) => { 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: "PATCH", 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()) };