import {z} from 'zod' export type UpdateWebhookInput = {url?: string | undefined, events?: (Array<'email.sent' | 'email.delivered' | 'email.bounced' | 'email.soft_bounced' | 'email.opened' | 'email.clicked' | 'email.complained' | 'email.unsubscribed' | 'email.failed' | 'email.inbound' | 'email.delivery_delayed' | 'email.scheduled' | 'email.suppressed' | 'contact.created' | 'contact.updated' | 'contact.deleted' | 'contact.unsubscribed' | 'domain.created' | 'domain.updated' | 'domain.deleted' | 'domain.verified' | 'domain.failed' | 'domain.dns_drift' | 'domain.dkim_rotation_started' | 'domain.dkim_rotation_activated' | 'domain.dkim_rotation_completed'>) | undefined, enabled?: boolean | undefined}; export type PatchApiWebhooksIdArgs = {id: number, body: UpdateWebhookInput}; export type PatchApiWebhooksIdOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhook = z.object({id: z.number().int(), url: z.string(), events: z.array(z.string()), signingSecret: z.string().optional(), active: z.boolean(), consecutiveFailures: z.number().int().optional(), disabledAt: z.string().nullable().optional(), disabledReason: z.string().nullable().optional(), secretRotatedAt: z.string().nullable().optional(), previousSecretExpiresAt: z.string().nullable().optional(), lastDeliveryAt: z.string().nullable().optional(), lastDeliveryStatus: z.number().int().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const patchApiWebhooksIdResponse = z.object({webhook: webhook}); 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 * * PATCH /webhooks/{id} * * Also accepts auth: bearerOAuth */ export const patchApiWebhooksId = async (args: PatchApiWebhooksIdArgs, options?: PatchApiWebhooksIdOptions) => { const {baseUrl = "https://api.poststack.dev", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(patchApiWebhooksIdResponse, await res.json()) };