import {z} from 'zod' export type GetApiApiV1DeploymentsDeploymentIdPoliciesPolicyIdArgs = {deploymentId: unknown, policyId: unknown, cursor?: string | undefined, limit?: number | undefined}; export type GetApiApiV1DeploymentsDeploymentIdPoliciesPolicyIdOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosCommonV1Policy = z.object({id: z.string().optional(), isDefault: z.boolean().optional(), name: z.string().optional(), productType: z.enum(["PRODUCT_TYPE_SAST", "PRODUCT_TYPE_SECRETS"]).optional(), slug: z.string().optional()}); 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 protosOpenapiV1ListPolicyRulesResponse = z.object({cursor: z.string().optional(), policy: protosCommonV1Policy.optional(), rules: z.array(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 }; /** * List policy rules * * GET /api/v1/deployments/{deploymentId}/policies/{policyId} */ export const getApiApiV1DeploymentsDeploymentIdPoliciesPolicyId = async (args: GetApiApiV1DeploymentsDeploymentIdPoliciesPolicyIdArgs, options: GetApiApiV1DeploymentsDeploymentIdPoliciesPolicyIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (SemgrepWebToken !== undefined) headers.set('authorization', `Bearer ${SemgrepWebToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/deployments/${encodeURIComponent(String(args.deploymentId))}/policies/${encodeURIComponent(String(args.policyId))}${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(protosOpenapiV1ListPolicyRulesResponse, await res.json()) };