import {z} from 'zod' export type ProtosPoliciesV2DetectionPolicyException = {exception_type: string, project?: string | undefined, project_tag_name?: string | undefined, rule: string, rule_type: string}; export type ProtosPoliciesV2DetectionPolicyBundle = {deployment_slug?: string | undefined, product?: string | undefined, rulesets?: Array | undefined, rules?: Array | undefined, disabled?: Array | undefined, exceptions?: Array | undefined}; export type ProtosPoliciesV2DryRunDetectionPolicyRequest = {bundle: ProtosPoliciesV2DetectionPolicyBundle}; export type CreateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductDryRunArgs = {deploymentId: number, product: string, body: ProtosPoliciesV2DryRunDetectionPolicyRequest}; export type CreateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductDryRunOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosPoliciesV2DetectionPolicyDiffKey = z.object({product: z.string().optional(), scope_type: z.string().optional(), scope_target: z.string().optional(), rule: z.string().optional(), rule_type: z.string().optional()}); export const protosPoliciesV2DetectionPolicyDiffContent = z.object({rulesets: z.array(z.string()).optional(), rules: z.array(z.string()).optional(), disabled: z.array(z.string()).optional(), exception_type: z.string().optional()}); export const protosPoliciesV2DetectionPolicyDiffEntry = z.object({kind: z.string().optional(), key: protosPoliciesV2DetectionPolicyDiffKey.optional(), before: protosPoliciesV2DetectionPolicyDiffContent.optional(), after: protosPoliciesV2DetectionPolicyDiffContent.optional()}); export const protosPoliciesV2BundleValidationError = z.object({code: z.string().optional(), message: z.string().optional(), policy_slug: z.string().optional(), context: z.record(z.string(), z.string()).optional()}); export const protosPoliciesV2DryRunDetectionPolicyResponse = z.object({creates: z.array(protosPoliciesV2DetectionPolicyDiffEntry).optional(), updates: z.array(protosPoliciesV2DetectionPolicyDiffEntry).optional(), deletes: z.array(protosPoliciesV2DetectionPolicyDiffEntry).optional(), state_version: z.string().optional(), validation_errors: z.array(protosPoliciesV2BundleValidationError).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 }; /** * Preview a detection policy apply * * POST /api/policies/v2/deployments/{deploymentId}/detection-policy/{product}:dryRun */ export const createApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductDryRun = async (args: CreateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductDryRunArgs, options: CreateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductDryRunOptions) => { 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/policies/v2/deployments/${encodeURIComponent(String(args.deploymentId))}/detection-policy/${encodeURIComponent(String(args.product))}:dryRun${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(protosPoliciesV2DryRunDetectionPolicyResponse, await res.json()) };