import {z} from 'zod' export type GetApiV1CoverageOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coverageTotals = z.object({companies: z.number().int(), listed_companies: z.number().int(), filings: z.number().int(), financial_data_points: z.number().int(), translated_sections: z.number().int(), financial_metrics: z.number().int()}); export const fiscalYearRange = z.object({min: z.number().int().nullable().optional(), max: z.number().int().nullable().optional()}); export const fiscalYearDepth = z.object({fiscal_year: z.number().int(), companies: z.number().int(), filings: z.number().int()}); export const metricCoverage = z.object({count: z.number().int(), total_filings: z.number().int(), coverage_pct: z.number()}); export const sectorBreakdown = z.object({sector: z.string(), companies: z.number().int()}); export const exchangeBreakdown = z.object({listing: z.string(), companies: z.number().int()}); export const filingTypeBreakdown = z.object({doc_type: z.string(), label: z.string(), count: z.number().int()}); export const dataSourceInfo = z.object({name: z.string(), operator: z.string(), url: z.string(), description: z.string()}); export const coverageOut = z.object({totals: coverageTotals, fiscal_year_range: fiscalYearRange, fiscal_year_depth: z.array(fiscalYearDepth), latest_filing_at: z.string().nullable().optional(), metric_coverage: z.record(z.string(), metricCoverage), accounting_standards: z.record(z.string(), z.number().int()), sector_breakdown: z.array(sectorBreakdown), exchange_breakdown: z.array(exchangeBreakdown), filing_types: z.array(filingTypeBreakdown), data_source: dataSourceInfo}); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const singleResponseCoverageOut = z.object({data: coverageOut, meta: axioraApiSchemasMeta}); 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 Coverage * * GET /v1/coverage */ export const getApiV1Coverage = async (options: GetApiV1CoverageOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/coverage${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(singleResponseCoverageOut, await res.json()) };