import {z} from 'zod' export type SecurityType = 'stock' | 'etf' | 'mf' | 'cef' | 'fund'; export type GetApiApiSymbolsArgs = {page?: number | undefined, pageSize?: number | undefined, type?: SecurityType | undefined, exchange?: string | undefined, sector?: string | undefined, status?: ('operating' | 'delisted') | undefined}; export type GetApiApiSymbolsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const symbolProfile = z.object({symbols: z.array(z.string()).optional(), exchanges: z.array(z.string()).optional(), name: z.string().optional(), cik: z.number().int().optional(), seriesId: z.number().int().optional(), type: z.enum(["stock", "etf", "mf", "cef", "fund"]).optional(), sector: z.string().nullable().optional(), industry: z.string().nullable().optional(), industryGroup: z.string().nullable().optional(), compositeFigi: z.string().nullable().optional(), ein: z.number().int().nullable().optional(), formerNames: z.array(z.string()).optional(), status: z.enum(["operating", "delisted"]).optional(), priorTickers: z.array(z.object({symbol: z.string().optional(), exchange: z.string().optional(), lastSeen: z.string().nullable().optional()})).optional()}); export const getApiApiSymbolsResponse = z.object({page: z.number().int().optional(), pageSize: z.number().int().optional(), totalPages: z.number().int().optional(), totalResults: z.number().int().optional(), data: z.array(symbolProfile).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 }; /** * List all symbols * * GET /api/symbols */ export const getApiApiSymbols = async (args: GetApiApiSymbolsArgs, options?: GetApiApiSymbolsOptions) => { 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.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.type != null) query.append("type", String(args.type)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.sector != null) query.append("sector", String(args.sector)) if (args.status != null) query.append("status", String(args.status)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/symbols${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(getApiApiSymbolsResponse, await res.json()) };