import {z} from 'zod' export type GetApiV1FundsArgs = {query: string, limit?: number | undefined}; export type GetApiV1FundsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const listingNotice = z.object({status: z.string().nullable().optional(), ticker: z.string().nullable().optional(), delistedOn: z.string().nullable().optional(), successorTicker: z.string().nullable().optional()}); export const fundDto = z.object({seriesName: z.string().nullable().optional(), registrantName: z.string().nullable().optional(), profileId: z.string().nullable().optional(), ticker: z.string().nullable().optional(), fundType: z.string().nullable().optional(), netAssets: z.number().optional(), holdingCount: z.number().int().optional(), reportedHoldingCount: z.number().int().nullable().optional(), holdingsCoverage: z.string().nullable().optional(), latestReportPeriodDate: z.string().optional()}); export const fundSearchResponse = z.object({totalMatches: z.number().int().optional(), listingNotice: listingNotice.optional(), data: z.array(fundDto).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 }; /** * Search the tracked directory of registered investment companies and UIT ETFs that file SEC Form NPORT-P. Exact identifiers and verified share-class aliases win; otherwise every punctuation-independent query word is required before a sparse any-word fallback. Results are largest by net assets first and include stored holdingCount, full reportedHoldingCount when available, holdingsCoverage, and a profileId accepted by the fund read endpoints. Money market funds and small business investment companies do not file NPORT-P; fixed-income-only trust series can also be absent from this tracked directory. * * GET /v1/funds */ export const getApiV1Funds = async (args: GetApiV1FundsArgs, options?: GetApiV1FundsOptions) => { 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.query != null) query.append("query", String(args.query)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/funds${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(fundSearchResponse, await res.json()) };