import {z} from 'zod' export type SlackParams = {webhookUrl?: string | undefined, token?: string | undefined, channel?: string | undefined, authMethod?: ('webhook' | 'token') | undefined}; export type GoogleChatParams = {webhookUrl: string}; export type MattermostParams = {webhookUrl: string}; export type PagerdutyParams = {routingKey: string, severity: 'critical' | 'error' | 'warning' | 'info'}; export type ServicenowParams = {url: string, username: string, password: string, category?: string | undefined, subcategory?: string | undefined, impact?: ('1' | '2' | '3') | undefined, urgency?: ('1' | '2' | '3') | undefined, severity?: ('1' | '2' | '3' | '4' | '5') | undefined, callerId?: string | undefined, group?: string | undefined, assignedTo?: string | undefined, openedBy?: string | undefined, notify?: ('1' | '2') | undefined, dueDate?: string | undefined}; export type OpsgenieParams = {apiKey: string, priority: 'P1' | 'P2' | 'P3' | 'P4' | 'P5'}; export type TelegramParams = {chatId: number}; export type TeamsParams = {webhookUrl: string}; export type PushoverParams = {token: string, userKey: string, priority?: number | undefined, sound?: string | undefined}; export type WebhookParams = {url: string, payload?: {[key: string]: unknown} | undefined}; export type AlertmanagerParams = {url: string, authMethod?: ('none' | 'basic_auth' | 'bearer') | undefined, username?: string | undefined, password?: string | undefined, token?: string | undefined}; export type IncidentioParams = {url: string, apiKey: string}; export type NotificationChannelRequest = {name: string, type: 'slack' | 'google_chat' | 'mattermost' | 'pagerduty' | 'servicenow' | 'opsgenie' | 'telegram' | 'teams' | 'pushover' | 'webhook' | 'alertmanager' | 'incidentio', matchAll?: boolean | undefined, monitorIds?: Array | undefined, condition?: string | undefined, priorities: Array<'info' | 'low' | 'medium' | 'high'>, params: SlackParams | GoogleChatParams | MattermostParams | PagerdutyParams | ServicenowParams | OpsgenieParams | TelegramParams | TeamsParams | PushoverParams | WebhookParams | AlertmanagerParams | IncidentioParams}; export type CreateApiInternalV1ProjectsProjectIdNotificationChannelsArgs = {project_id: number, body: NotificationChannelRequest}; export type CreateApiInternalV1ProjectsProjectIdNotificationChannelsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const slackParams = z.object({webhookUrl: z.string().optional(), token: z.string().optional(), channel: z.string().optional(), authMethod: z.enum(["webhook", "token"]).optional()}); export const googleChatParams = z.object({webhookUrl: z.string()}); export const mattermostParams = z.object({webhookUrl: z.string()}); export const pagerdutyParams = z.object({routingKey: z.string(), severity: z.enum(["critical", "error", "warning", "info"])}); export const servicenowParams = z.object({url: z.string(), username: z.string(), password: z.string(), category: z.string().optional(), subcategory: z.string().optional(), impact: z.enum(["1", "2", "3"]).optional(), urgency: z.enum(["1", "2", "3"]).optional(), severity: z.enum(["1", "2", "3", "4", "5"]).optional(), callerId: z.string().optional(), group: z.string().optional(), assignedTo: z.string().optional(), openedBy: z.string().optional(), notify: z.enum(["1", "2"]).optional(), dueDate: z.string().optional()}); export const opsgenieParams = z.object({apiKey: z.string(), priority: z.enum(["P1", "P2", "P3", "P4", "P5"])}); export const telegramParams = z.object({chatId: z.number().int()}); export const teamsParams = z.object({webhookUrl: z.string()}); export const pushoverParams = z.object({token: z.string(), userKey: z.string(), priority: z.number().int().optional(), sound: z.string().optional()}); export const webhookParams = z.object({url: z.string(), payload: z.record(z.string(), z.unknown()).optional()}); export const alertmanagerParams = z.object({url: z.string(), authMethod: z.enum(["none", "basic_auth", "bearer"]).optional(), username: z.string().optional(), password: z.string().optional(), token: z.string().optional()}); export const incidentioParams = z.object({url: z.string(), apiKey: z.string()}); export const notificationChannel = z.object({id: z.number().int(), projectId: z.number().int(), name: z.string(), type: z.enum(["slack", "google_chat", "mattermost", "pagerduty", "servicenow", "opsgenie", "telegram", "teams", "pushover", "webhook", "alertmanager", "incidentio"]), status: z.enum(["draft", "delivering", "paused", "disabled"]), error: z.string().optional(), matchAll: z.boolean().optional(), condition: z.string().optional(), priorities: z.array(z.enum(["info", "low", "medium", "high"])).optional(), params: z.union([slackParams, googleChatParams, mattermostParams, pagerdutyParams, servicenowParams, opsgenieParams, telegramParams, teamsParams, pushoverParams, webhookParams, alertmanagerParams, incidentioParams]), monitorIds: z.array(z.number().int()).optional(), monitorCount: z.number().int().optional(), sentCount: z.number().int().optional(), lastSentAt: z.string().optional()}); export const notificationChannelResponse = z.object({channel: notificationChannel}); 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 }; /** * Create notification channel * * POST /internal/v1/projects/{project_id}/notification-channels * * Also accepts auth: authTokenQuery */ export const createApiInternalV1ProjectsProjectIdNotificationChannels = async (args: CreateApiInternalV1ProjectsProjectIdNotificationChannelsArgs, options?: CreateApiInternalV1ProjectsProjectIdNotificationChannelsOptions) => { const {baseUrl = "https://api.uptrace.dev", 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(/\/+$/, '')}/internal/v1/projects/${encodeURIComponent(String(args.project_id))}/notification-channels${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(notificationChannelResponse, await res.json()) };