import {z} from 'zod' export type GetApiAccountsAccountIdBalancesArgs = {AccountId: string, 'x-fapi-auth-date'?: string | undefined, 'x-fapi-customer-ip-address'?: string | undefined, 'x-fapi-interaction-id'?: string | undefined, Authorization: string, 'x-customer-user-agent'?: string | undefined, 'x-client-id'?: string | undefined}; export type GetApiAccountsAccountIdBalancesOptions = {baseUrl: string, PSUOAuth2Security?: string | undefined, fetch?: typeof fetch | undefined}; export const accountId = z.string().min(1).max(40); export const oBCreditDebitCode2 = z.enum(["Credit", "Debit"]); export const oBBalanceType1Code = z.enum(["CLAV", "CLBD", "FWAV", "INFO", "ITAV", "ITBD", "OPAV", "OPBD", "PRCD", "XPCD"]); export const oBActiveCurrencyAndAmountSimpleType = z.string(); export const activeOrHistoricCurrencyCode1 = z.string(); export const links = z.object({Self: z.string(), First: z.string().optional(), Prev: z.string().optional(), Next: z.string().optional(), Last: z.string().optional()}); export const iSODateTime = z.string(); export const meta = z.object({TotalPages: z.number().int().optional(), FirstAvailableDateTime: iSODateTime.optional(), LastAvailableDateTime: iSODateTime.optional()}); export const oBReadBalance1 = z.object({Data: z.object({Balance: z.array(z.object({AccountId: accountId, CreditDebitIndicator: oBCreditDebitCode2, Type: oBBalanceType1Code, DateTime: z.string(), Amount: z.object({Amount: oBActiveCurrencyAndAmountSimpleType, Currency: activeOrHistoricCurrencyCode1, SubType: z.enum(["BCUR", "LCUR"]).optional()}), CreditLine: z.array(z.object({Included: z.boolean(), Type: z.enum(["Available", "Credit", "Emergency", "Pre-Agreed", "Temporary"]).optional(), Amount: z.object({Amount: oBActiveCurrencyAndAmountSimpleType, Currency: activeOrHistoricCurrencyCode1}).optional()})).optional(), LocalAmount: z.object({Amount: oBActiveCurrencyAndAmountSimpleType, Currency: activeOrHistoricCurrencyCode1, SubType: z.enum(["BCUR", "LCUR"]).optional()}).optional()})), TotalValue: z.object({Amount: oBActiveCurrencyAndAmountSimpleType, Currency: activeOrHistoricCurrencyCode1}).optional()}), Links: links.optional(), Meta: meta.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 Balances for an AccountId * * GET /accounts/{AccountId}/balances */ export const getApiAccountsAccountIdBalances = async (args: GetApiAccountsAccountIdBalancesArgs, options: GetApiAccountsAccountIdBalancesOptions) => { const {baseUrl, fetch: fetchFn = fetch, PSUOAuth2Security} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-fapi-auth-date'] != null) headers.set("x-fapi-auth-date", String(args['x-fapi-auth-date'])) if (args['x-fapi-customer-ip-address'] != null) headers.set("x-fapi-customer-ip-address", String(args['x-fapi-customer-ip-address'])) if (args['x-fapi-interaction-id'] != null) headers.set("x-fapi-interaction-id", String(args['x-fapi-interaction-id'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['x-customer-user-agent'] != null) headers.set("x-customer-user-agent", String(args['x-customer-user-agent'])) if (args['x-client-id'] != null) headers.set("x-client-id", String(args['x-client-id'])) if (PSUOAuth2Security !== undefined) headers.set('authorization', `Bearer ${PSUOAuth2Security}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(oBReadBalance1, await res.json()) };