import {z} from 'zod' export type UpdateEndpointRequest = {url: string, removeHeaders: Array, addHeaders: {[key: string]: Array}, secureSsl?: boolean | undefined, hmacEnabled?: boolean | undefined, secret?: string | undefined, tag?: string | undefined}; export type RetryStrategyUpdateRequest = {type: 'linear' | 'exponential', interval: number, attempts: number}; export type FailureHandlingUpdateRequest = {removeAlertEndpoint: boolean, alertEndpoint: UpdateEndpointRequest, removeTriggers: Array, addTriggers: Array, removeRetryStrategy: boolean, retryStrategy: RetryStrategyUpdateRequest, divert: boolean, suspend: boolean}; export type UpdateWebhookRequest = {name: string, enabled: boolean, removeEventTypes: Array, addEventTypes: Array, endpoint: UpdateEndpointRequest, removeFailureHandling: boolean, failureHandling: FailureHandlingUpdateRequest}; export type PatchApiEventTenantLocatorWebhooksWebhookLocatorArgs = {tenantLocator: string, webhookLocator: unknown, body: UpdateWebhookRequest}; export type PatchApiEventTenantLocatorWebhooksWebhookLocatorOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const endpointResponse = z.object({url: z.string(), headers: z.record(z.string(), z.array(z.string())), secureSsl: z.boolean(), hmacEnabled: z.boolean(), secret: z.string(), tag: z.string()}); export const retryStrategyResponse = z.object({type: z.enum(["linear", "exponential"]), interval: z.number().int(), attempts: z.number().int()}); export const failureHandlingResponse = z.object({alertEndpoint: endpointResponse, triggers: z.array(z.string()), retryStrategy: retryStrategyResponse, divert: z.boolean(), suspend: z.boolean()}); export const webhookResponse = z.object({locator: z.string(), name: z.string(), enabled: z.boolean(), suspended: z.boolean(), suspendedAt: z.string(), eventTypes: z.array(z.string()), endpoint: endpointResponse, failureHandling: failureHandlingResponse}); 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 /event/{tenantLocator}/webhooks/{webhookLocator} */ export const patchApiEventTenantLocatorWebhooksWebhookLocator = async (args: PatchApiEventTenantLocatorWebhooksWebhookLocatorArgs, options: PatchApiEventTenantLocatorWebhooksWebhookLocatorOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/event/${encodeURIComponent(String(args.tenantLocator))}/webhooks/${encodeURIComponent(String(args.webhookLocator))}${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(webhookResponse, await res.json()) };