import {z} from 'zod' export type AssetBuyCreate = {ticker: string, quantity: number | string, price: number | string, fee?: (number | string) | undefined, date: string, name?: (string | null) | undefined, group_id?: (string | null) | undefined, notes?: (string | null) | undefined}; export type CreateApiApiAssetsBuyArgs = {'X-Workspace-Id'?: (string | null) | undefined, body: AssetBuyCreate}; export type CreateApiApiAssetsBuyOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const assetRead = z.object({id: z.string(), user_id: z.string(), name: z.string(), type: z.string(), currency: z.string(), units: z.number().nullable().optional(), valuation_method: z.string(), purchase_date: z.string().nullable().optional(), purchase_price: z.number().nullable().optional(), sell_date: z.string().nullable().optional(), sell_price: z.number().nullable().optional(), growth_type: z.string().nullable().optional(), growth_rate: z.number().nullable().optional(), growth_frequency: z.string().nullable().optional(), growth_start_date: z.string().nullable().optional(), is_archived: z.boolean(), position: z.number().int(), current_value: z.number().nullable().optional(), current_value_primary: z.number().nullable().optional(), gain_loss: z.number().nullable().optional(), gain_loss_primary: z.number().nullable().optional(), value_count: z.number().int().optional(), source: z.string().optional(), external_id: z.string().nullable().optional(), connection_id: z.string().nullable().optional(), isin: z.string().nullable().optional(), maturity_date: z.string().nullable().optional(), group_id: z.string().nullable().optional(), ticker: z.string().nullable().optional(), ticker_exchange: z.string().nullable().optional(), last_price: z.number().nullable().optional(), last_price_at: z.string().nullable().optional(), logo_url: z.string().nullable().optional(), average_price: z.number().nullable().optional(), total_invested: z.number().nullable().optional(), realized_gain: z.number().nullable().optional(), transaction_count: z.number().int().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 }; /** * Buy Into Holding * * POST /api/assets/buy */ export const createApiApiAssetsBuy = async (args: CreateApiApiAssetsBuyArgs, options: CreateApiApiAssetsBuyOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Workspace-Id'] != null) headers.set("X-Workspace-Id", String(args['X-Workspace-Id'])) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/assets/buy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(assetRead, await res.json()) };