import {z} from 'zod' export type GetApiOsaScansArgs = {projectId: number, page?: number | undefined, itemsPerPage?: number | undefined}; export type GetApiOsaScansOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const cxOsaApplicationDTOsStateDTO = z.object({id: z.number().int().optional(), name: z.string().optional(), failureReason: z.string().optional()}); export const cxOsaApplicationDTOsResponsesScanDtoV2 = z.object({findingsStatus: z.string().optional(), id: z.string().optional(), startAnalyzeTime: z.string().optional(), endAnalyzeTime: z.string().optional(), origin: z.string().optional(), sourceCodeOrigin: z.string().optional(), state: cxOsaApplicationDTOsStateDTO.optional(), sharedSourceLocationPaths: z.array(z.string()).optional()}); export const getApiOsaScansResponse = z.array(cxOsaApplicationDTOsResponsesScanDtoV2); 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 }; /** * Returns basic scan details for all scans associated with a specified project id * * GET /osa/scans */ export const getApiOsaScans = async (args: GetApiOsaScansArgs, options?: GetApiOsaScansOptions) => { const {baseUrl = "https://desktop-rmvpboc/cxrestapi/help", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;v=2.0") if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.page != null) query.append("page", String(args.page)) if (args.itemsPerPage != null) query.append("itemsPerPage", String(args.itemsPerPage)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/osa/scans${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(getApiOsaScansResponse, await res.json()) };