import {z} from 'zod' export type GetApiProjectsArgs = {'X-Request-Id'?: string | undefined, q?: string | undefined, page?: number | undefined, page_size?: number | undefined, sort?: string | undefined, name?: string | undefined, public?: boolean | undefined, owner?: string | undefined, with_detail?: boolean | undefined}; export type GetApiProjectsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const projectMetadata = z.object({public: z.string().optional(), enable_content_trust: z.string().nullable().optional(), enable_content_trust_cosign: z.string().nullable().optional(), prevent_vul: z.string().nullable().optional(), severity: z.string().nullable().optional(), auto_scan: z.string().nullable().optional(), auto_sbom_generation: z.string().nullable().optional(), reuse_sys_cve_allowlist: z.string().nullable().optional(), retention_id: z.string().nullable().optional(), proxy_speed_kb: z.string().nullable().optional(), max_upstream_conn: z.string().nullable().optional(), proxy_cache_local_on_not_found: z.string().nullable().optional()}); export const cVEAllowlistItem = z.object({cve_id: z.string().optional()}); export const cVEAllowlist = z.object({id: z.number().int().optional(), project_id: z.number().int().optional(), expires_at: z.number().int().nullable().optional(), items: z.array(cVEAllowlistItem).optional(), creation_time: z.string().optional(), update_time: z.string().optional()}); export const project = z.object({project_id: z.number().int().optional(), owner_id: z.number().int().optional(), name: z.string().optional(), registry_id: z.number().int().optional(), creation_time: z.string().optional(), update_time: z.string().optional(), deleted: z.boolean().optional(), owner_name: z.string().optional(), togglable: z.boolean().optional(), current_user_role_id: z.number().int().optional(), current_user_role_ids: z.array(z.number().int()).optional(), repo_count: z.number().int().optional(), metadata: projectMetadata.optional(), cve_allowlist: cVEAllowlist.optional()}); export const getApiProjectsResponse = z.array(project); 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 }; /** * List projects * * GET /projects */ export const getApiProjects = async (args: GetApiProjectsArgs, options?: GetApiProjectsOptions) => { const {baseUrl = "http://localhost/api/v2.0", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Request-Id'] != null) headers.set("X-Request-Id", String(args['X-Request-Id'])) if (args.q != null) query.append("q", String(args.q)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.name != null) query.append("name", String(args.name)) if (args.public != null) query.append("public", String(args.public)) if (args.owner != null) query.append("owner", String(args.owner)) if (args.with_detail != null) query.append("with_detail", String(args.with_detail)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects${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(getApiProjectsResponse, await res.json()) };