import {z} from 'zod' export type GetApiV1ShortInterestSnapshotArgs = {minDaysToCover?: number | undefined, minAverageDailyVolume?: number | undefined, minAvgDailyVolume?: number | undefined, sortBy?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1ShortInterestSnapshotOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const shortInterestSnapshotDto = z.object({ticker: z.string().nullable().optional(), name: z.string().nullable().optional(), shortPosition: z.number().int().optional(), changeInShortPosition: z.number().int().optional(), averageDailyVolume: z.number().int().nullable().optional(), daysToCover: z.number().nullable().optional()}); export const shortInterestSnapshotResponse = z.object({settlementDate: z.string().nullable().optional(), data: z.array(shortInterestSnapshotDto).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 }; /** * Latest market-wide short interest snapshot, one row per exact stock or ETF listing, sorted by days to cover descending by default. FINRA caps days to cover at 999.99: capped rows are a sentinel (almost always illiquid names with a tiny average-daily-volume denominator) and rank after real readings; pass minAverageDailyVolume (e.g. 100000) to drop illiquid names entirely. * * GET /v1/short-interest/snapshot */ export const getApiV1ShortInterestSnapshot = async (args: GetApiV1ShortInterestSnapshotArgs, options?: GetApiV1ShortInterestSnapshotOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.minDaysToCover != null) query.append("minDaysToCover", String(args.minDaysToCover)) if (args.minAverageDailyVolume != null) query.append("minAverageDailyVolume", String(args.minAverageDailyVolume)) if (args.minAvgDailyVolume != null) query.append("minAvgDailyVolume", String(args.minAvgDailyVolume)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/short-interest/snapshot${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(shortInterestSnapshotResponse, await res.json()) };