import {z} from 'zod' export type GetApiV1StocksTickerSplitsArgs = {ticker: string, startDate?: string | undefined, endDate?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1StocksTickerSplitsOptions = {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 stockSplitDto = z.object({effectiveDate: z.string().optional(), numerator: z.number().optional(), denominator: z.number().optional(), priceHistoryAdjusted: 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 stockSplitDtoPagedResponse = z.object({listingNotice: listingNotice.optional(), data: z.array(stockSplitDto).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 }; /** * Captured stock splits and reverse splits for an exact stock or ETF listing, newest effective date first. Each row says whether the stored daily price history has already been restated onto the post-split basis; for a listing on another exchange that is never confirmed, so treat each effective date as a break in its series. Announced splits can appear before their effective date. A share class or fund series never inherits another listing's splits. * * GET /v1/stocks/{ticker}/splits */ export const getApiV1StocksTickerSplits = async (args: GetApiV1StocksTickerSplitsArgs, options?: GetApiV1StocksTickerSplitsOptions) => { 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.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) 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/stocks/${encodeURIComponent(String(args.ticker))}/splits${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(stockSplitDtoPagedResponse, await res.json()) };