import {z} from 'zod' export type ProtosScaV1SbomFormatVersion = {format?: 'SBOM_FORMAT_CYCLONEDX' | undefined, cyclonedxVersion?: ('SBOM_CYCLONE_DX_VERSION_V1_4' | 'SBOM_CYCLONE_DX_VERSION_V1_5' | 'SBOM_CYCLONE_DX_VERSION_V1_6' | 'SBOM_CYCLONE_DX_VERSION_V1_7') | undefined}; export type ProtosScaV1SbomMetadataContact = {name?: string | undefined, email?: string | undefined, phone?: string | undefined}; export type ProtosScaV1SbomMetadataSupplier = {name?: string | undefined, url?: string | undefined, contact?: ProtosScaV1SbomMetadataContact | undefined}; export type ProtosScaV1GenerateSbomAsyncRequest = {deploymentId?: unknown | undefined, repositoryId?: unknown | undefined, ref?: string | undefined, formatVersion?: ProtosScaV1SbomFormatVersion | undefined, sbomOutputFormat?: ('SBOM_OUTPUT_FORMAT_XML' | 'SBOM_OUTPUT_FORMAT_JSON') | undefined, metadataComponentType?: ('SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_APPLICATION' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_FRAMEWORK' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_LIBRARY' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_CONTAINER' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_PLATFORM' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_OPERATING_SYSTEM' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_DEVICE' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_DEVICE_DRIVER' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_FIRMWARE' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_FILE' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_MACHINE_LEARNING_MODEL' | 'SBOM_METADATA_COMPONENT_TYPE_CYCLONE_DX_V15_DATA') | undefined, metadataSupplier?: ProtosScaV1SbomMetadataSupplier | undefined}; export type CreateApiApiScaDeploymentsDeploymentIdSbomAsyncArgs = {deploymentId: unknown, body: ProtosScaV1GenerateSbomAsyncRequest}; export type CreateApiApiScaDeploymentsDeploymentIdSbomAsyncOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosScaV1GenerateSbomAsyncResponse = z.object({taskTokenJwt: z.string().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 }; /** * Generate a project's SBOM * * POST /api/sca/deployments/{deploymentId}/sbom_async * * Also accepts auth: SemgrepJWT */ export const createApiApiScaDeploymentsDeploymentIdSbomAsync = async (args: CreateApiApiScaDeploymentsDeploymentIdSbomAsyncArgs, options: CreateApiApiScaDeploymentsDeploymentIdSbomAsyncOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SemgrepWebToken !== undefined) headers.set('authorization', `Bearer ${SemgrepWebToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/sca/deployments/${encodeURIComponent(String(args.deploymentId))}/sbom_async${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(protosScaV1GenerateSbomAsyncResponse, await res.json()) };