import {z} from 'zod' export type GetApiV1StocksTickerArgs = {ticker: string}; export type GetApiV1StocksTickerOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; 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 stockProfileResponse = 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(), description: z.string().nullable().optional(), website: z.string().nullable().optional(), sector: z.string().nullable().optional(), industry: z.string().nullable().optional(), cik: z.string().nullable().optional(), lei: z.string().nullable().optional(), fiscalYearEnd: z.string().nullable().optional(), otherListings: z.array(listingDto).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 }; /** * One listing's profile: the company's description, website, sector and industry, CIK, LEI and fiscal year end, the security's ISIN and type, and the company's other listings on every covered exchange. * * GET /v1/stocks/{ticker} */ export const getApiV1StocksTicker = async (args: GetApiV1StocksTickerArgs, options?: GetApiV1StocksTickerOptions) => { 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 (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/${encodeURIComponent(String(args.ticker))}${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(stockProfileResponse, await res.json()) };