import {z} from 'zod' export type WebhookEventType = 'alerts.triggered' | 'alerts.resolved'; export type UpdateWebhookEndpointRequest = {name?: string | undefined, url?: string | undefined, event_types?: Array | undefined, headers?: {[key: string]: string} | undefined, timeout_ms?: number | undefined, enabled?: boolean | undefined}; export type PatchApiV1WebhookEndpointsEndpointIdArgs = {endpoint_id: string, body: UpdateWebhookEndpointRequest}; export type PatchApiV1WebhookEndpointsEndpointIdOptions = {baseUrl?: string | undefined, polyApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEventType = z.enum(["alerts.triggered", "alerts.resolved"]); export const webhookEndpoint = z.object({id: z.string(), name: z.string().min(1).max(255), url: z.string(), enabled: z.boolean(), event_types: z.array(webhookEventType), headers: z.record(z.string(), z.string()).optional(), timeout_ms: z.number().int().gte(1000).lte(30000), created_at: z.string(), updated_at: z.string()}); 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 webhook endpoint * * PATCH /v1/webhook-endpoints/{endpoint_id} */ export const patchApiV1WebhookEndpointsEndpointId = async (args: PatchApiV1WebhookEndpointsEndpointIdArgs, options?: PatchApiV1WebhookEndpointsEndpointIdOptions) => { const {baseUrl = "https://api.us.poly.ai", fetch: fetchFn = fetch, polyApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (polyApiKey !== undefined) headers.set("X-API-KEY", polyApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhook-endpoints/${encodeURIComponent(String(args.endpoint_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(webhookEndpoint, await res.json()) };