import {z} from 'zod' export type GetApiV1CompaniesCodeOwnershipChartArgs = {code: string}; export type GetApiV1CompaniesCodeOwnershipChartOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ownershipChartPointOut = z.object({base_date: z.string(), holding_ratio_pct: z.number(), delta_pct: z.number().optional(), purpose: z.string().nullable().optional(), doc_id: z.string().nullable().optional()}); export const ownershipChartSeriesOut = z.object({filer_code: z.string().nullable().optional(), filer_name: z.string().nullable().optional(), source: z.string(), trajectory_type: z.string().nullable().optional(), latest_holding_pct: z.number().nullable().optional(), points: z.array(ownershipChartPointOut)}); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const listResponseOwnershipChartSeriesOut = z.object({data: z.array(ownershipChartSeriesOut), 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 Ownership Chart * * GET /v1/companies/{code}/ownership/chart */ export const getApiV1CompaniesCodeOwnershipChart = async (args: GetApiV1CompaniesCodeOwnershipChartArgs, options: GetApiV1CompaniesCodeOwnershipChartOptions) => { 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/companies/${encodeURIComponent(String(args.code))}/ownership/chart${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(listResponseOwnershipChartSeriesOut, await res.json()) };