import {z} from 'zod' export type GetApiApiInsiderTransactionsArgs = {symbol?: string | undefined, cik?: number | undefined, cusip?: string | undefined, composite_figi?: string | undefined, share_class_figi?: string | undefined, filter?: string | undefined, page?: number | undefined, pageSize?: number | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiApiInsiderTransactionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const acceptedAt = z.string(); export const getApiApiInsiderTransactionsResponse = z.object({page: z.number().int().optional(), pageSize: z.number().int().optional(), totalPages: z.number().int().optional(), totalResults: z.number().int().optional(), data: z.array(z.object({ownerName: z.string().optional(), ownerCik: z.number().int().nullable().optional(), isDirector: z.boolean().optional(), isOfficer: z.boolean().optional(), isTenPercentOwner: z.boolean().optional(), officerTitle: z.string().nullable().optional(), documentType: z.string().nullable().optional(), securityTitle: z.string().nullable().optional(), transactionDate: z.string().nullable().optional(), transactionCode: z.string().nullable().optional(), shares: z.number().nullable().optional(), pricePerShare: z.number().nullable().optional(), acquiredDisposed: z.string().nullable().optional(), sharesAfter: z.number().nullable().optional(), directIndirect: z.string().nullable().optional(), isDerivative: z.boolean().optional(), exercisePrice: z.number().nullable().optional(), exerciseDate: z.string().nullable().optional(), expirationDate: z.string().nullable().optional(), underlyingSecurity: z.string().nullable().optional(), underlyingShares: z.number().nullable().optional(), accessionNumber: z.string().nullable().optional(), filingUrl: z.string().nullable().optional(), transactionValue: z.number().nullable().optional(), filingDate: z.string().nullable().optional(), acceptedAt: acceptedAt.optional()})).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 * * GET /api/insider-transactions */ export const getApiApiInsiderTransactions = async (args: GetApiApiInsiderTransactionsArgs, options?: GetApiApiInsiderTransactionsOptions) => { const {baseUrl = "https://api.stockfit.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.cik != null) query.append("cik", String(args.cik)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.composite_figi != null) query.append("composite_figi", String(args.composite_figi)) if (args.share_class_figi != null) query.append("share_class_figi", String(args.share_class_figi)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/insider-transactions${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(getApiApiInsiderTransactionsResponse, await res.json()) };