import {z} from 'zod' export type GetApiSubaccountsCustomersCustomerIdAccountsArgs = {customerId: string, endDate?: string | undefined, afterId?: string | undefined, statuses?: Array | undefined, limit?: number | undefined}; export type GetApiSubaccountsCustomersCustomerIdAccountsOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const feeCalculationStateType = z.enum(["ONGOING", "DONE"]); export const feeType = z.enum(["ADVISORY", "MODEL", "MANAGEMENT", "CUSTODY"]); export const accruedFeeType = z.object({accruedValue: z.string(), endPeriod: z.string(), rate: z.number().gte(0).lte(1), startPeriod: z.string(), state: feeCalculationStateType, totalBalanceInUSD: z.string().optional(), type: feeType}); export const balance = z.object({amount: z.string().optional(), description: z.string().optional()}); export const balanceDetails = z.object({assetType: z.string().optional(), availableForTrading: z.string().optional(), availableForWithdrawal: z.string().optional(), entries: z.array(balance).optional(), totalBalance: z.string().optional()}); export const subaccountCustomer = z.object({customerId: z.string().optional(), role: z.enum(["PRIMARY", "SECONDARY"]).optional()}); export const subaccountCustomers = z.array(subaccountCustomer); export const feeDetailType = z.object({isBillable: z.boolean(), rate: z.number().gte(0).lte(1), startDate: z.string(), type: feeType}); export const subaccountStatus = z.enum(["OPEN", "CLOSED", "SUBMITTED"]); export const subaccountSubType = z.enum(["ROLLOVER", "CONVERSION"]); export const subaccountType = z.enum(["IRA", "ROTH", "JOINT", "CUSTODIAL", "INDIVIDUAL_TAXABLE", "INSTITUTIONAL_TAXABLE"]); export const subaccountsDetails = z.object({accruedFees: z.array(accruedFeeType), balances: z.array(balanceDetails), createdAt: z.string(), customerId: z.string(), customers: subaccountCustomers.optional(), externalSubaccountId: z.string(), fees: z.array(feeDetailType), name: z.string(), status: subaccountStatus.optional(), subaccountId: z.string(), subType: subaccountSubType.optional(), type: subaccountType.optional()}); export const page = z.object({next: z.string().nullable().optional()}); export const subaccountsResponse = z.object({data: z.array(subaccountsDetails), page: page.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 }; /** * List subaccounts * * GET /subaccounts/customers/{customerId}/accounts */ export const getApiSubaccountsCustomersCustomerIdAccounts = async (args: GetApiSubaccountsCustomersCustomerIdAccountsArgs, options?: GetApiSubaccountsCustomersCustomerIdAccountsOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.afterId != null) query.append("afterId", String(args.afterId)) if (args.statuses != null) query.append("statuses", args.statuses.join(',')) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/subaccounts/customers/${encodeURIComponent(String(args.customerId))}/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(subaccountsResponse, await res.json()) };