import {z} from 'zod' export type GetApiTeamsTeamIdInsightsHealthArgs = {team_id: string}; export type GetApiTeamsTeamIdInsightsHealthOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const teamHealthRollup = z.object({team_id: z.string(), repo_count: z.number().int().optional(), repos_with_health: z.number().int().optional(), average_health: z.number().nullable().optional(), worst_repo_name: z.string().nullable().optional(), worst_repo_score: z.number().nullable().optional(), total_critical_findings: z.number().int().optional(), total_warning_findings: z.number().int().optional()}); export const teamHealthBiomarker = z.object({file_path: z.string(), biomarker_type: z.string(), severity: z.string(), health_impact: z.number().optional()}); export const teamRepoHealthCard = z.object({repo_id: z.string(), repo_name: z.string(), snapshot_id: z.string().nullable().optional(), state: z.string().optional(), average_health: z.number().nullable().optional(), hotspot_health: z.number().nullable().optional(), worst_performer_path: z.string().nullable().optional(), worst_performer_score: z.number().nullable().optional(), file_count: z.number().int().optional(), critical_finding_count: z.number().int().optional(), warning_finding_count: z.number().int().optional(), worst_biomarkers: z.array(teamHealthBiomarker).optional()}); export const teamHealthResponse = z.object({rollup: teamHealthRollup, repos: z.array(teamRepoHealthCard)}); 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 Team Health * * GET /teams/{team_id}/insights/health */ export const getApiTeamsTeamIdInsightsHealth = async (args: GetApiTeamsTeamIdInsightsHealthArgs, options: GetApiTeamsTeamIdInsightsHealthOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/teams/${encodeURIComponent(String(args.team_id))}/insights/health${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(teamHealthResponse, await res.json()) };