import {z} from 'zod' export type GetApiV1MitigationDetectionContentSourceRulesArgs = {source: string, limit?: number | undefined, offset?: number | undefined, search?: string | undefined, orderBy?: string | undefined, asc?: boolean | undefined, ruleSeverity?: Array | undefined, logSources?: Array | undefined, alerted?: boolean | undefined, threats?: Array | undefined, type?: string | undefined}; export type GetApiV1MitigationDetectionContentSourceRulesOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationDTO = z.object({currentPage: z.number().int().optional(), currentPageSize: z.number().int().optional(), pageSize: z.number().int().optional(), totalCount: z.number().int().optional(), totalPageCount: z.number().int().optional()}); export const idNameInfoDTO = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const techniqueInfoDTO = z.object({mitreId: z.string().optional(), name: z.string().optional(), url: z.string().optional()}); export const ruleDTO = z.object({actions: z.array(idNameInfoDTO).optional(), id: z.string().optional(), mitre: z.array(techniqueInfoDTO).optional(), name: z.string().optional(), releaseDate: z.number().int().optional(), score: z.number().int().optional(), severity: z.string().optional(), type: z.string().optional(), updateDate: z.number().int().optional()}); export const getApiV1MitigationDetectionContentSourceRulesResponse = z.object({pagination: paginationDTO.optional(), rules: z.array(ruleDTO).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 }; /** * # Detection Content Rules * * GET /v1/mitigation/detection-content/{source}/rules */ export const getApiV1MitigationDetectionContentSourceRules = async (args: GetApiV1MitigationDetectionContentSourceRulesArgs, options?: GetApiV1MitigationDetectionContentSourceRulesOptions) => { const {baseUrl = "https://api.picussecurity.com", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.search != null) query.append("search", String(args.search)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.asc != null) query.append("asc", String(args.asc)) for (const item of args.ruleSeverity ?? []) query.append("ruleSeverity", String(item)) for (const item of args.logSources ?? []) query.append("logSources", String(item)) if (args.alerted != null) query.append("alerted", String(args.alerted)) for (const item of args.threats ?? []) query.append("threats", String(item)) if (args.type != null) query.append("type", String(args.type)) if (AccessToken !== undefined) headers.set("Authorization", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/mitigation/detection-content/${encodeURIComponent(String(args.source))}/rules${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(getApiV1MitigationDetectionContentSourceRulesResponse, await res.json()) };