import {z} from 'zod' export type EventTriggerTypes = 1 | 2 | 3 | 4 | 5 | 6 | 7; export type Schedule = {hour?: string | undefined, day_of_week?: string | undefined, day_of_month?: string | undefined, month?: string | undefined}; export type ConditionPattern = {pattern?: string | undefined, inverse_match?: boolean | undefined}; export type ConditionOptions = {names?: Array | undefined, group_names?: Array | undefined, role_names?: Array | undefined, fs_paths?: Array | undefined, protocols?: (Array<'SFTP' | 'SCP' | 'SSH' | 'FTP' | 'DAV' | 'HTTP' | 'HTTPShare' | 'OIDC'>) | undefined, provider_objects?: (Array<'user' | 'group' | 'admin' | 'api_key' | 'share' | 'event_action' | 'event_rule'>) | undefined, min_size?: number | undefined, max_size?: number | undefined, concurrent_execution?: boolean | undefined}; export type EventConditions = {fs_events?: (Array<'upload' | 'download' | 'delete' | 'rename' | 'mkdir' | 'rmdir' | 'copy' | 'ssh_cmd' | 'pre-upload' | 'pre-download' | 'pre-delete' | 'first-upload' | 'first-download'>) | undefined, provider_events?: (Array<'add' | 'update' | 'delete'>) | undefined, schedules?: Array | undefined, idp_login_event?: (0 | 1 | 2) | undefined, options?: ConditionOptions | undefined}; export type EventActionOptions = {is_failure_action?: boolean | undefined, stop_on_failure?: boolean | undefined, execute_sync?: boolean | undefined}; export type EventActionMinimal = {name?: string | undefined, order?: number | undefined, relation_options?: EventActionOptions | undefined}; export type EventRuleMinimal = {id?: number | undefined, name?: string | undefined, status?: (0 | 1) | undefined, description?: string | undefined, created_at?: number | undefined, updated_at?: number | undefined, trigger?: EventTriggerTypes | undefined, conditions?: EventConditions | undefined, actions?: Array | undefined}; export type UpdateApiEventrulesNameArgs = {body: EventRuleMinimal}; export type UpdateApiEventrulesNameOptions = {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 rule * * PUT /eventrules/{name} * * Also accepts auth: APIKeyAuth */ export const updateApiEventrulesName = async (args: UpdateApiEventrulesNameArgs, options: UpdateApiEventrulesNameOptions) => { 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(/\/+$/, '')}/eventrules/${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()) };