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 CreateWebhookSubscriptionRequest = {name: string, url: string, event_types?: (Array | null) | undefined, payload_mode?: WebhookPayloadMode | undefined}; export type CreateApiPartnerWebhooksSubscriptionsArgs = {body: CreateWebhookSubscriptionRequest}; export type CreateApiPartnerWebhooksSubscriptionsOptions = {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 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 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 }; /** * Create Partner Webhook Subscription * * POST /partner/webhooks/subscriptions */ export const createApiPartnerWebhooksSubscriptions = async (args: CreateApiPartnerWebhooksSubscriptionsArgs, options?: CreateApiPartnerWebhooksSubscriptionsOptions) => { 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 (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/partner/webhooks/subscriptions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(webhookSubscriptionWithSecretResponse, await res.json()) };