import {z} from 'zod' export type AddWatchRequest = {ticker: string, optionContract?: (string | null) | undefined}; export type CreateApiV1PortfoliosIdWatchesArgs = {id: string, body: AddWatchRequest}; export type CreateApiV1PortfoliosIdWatchesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const portfolioWatchDto = z.object({id: z.string().optional(), instrumentKind: z.string().nullable().optional(), ticker: z.string().nullable().optional(), exchange: z.string().nullable().optional(), name: z.string().nullable().optional(), occTicker: z.string().nullable().optional(), expirationDate: z.string().nullable().optional(), strikePrice: z.number().nullable().optional(), contractType: z.string().nullable().optional(), sharesPerContract: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), markPerUnit: z.number().nullable().optional(), markSource: z.string().nullable().optional(), markAsOf: z.string().nullable().optional(), markUnavailableReason: z.string().nullable().optional(), expired: z.boolean().optional()}); export const portfolioWatchResponse = z.object({portfolioId: z.string().optional(), portfolioName: z.string().nullable().optional(), watch: portfolioWatchDto.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 }; /** * Puts an instrument on this portfolio's watchlist without recording any position. * Pass `optionContract` with the OCC symbol to watch an option, or omit it to watch the stock. * The instrument is verified against the live reference data before anything is stored, and * the priced portfolio then lists it in `watching` with a mark and no economics. Watching an * instrument the portfolio already watches answers 400; buying an instrument you watch folds * the watch row into the held position rather than showing two rows. * * POST /v1/portfolios/{id}/watches */ export const createApiV1PortfoliosIdWatches = async (args: CreateApiV1PortfoliosIdWatchesArgs, options?: CreateApiV1PortfoliosIdWatchesOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/portfolios/${encodeURIComponent(String(args.id))}/watches${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(portfolioWatchResponse, await res.json()) };