import {z} from 'zod' export type WebhookURL = string; export type WebhookJSONPatchReplaceUrl = {op: 'replace', path: '/url', value: WebhookURL}; export type EventType = 'CREATED' | 'UPDATED' | 'DELETED'; export type CreateWebhookFilter = {resource: string, eventTypes: Array}; export type WebhookJSONPatchReplaceFilters = {op: 'replace', path: '/filters', value: Array}; export type WebhookJSONPatchReplaceAlias = {op: 'replace', path: '/alias', value: string}; export type WebhookJSONPatchRequest = Array; export type PatchApiPlatformV2betaWebhooksIdArgs = {id: string, body: WebhookJSONPatchRequest}; export type PatchApiPlatformV2betaWebhooksIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const resourceID = z.string().min(32).max(64); export const organizationID = z.string(); export const webhookURL = z.string().min(1).max(1024); export const eventType = z.enum(["CREATED", "UPDATED", "DELETED"]); export const webhookFilter = z.object({resource: z.string(), eventTypes: z.array(eventType), apiVersion: z.number().int(), legacyEventNames: z.array(z.string()).optional()}); export const createdTimestamp = z.string(); export const updatedTimestamp = z.string(); export const webhook = z.object({id: resourceID, organizationId: organizationID, url: webhookURL, filters: z.array(webhookFilter), verified: z.boolean(), alias: z.string().max(100).optional(), created: createdTimestamp, updated: updatedTimestamp}); 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 webhook * * PATCH /platform/v2beta/webhooks/{id} */ export const patchApiPlatformV2betaWebhooksId = async (args: PatchApiPlatformV2betaWebhooksIdArgs, options?: PatchApiPlatformV2betaWebhooksIdOptions) => { const {baseUrl = "https://api.atlar.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json-patch+json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/platform/v2beta/webhooks/${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(webhook, await res.json()) };