import {z} from 'zod' export type GetApiApiV2MarketDataStocksSplitsArgs = {page?: number | undefined, page_size?: number | undefined, limit?: number | undefined, order?: ('asc' | 'desc') | undefined, next?: (string | null) | undefined, previous?: (string | null) | undefined}; export type GetApiApiV2MarketDataStocksSplitsOptions = {baseUrl?: string | undefined, ApiKeyId?: string | undefined, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const stockSplitStatus1 = z.enum(["PENDING", "IN_PROGRESS", "COMPLETE"]); export const stockSplit = z.object({id: z.string(), stock_id: z.string(), split_from: z.string(), split_to: z.string(), record_date: z.string(), payable_date: z.string(), ex_date: z.string(), status: stockSplitStatus1}); export const baseOpenApiPaginationMetadata = z.object({previous: z.string().optional(), next: z.string().optional()}); export const paginatedStockSplitResponse = z.object({_sv: z.literal("PaginatedStockSplitResponse:v1").optional(), data: z.array(stockSplit), pagination_metadata: baseOpenApiPaginationMetadata}); export const getApiApiV2MarketDataStocksSplitsResponse = z.union([z.array(stockSplit), paginatedStockSplitResponse]); 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 }; /** * Get Stock Splits * * GET /api/v2/market_data/stocks/splits */ export const getApiApiV2MarketDataStocksSplits = async (args: GetApiApiV2MarketDataStocksSplitsArgs, options?: GetApiApiV2MarketDataStocksSplitsOptions) => { const {baseUrl = "https://api-enterprise.sbt.dinari.com", fetch: fetchFn = fetch, ApiKeyId, ApiSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.next != null) query.append("next", String(args.next)) if (args.previous != null) query.append("previous", String(args.previous)) if (ApiKeyId !== undefined) headers.set("X-API-Key-Id", ApiKeyId) if (ApiSecretKey !== undefined) headers.set("X-API-Secret-Key", ApiSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/market_data/stocks/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(getApiApiV2MarketDataStocksSplitsResponse, await res.json()) };