import {z} from 'zod' export type GetApiFiatAccountsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const fiatAccountType = z.enum(["BLINC", "BLINC_TEST", "CROSSRIVER", "CROSSRIVER_TEST", "CUSTOMERSBANK", "CUSTOMERSBANK_TEST", "FIFTHTHIRD", "FIFTHTHIRD_TEST", "KINGDOM_BANK", "KINGDOM_BANK_TESTNET", "LYNQ", "LYNQ_DEV", "LYNQ_QA", "LYNQ_UAT", "N3XT", "N3XT_ALPHA", "N3XT_BETA", "N3XT_OMEGA", "SYGNUM_CONNECT", "SYGNUM_CONNECT_STAGING", "TRANSFERO", "TRANSFERO_STAGING", "ZENUS_DEV", "OTHER"]); export const fiatAsset = z.object({id: z.string().optional(), balance: z.string().optional()}); export const fiatAccount = z.object({id: z.string().optional(), type: fiatAccountType.optional(), name: z.string().optional(), address: z.string().nullable().optional(), assets: z.array(fiatAsset).optional()}); export const getFiatAccountsResponse = z.array(fiatAccount); 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 }; /** * List fiat accounts * * GET /fiat_accounts */ export const getApiFiatAccounts = async (options?: GetApiFiatAccountsOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/fiat_accounts${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(getFiatAccountsResponse, await res.json()) };