import {z} from 'zod' export type Uuid = string; export type GetApiProductReleaseUuidCollectionLatestArgs = {uuid: Uuid}; export type GetApiProductReleaseUuidCollectionLatestOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const uuid = z.string(); export const dateTime = z.string(); export const collectionBelongsToType = z.enum(["COMPONENT_RELEASE", "PRODUCT_RELEASE"]); export const collectionUpdateReasonType = z.enum(["INITIAL_RELEASE", "VEX_UPDATED", "ARTIFACT_UPDATED", "ARTIFACT_ADDED", "ARTIFACT_REMOVED"]); export const collectionUpdateReason = z.object({type: collectionUpdateReasonType, comment: z.string().optional()}); export const artifactType = z.enum(["ATTESTATION", "BOM", "BUILD_META", "CERTIFICATION", "FORMULATION", "LICENSE", "RELEASE_NOTES", "SECURITY_TXT", "THREAT_MODEL", "VULNERABILITIES", "OTHER"]); export const checksumType = z.enum(["SHA-1", "SHA-256", "SHA-384", "SHA-512", "SHA3-256", "SHA3-384", "SHA3-512", "BLAKE2b-256", "BLAKE2b-384", "BLAKE2b-512", "BLAKE3"]); export const checksum = z.object({algType: checksumType, algValue: z.string().min(40).max(128)}); export const artifactFormat = z.object({mediaType: z.string(), description: z.string().optional(), url: z.string().optional(), signatureUrl: z.string().optional(), checksums: z.array(checksum).optional()}); export const artifact = z.object({uuid: uuid, version: z.number().int(), name: z.string().optional(), type: artifactType, createdDate: dateTime, distributionIds: z.array(uuid).optional(), formats: z.array(artifactFormat)}); export const collection = z.object({uuid: uuid, version: z.number().int(), createdDate: dateTime, belongsTo: collectionBelongsToType, updateReason: collectionUpdateReason, artifacts: z.array(artifact)}); 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 the latest collection of a TEA Product Release * * GET /productRelease/{uuid}/collection/latest */ export const getApiProductReleaseUuidCollectionLatest = async (args: GetApiProductReleaseUuidCollectionLatestArgs, options?: GetApiProductReleaseUuidCollectionLatestOptions) => { const {baseUrl = "http://localhost/tea/v1.0.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/productRelease/${encodeURIComponent(String(args.uuid))}/collection/latest${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(collection, await res.json()) };