import {z} from 'zod' export type WebhookEventType = 'ach_transfer.created' | 'ach_transfer.updated' | 'wire_transfer.created' | 'wire_transfer.updated' | 'book_transfer.created' | 'book_transfer.updated' | 'international_payment.created' | 'international_payment.updated' | 'inbound_ach_transfer.created' | 'inbound_ach_transfer.updated' | 'inbound_wire_transfer.created' | 'inbound_wire_transfer.updated' | 'check_deposit.created' | 'check_deposit.updated' | 'global_account_transfer.created' | 'crypto_transfer.created' | 'crypto_transfer.updated' | 'account.created' | 'account.updated' | 'card.created' | 'card.updated' | 'bill.created' | 'bill.updated' | 'onboarding_consent.updated' | 'info_request.created' | 'info_request.updated' | 'application.created' | 'application.under_review' | 'application.submitted' | 'application.approved' | 'application.rejected' | 'identity_verification.action_required' | 'identity_verification.approved' | 'identity_verification.rejected' | 'message.attempt.exhausted' | 'webhook.test'; export type WebhookPayloadMode = 'snapshot' | 'thin'; export type UpdateWebhookSubscriptionRequest = {name?: (string | null) | undefined, url?: (string | null) | undefined, event_types?: (Array | null) | undefined, rotate_secret?: boolean | undefined, expire_previous_secret?: boolean | undefined, is_enabled?: (boolean | null) | undefined, payload_mode?: (WebhookPayloadMode | null) | undefined}; export type PatchApiWebhooksSubscriptionsSubscriptionIdArgs = {subscription_id: string, 'x-entity-id'?: string | undefined, body: UpdateWebhookSubscriptionRequest}; export type PatchApiWebhooksSubscriptionsSubscriptionIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEventType = z.enum(["ach_transfer.created", "ach_transfer.updated", "wire_transfer.created", "wire_transfer.updated", "book_transfer.created", "book_transfer.updated", "international_payment.created", "international_payment.updated", "inbound_ach_transfer.created", "inbound_ach_transfer.updated", "inbound_wire_transfer.created", "inbound_wire_transfer.updated", "check_deposit.created", "check_deposit.updated", "global_account_transfer.created", "crypto_transfer.created", "crypto_transfer.updated", "account.created", "account.updated", "card.created", "card.updated", "bill.created", "bill.updated", "onboarding_consent.updated", "info_request.created", "info_request.updated", "application.created", "application.under_review", "application.submitted", "application.approved", "application.rejected", "identity_verification.action_required", "identity_verification.approved", "identity_verification.rejected", "message.attempt.exhausted", "webhook.test"]); export const webhookPayloadMode = z.enum(["snapshot", "thin"]); export const webhookSubscriptionResponse = z.object({account_id: z.string().nullable().optional(), id: z.string(), name: z.string(), url: z.string(), is_enabled: z.boolean(), event_types: z.array(webhookEventType).nullable(), payload_mode: webhookPayloadMode, created_time: z.string(), updated_time: z.string()}); export const webhookSubscriptionWithSecretResponse = z.object({account_id: z.string().nullable().optional(), id: z.string(), name: z.string(), url: z.string(), is_enabled: z.boolean(), event_types: z.array(webhookEventType).nullable(), payload_mode: webhookPayloadMode, created_time: z.string(), updated_time: z.string(), secret: z.string()}); export const patchApiWebhooksSubscriptionsSubscriptionIdResponse = z.union([webhookSubscriptionResponse, webhookSubscriptionWithSecretResponse]); 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 Subscription * * PATCH /webhooks/subscriptions/{subscription_id} */ export const patchApiWebhooksSubscriptionsSubscriptionId = async (args: PatchApiWebhooksSubscriptionsSubscriptionIdArgs, options?: PatchApiWebhooksSubscriptionsSubscriptionIdOptions) => { const {baseUrl = "https://api.meow.com/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-entity-id'] != null) headers.set("x-entity-id", String(args['x-entity-id'])) if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/subscriptions/${encodeURIComponent(String(args.subscription_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(patchApiWebhooksSubscriptionsSubscriptionIdResponse, await res.json()) };