import {z} from 'zod' export type AmpConfig = {webhookUrl: string}; export type ChatGPTWorkspaceAgentConfig = {token: string, triggerId: string}; export type ClaudeCodeConfig = {routineId: string, token: string}; export type CursorConfig = {token: string, webhookUrl: string}; export type CustomNotifierConfig = {body: string, headers?: {[key: string]: string} | undefined, secretHeaders?: {[key: string]: string} | undefined, url: string}; export type MessageTemplate = {body?: string | undefined, title?: string | undefined}; export type DiscordConfig = {discordChannel?: string | undefined, discordToken?: string | undefined, messageTemplate?: MessageTemplate | undefined}; export type DiscordWebhookConfig = {discordWebhookUrl?: string | undefined, messageTemplate?: MessageTemplate | undefined}; export type EmailConfig = {emails?: Array | undefined, messageTemplate?: MessageTemplate | undefined}; export type JiraServiceManagementConfig = {apiKey: string}; export type MicrosoftTeamsConfig = {messageTemplate?: MessageTemplate | undefined, microsoftTeamsUrl?: string | undefined}; export type OpsGenieConfig = {apiKey?: string | undefined, isEU?: boolean | undefined}; export type PagerDutyConfig = {routingKey?: string | undefined, token?: string | undefined}; export type SlackConfig = {messageTemplate?: MessageTemplate | undefined, slackUrl?: string | undefined}; export type WebhookConfig = {url?: string | undefined}; export type NotifierProperties = {amp?: AmpConfig | undefined, chatgptWorkspaceAgent?: ChatGPTWorkspaceAgentConfig | undefined, claudeCode?: ClaudeCodeConfig | undefined, cursor?: CursorConfig | undefined, customWebhook?: CustomNotifierConfig | undefined, discord?: DiscordConfig | undefined, discordWebhook?: DiscordWebhookConfig | undefined, email?: EmailConfig | undefined, jiraServiceManagement?: JiraServiceManagementConfig | undefined, microsoftTeams?: MicrosoftTeamsConfig | undefined, opsgenie?: OpsGenieConfig | undefined, pagerduty?: PagerDutyConfig | undefined, slack?: SlackConfig | undefined, webhook?: WebhookConfig | undefined}; export type LabelID = string; export type NotifierUpsertRequest = {createdAt?: string | undefined, createdBy?: string | undefined, createdByAgent?: boolean | undefined, disabled?: boolean | undefined, disabledUntil?: string | undefined, lastError?: string | undefined, name: string, properties: NotifierProperties, updatedAt?: string | undefined, labelIds?: Array | undefined}; export type CreateApiNotifiersArgs = {body: NotifierUpsertRequest}; export type CreateApiNotifiersOptions = {baseUrl?: string | undefined, Auth?: string | undefined, fetch?: typeof fetch | undefined}; export const ampConfig = z.object({webhookUrl: z.string()}); export const chatGPTWorkspaceAgentConfig = z.object({token: z.string(), triggerId: z.string()}); export const claudeCodeConfig = z.object({routineId: z.string(), token: z.string()}); export const cursorConfig = z.object({token: z.string(), webhookUrl: z.string()}); export const customNotifierConfig = z.object({body: z.string(), headers: z.record(z.string(), z.string()).optional(), secretHeaders: z.record(z.string(), z.string()).optional(), url: z.string()}); export const messageTemplate = z.object({body: z.string().optional(), title: z.string().optional()}); export const discordConfig = z.object({discordChannel: z.string().optional(), discordToken: z.string().optional(), messageTemplate: messageTemplate.optional()}); export const discordWebhookConfig = z.object({discordWebhookUrl: z.string().optional(), messageTemplate: messageTemplate.optional()}); export const emailConfig = z.object({emails: z.array(z.string()).optional(), messageTemplate: messageTemplate.optional()}); export const jiraServiceManagementConfig = z.object({apiKey: z.string()}); export const microsoftTeamsConfig = z.object({messageTemplate: messageTemplate.optional(), microsoftTeamsUrl: z.string().optional()}); export const opsGenieConfig = z.object({apiKey: z.string().optional(), isEU: z.boolean().optional()}); export const pagerDutyConfig = z.object({routingKey: z.string().optional(), token: z.string().optional()}); export const slackConfig = z.object({messageTemplate: messageTemplate.optional(), slackUrl: z.string().optional()}); export const webhookConfig = z.object({url: z.string().optional()}); export const notifierProperties = z.object({amp: ampConfig.optional(), chatgptWorkspaceAgent: chatGPTWorkspaceAgentConfig.optional(), claudeCode: claudeCodeConfig.optional(), cursor: cursorConfig.optional(), customWebhook: customNotifierConfig.optional(), discord: discordConfig.optional(), discordWebhook: discordWebhookConfig.optional(), email: emailConfig.optional(), jiraServiceManagement: jiraServiceManagementConfig.optional(), microsoftTeams: microsoftTeamsConfig.optional(), opsgenie: opsGenieConfig.optional(), pagerduty: pagerDutyConfig.optional(), slack: slackConfig.optional(), webhook: webhookConfig.optional()}); export const labelID = z.string(); export const labelColor = z.object({name: z.enum(["red", "coral", "orange", "amber", "yellow", "lime", "green", "teal", "blue", "iris", "purple", "pink"]), type: z.literal("palette")}); export const label = z.object({color: labelColor, description: z.string().nullable().optional(), id: labelID, name: z.string()}); export const notifierWithId = z.object({createdAt: z.string().optional(), createdBy: z.string().optional(), createdByAgent: z.boolean().optional(), disabled: z.boolean().optional(), disabledUntil: z.string().optional(), lastError: z.string().optional(), name: z.string(), properties: notifierProperties, updatedAt: z.string().optional(), id: z.string().optional(), labelIds: z.array(labelID), labelNames: z.array(z.string()), labels: z.array(label).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 }; /** * Creates a notifier. Set exactly one channel under `properties`, such as `slack`, `email`, or `webhook`. Returns `400` if the channel type isn't enabled for your organization. `labelIds` replaces assignments when supplied; [] clears them. Omitted or null `labelIds` preserves assignments on updates and creates an unlabeled resource on creation. IDs must belong to existing labels in your organization and are validated before saving. * * POST /notifiers */ export const createApiNotifiers = async (args: CreateApiNotifiersArgs, options?: CreateApiNotifiersOptions) => { const {baseUrl = "https://api.axiom.co/v2", fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/notifiers${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(notifierWithId, await res.json()) };