import {z} from 'zod' export type V2AlertAttachmentEncoding = 0 | 1; export type V2AlertAttachment = {content?: (string | null) | undefined, encoding?: V2AlertAttachmentEncoding | undefined, id?: (string | null) | undefined, name?: (string | null) | undefined, contentType?: (string | null) | undefined, annotationId?: (string | null) | undefined}; export type EventParameterTypes = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 100; export type EventParameterInfo = {name?: (string | null) | undefined, order?: number | undefined, type?: EventParameterTypes | undefined, value?: (string | null) | undefined}; export type V2AlertSeverities = 0 | 1 | 2; export type V1RaiseAlertInfo = {attachments?: (Array | null) | undefined, category?: (string | null) | undefined, externalId?: (string | null) | undefined, flags?: number | undefined, parameters?: (Array | null) | undefined, severity?: V2AlertSeverities | undefined, text: string, title: string}; export type CreateApiAlertsArgs = {body: V1RaiseAlertInfo}; export type CreateApiAlertsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v2AlertAttachmentEncoding = z.union([z.literal(0), z.literal(1)]); export const v2AlertAttachment = z.object({content: z.string().nullable().optional(), encoding: v2AlertAttachmentEncoding.optional(), id: z.string().nullable().optional(), name: z.string().nullable().optional(), contentType: z.string().nullable().optional(), annotationId: z.string().nullable().optional()}); export const eventSourceTypes = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]); export const v1AlertHistoryInfoUserEntryType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10), z.literal(11), z.literal(12), z.literal(13)]); export const v1AlertHistoryInfoEntry = z.object({id: z.string().nullable().optional(), timestamp: z.string().optional(), type: v1AlertHistoryInfoUserEntryType.optional(), userId: z.string().nullable().optional()}); export const v1AlertHistoryInfoDetailed = z.object({alertHistoryEntries: z.array(v1AlertHistoryInfoEntry).nullable().optional(), acknowledgedgements: z.array(z.string()).nullable().optional(), closed: z.string().nullable().optional(), closedBy: z.string().nullable().optional(), acknowledged: z.string().nullable().optional(), created: z.string().optional(), createdBy: z.string().nullable().optional()}); export const eventParameterTypes = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(17), z.literal(100)]); export const eventParameterInfo = z.object({name: z.string().nullable().optional(), order: z.number().int().optional(), type: eventParameterTypes.optional(), value: z.string().nullable().optional()}); export const v2AlertSeverities = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const v1AlertStatusCodesV1 = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(4), z.literal(8), z.literal(16), z.literal(32)]); export const v2AlertDestinationTypeDetails = z.union([z.literal(2), z.literal(3)]); export const v2AlertDestinationDetails = z.object({value: z.string().nullable().optional(), type: v2AlertDestinationTypeDetails.optional()}); export const v1AlertAnnotationInfoV1 = z.object({type: z.number().int().optional(), text: z.string().nullable().optional(), timestamp: z.string().optional(), userId: z.string().nullable().optional(), id: z.string().nullable().optional(), teamId: z.string().nullable().optional()}); export const v1AlertInfoV1 = z.object({attachments: z.array(v2AlertAttachment).nullable().optional(), category: z.string().nullable().optional(), eventSourceId: z.string().nullable().optional(), eventSourceType: eventSourceTypes.optional(), history: v1AlertHistoryInfoDetailed.optional(), parameters: z.array(eventParameterInfo).nullable().optional(), severity: v2AlertSeverities.optional(), categoryId: z.string().nullable().optional(), externalId: z.string().nullable().optional(), eventId: z.string().nullable().optional(), flags: z.number().int().optional(), id: z.string().nullable().optional(), lastModified: z.string().optional(), status: v1AlertStatusCodesV1.optional(), subscriptionId: z.string().nullable().optional(), destination: v2AlertDestinationDetails.optional(), text: z.string().nullable().optional(), title: z.string().nullable().optional(), annotations: z.array(v1AlertAnnotationInfoV1).nullable().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 }; /** * Trigger Alert * * POST /alerts */ export const createApiAlerts = async (args: CreateApiAlertsArgs, options?: CreateApiAlertsOptions) => { const {baseUrl = "https://connect.signl4.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/alerts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(v1AlertInfoV1, await res.json()) };