import {z} from 'zod' export type GetApiPaymentlinksArgs = {start_date?: string | undefined, end_date?: string | undefined, status?: ('opened' | 'paid' | 'paid_in_cash') | undefined, device_id?: string | undefined, page_offset?: number | undefined, page_size?: number | undefined}; export type GetApiPaymentlinksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentLinkStatus = z.enum(["opened", "paid", "paid_in_cash", "expired"]); export const paymentLinkObject = z.union([z.object({id: z.string().optional(), reference: z.string().optional(), description: z.string().optional(), amount: z.number(), currency: z.string(), dueDate: z.string().optional(), status: paymentLinkStatus.optional(), paymentUrl: z.string().optional(), redirectUrlAfterPayment: z.string().optional(), enableMultiplePayment: z.boolean().optional(), nbrPayments: z.number().int().optional(), deviceId: z.string().optional(), cashier: z.string().optional(), enableCardPaymentOnly: z.boolean().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), customerName: z.string().optional(), customerPhone: z.string().optional(), customerEmail: z.string().optional(), customerLocale: z.string().optional()}), z.object({id: z.string().optional(), reference: z.string().optional(), description: z.string().optional(), amount: z.number(), currency: z.string(), dueDate: z.string().optional(), status: paymentLinkStatus.optional(), paymentUrl: z.string().optional(), redirectUrlAfterPayment: z.string().optional(), enableMultiplePayment: z.boolean().optional(), nbrPayments: z.number().int().optional(), deviceId: z.string().optional(), cashier: z.string().optional(), enableCardPaymentOnly: z.boolean().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), customerId: z.string().optional()})]); export const getApiPaymentlinksResponse = z.array(paymentLinkObject); 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 }; /** * Returns list of payment links * * GET /paymentlinks */ export const getApiPaymentlinks = async (args: GetApiPaymentlinksArgs, options?: GetApiPaymentlinksOptions) => { const {baseUrl = "https://api.test.bictorys.com/pay/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.status != null) query.append("status", String(args.status)) if (args.device_id != null) query.append("device_id", String(args.device_id)) if (args.page_offset != null) query.append("page_offset", String(args.page_offset)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/paymentlinks${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(getApiPaymentlinksResponse, await res.json()) };