import {z} from 'zod' export type GetApiV1FndStocksTickerInsidersArgs = {ticker: string, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV1FndStocksTickerInsidersOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const insiderTransaction = z.object({accession: z.string().optional(), filed_at: z.string().optional(), reporter: z.string().optional(), reporter_cik: z.string().optional(), roles: z.array(z.string()).optional(), officer_title: z.string().optional(), security: z.string().optional(), transaction_date: z.string().optional(), transaction_code: z.string().optional(), transaction_description: z.string().optional(), direction: z.string().optional(), shares: z.number().optional(), price_per_share: z.number().optional(), notional_usd: z.number().optional(), shares_owned_after: z.number().optional(), ownership: z.string().optional(), derivative: z.boolean().optional()}); export const listMeta = z.object({next_cursor: z.string().optional(), as_of: z.string().optional(), total: z.number().int().optional()}); export const insidersResponse = z.object({data: z.array(insiderTransaction).optional(), meta: listMeta.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 }; /** * Insider transactions (Form 3/4/5) * * GET /v1/fnd/stocks/{ticker}/insiders * * Also accepts auth: ApiKeyQuery */ export const getApiV1FndStocksTickerInsiders = async (args: GetApiV1FndStocksTickerInsidersArgs, options?: GetApiV1FndStocksTickerInsidersOptions) => { const {baseUrl = "https://api.sifting.io", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyHeader !== undefined) headers.set("X-API-Key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fnd/stocks/${encodeURIComponent(String(args.ticker))}/insiders${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(insidersResponse, await res.json()) };