import {z} from 'zod' export type GetApiExperimentalOrgsOrgIdFindingsCountsByContextFamilyArgs = {'filter[contextLineageId]': string | Array, 'filter[status]'?: ('active' | 'ignored') | undefined, orgId: string, Authorization?: string | undefined}; export type GetApiExperimentalOrgsOrgIdFindingsCountsByContextFamilyOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiExperimentalOrgsOrgIdFindingsCountsByContextFamilyResponse = z.object({links: z.object({self: z.string(), next: z.string().nullable(), prev: z.string().nullable()}), meta: z.object({count: z.number()}), data: z.array(z.object({id: z.string(), type: z.literal("finding-count"), attributes: z.object({count: z.number().int(), worstPriority: z.union([z.literal("critical"), z.literal("high"), z.literal("medium"), z.literal("low")])})}))}); 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 }; /** * Counts Findings for the specified context families. The repeatable `filter[contextLineageId]` parameter identifies the context lineages to aggregate. * * GET /experimental/orgs/{orgId}/findings/counts-by-context-family */ export const getApiExperimentalOrgsOrgIdFindingsCountsByContextFamily = async (args: GetApiExperimentalOrgsOrgIdFindingsCountsByContextFamilyArgs, options?: GetApiExperimentalOrgsOrgIdFindingsCountsByContextFamilyOptions) => { const {baseUrl = "https://api.tessl.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[contextLineageId]'] != null) query.append("filter[contextLineageId]", String(args['filter[contextLineageId]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/experimental/orgs/${encodeURIComponent(String(args.orgId))}/findings/counts-by-context-family${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(getApiExperimentalOrgsOrgIdFindingsCountsByContextFamilyResponse, await res.json()) };