import {z} from 'zod' export type GetApiApiV2CompaniesIdFindingsStatsArgs = {id: number, start_date?: string | undefined, end_date?: string | undefined}; export type GetApiApiV2CompaniesIdFindingsStatsOptions = {baseUrl?: string | undefined, OAuthv2?: string | undefined, fetch?: typeof fetch | undefined}; export const findingStats = z.object({Critical_Failed: z.number().int().optional(), Critical_Warning: z.number().int().optional(), Critical_Passed: z.number().int().optional(), High_Failed: z.number().int().optional(), High_Warning: z.number().int().optional(), High_Passed: z.number().int().optional(), Medium_Failed: z.number().int().optional(), Medium_Warning: z.number().int().optional(), Medium_Passed: z.number().int().optional(), Low_Failed: z.number().int().optional(), Low_Warning: z.number().int().optional(), Low_Passed: z.number().int().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 }; /** * Get the finding stats of a company. * * GET /api/v2/companies/{id}/findings/stats */ export const getApiApiV2CompaniesIdFindingsStats = async (args: GetApiApiV2CompaniesIdFindingsStatsArgs, options?: GetApiApiV2CompaniesIdFindingsStatsOptions) => { const {baseUrl = "https://app.blackkitetech.com", fetch: fetchFn = fetch, OAuthv2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (OAuthv2 !== undefined) headers.set('authorization', `Bearer ${OAuthv2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/companies/${encodeURIComponent(String(args.id))}/findings/stats${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(findingStats, await res.json()) };