import {z} from 'zod' export type WebhookEventSubscriptions = {'invoice.created'?: boolean | undefined, 'invoice.balance_updated'?: boolean | undefined, 'order.created'?: boolean | undefined, 'order.updated'?: boolean | undefined, 'customer.created'?: boolean | undefined, 'customer.status_updated'?: boolean | undefined, 'customer.line_amount_updated'?: boolean | undefined, 'customer.advance_rate_updated'?: boolean | undefined, 'customer.credit_decision_created'?: boolean | undefined, 'payment.created'?: boolean | undefined, 'payment.status_changed'?: boolean | undefined, 'payout.created'?: boolean | undefined, 'payout.status_changed'?: boolean | undefined, 'merchant.created'?: boolean | undefined, 'merchant.underwriting_completed'?: boolean | undefined, 'merchant.more_info_needed'?: boolean | undefined, 'merchant.mal_updated'?: boolean | undefined}; export type CreateApiWebhooksArgs = {body: {endpoint_url: string, events: WebhookEventSubscriptions}}; export type CreateApiWebhooksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEndpoint = z.object({id: z.string().optional(), merchant_id: z.string().optional(), endpoint_url: z.string().optional(), topic: z.object({name: z.string().optional(), description: z.string().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const createApiWebhooksResponse = z.array(webhookEndpoint); 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 or update webhook endpoints * * POST /webhooks * * Also accepts auth: basicAuth */ export const createApiWebhooks = async (args: CreateApiWebhooksArgs, options?: CreateApiWebhooksOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks${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(createApiWebhooksResponse, await res.json()) };