import {z} from 'zod' export type TemplateRule = {affected_operating_systems?: Array | undefined, category_names?: Array | undefined, date_label?: string | undefined, kill_chain_phases?: Array | undefined, mitre_tactics?: Array | undefined, modules?: Array | undefined, only_name?: string | undefined, severities?: Array | undefined, tags?: Array | undefined, threat_actors?: Array | undefined}; export type TemplateThreatParams = {rules?: Array | undefined, threats?: Array | undefined}; export type CreateApiV1TemplatesArgs = {body: {content?: TemplateThreatParams | undefined, description?: string | undefined, name?: string | undefined}}; export type CreateApiV1TemplatesOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const singleTemplateContentResponse = z.object({threats: z.array(z.number().int()).optional()}); export const templateRule = z.object({affected_operating_systems: z.array(z.string()).optional(), category_names: z.array(z.string()).optional(), date_label: z.string().optional(), kill_chain_phases: z.array(z.string()).optional(), mitre_tactics: z.array(z.number().int()).optional(), modules: z.array(z.number().int()).optional(), only_name: z.string().optional(), severities: z.array(z.string()).optional(), tags: z.array(z.string()).optional(), threat_actors: z.array(z.number().int()).optional()}); export const createApiV1TemplatesResponse = z.object({content: singleTemplateContentResponse.optional(), content_type: z.string().optional(), content_update_time: z.number().int().optional(), creation_time: z.number().int().optional(), description: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), rules: z.array(templateRule).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 }; /** * # Create Template * * POST /v1/templates */ export const createApiV1Templates = async (args: CreateApiV1TemplatesArgs, options?: CreateApiV1TemplatesOptions) => { const {baseUrl = "https://api.picussecurity.com", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AccessToken !== undefined) headers.set("Authorization", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/templates${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(createApiV1TemplatesResponse, await res.json()) };