import {z} from 'zod' export type GetApiAdminNotificationOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAdminNotificationResponse = z.object({type: z.enum(["SMTP", "MS365", "GMAIL"]).optional(), server: z.string().optional(), port: z.number().int().gte(0).lte(65535).optional(), reply_to: z.string().optional(), username: z.string().max(128).optional(), auth: z.enum(["DISABLE", "AUTO", "LOGIN", "PLAIN"]).optional(), enable_tls_ssl: z.boolean().optional(), enable_starttls_auto: z.boolean().optional(), recipients: z.string().optional(), enable_tenant_access: z.boolean().optional(), hard_disk_threshold: z.number().optional(), notify_hard_disk_exceeded_threshold: z.boolean().optional(), cpu_threshold: z.number().optional(), notify_cpu_exceeded_threshold: z.boolean().optional(), memory_threshold: z.number().optional(), notify_memory_exceeded_threshold: z.boolean().optional(), notify_ip_blocked: z.boolean().optional(), notify_license_limited: z.boolean().optional(), notify_service_disconnected: z.boolean().optional(), notify_push_certs_update_failed: z.boolean().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 }; /** * Retrieve system notification settings * * GET /admin/notification */ export const getApiAdminNotification = async (options?: GetApiAdminNotificationOptions) => { const {baseUrl = "https://HOSTNAME:8887/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/admin/notification${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiAdminNotificationResponse, await res.json()) };