import {z} from 'zod' export type GetApiApiPriceHistoryArgs = {symbol?: string | undefined, cik?: number | undefined, cusip?: string | undefined, composite_figi?: string | undefined, share_class_figi?: string | undefined, resolution?: ('1d' | '1wk' | '1mo') | undefined, from?: string | undefined, to?: string | undefined, adjusted?: boolean | undefined}; export type GetApiApiPriceHistoryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPriceHistoryResponse = z.object({cik: z.number().nullable().optional(), delayNotice: z.string().optional(), symbol: z.string().optional(), resolution: z.string().optional(), adjustedAsOf: z.string().nullable().optional(), data: z.array(z.array(z.number().nullable())).optional(), latestQuote: z.object({ts: z.string().optional(), open: z.number().nullable().optional(), high: z.number().nullable().optional(), low: z.number().nullable().optional(), close: z.number().optional(), adjClose: z.number().nullable().optional(), volume: z.number().optional()}).nullable().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 }; /** * Historical Prices * * GET /api/price/history */ export const getApiApiPriceHistory = async (args: GetApiApiPriceHistoryArgs, options?: GetApiApiPriceHistoryOptions) => { const {baseUrl = "https://api.stockfit.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.cik != null) query.append("cik", String(args.cik)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.composite_figi != null) query.append("composite_figi", String(args.composite_figi)) if (args.share_class_figi != null) query.append("share_class_figi", String(args.share_class_figi)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.adjusted != null) query.append("adjusted", String(args.adjusted)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/price/history${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(getApiApiPriceHistoryResponse, await res.json()) };