import {z} from 'zod' export type CreateApiV2PoliciesPolicyIdConditionCustomFieldsArgs = {policy_id: number, body: {enabled?: boolean | undefined, displayName?: string | undefined, severity?: ('NONE' | 'MINOR' | 'MODERATE' | 'MAJOR' | 'CRITICAL') | undefined, priority?: ('NONE' | 'LOW' | 'MEDIUM' | 'HIGH') | undefined, channels?: Array | undefined, scripts?: (Array<{scriptId?: number | undefined, runAs?: ('SYSTEM' | 'LOGGED_ON_USER' | 'LOCAL_ADMIN' | 'DOMAIN_ADMIN' | 'PREFERRED_CREDENTIAL_MAC' | 'PREFERRED_CREDENTIAL_LINUX') | undefined, scriptParam?: string | undefined, scriptVariables?: (Array<{id?: string | undefined, value?: string | undefined}>) | undefined}>) | undefined, notificationAction?: ('NONE' | 'SEND') | undefined, notifyOnReset?: boolean | undefined, resetThreshold?: number | undefined, matchAll?: (Array<{fieldName: string, operator: 'EQUALS' | 'NOT_EQUALS' | 'LESS_THAN' | 'LESS_OR_EQUAL_THAN' | 'GREATER_THAN' | 'GREATER_OR_EQUAL_THAN' | 'IS_NOT_NULL' | 'CONTAINS' | 'CONTAINS_NONE' | 'CONTAINS_ANY', value: string}>) | undefined, matchAny?: (Array<{fieldName: string, operator: 'EQUALS' | 'NOT_EQUALS' | 'LESS_THAN' | 'LESS_OR_EQUAL_THAN' | 'GREATER_THAN' | 'GREATER_OR_EQUAL_THAN' | 'IS_NOT_NULL' | 'CONTAINS' | 'CONTAINS_NONE' | 'CONTAINS_ANY', value: string}>) | undefined}}; export type CreateApiV2PoliciesPolicyIdConditionCustomFieldsOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2PoliciesPolicyIdConditionCustomFieldsResponse = z.object({id: z.string().optional(), conditionName: z.string().optional(), displayName: z.string().optional(), enabled: z.boolean().optional(), severity: z.enum(["NONE", "MINOR", "MODERATE", "MAJOR", "CRITICAL"]).optional(), priority: z.enum(["NONE", "LOW", "MEDIUM", "HIGH"]).optional(), channels: z.array(z.number().int()).optional(), scripts: z.array(z.object({scriptId: z.number().int().optional(), runAs: z.enum(["SYSTEM", "LOGGED_ON_USER", "LOCAL_ADMIN", "DOMAIN_ADMIN", "PREFERRED_CREDENTIAL_MAC", "PREFERRED_CREDENTIAL_LINUX"]).optional(), scriptParam: z.string().optional(), scriptVariables: z.array(z.object({id: z.string().optional(), value: z.string().max(65535).optional()})).optional()})).optional(), notificationAction: z.enum(["NONE", "SEND"]).optional(), notifyOnReset: z.boolean().optional(), resetThreshold: z.number().int().optional(), inheritance: z.object({inherited: z.boolean().optional(), overridden: z.boolean().optional(), sourcePolicyId: z.number().int().optional()}).optional(), matchAll: z.array(z.object({fieldName: z.string().max(250), operator: z.enum(["EQUALS", "NOT_EQUALS", "LESS_THAN", "LESS_OR_EQUAL_THAN", "GREATER_THAN", "GREATER_OR_EQUAL_THAN", "IS_NOT_NULL", "CONTAINS", "CONTAINS_NONE", "CONTAINS_ANY"]), value: z.string().max(65535)})).optional(), matchAny: z.array(z.object({fieldName: z.string().max(250), operator: z.enum(["EQUALS", "NOT_EQUALS", "LESS_THAN", "LESS_OR_EQUAL_THAN", "GREATER_THAN", "GREATER_OR_EQUAL_THAN", "IS_NOT_NULL", "CONTAINS", "CONTAINS_NONE", "CONTAINS_ANY"]), value: z.string().max(65535)})).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 custom fields policy condition * * POST /v2/policies/{policy_id}/condition/custom-fields * * Also accepts auth: sessionKey */ export const createApiV2PoliciesPolicyIdConditionCustomFields = async (args: CreateApiV2PoliciesPolicyIdConditionCustomFieldsArgs, options: CreateApiV2PoliciesPolicyIdConditionCustomFieldsOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/policies/${encodeURIComponent(String(args.policy_id))}/condition/custom-fields${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(createApiV2PoliciesPolicyIdConditionCustomFieldsResponse, await res.json()) };