import {z} from 'zod' export type V2AlertEscalationModes = 0 | 1; export type V2AlertFilterModes = 0 | 1; export type V2AlertFilterActions = 0 | 1 | 2; export type V2AlertResponseModes = 2 | 4; export type V2PersistentNotificationModes = 0 | 1; export type V2TeamCategoryOptOutModes = 0 | 1 | 2; export type V2AlertSettingsChannelType = 0 | 1 | 2 | 3 | 4; export type V2AlertSettingsNotificationProfile = {channel?: V2AlertSettingsChannelType | undefined, delay?: number | undefined, enabled?: boolean | undefined}; export type V2InAppSignalingModes = 0 | 1; export type V2NotifyOnAlertStatusChanges = 0 | 1 | 2 | 4 | 8; export type V2AlertSettings = {escEnabled?: boolean | undefined, escMode?: V2AlertEscalationModes | undefined, escRefId?: (string | null) | undefined, filterMode?: V2AlertFilterModes | undefined, filterAction?: V2AlertFilterActions | undefined, responseMode?: V2AlertResponseModes | undefined, responseTime?: number | undefined, tierResponseTime?: number | undefined, persNotMode?: V2PersistentNotificationModes | undefined, persNotInterval?: number | undefined, optOut?: V2TeamCategoryOptOutModes | undefined, overrideNotificationProfiles?: boolean | undefined, notificationProfileOverrides?: (Array | null) | undefined, signalingMode?: V2InAppSignalingModes | undefined, notifyOnAlertStatus?: V2NotifyOnAlertStatusChanges | undefined}; export type CreateApiTeamsTeamIdAlertSettingsArgs = {teamId: string, body: V2AlertSettings}; export type CreateApiTeamsTeamIdAlertSettingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v2AlertEscalationModes = z.union([z.literal(0), z.literal(1)]); export const v2AlertFilterModes = z.union([z.literal(0), z.literal(1)]); export const v2AlertFilterActions = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const v2AlertResponseModes = z.union([z.literal(2), z.literal(4)]); export const v2PersistentNotificationModes = z.union([z.literal(0), z.literal(1)]); export const v2TeamCategoryOptOutModes = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const v2AlertSettingsChannelType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const v2AlertSettingsNotificationProfile = z.object({channel: v2AlertSettingsChannelType.optional(), delay: z.number().int().optional(), enabled: z.boolean().optional()}); export const v2InAppSignalingModes = z.union([z.literal(0), z.literal(1)]); export const v2NotifyOnAlertStatusChanges = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(4), z.literal(8)]); export const v2AlertSettings = z.object({escEnabled: z.boolean().optional(), escMode: v2AlertEscalationModes.optional(), escRefId: z.string().nullable().optional(), filterMode: v2AlertFilterModes.optional(), filterAction: v2AlertFilterActions.optional(), responseMode: v2AlertResponseModes.optional(), responseTime: z.number().int().optional(), tierResponseTime: z.number().int().optional(), persNotMode: v2PersistentNotificationModes.optional(), persNotInterval: z.number().int().optional(), optOut: v2TeamCategoryOptOutModes.optional(), overrideNotificationProfiles: z.boolean().optional(), notificationProfileOverrides: z.array(v2AlertSettingsNotificationProfile).nullable().optional(), signalingMode: v2InAppSignalingModes.optional(), notifyOnAlertStatus: v2NotifyOnAlertStatusChanges.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 }; /** * Sets alert settings of a specific team. * * POST /teams/{teamId}/alertSettings */ export const createApiTeamsTeamIdAlertSettings = async (args: CreateApiTeamsTeamIdAlertSettingsArgs, options?: CreateApiTeamsTeamIdAlertSettingsOptions) => { 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(/\/+$/, '')}/teams/${encodeURIComponent(String(args.teamId))}/alertSettings${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(v2AlertSettings, await res.json()) };