import {z} from 'zod' export type GetApiPortfolioAccountIdAllocationArgs = {accountId: string}; export type GetApiPortfolioAccountIdAllocationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const allocation = z.array(z.object({assetClass: z.object({long: z.object({STK: z.number().optional(), OPT: z.number().optional(), FUT: z.number().optional(), WAR: z.number().optional(), BOND: z.number().optional(), CASH: z.number().optional()}).optional(), short: z.object({STK: z.number().optional(), OPT: z.number().optional(), FUT: z.number().optional(), WAR: z.number().optional(), BOND: z.number().optional(), CASH: z.number().optional()}).optional()}).optional(), sector: z.object({long: z.object({Others: z.number().optional(), Utilities: z.number().optional(), Energy: z.number().optional(), Technology: z.number().optional(), Financial: z.number().optional(), Communications: z.number().optional()}).optional(), short: z.object({Industrial: z.number().optional(), Consumer: z.number().optional(), Diversified: z.number().optional()}).optional()}).optional(), group: z.object({long: z.object({Computers: z.number().optional(), Semiconductors: z.number().optional(), Others: z.number().optional(), Chemicals: z.number().optional(), Apparel: z.number().optional(), Communications: z.number().optional()}).optional(), short: z.object({Banks: z.number().optional(), Airlines: z.number().optional(), Internet: z.number().optional()}).optional()}).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 }; /** * Account Allocation * * GET /portfolio/{accountId}/allocation */ export const getApiPortfolioAccountIdAllocation = async (args: GetApiPortfolioAccountIdAllocationArgs, options?: GetApiPortfolioAccountIdAllocationOptions) => { const {baseUrl = "http://localhost:5000/v1/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/portfolio/${encodeURIComponent(String(args.accountId))}/allocation${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(allocation, await res.json()) };