import {z} from 'zod' export type CustomCampaignObjectiveParams = {action_ids: Array, name: string, result_condition: string}; export type NullInt64 = {Int64?: number | undefined, Valid?: boolean | undefined}; export type UpdateApiV1ThreatLibraryThreatsThreatIdArgs = {ThreatId: number, body: {affected_os?: Array | undefined, description?: string | undefined, from_campaign?: number | undefined, name?: string | undefined, objectives?: Array | undefined, result_condition?: string | undefined, severity?: string | undefined, tags?: Array | undefined, threat_actor_id?: NullInt64 | undefined}}; export type UpdateApiV1ThreatLibraryThreatsThreatIdOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const capiThreatDTO = z.object({affected_os: z.array(z.string()).optional(), created_at: z.string().optional(), description: z.string().optional(), display_id: z.number().int().optional(), id: z.number().int().optional(), name: z.string().optional(), severity: z.string().optional(), tags: z.array(z.string()).optional()}); export const updateApiV1ThreatLibraryThreatsThreatIdResponse = z.object({threat: capiThreatDTO.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 }; /** * # Update Threat * * PUT /v1/threat-library/threats/{ThreatId} */ export const updateApiV1ThreatLibraryThreatsThreatId = async (args: UpdateApiV1ThreatLibraryThreatsThreatIdArgs, options?: UpdateApiV1ThreatLibraryThreatsThreatIdOptions) => { 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/threat-library/threats/${encodeURIComponent(String(args.ThreatId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV1ThreatLibraryThreatsThreatIdResponse, await res.json()) };