import {z} from 'zod' export type EventActionTypes = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15; export type Secret = {status?: ('Plain' | 'AES-256-GCM' | 'Secretbox' | 'GCP' | 'AWS' | 'VaultTransit' | 'AzureKeyVault' | 'Redacted') | undefined, payload?: string | undefined, key?: string | undefined, additional_data?: string | undefined, mode?: number | undefined}; export type KeyValue = {key?: string | undefined, value?: string | undefined}; export type HTTPPart = {name?: string | undefined, headers?: Array | undefined, filepath?: string | undefined, body?: string | undefined}; export type EventActionHTTPConfig = {endpoint?: string | undefined, username?: string | undefined, password?: Secret | undefined, headers?: Array | undefined, timeout?: number | undefined, skip_tls_verify?: boolean | undefined, method?: ('GET' | 'POST' | 'PUT' | 'DELETE') | undefined, query_parameters?: Array | undefined, body?: string | undefined, parts?: Array | undefined}; export type EventActionCommandConfig = {cmd?: string | undefined, args?: Array | undefined, timeout?: number | undefined, env_vars?: Array | undefined}; export type EventActionEmailConfig = {recipients?: Array | undefined, bcc?: Array | undefined, subject?: string | undefined, body?: string | undefined, content_type?: (0 | 1) | undefined, attachments?: Array | undefined}; export type FolderRetention = {path?: string | undefined, retention?: number | undefined, delete_empty_dirs?: boolean | undefined, ignore_user_permissions?: boolean | undefined}; export type EventActionDataRetentionConfig = {folders?: Array | undefined}; export type FilesystemActionTypes = 1 | 2 | 3 | 4 | 5 | 6; export type EventActionFsCompress = {name?: string | undefined, paths?: Array | undefined}; export type EventActionFilesystemConfig = {type?: FilesystemActionTypes | undefined, renames?: Array | undefined, mkdirs?: Array | undefined, deletes?: Array | undefined, exist?: Array | undefined, copy?: Array | undefined, compress?: EventActionFsCompress | undefined}; export type EventActionPasswordExpiration = {threshold?: number | undefined}; export type EventActionUserInactivity = {disable_threshold?: number | undefined, delete_threshold?: number | undefined}; export type EventActionIDPAccountCheck = {mode?: (0 | 1) | undefined, template_user?: string | undefined, template_admin?: string | undefined}; export type BaseEventActionOptions = {http_config?: EventActionHTTPConfig | undefined, cmd_config?: EventActionCommandConfig | undefined, email_config?: EventActionEmailConfig | undefined, retention_config?: EventActionDataRetentionConfig | undefined, fs_config?: EventActionFilesystemConfig | undefined, pwd_expiration_config?: EventActionPasswordExpiration | undefined, user_inactivity_config?: EventActionUserInactivity | undefined, idp_config?: EventActionIDPAccountCheck | undefined}; export type BaseEventAction = {id?: number | undefined, name?: string | undefined, description?: string | undefined, type?: EventActionTypes | undefined, options?: BaseEventActionOptions | undefined, rules?: Array | undefined}; export type CreateApiEventactionsArgs = {confidential_data?: number | undefined, body: BaseEventAction}; export type CreateApiEventactionsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eventActionTypes = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15)]); export const secret = z.object({status: z.enum(["Plain", "AES-256-GCM", "Secretbox", "GCP", "AWS", "VaultTransit", "AzureKeyVault", "Redacted"]).optional(), payload: z.string().optional(), key: z.string().optional(), additional_data: z.string().optional(), mode: z.number().int().optional()}); export const keyValue = z.object({key: z.string().optional(), value: z.string().optional()}); export const hTTPPart = z.object({name: z.string().optional(), headers: z.array(keyValue).optional(), filepath: z.string().optional(), body: z.string().optional()}); export const eventActionHTTPConfig = z.object({endpoint: z.string().optional(), username: z.string().optional(), password: secret.optional(), headers: z.array(keyValue).optional(), timeout: z.number().int().gte(1).lte(180).optional(), skip_tls_verify: z.boolean().optional(), method: z.enum(["GET", "POST", "PUT", "DELETE"]).optional(), query_parameters: z.array(keyValue).optional(), body: z.string().optional(), parts: z.array(hTTPPart).optional()}); export const eventActionCommandConfig = z.object({cmd: z.string().optional(), args: z.array(z.string()).optional(), timeout: z.number().int().gte(1).lte(120).optional(), env_vars: z.array(keyValue).optional()}); export const eventActionEmailConfig = z.object({recipients: z.array(z.string()).optional(), bcc: z.array(z.string()).optional(), subject: z.string().optional(), body: z.string().optional(), content_type: z.union([z.literal(0), z.literal(1)]).optional(), attachments: z.array(z.string()).optional()}); export const folderRetention = z.object({path: z.string().optional(), retention: z.number().int().optional(), delete_empty_dirs: z.boolean().optional(), ignore_user_permissions: z.boolean().optional()}); export const eventActionDataRetentionConfig = z.object({folders: z.array(folderRetention).optional()}); export const filesystemActionTypes = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6)]); export const eventActionFsCompress = z.object({name: z.string().optional(), paths: z.array(z.string()).optional()}); export const eventActionFilesystemConfig = z.object({type: filesystemActionTypes.optional(), renames: z.array(keyValue).optional(), mkdirs: z.array(z.string()).optional(), deletes: z.array(z.string()).optional(), exist: z.array(z.string()).optional(), copy: z.array(keyValue).optional(), compress: eventActionFsCompress.optional()}); export const eventActionPasswordExpiration = z.object({threshold: z.number().int().optional()}); export const eventActionUserInactivity = z.object({disable_threshold: z.number().int().optional(), delete_threshold: z.number().int().optional()}); export const eventActionIDPAccountCheck = z.object({mode: z.union([z.literal(0), z.literal(1)]).optional(), template_user: z.string().optional(), template_admin: z.string().optional()}); export const baseEventActionOptions = z.object({http_config: eventActionHTTPConfig.optional(), cmd_config: eventActionCommandConfig.optional(), email_config: eventActionEmailConfig.optional(), retention_config: eventActionDataRetentionConfig.optional(), fs_config: eventActionFilesystemConfig.optional(), pwd_expiration_config: eventActionPasswordExpiration.optional(), user_inactivity_config: eventActionUserInactivity.optional(), idp_config: eventActionIDPAccountCheck.optional()}); export const baseEventAction = z.object({id: z.number().int().gte(1).optional(), name: z.string().optional(), description: z.string().optional(), type: eventActionTypes.optional(), options: baseEventActionOptions.optional(), rules: 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 }; /** * Add event action * * POST /eventactions * * Also accepts auth: APIKeyAuth */ export const createApiEventactions = async (args: CreateApiEventactionsArgs, options: CreateApiEventactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.confidential_data != null) query.append("confidential_data", String(args.confidential_data)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/eventactions${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(baseEventAction, await res.json()) };