import {z} from 'zod' export type GetApiApiV1NotificationRulesArgs = {page?: number | undefined, size?: number | undefined}; export type GetApiApiV1NotificationRulesOptions = {baseUrl?: string | undefined, basicAuthApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const notificationCustomizationTypeDTO = z.enum(["rich", "minimal"]); export const notificationCustomizationDTO = z.object({message: z.string().optional(), type: notificationCustomizationTypeDTO}); export const notificationFilterFieldTypeDTO = z.enum(["datasourceId", "datasourceLabel", "datasetId", "datasetLabel", "datasetOwnerId", "datasetTags", "checkId", "checkName", "checkOwnerId", "columnId", "metricType", "datasetResourceAttribute", "checkResourceAttribute"]); export const notificationFilterFieldDTO = z.object({attributeName: z.string().optional(), field: notificationFilterFieldTypeDTO}); export const notificationFilterTypeDTO = z.enum(["equals", "contains", "in", "and", "or", "not"]); export const notificationFilterDTO: z.ZodType = z.object({expression: z.lazy(() => notificationFilterDTO.optional()), expressions: z.array(z.lazy(() => notificationFilterDTO)).optional(), field: notificationFilterFieldDTO.optional(), type: notificationFilterTypeDTO, value: z.string().optional(), values: z.array(z.string()).optional()}); export const notificationRecipientRoleDTO = z.enum(["datasetOwner", "monitorOwner"]); export const notificationLevelDTO = z.enum(["info", "warning", "critical", "not_evaluated"]); export const notificationScanStateDTO = z.enum(["failed", "timedOut", "canceled", "completedWithErrors"]); export const notificationTriggerTypeDTO = z.enum(["testResultLevel", "scanState"]); export const notificationRecipientTriggerDTO = z.object({level: notificationLevelDTO.optional(), scanState: notificationScanStateDTO.optional(), type: notificationTriggerTypeDTO}); export const notificationRecipientTypeDTO = z.enum(["email", "user", "userGroup", "integration", "resourceRole", "customRole"]); export const notificationRecipientDTO = z.object({customRoleId: z.string().optional(), email: z.string().optional(), integrationChannel: z.string().optional(), integrationId: z.string().optional(), role: notificationRecipientRoleDTO.optional(), triggers: z.array(notificationRecipientTriggerDTO), type: notificationRecipientTypeDTO, userGroupId: z.string().optional(), userId: z.string().optional()}); export const publicApiRuleTypeDTO = z.enum(["check", "monitor"]); export const notificationRuleStatusDTO = z.enum(["active", "paused"]); export const publicApiNotificationRuleDTO = z.object({createdAt: z.string().optional(), customization: notificationCustomizationDTO.optional(), filter: notificationFilterDTO.optional(), id: z.string().optional(), isDefault: z.boolean().optional(), lastTriggeredAt: z.string().optional(), name: z.string().optional(), ownerId: z.string().optional(), recipients: z.array(notificationRecipientDTO).optional(), ruleType: publicApiRuleTypeDTO.optional(), status: notificationRuleStatusDTO.optional(), updatedAt: z.string().optional()}); export const publicApiListNotificationRulesResponse = z.object({content: z.array(publicApiNotificationRuleDTO), first: z.boolean(), last: z.boolean(), number: z.number().int(), size: z.number().int(), totalElements: z.number().int(), totalPages: z.number().int()}); 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 notification rules * * GET /api/v1/notificationRules * * Also accepts auth: cookieToken */ export const getApiApiV1NotificationRules = async (args: GetApiApiV1NotificationRulesArgs, options?: GetApiApiV1NotificationRulesOptions) => { const {baseUrl = "https://cloud.soda.io", fetch: fetchFn = fetch, basicAuthApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (basicAuthApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuthApiKey.username}:${basicAuthApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/notificationRules${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(publicApiListNotificationRulesResponse, await res.json()) };