import {z} from 'zod' export type ProtosPoliciesV1UpdatePolicyRuleRequest = {policyMode?: ('MODE_MONITOR' | 'MODE_COMMENT' | 'MODE_BLOCK' | 'MODE_DISABLED') | undefined}; export type UpdateApiApiPoliciesV1DeploymentsDeploymentIdPolicyIdRulesRulePathArgs = {deploymentId: unknown, policyId: unknown, rulePath: string, body: ProtosPoliciesV1UpdatePolicyRuleRequest}; export type UpdateApiApiPoliciesV1DeploymentsDeploymentIdPolicyIdRulesRulePathOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosCommonV1Rule = z.object({id: z.string().optional(), category: z.string().optional(), confidence: z.enum(["CONFIDENCE_HIGH", "CONFIDENCE_MEDIUM", "CONFIDENCE_LOW"]).optional(), cweCategories: z.array(z.string()).optional(), hasValidators: z.boolean().optional(), languages: z.array(z.string()).optional(), lastChangeAt: z.string().optional(), lastChangeBy: z.string().optional(), owaspCategories: z.array(z.string()).optional(), path: z.string().optional(), policyMode: z.enum(["MODE_MONITOR", "MODE_COMMENT", "MODE_BLOCK", "MODE_DISABLED"]).optional(), registryMaintainer: z.string().optional(), rulesets: z.array(z.string()).optional(), secretType: z.string().optional(), severity: z.enum(["SEVERITY_HIGH", "SEVERITY_MEDIUM", "SEVERITY_LOW", "SEVERITY_CRITICAL"]).optional(), source: z.enum(["SOURCE_PRO", "SOURCE_COMMUNITY", "SOURCE_CUSTOM"]).optional(), technologies: z.array(z.string()).optional(), url: z.string().optional(), vulnerabilityClass: z.array(z.string()).optional()}); export const protosPoliciesV1UpdatePolicyRuleResponse = z.object({policyId: z.string().optional(), updatedRule: protosCommonV1Rule.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 }; /** * Update Policy Rule * * PUT /api/policies/v1/deployments/{deploymentId}/{policyId}/rules/{rulePath} */ export const updateApiApiPoliciesV1DeploymentsDeploymentIdPolicyIdRulesRulePath = async (args: UpdateApiApiPoliciesV1DeploymentsDeploymentIdPolicyIdRulesRulePathArgs, options: UpdateApiApiPoliciesV1DeploymentsDeploymentIdPolicyIdRulesRulePathOptions) => { 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/v1/deployments/${encodeURIComponent(String(args.deploymentId))}/${encodeURIComponent(String(args.policyId))}/rules/${encodeURIComponent(String(args.rulePath))}${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(protosPoliciesV1UpdatePolicyRuleResponse, await res.json()) };