import {z} from 'zod' export type BasePeriod = 'annual' | 'quarter'; export type GetApiApiFootnotesRestructuringArgs = {symbol?: string | undefined, cik?: number | undefined, cusip?: string | undefined, composite_figi?: string | undefined, share_class_figi?: string | undefined, currency?: string | undefined, period?: BasePeriod | undefined, limit?: number | undefined}; export type GetApiApiFootnotesRestructuringOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const baseFiscalPeriod = z.enum(["FY", "Q1", "Q2", "Q3", "Q4"]); export const fxConversion = z.object({targetCurrency: z.string(), originalCurrencies: z.array(z.string()), rates: z.array(z.object({from: z.string(), method: z.enum(["spot", "average"]), rate: z.number(), effectiveDate: z.string().optional(), start: z.string().optional(), end: z.string().optional()})), unconverted: z.record(z.string(), z.array(z.string())).optional()}); export const getApiApiFootnotesRestructuringResponse = z.object({programs: z.array(z.object({member: z.string().optional(), name: z.string().optional(), currency: z.string().nullable().optional(), expectedCost: z.number().optional(), costToDate: z.number().optional(), history: z.array(z.object({period: z.string().optional(), fiscalYear: z.number().int().optional(), fiscalPeriod: baseFiscalPeriod.optional(), charges: z.number().optional(), reserve: z.number().optional(), cashPayments: z.number().optional(), costIncurred: z.number().optional(), currency: z.string().nullable().optional(), dateFiled: z.string().nullable().optional()})).optional()})).optional(), costTypes: z.array(z.object({member: z.string().optional(), name: z.string().optional(), currency: z.string().nullable().optional(), expectedCost: z.number().optional(), costToDate: z.number().optional(), history: z.array(z.object({period: z.string().optional(), fiscalYear: z.number().int().optional(), fiscalPeriod: baseFiscalPeriod.optional(), charges: z.number().optional(), reserve: z.number().optional(), cashPayments: z.number().optional(), costIncurred: z.number().optional(), currency: z.string().nullable().optional(), dateFiled: z.string().nullable().optional()})).optional()})).optional(), fx: fxConversion.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 }; /** * Restructuring Programs * * GET /api/footnotes/restructuring */ export const getApiApiFootnotesRestructuring = async (args: GetApiApiFootnotesRestructuringArgs, options?: GetApiApiFootnotesRestructuringOptions) => { 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.currency != null) query.append("currency", String(args.currency)) if (args.period != null) query.append("period", String(args.period)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/footnotes/restructuring${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(getApiApiFootnotesRestructuringResponse, await res.json()) };