import {z} from 'zod' export type GetApiV1InstitutionsCikSectorAllocationArgs = {cik: string, reportDate?: string | undefined, groupBy?: string | undefined}; export type GetApiV1InstitutionsCikSectorAllocationOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const sectorAllocationSliceDto = z.object({name: z.string().nullable().optional(), industry: z.string().nullable().optional(), positionCount: z.number().int().optional(), value: z.number().int().optional(), percentOfPortfolio: z.number().optional()}); export const sectorAllocationResponse = z.object({name: z.string().nullable().optional(), cik: z.string().nullable().optional(), reportDate: z.string().optional(), groupBy: z.string().nullable().optional(), data: z.array(sectorAllocationSliceDto).nullable().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 }; /** * An institution's 13F portfolio allocation grouped by industry or broad sector, sorted * by percent of portfolio descending with unclassified holdings in one final slice. * * GET /v1/institutions/{cik}/sector-allocation */ export const getApiV1InstitutionsCikSectorAllocation = async (args: GetApiV1InstitutionsCikSectorAllocationArgs, options?: GetApiV1InstitutionsCikSectorAllocationOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.reportDate != null) query.append("reportDate", String(args.reportDate)) if (args.groupBy != null) query.append("groupBy", String(args.groupBy)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/institutions/${encodeURIComponent(String(args.cik))}/sector-allocation${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(sectorAllocationResponse, await res.json()) };