import {z} from 'zod' export type GetApiSnapshotsSnapshotIdHealthModulesArgs = {snapshot_id: string, scope?: string | undefined, counts?: string | undefined}; export type GetApiSnapshotsSnapshotIdHealthModulesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const healthModuleRollup = z.object({module: z.string(), file_count: z.number().int(), average_health: z.number(), worst_file_path: z.string().nullable().optional(), worst_file_score: z.number().nullable().optional(), nloc_weighted_health: z.number(), critical_finding_count: z.number().int().optional(), warning_finding_count: z.number().int().optional()}); export const getApiSnapshotsSnapshotIdHealthModulesResponse = z.array(healthModuleRollup); 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 }; /** * Health Modules * * GET /snapshots/{snapshot_id}/health/modules */ export const getApiSnapshotsSnapshotIdHealthModules = async (args: GetApiSnapshotsSnapshotIdHealthModulesArgs, options: GetApiSnapshotsSnapshotIdHealthModulesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.scope != null) query.append("scope", String(args.scope)) if (args.counts != null) query.append("counts", String(args.counts)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/snapshots/${encodeURIComponent(String(args.snapshot_id))}/health/modules${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(getApiSnapshotsSnapshotIdHealthModulesResponse, await res.json()) };