import {z} from 'zod' export type Amount = unknown; export type Status543Enum = 'active' | 'expired' | 'cancelled'; export type PaymentLinkList = {widget_ids: Array}; export type CreateApiApiV1PaymentLinkDownloadArgs = {amount?: Amount | undefined, amount__gt?: Amount | undefined, amount__gte?: Amount | undefined, amount__lt?: Amount | undefined, amount__lte?: Amount | undefined, amount__range?: Array | undefined, created_at?: string | undefined, created_at__gt?: string | undefined, created_at__gte?: string | undefined, created_at__lt?: string | undefined, created_at__lte?: string | undefined, created_at__range?: Array | undefined, ordering?: string | undefined, paid?: boolean | undefined, search?: string | undefined, status?: Status543Enum | undefined, status__in?: Array | undefined, body: PaymentLinkList}; export type CreateApiApiV1PaymentLinkDownloadOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const status543Enum = z.enum(["active", "expired", "cancelled"]); export const paymentLinkDownloadResponse = z.object({id: z.string(), product_id: z.string(), url: z.string(), status: status543Enum.optional(), amount: z.string().nullable().optional(), currency: z.number().int().nullable().optional(), concept: z.string().max(50).nullable().optional(), external_id: z.string().max(50).nullable().optional(), created_at: z.string(), expires_at: z.string().nullable().optional(), email: z.string().max(254).nullable().optional(), reusable: z.boolean().optional(), paid: z.boolean().optional()}); 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 }; /** * Download payment links as CSV * * POST /api/v1/payment-link/download */ export const createApiApiV1PaymentLinkDownload = async (args: CreateApiApiV1PaymentLinkDownloadArgs, options: CreateApiApiV1PaymentLinkDownloadOptions) => { const {baseUrl, 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 (args.amount != null) query.append("amount", String(args.amount)) if (args.amount__gt != null) query.append("amount__gt", String(args.amount__gt)) if (args.amount__gte != null) query.append("amount__gte", String(args.amount__gte)) if (args.amount__lt != null) query.append("amount__lt", String(args.amount__lt)) if (args.amount__lte != null) query.append("amount__lte", String(args.amount__lte)) for (const item of args.amount__range ?? []) query.append("amount__range", String(item)) if (args.created_at != null) query.append("created_at", String(args.created_at)) if (args.created_at__gt != null) query.append("created_at__gt", String(args.created_at__gt)) if (args.created_at__gte != null) query.append("created_at__gte", String(args.created_at__gte)) if (args.created_at__lt != null) query.append("created_at__lt", String(args.created_at__lt)) if (args.created_at__lte != null) query.append("created_at__lte", String(args.created_at__lte)) for (const item of args.created_at__range ?? []) query.append("created_at__range", String(item)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.paid != null) query.append("paid", String(args.paid)) if (args.search != null) query.append("search", String(args.search)) if (args.status != null) query.append("status", String(args.status)) for (const item of args.status__in ?? []) query.append("status__in", String(item)) if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment-link/download${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(paymentLinkDownloadResponse, await res.json()) };