import {z} from 'zod' export type GetApiNetworkNotificationsV1AlertConfigArgs = {'scope-id': string, 'scope-type'?: ('GLOBAL' | 'SITE' | 'DEVICE') | undefined, limit?: number | undefined, next?: number | undefined}; export type GetApiNetworkNotificationsV1AlertConfigOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const alertConfigRuleResponse = z.object({ruleNumber: z.number().int().optional(), rule: z.object({duration: z.number().int().optional(), condition: z.array(z.object({severity: z.string().optional(), expression: z.object({metric: z.string().optional(), operator: z.string().optional(), valueNumber: z.number().optional()}).optional()})).optional(), additionalCondition: z.array(z.object({index: z.number().int().optional(), expression: z.object({metric: z.string().optional(), operator: z.string().optional(), valueNumber: z.number().optional()}).optional()})).optional()}).optional()}); export const alertConfigResponse = z.object({name: z.string().optional(), description: z.string().optional(), label: z.string().optional(), category: z.string().optional(), deviceType: z.string().optional(), enabled: z.boolean().optional(), ruleSource: z.enum(["SYSTEM", "USER"]).optional(), clearTimeout: z.string().optional(), inherited: z.boolean().optional(), scopeId: z.string().optional(), scopeType: z.enum(["GLOBAL", "SITE", "DEVICE"]).optional(), rules: z.array(alertConfigRuleResponse).optional()}); export const getApiNetworkNotificationsV1AlertConfigResponse = z.object({id: z.string(), type: z.string(), items: z.array(alertConfigResponse), count: z.number().int(), total: z.number().int().optional(), offset: z.number().int().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 }; /** * List alert configurations * * GET /network-notifications/v1/alert-config */ export const getApiNetworkNotificationsV1AlertConfig = async (args: GetApiNetworkNotificationsV1AlertConfigArgs, options?: GetApiNetworkNotificationsV1AlertConfigOptions) => { const {baseUrl = "https://sso.common.cloud.hpe.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['scope-id'] != null) query.append("scope-id", String(args['scope-id'])) if (args['scope-type'] != null) query.append("scope-type", String(args['scope-type'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.next != null) query.append("next", String(args.next)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/network-notifications/v1/alert-config${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(getApiNetworkNotificationsV1AlertConfigResponse, await res.json()) };