import {z} from 'zod' export type CurrencyModel = 'rub' | 'uah' | 'kzt' | 'byn' | 'usd' | 'eur' | 'gbp' | 'cny' | 'try' | 'jpy' | 'brl'; export type GetApiInvoiceListArgs = {page?: number | undefined, currency?: CurrencyModel | undefined, status?: ('paid' | 'not_paid') | undefined, amount?: number | undefined, merchant_id?: number | undefined}; export type GetApiInvoiceListOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceModel = z.object({additional_data: z.string(), amount: z.number().int(), comment: z.string(), expires_at: z.number().int(), invoice_date: z.number().int(), invoice_id: z.number().int(), is_test: z.boolean(), merchant_id: z.number().int(), paid_date: z.number().int(), payer_user_id: z.number().int(), payment_id: z.string(), resend_attempts: z.number().int(), status: z.string(), url: z.string(), url_callback: z.string(), url_success: z.string(), user_id: z.number().int()}); export const respSystemInfo = z.object({rate_limit: z.object({limit: z.number().int(), remaining: z.number().int(), reset: z.number().int(), bucket: z.string().optional()}), visitor_id: z.number().int(), time: z.number().int(), log_id: z.number().int()}); export const getApiInvoiceListResponse = z.object({invoices: z.array(invoiceModel), count: z.number().int(), page: z.number().int(), perPage: z.number().int(), system_info: respSystemInfo}); 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 Invoice List * * GET /invoice/list */ export const getApiInvoiceList = async (args: GetApiInvoiceListArgs, options?: GetApiInvoiceListOptions) => { const {baseUrl = "https://api.lzt.market", fetch: fetchFn = fetch, HTTPBearer} = 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.currency != null) query.append("currency", String(args.currency)) if (args.status != null) query.append("status", String(args.status)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.merchant_id != null) query.append("merchant_id", String(args.merchant_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoice/list${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(getApiInvoiceListResponse, await res.json()) };