import {z} from 'zod' export type GetApiApi3AssetGroupsArgs = {name?: string | undefined, page?: number | undefined, size?: number | undefined, sort?: Array | undefined, type?: string | undefined}; export type GetApiApi3AssetGroupsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({deprecation: z.string().optional(), href: z.string().optional(), hreflang: z.string().optional(), media: z.string().optional(), rel: z.string().optional(), title: z.string().optional(), type: z.string().optional()}); export const pageMetadata = z.object({number: z.number().int().optional(), size: z.number().int().optional(), totalPages: z.number().int().optional(), totalResources: z.number().int().optional()}); export const swaggerSearchCriteriaFilter = z.object({field: z.string().optional(), lower: z.object({}).optional(), operator: z.string().optional(), upper: z.object({}).optional(), value: z.object({}).optional(), values: z.array(z.object({})).optional()}); export const searchCriteria = z.object({filters: z.array(swaggerSearchCriteriaFilter).optional(), match: z.enum(["any", "all"]).optional()}); export const vulnerabilities = z.object({critical: z.number().int().optional(), moderate: z.number().int().optional(), severe: z.number().int().optional(), total: z.number().int().optional()}); export const assetGroup = z.object({assets: z.number().int().optional(), description: z.string().optional(), id: z.object({}).optional(), links: z.array(link), name: z.string(), riskScore: z.number().optional(), searchCriteria: searchCriteria.optional(), type: z.enum(["static", "dynamic"]), vulnerabilities: vulnerabilities.optional()}); export const pagedModelAssetGroup = z.object({links: z.array(link), page: pageMetadata.optional(), resources: z.array(assetGroup)}); 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 }; /** * Asset Groups * * GET /api/3/asset_groups */ export const getApiApi3AssetGroups = async (args: GetApiApi3AssetGroupsArgs, options?: GetApiApi3AssetGroupsOptions) => { const {baseUrl = "https://:", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;charset=UTF-8") if (args.name != null) query.append("name", String(args.name)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (args.type != null) query.append("type", String(args.type)) const url = `${baseUrl.replace(/\/+$/, '')}/api/3/asset_groups${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(pagedModelAssetGroup, await res.json()) };