import {z} from 'zod' export type GetApiTicketsV1AccountsAccountIdArgs = {accountId: string, page?: number | undefined, limit?: number | undefined, displayStatus?: string | undefined, state?: string | undefined, type?: string | undefined, sortBy?: string | undefined, sortOrder?: string | undefined}; export type GetApiTicketsV1AccountsAccountIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v1Pagination = z.object({currentPage: z.number().int(), limit: z.number().int(), totalItems: z.number().int(), totalPages: z.number().int()}); export const v1AssetRef = z.object({amount: z.string(), code: z.string(), type: z.string()}); export const v1QuotePayload = z.object({accountId: z.string(), assetIn: v1AssetRef, assetOut: v1AssetRef, exp: z.string(), expirationDate: z.string(), externalId: z.string().optional(), id: z.string(), meta: z.record(z.string(), z.string()), price: z.string(), productFee: v1AssetRef, providerFee: v1AssetRef, targetId: z.string(), targetType: z.string()}); export const v1Quote = z.object({quotePayload: v1QuotePayload, signature: z.string()}); export const v1Ticket = z.object({accountId: z.string(), amount: z.string(), amountUsd: z.string(), createdAt: z.string(), deadline: z.string().optional(), displayStatus: z.string(), id: z.string(), metadata: z.record(z.string(), z.string()).optional(), productFee: z.string(), quote: v1Quote.optional(), state: z.string(), targetId: z.string(), targetType: z.string(), template: z.object({}).optional(), type: z.string(), updatedAt: z.string(), userChainAddress: z.string(), withdrawDestinationId: z.string().optional()}); export const v1GetTicketsByAccountResponse = z.object({pagination: v1Pagination, tickets: z.array(v1Ticket)}); 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 }; /** * GetTicketsByAccount returns tickets for a specific account with pagination and filtering * * GET /tickets/v1/accounts/{accountId} * * Also accepts auth: BearerAuth */ export const getApiTicketsV1AccountsAccountId = async (args: GetApiTicketsV1AccountsAccountIdArgs, options?: GetApiTicketsV1AccountsAccountIdOptions) => { const {baseUrl = "https://backend.pay.kiichain.io", fetch: fetchFn = fetch, ApiKeyAuth} = 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.displayStatus != null) query.append("displayStatus", String(args.displayStatus)) if (args.state != null) query.append("state", String(args.state)) if (args.type != null) query.append("type", String(args.type)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/tickets/v1/accounts/${encodeURIComponent(String(args.accountId))}${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(v1GetTicketsByAccountResponse, await res.json()) };