import {z} from 'zod' export type WebhookEvents = '*' | 'withdrawal_address.created' | 'withdrawal_address.updated' | 'withdrawal_address.deleted' | 'checkout_session.completed' | 'checkout_session.expired' | 'checkout_session.canceled' | 'customer.subscription.created' | 'customer.subscription.started' | 'customer.subscription.deleted' | 'customer.subscription.past_due' | 'customer.subscription.unpaid' | 'customer.subscription.incomplete_expired' | 'payment_intent.payment_failed' | 'payment_intent.canceled' | 'payment_intent.succeeded' | 'invoice.finalized' | 'invoice.paid' | 'invoice.marked_as_paid' | 'invoice.marked_uncollectible' | 'invoice.payment_failed'; export type WebhookEventsDto = {events: Array}; export type UpdateWebhookEndpointDto = {description: string, url: string, isDisabled?: boolean | undefined, enabledEvents: WebhookEventsDto}; export type UpdateApiApiV1WebhookEndpointsIdArgs = {id: string, body: UpdateWebhookEndpointDto}; export type UpdateApiApiV1WebhookEndpointsIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEvents = z.enum(["*", "withdrawal_address.created", "withdrawal_address.updated", "withdrawal_address.deleted", "checkout_session.completed", "checkout_session.expired", "checkout_session.canceled", "customer.subscription.created", "customer.subscription.started", "customer.subscription.deleted", "customer.subscription.past_due", "customer.subscription.unpaid", "customer.subscription.incomplete_expired", "payment_intent.payment_failed", "payment_intent.canceled", "payment_intent.succeeded", "invoice.finalized", "invoice.paid", "invoice.marked_as_paid", "invoice.marked_uncollectible", "invoice.payment_failed"]); export const webhookEventsDto = z.object({events: z.array(webhookEvents)}); export const webhookEndpointDto = z.object({id: z.string(), description: z.string(), url: z.string(), secret: z.string(), isDisabled: z.boolean().optional(), enabledEvents: webhookEventsDto, createdAt: z.string(), updatedAt: 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 }; /** * PUT /api/v1/webhook-endpoints/{id} */ export const updateApiApiV1WebhookEndpointsId = async (args: UpdateApiApiV1WebhookEndpointsIdArgs, options?: UpdateApiApiV1WebhookEndpointsIdOptions) => { const {baseUrl = "https://api.copperx.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webhook-endpoints/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(webhookEndpointDto, await res.json()) };