import {z} from 'zod' export type BasePeriod = 'annual' | 'quarter'; export type GetApiApiEarningsChartEpsArgs = {symbol?: string | undefined, cik?: number | undefined, cusip?: string | undefined, composite_figi?: string | undefined, share_class_figi?: string | undefined, currency?: string | undefined, period?: BasePeriod | undefined, limit?: number | undefined, splitAdjust?: boolean | undefined}; export type GetApiApiEarningsChartEpsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiEarningsChartEpsResponse = z.object({periods: z.array(z.string()).optional(), series: z.array(z.object({name: z.string().optional(), data: z.array(z.number().nullable()).optional()})).optional(), rates: z.array(z.object({name: z.string().optional(), data: z.array(z.number().nullable()).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 }; /** * EPS chart * * GET /api/earnings/chart/eps */ export const getApiApiEarningsChartEps = async (args: GetApiApiEarningsChartEpsArgs, options?: GetApiApiEarningsChartEpsOptions) => { 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.currency != null) query.append("currency", String(args.currency)) if (args.period != null) query.append("period", String(args.period)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.splitAdjust != null) query.append("splitAdjust", String(args.splitAdjust)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/earnings/chart/eps${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(getApiApiEarningsChartEpsResponse, await res.json()) };