import {z} from 'zod' export type CryptoCurrency = 'TON' | 'USDT'; export type CreateApiApiV1ServicesInvoicesInvoiceArgs = {app_id?: number | undefined, body: {amount: string, life_time: number, description?: string | undefined, currency?: CryptoCurrency | undefined}}; export type CreateApiApiV1ServicesInvoicesInvoiceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceStatus = z.enum(["pending", "paid", "cancelled", "expired"]); export const cryptoCurrency = z.enum(["TON", "USDT"]); export const invoicesInvoice = z.object({id: z.string(), amount: z.string(), overpayment: z.string().optional(), description: z.string(), status: invoiceStatus, pay_to_address: z.string(), paid_by_address: z.string().optional(), payment_link: z.string(), info: z.unknown(), date_change: z.number().int(), date_expire: z.number().int(), date_create: z.number().int(), currency: cryptoCurrency}); 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 }; /** * Create invoice * * POST /api/v1/services/invoices/invoice */ export const createApiApiV1ServicesInvoicesInvoice = async (args: CreateApiApiV1ServicesInvoicesInvoiceArgs, options?: CreateApiApiV1ServicesInvoicesInvoiceOptions) => { const {baseUrl = "http://localhost:8888", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.app_id != null) query.append("app_id", String(args.app_id)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/services/invoices/invoice${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(invoicesInvoice, await res.json()) };