import {z} from 'zod' export type GetApiV1SettingsBalanceOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountsAccountLedgerMeta = z.object({id: z.string().nullable(), payable_id: z.string().nullable(), receivable_id: z.string().nullable()}); export const sharedSelfLink = z.string(); export const accountsAccountLinks = z.object({self: sharedSelfLink}); export const accountsAccountBalanceTypeDetails = z.object({available_amount: z.string(), is_usable: z.boolean(), pending_amount: z.string()}); export const accountsAccountBalance = z.object({object: z.literal("Account"), id: z.string(), created_at: z.string(), is_deleted: z.boolean(), is_disabled: z.boolean(), is_usable_for_payables: z.boolean(), is_usable_for_receivables: z.boolean(), is_valid: z.boolean(), ledger: accountsAccountLedgerMeta, name: z.string().max(128), type: z.literal("balance"), links: accountsAccountLinks, type_details: accountsAccountBalanceTypeDetails}); 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 }; /** * Retrieve Routable Balance Account * * GET /v1/settings/balance */ export const getApiV1SettingsBalance = async (options?: GetApiV1SettingsBalanceOptions) => { const {baseUrl = "https://api.routable.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/settings/balance${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(accountsAccountBalance, await res.json()) };