import {z} from 'zod' export type GetApiSuperuserBuyPixToTokenHistoryArgs = {page?: number | undefined, pageSize?: number | undefined, id?: string | undefined, taxId?: string | undefined, externalId?: string | undefined}; export type GetApiSuperuserBuyPixToTokenHistoryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coin = z.enum(["BRLA", "USDC", "USDT", "ETH", "GLMR", "MATIC", "CHZ"]); export const smartcontractOperationFeedbackLog = z.object({id: z.string().optional(), success: z.boolean().optional(), errorMsg: z.string().optional(), createdAt: z.string().optional()}); export const smartcontractOperationLog = z.object({id: z.string().optional(), operationName: z.string().optional(), posted: z.boolean().optional(), tx: z.string().optional(), notPostedReason: z.string().optional(), createdAt: z.string().optional(), isRetry: z.boolean().optional(), feedback: smartcontractOperationFeedbackLog.optional()}); export const pixToTokenOpLog = z.object({id: z.string().optional(), token: coin.optional(), amountBrl: z.string().optional(), amountUsd: z.string().optional(), amountToken: z.string().optional(), receiverAddress: z.string().optional(), markupAddress: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), smartContractOps: z.array(smartcontractOperationLog).optional()}); export const pixToTokenDepositLog = z.object({id: z.string().optional(), token: coin.optional(), taxId: z.string().optional(), referenceLabel: z.string().optional(), externalId: z.string().optional(), walletAddress: z.string().optional(), amountBrl: z.number().int().optional(), markupFee: z.string().optional(), markupAddress: z.string().optional(), status: z.string().optional(), payerName: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), due: z.string().optional(), pixToTokenOps: z.array(pixToTokenOpLog).optional()}); export const buyPixToTokenHistoryOutput = z.object({depositsLogs: z.array(pixToTokenDepositLog).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 }; /** * Retrieves history of pix-to-token operations * * GET /superuser/buy/pix-to-token/history * * Also accepts auth: apiKeyAuth + apiSignatureAuth + apiTimestampAuth */ export const getApiSuperuserBuyPixToTokenHistory = async (args: GetApiSuperuserBuyPixToTokenHistoryArgs, options?: GetApiSuperuserBuyPixToTokenHistoryOptions) => { const {baseUrl = "https://api.brla.digital:5567/v1", fetch: fetchFn = fetch, bearerAuth} = 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.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.id != null) query.append("id", String(args.id)) if (args.taxId != null) query.append("taxId", String(args.taxId)) if (args.externalId != null) query.append("externalId", String(args.externalId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/superuser/buy/pix-to-token/history${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(buyPixToTokenHistoryOutput, await res.json()) };