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 ProtosPoliciesV2ApplyDetectionPolicyRequest = {bundle: ProtosPoliciesV2DetectionPolicyBundle}; export type UpdateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductArgs = {deploymentId: number, product: string, body: ProtosPoliciesV2ApplyDetectionPolicyRequest}; export type UpdateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosPoliciesV2DetectionPolicyException = z.object({exception_type: z.string(), project: z.string().optional(), project_tag_name: z.string().optional(), rule: z.string(), rule_type: z.string()}); export const protosPoliciesV2DetectionPolicyBundle = z.object({deployment_slug: z.string().optional(), product: z.string().optional(), rulesets: z.array(z.string()).optional(), rules: z.array(z.string()).optional(), disabled: z.array(z.string()).optional(), exceptions: z.array(protosPoliciesV2DetectionPolicyException).optional()}); export const protosPoliciesV2ApplyDetectionPolicyResponse = z.object({bundle: protosPoliciesV2DetectionPolicyBundle.optional(), state_version: 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 }; /** * Apply a detection policy * * PUT /api/policies/v2/deployments/{deploymentId}/detection-policy/{product} */ export const updateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProduct = async (args: UpdateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductArgs, options: UpdateApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyProductOptions) => { 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))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(protosPoliciesV2ApplyDetectionPolicyResponse, await res.json()) };