import {z} from 'zod' export type GetApiV1SectorsSectorNameArgs = {sector_name: string, fiscal_year?: (number | null) | undefined}; export type GetApiV1SectorsSectorNameOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sectorAggregates = z.object({total_revenue: z.number().int().nullable().optional(), avg_revenue: z.number().nullable().optional(), min_revenue: z.number().int().nullable().optional(), max_revenue: z.number().int().nullable().optional(), total_net_income: z.number().int().nullable().optional(), avg_net_income: z.number().nullable().optional(), avg_roe: z.number().nullable().optional(), avg_roa: z.number().nullable().optional(), avg_operating_margin: z.number().nullable().optional()}); export const sectorStatsOut = z.object({sector: z.string(), company_count: z.number().int(), aggregates: sectorAggregates}); export const axioraApiSchemasMeta = z.record(z.string(), z.unknown()); export const singleResponseSectorStatsOut = z.object({data: sectorStatsOut, 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 Sector Stats * * GET /v1/sectors/{sector_name} */ export const getApiV1SectorsSectorName = async (args: GetApiV1SectorsSectorNameArgs, options: GetApiV1SectorsSectorNameOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fiscal_year != null) query.append("fiscal_year", String(args.fiscal_year)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sectors/${encodeURIComponent(String(args.sector_name))}${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(singleResponseSectorStatsOut, await res.json()) };