import {z} from 'zod' export type ProtosEditorV1RunPatternRequest = {pattern?: string | undefined, target?: string | undefined, language?: string | undefined, isDeep?: boolean | undefined, isDebug?: boolean | undefined}; export type CreateApiApiRunArgs = {body: ProtosEditorV1RunPatternRequest}; export type CreateApiApiRunOptions = {baseUrl: string, SemgrepJWT?: string | undefined, fetch?: typeof fetch | undefined}; export const protosEditorV1SemgrepRunExceptionData = z.object({message: z.string().optional(), type: z.string().optional(), extra: z.object({}).optional(), uid: z.string().optional()}); export const protosEditorV1SemgrepResultData = z.object({status_code: z.number().int().optional(), output: z.object({}).optional(), run_error: protosEditorV1SemgrepRunExceptionData.optional(), run_time: z.number().optional(), environment: z.string().optional()}); export const protosEditorV1TestCaseData = z.object({must_match: z.boolean().optional(), line_no: z.number().int().optional(), rule_id: z.string().optional(), status: z.string().optional(), message: z.string().optional()}); export const protosEditorV1RunPatternResponse = z.object({semgrep_result: protosEditorV1SemgrepResultData.optional(), test_result: z.array(protosEditorV1TestCaseData).optional(), ai_autofixes: z.array(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 }; /** * Run Pattern * * POST /api/run */ export const createApiApiRun = async (args: CreateApiApiRunArgs, options: CreateApiApiRunOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepJWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SemgrepJWT !== undefined) headers.set('authorization', `Bearer ${SemgrepJWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/run${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(protosEditorV1RunPatternResponse, await res.json()) };