import {z} from 'zod' export type Media = {auto_download: Array<'audio' | 'document' | 'voice' | 'video' | 'image.'>}; export type Webhooks = {url?: string | undefined, max_concurrent_requests?: number | undefined}; export type ApplicationSettings = {callback_backoff_delay_ms?: string | undefined, max_callback_backoff_delay_ms?: string | undefined, callback_persist?: boolean | undefined, media?: Media | undefined, webhooks?: Webhooks | undefined, on_call_pager?: string | undefined, pass_through?: boolean | undefined, sent_status?: boolean | undefined, unhealthy_interval?: number | undefined, heartbeat_interval?: number | undefined}; export type PatchApiSettingsApplicationArgs = {body: ApplicationSettings}; export type PatchApiSettingsApplicationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const meta = z.object({version: z.string().optional(), api_status: z.enum(["deprecated", "experimental", "stable"]).optional()}); export const error = z.object({code: z.number().int().optional(), title: z.string().optional(), details: z.string().optional(), href: z.string().optional()}); export const response = z.object({meta: meta.optional(), errors: z.array(error).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-Application-Settings * * PATCH /settings/application */ export const patchApiSettingsApplication = async (args: PatchApiSettingsApplicationArgs, options?: PatchApiSettingsApplicationOptions) => { const {baseUrl = "http://example.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/settings/application${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(response, await res.json()) };