import {z} from 'zod' export type GetApiV3CompanyFilingsArgs = {apiKey?: string | undefined, ticker?: string | undefined, micCode?: string | undefined, exchange?: string | undefined, companyKey?: string | undefined, cusip?: string | undefined, isin?: string | undefined, figi?: string | undefined, cik?: string | undefined, fscl?: string | undefined, page?: number | undefined}; export type GetApiV3CompanyFilingsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3CompanyFilingsResponse = z.object({data: z.array(z.object({filingId: z.string(), documentType: z.enum(["annual_report", "interim_report", "earnings_press_release", "current_report", "preliminary_results", "initial_registration_statement", "follow_on_registration_statement", "merger_registration_statement", "compensation_registration_statement", "draft_registration_statement", "final_prospectus_statement", "definitive_proxy_statement", "annual_information_form", "annual_mda", "interim_mda", "investment_fund_annual_report", "investment_fund_semi_annual_report", "investment_fund_registration_statement", "investment_fund_initial_registration_statement"]), isAmendment: z.boolean(), filingDate: z.string(), reportDate: z.string().nullable(), fiscalYear: z.number().int().nullable(), fiscalQuarter: z.number().int().nullable(), hasFinancialData: z.boolean(), language: z.string().nullable(), formType: z.string().nullable(), exhibitType: z.string().nullable(), pdfUrl: z.string(), sourceUrl: z.string().nullable()})), meta: z.object({page: z.number().int(), pageSize: z.number().int(), totalCount: z.number().int(), returnedCount: z.number().int(), hasMore: z.boolean(), nextPage: z.number().int().nullable(), publicationVersion: z.string()})}); 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 filings for a company, paginated * * GET /v3/company/filings */ export const getApiV3CompanyFilings = async (args: GetApiV3CompanyFilingsArgs, options: GetApiV3CompanyFilingsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.apiKey != null) query.append("apiKey", String(args.apiKey)) if (args.ticker != null) query.append("ticker", String(args.ticker)) if (args.micCode != null) query.append("micCode", String(args.micCode)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.companyKey != null) query.append("companyKey", String(args.companyKey)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.isin != null) query.append("isin", String(args.isin)) if (args.figi != null) query.append("figi", String(args.figi)) if (args.cik != null) query.append("cik", String(args.cik)) if (args.fscl != null) query.append("fscl", String(args.fscl)) if (args.page != null) query.append("page", String(args.page)) if (ApiKeyAuth !== undefined) query.set("apiKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/company/filings${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(getApiV3CompanyFilingsResponse, await res.json()) };