import {z} from 'zod' export type RepositoryKind = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27; export type GetApiPackagesSearchArgs = {offset?: number | undefined, limit?: number | undefined, facets: boolean, ts_query_web?: string | undefined, kind?: Array | undefined, category?: (Array<1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11>) | undefined, user?: Array | undefined, org?: Array | undefined, repo?: Array | undefined, license?: Array | undefined, capabilities?: Array | undefined, deprecated?: boolean | undefined, operators?: boolean | undefined, verified_publisher?: boolean | undefined, official?: boolean | undefined, cncf?: boolean | undefined, sort?: ('relevance' | 'stars' | 'last_updated') | undefined}; export type GetApiPackagesSearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const repositoryKind = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(17), z.literal(18), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(24), z.literal(25), z.literal(26), z.literal(27)]); export const repositorySummary = z.object({repository_id: z.string(), kind: repositoryKind, name: z.string(), display_name: z.string().optional(), url: z.string(), verified_publisher: z.boolean(), official: z.boolean(), cncf: z.boolean().nullable().optional(), private: z.boolean().optional(), scanner_disabled: z.boolean(), user_alias: z.string().optional(), organization_name: z.string().optional(), organization_display_name: z.string().optional()}); export const packageCategoryId = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10), z.literal(11)]); export const packageSummary = z.object({package_id: z.string(), name: z.string(), normalized_name: z.string(), logo_image_id: z.string().optional(), display_name: z.string().optional(), description: z.string().optional(), version: z.string(), app_version: z.string().optional(), license: z.string().optional(), deprecated: z.boolean().optional(), signed: z.boolean().optional(), signatures: z.array(z.enum(["prov", "cosign"])).optional(), official: z.boolean().optional(), cncf: z.boolean().nullable().optional(), ts: z.number().int(), repository: repositorySummary, security_report_summary: z.object({critical: z.number().optional(), high: z.number().optional(), medium: z.number().optional(), low: z.number().optional(), unknown: z.number().optional()}).optional(), all_containers_images_whitelisted: z.boolean().optional(), production_organizations_count: z.number().optional(), category: packageCategoryId.optional(), category_display_name: z.string().optional(), stars: z.number().int()}); export const facets = z.object({filter_key: z.string(), title: z.string(), options: z.array(z.object({id: z.string(), name: z.string(), total: z.number().int(), filter_key: z.string().optional()}))}); export const getApiPackagesSearchResponse = z.object({packages: z.array(packageSummary).optional(), facets: z.array(facets).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 }; /** * Search packages that meet the provided criteria * * GET /packages/search */ export const getApiPackagesSearch = async (args: GetApiPackagesSearchArgs, options: GetApiPackagesSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.facets != null) query.append("facets", String(args.facets)) if (args.ts_query_web != null) query.append("ts_query_web", String(args.ts_query_web)) for (const item of args.kind ?? []) query.append("kind", String(item)) for (const item of args.category ?? []) query.append("category", String(item)) for (const item of args.user ?? []) query.append("user", String(item)) for (const item of args.org ?? []) query.append("org", String(item)) for (const item of args.repo ?? []) query.append("repo", String(item)) for (const item of args.license ?? []) query.append("license", String(item)) for (const item of args.capabilities ?? []) query.append("capabilities", String(item)) if (args.deprecated != null) query.append("deprecated", String(args.deprecated)) if (args.operators != null) query.append("operators", String(args.operators)) if (args.verified_publisher != null) query.append("verified_publisher", String(args.verified_publisher)) if (args.official != null) query.append("official", String(args.official)) if (args.cncf != null) query.append("cncf", String(args.cncf)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/packages/search${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(getApiPackagesSearchResponse, await res.json()) };