import {z} from 'zod' export type GetApiV1EtfsArgs = {query: string, exchange?: string | undefined, limit?: number | undefined}; export type GetApiV1EtfsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const etfSummaryDto = z.object({ticker: z.string().nullable().optional(), name: z.string().nullable().optional(), sponsor: z.string().nullable().optional(), structure: z.string().nullable().optional(), netAssets: z.number().nullable().optional(), holdingCount: z.number().int().nullable().optional(), reportPeriodDate: z.string().nullable().optional(), exchange: z.string().nullable().optional(), currency: z.string().nullable().optional(), isin: z.string().nullable().optional()}); export const etfSearchResponse = z.object({totalMatches: z.number().int().optional(), data: z.array(etfSummaryDto).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 exchange-traded funds and products by ticker, fund name, sponsor or ISIN, US and other exchanges. US ETF identity comes from the authoritative active security-type directory, with SEC series facts attached when a matching Form NPORT-P series is stored; a fund listed on another exchange (MIC:TICKER, with its exchange, currency and ISIN) is one the exchange's directory lists and the EU instrument register files as an exchange-traded fund, commodity or note. US matches come first, largest net assets first, then other exchanges in listing-search order. * * GET /v1/etfs */ export const getApiV1Etfs = async (args: GetApiV1EtfsArgs, options?: GetApiV1EtfsOptions) => { 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.exchange != null) query.append("exchange", String(args.exchange)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/etfs${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(etfSearchResponse, await res.json()) };