import {z} from 'zod' export type GetApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyArgs = {deploymentId: number}; export type GetApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyOptions = {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 protosPoliciesV2DetectionPolicyState = z.object({bundle: protosPoliciesV2DetectionPolicyBundle.optional(), state_version: z.string().optional()}); export const protosPoliciesV2GetDetectionPolicySummaryResponse = z.object({deployment_slug: z.string().optional(), code: protosPoliciesV2DetectionPolicyState.optional(), secrets: protosPoliciesV2DetectionPolicyState.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 }; /** * Get the detection policy summary * * GET /api/policies/v2/deployments/{deploymentId}/detection-policy */ export const getApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicy = async (args: GetApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyArgs, options: GetApiApiPoliciesV2DeploymentsDeploymentIdDetectionPolicyOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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${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(protosPoliciesV2GetDetectionPolicySummaryResponse, await res.json()) };