import {z} from 'zod' export type DateFilterDto = {isnull?: boolean | undefined, notnull?: boolean | undefined, gt?: string | undefined, gte?: string | undefined, lt?: string | undefined, lte?: string | undefined}; export type GetApiApiV1WebhookEndpointsArgs = {page?: number | undefined, limit?: number | undefined, createdAt?: DateFilterDto | undefined, ids?: string | undefined, isDisabled?: boolean | undefined}; export type GetApiApiV1WebhookEndpointsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dateFilterDto = z.object({isnull: z.boolean().optional(), notnull: z.boolean().optional(), gt: z.string().optional(), gte: z.string().optional(), lt: z.string().optional(), lte: z.string().optional()}); export const paginationFilterDto = z.object({page: z.number().gte(1).optional(), limit: z.number().gte(1).lte(200).optional(), createdAt: dateFilterDto.optional()}); 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 getApiApiV1WebhookEndpointsResponse = z.object({page: z.number(), limit: z.number(), count: z.number(), hasMore: z.boolean(), query: paginationFilterDto.optional(), data: z.array(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 }; /** * GET /api/v1/webhook-endpoints */ export const getApiApiV1WebhookEndpoints = async (args: GetApiApiV1WebhookEndpointsArgs, options?: GetApiApiV1WebhookEndpointsOptions) => { const {baseUrl = "https://api.copperx.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.createdAt != null) query.append("createdAt", JSON.stringify(args.createdAt)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.isDisabled != null) query.append("isDisabled", String(args.isDisabled)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webhook-endpoints${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(getApiApiV1WebhookEndpointsResponse, await res.json()) };