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 UpdateApiEventactionsNameArgs = {body: BaseEventAction}; export type UpdateApiEventactionsNameOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiResponse = z.object({message: z.string().optional(), error: 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 }; /** * Update event action * * PUT /eventactions/{name} * * Also accepts auth: APIKeyAuth */ export const updateApiEventactionsName = async (args: UpdateApiEventactionsNameArgs, options: UpdateApiEventactionsNameOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/eventactions/${encodeURIComponent(String(args.name))}${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(apiResponse, await res.json()) };