import {z} from 'zod' export type GetApiV1ListingsArgs = {query?: string | undefined, isin?: string | undefined, exchange?: string | undefined, market?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1ListingsOptions = {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 listingDto = z.object({ticker: z.string().nullable().optional(), exchange: z.string().nullable().optional(), market: z.string().nullable().optional(), currency: z.string().nullable().optional(), isin: z.string().nullable().optional(), cusip: z.string().nullable().optional(), companyName: z.string().nullable().optional(), securityType: z.string().nullable().optional(), primary: z.boolean().optional(), exchangeTradedProduct: z.boolean().optional()}); export const pageMeta = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), count: z.number().int().optional(), hasMore: z.boolean().optional(), total: z.number().int().nullable().optional()}); export const listingDtoPagedResponse = z.object({listingNotice: listingNotice.optional(), data: z.array(listingDto).nullable().optional(), meta: pageMeta.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 }; /** * Find listings by company name or ticker (query), by ISIN (isin), or list every listing of one market (exchange or market), across US and other exchanges. A US listing is its bare ticker and a listing on another exchange is MIC:TICKER, the spelling every price and company route accepts. An ISIN returns every listing of that security, each in its own currency; a US or Canadian ISIN also matches a US listing by its CUSIP. Search results rank exact tickers, then names, then larger companies; a market listing is in exchange then ticker order. * * GET /v1/listings */ export const getApiV1Listings = async (args: GetApiV1ListingsArgs, options?: GetApiV1ListingsOptions) => { 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.isin != null) query.append("isin", String(args.isin)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.market != null) query.append("market", String(args.market)) 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/listings${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(listingDtoPagedResponse, await res.json()) };