import {z} from 'zod' export type GetApiApiV1ScansScanIdArgs = {scanId: string}; export type GetApiApiV1ScansScanIdOptions = {baseUrl?: string | undefined, basicAuthApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const evaluationStatusDTO = z.enum(["pass", "warn", "fail", "notEvaluated", "excluded"]); export const publicApiCheckSlimDTO = z.object({evaluationStatus: evaluationStatusDTO.optional(), id: z.string().optional()}); export const publicApiPostProcessingStageTypeDTO = z.literal("diagnosticWarehouse"); export const publicApiPostProcessingStageStateDTO = z.enum(["ongoing", "completed", "failed"]); export const publicApiPostProcessingStageDTO = z.object({ended: z.string().optional(), name: publicApiPostProcessingStageTypeDTO, started: z.string().optional(), state: publicApiPostProcessingStageStateDTO.optional()}); export const publicApiScanDefinitionSlim = z.object({id: z.string(), name: z.string()}); export const scanStateDTO = z.enum(["queuing", "executing", "started", "cancelationRequested", "timeOutRequested", "canceled", "timedOut", "failed", "completedWithErrors", "completedWithFailures", "completedWithWarnings", "completed"]); export const publicApiScanStatusResponse = z.object({agentId: z.string().optional(), checks: z.array(publicApiCheckSlimDTO).optional(), cloudUrl: z.string(), contractDatasetCloudUrl: z.string().optional(), created: z.string(), ended: z.string().optional(), errors: z.number().int().optional(), failures: z.number().int().optional(), id: z.string(), postProcessingStages: z.array(publicApiPostProcessingStageDTO).optional(), runnerId: z.string().optional(), scanDefinition: publicApiScanDefinitionSlim.optional(), scanTime: z.string().optional(), started: z.string().optional(), state: scanStateDTO, submitted: z.string().optional(), warnings: 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 scan status * * GET /api/v1/scans/{scanId} * * Also accepts auth: cookieToken */ export const getApiApiV1ScansScanId = async (args: GetApiApiV1ScansScanIdArgs, options?: GetApiApiV1ScansScanIdOptions) => { const {baseUrl = "https://cloud.soda.io", fetch: fetchFn = fetch, basicAuthApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuthApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuthApiKey.username}:${basicAuthApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/scans/${encodeURIComponent(String(args.scanId))}${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(publicApiScanStatusResponse, await res.json()) };