import {z} from 'zod' export type GetApiV1SdkWebhooksEventSchemaOptions = {baseUrl?: string | undefined, SecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEventDataDto = z.object({object: z.record(z.string(), z.unknown()), previous_attributes: z.record(z.string(), z.unknown()).nullable().optional()}); export const webhookEventRequestDto = z.object({id: z.object({}).nullable(), idempotency_key: z.object({}).nullable()}); export const webhookEventType = z.enum(["customer.created", "customer.updated", "customer.deleted", "purchase.created", "purchase.activated", "purchase.updated", "purchase.trial_ending", "purchase.trial_converted", "purchase.suspended", "purchase.past_due", "purchase.cancellation_scheduled", "purchase.cancelled", "purchase.reactivated", "purchase.expired", "purchase.renewed", "purchase.renewal_reminder", "purchase.refunded", "purchase.plan_changed", "payment.succeeded", "payment.failed", "payment.refunded", "payment.refund_failed", "payment.refund_pending", "payment.canceled", "payment.disputed", "payment.dispute_closed", "payout.paid", "payout.failed", "checkout_session.created", "checkout_session.completed", "checkout_session.expired", "customer.credit.topped_up", "customer.credit.low_balance", "customer.credit.exhausted", "customer.credit.debited", "customer.credit.granted", "customer.credit.adjusted", "customer.credit.auto_topup_failed", "usage.charged", "usage.recorded", "usage.reset", "product.created", "product.updated", "product.archived", "plan.created", "plan.updated", "plan.archived"]); export const webhookEventDto = z.object({api_version: z.string(), created: z.number(), data: webhookEventDataDto, id: z.string(), livemode: z.boolean(), request: webhookEventRequestDto, type: webhookEventType}); 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 }; /** * Get the webhook event envelope schema * * GET /v1/sdk/webhooks/event-schema */ export const getApiV1SdkWebhooksEventSchema = async (options?: GetApiV1SdkWebhooksEventSchemaOptions) => { const {baseUrl = "https://api.solvapay.com", fetch: fetchFn = fetch, SecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (SecretKey !== undefined) headers.set('authorization', `Bearer ${SecretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sdk/webhooks/event-schema${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(webhookEventDto, await res.json()) };