import {z} from 'zod' export type GetApiApiPoliciesV2DeploymentsDeploymentIdVocabArgs = {deploymentId: number, product?: string | undefined}; export type GetApiApiPoliciesV2DeploymentsDeploymentIdVocabOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosPoliciesV2VocabConditionType = z.object({type: z.string().optional(), description: z.string().optional()}); export const protosPoliciesV2VocabActionType = z.object({type: z.string().optional(), description: z.string().optional(), requires: z.array(z.string()).optional()}); export const protosPoliciesV2VocabValueEnum = z.object({type: z.string().optional(), values: z.array(z.string()).optional()}); export const protosPoliciesV2VocabDetectionConstraints = z.object({rulesets_accepted: z.boolean().optional(), exception_types: z.array(z.string()).optional(), rule_types: z.array(z.string()).optional()}); export const protosPoliciesV2GetVocabResponse = z.object({condition_types: z.array(protosPoliciesV2VocabConditionType).optional(), action_types: z.array(protosPoliciesV2VocabActionType).optional(), value_enums: z.array(protosPoliciesV2VocabValueEnum).optional(), detection_constraints: protosPoliciesV2VocabDetectionConstraints.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 policy vocabulary * * GET /api/policies/v2/deployments/{deploymentId}/vocab */ export const getApiApiPoliciesV2DeploymentsDeploymentIdVocab = async (args: GetApiApiPoliciesV2DeploymentsDeploymentIdVocabArgs, options: GetApiApiPoliciesV2DeploymentsDeploymentIdVocabOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.product != null) query.append("product", String(args.product)) if (SemgrepWebToken !== undefined) headers.set('authorization', `Bearer ${SemgrepWebToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/policies/v2/deployments/${encodeURIComponent(String(args.deploymentId))}/vocab${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(protosPoliciesV2GetVocabResponse, await res.json()) };