import {z} from 'zod' export type CoreSortOrder = 'ASC' | 'DESC'; export type CoreTickerValidationStatusFilter = 'Validated' | 'NonValidated'; export type CoreLockStatus = 'Unlocked' | 'Locked' | 'Archived'; export type GetApiV1DomainsDomainIdAccountsAccountIdBalancesArgs = {domainId: string, accountId: string, limit?: number | undefined, startingAfter?: string | undefined, sortBy?: ('id' | 'tickerId' | 'totalAmount' | 'reservedAmount' | 'quarantinedAmount' | 'lastUpdatedAt') | undefined, sortOrder?: CoreSortOrder | undefined, 'ticker.id'?: Array | undefined, 'ticker.name'?: Array | undefined, 'ticker.symbol'?: Array | undefined, 'ticker.ledgerId'?: Array | undefined, 'ticker.validationStatus'?: CoreTickerValidationStatusFilter | undefined, 'ticker.lock'?: Array | undefined}; export type GetApiV1DomainsDomainIdAccountsAccountIdBalancesOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreAccountReference = z.object({id: z.string(), domainId: z.string()}); export const coreBalance = z.object({accountReference: coreAccountReference, tickerId: z.string(), totalAmount: z.string(), reservedAmount: z.string(), quarantinedAmount: z.string(), availableAmount: z.string(), lastUpdatedAt: z.string()}); export const coreBalancesCollection = z.object({items: z.array(coreBalance), count: z.number().int(), currentStartingAfter: z.string().optional(), nextStartingAfter: z.string().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 }; /** * Get account balances * * GET /v1/domains/{domainId}/accounts/{accountId}/balances */ export const getApiV1DomainsDomainIdAccountsAccountIdBalances = async (args: GetApiV1DomainsDomainIdAccountsAccountIdBalancesArgs, options: GetApiV1DomainsDomainIdAccountsAccountIdBalancesOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.startingAfter != null) query.append("startingAfter", String(args.startingAfter)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) for (const item of args['ticker.id'] ?? []) query.append("ticker.id", String(item)) for (const item of args['ticker.name'] ?? []) query.append("ticker.name", String(item)) for (const item of args['ticker.symbol'] ?? []) query.append("ticker.symbol", String(item)) for (const item of args['ticker.ledgerId'] ?? []) query.append("ticker.ledgerId", String(item)) if (args['ticker.validationStatus'] != null) query.append("ticker.validationStatus", String(args['ticker.validationStatus'])) for (const item of args['ticker.lock'] ?? []) query.append("ticker.lock", String(item)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains/${encodeURIComponent(String(args.domainId))}/accounts/${encodeURIComponent(String(args.accountId))}/balances${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(coreBalancesCollection, await res.json()) };