import {z} from 'zod' export type SecurityType = 'stock' | 'etf' | 'mf' | 'cef' | 'fund'; export type GetApiApiLookupSearchArgs = {searchString: string, type?: SecurityType | undefined, exchange?: string | undefined, sector?: string | undefined, includeDelisted?: boolean | undefined, limit?: number | undefined}; export type GetApiApiLookupSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const symbolSearchResult = z.object({symbol: z.string().nullable().optional(), exchange: z.string().nullable().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 getApiApiLookupSearchResponse = z.array(symbolSearchResult); 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 }; /** * Search symbols * * GET /api/lookup/search */ export const getApiApiLookupSearch = async (args: GetApiApiLookupSearchArgs, options?: GetApiApiLookupSearchOptions) => { 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.searchString != null) query.append("searchString", String(args.searchString)) 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.includeDelisted != null) query.append("includeDelisted", String(args.includeDelisted)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/lookup/search${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(getApiApiLookupSearchResponse, await res.json()) };