import {z} from 'zod' export type EngineParamBindingValuePayloadV2 = {literal?: string | undefined, reference?: string | undefined}; export type EngineParamBindingPayloadV2 = {array_value?: Array | undefined, value?: EngineParamBindingValuePayloadV2 | undefined}; export type ConditionPayloadV2 = {operation: string, param_bindings: Array, subject: string}; export type EscalationPathNodeIfElsePayloadV2 = {conditions?: Array | undefined, else_path: Array, then_path: Array}; export type EscalationPathRoundRobinConfigV2 = {enabled: boolean, rotate_after_seconds?: number | undefined}; export type EscalationPathTargetV2 = {id: string, schedule_mode?: ('currently_on_call' | 'all_users_for_rota' | 'all_users' | '') | undefined, type: 'schedule' | 'user' | 'slack_channel', urgency: 'high' | 'low'}; export type EscalationPathNodeLevelV2 = {round_robin_config?: EscalationPathRoundRobinConfigV2 | undefined, targets: Array, time_to_ack_interval_condition?: ('active' | 'inactive') | undefined, time_to_ack_seconds?: number | undefined, time_to_ack_weekday_interval_config_id?: string | undefined}; export type EscalationPathNodeNotifyChannelV2 = {targets: Array, time_to_ack_interval_condition?: ('active' | 'inactive') | undefined, time_to_ack_seconds?: number | undefined, time_to_ack_weekday_interval_config_id?: string | undefined}; export type EscalationPathNodeRepeatV2 = {repeat_times: number, to_node: string}; export type EscalationPathNodePayloadV2 = {id: string, if_else?: EscalationPathNodeIfElsePayloadV2 | undefined, level?: EscalationPathNodeLevelV2 | undefined, notify_channel?: EscalationPathNodeNotifyChannelV2 | undefined, repeat?: EscalationPathNodeRepeatV2 | undefined, type: 'if_else' | 'repeat' | 'level' | 'notify_channel'}; export type WeekdayIntervalV2 = {end_time: string, start_time: string, weekday: string}; export type WeekdayIntervalConfigV2 = {id: string, name: string, timezone: string, weekday_intervals: Array}; export type EscalationsV2CreatePathRequestBody = {name: string, path: Array, working_hours?: Array | undefined}; export type CreateApiV2EscalationPathsArgs = {body: EscalationsV2CreatePathRequestBody}; export type CreateApiV2EscalationPathsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const conditionOperationV2 = z.object({label: z.string(), value: z.string()}); export const engineParamBindingValueV2 = z.object({label: z.string(), literal: z.string().optional(), reference: z.string().optional()}); export const engineParamBindingV2 = z.object({array_value: z.array(engineParamBindingValueV2).optional(), value: engineParamBindingValueV2.optional()}); export const conditionSubjectV2 = z.object({label: z.string(), reference: z.string()}); export const conditionV2 = z.object({operation: conditionOperationV2, param_bindings: z.array(engineParamBindingV2), subject: conditionSubjectV2}); export const escalationPathNodeIfElseV2 = z.object({conditions: z.array(conditionV2), else_path: z.array(z.lazy(() => escalationPathNodeV2)), then_path: z.array(z.lazy(() => escalationPathNodeV2))}); export const escalationPathRoundRobinConfigV2 = z.object({enabled: z.boolean(), rotate_after_seconds: z.number().int().optional()}); export const escalationPathTargetV2 = z.object({id: z.string(), schedule_mode: z.enum(["currently_on_call", "all_users_for_rota", "all_users", ""]).optional(), type: z.enum(["schedule", "user", "slack_channel"]), urgency: z.enum(["high", "low"])}); export const escalationPathNodeLevelV2 = z.object({round_robin_config: escalationPathRoundRobinConfigV2.optional(), targets: z.array(escalationPathTargetV2), time_to_ack_interval_condition: z.enum(["active", "inactive"]).optional(), time_to_ack_seconds: z.number().int().optional(), time_to_ack_weekday_interval_config_id: z.string().optional()}); export const escalationPathNodeNotifyChannelV2 = z.object({targets: z.array(escalationPathTargetV2), time_to_ack_interval_condition: z.enum(["active", "inactive"]).optional(), time_to_ack_seconds: z.number().int().optional(), time_to_ack_weekday_interval_config_id: z.string().optional()}); export const escalationPathNodeRepeatV2 = z.object({repeat_times: z.number().int(), to_node: z.string()}); export const escalationPathNodeV2: z.ZodType = z.object({id: z.string(), if_else: escalationPathNodeIfElseV2.optional(), level: escalationPathNodeLevelV2.optional(), notify_channel: escalationPathNodeNotifyChannelV2.optional(), repeat: escalationPathNodeRepeatV2.optional(), type: z.enum(["if_else", "repeat", "level", "notify_channel"])}); export const weekdayIntervalV2 = z.object({end_time: z.string(), start_time: z.string(), weekday: z.string()}); export const weekdayIntervalConfigV2 = z.object({id: z.string(), name: z.string(), timezone: z.string(), weekday_intervals: z.array(weekdayIntervalV2)}); export const escalationPathV2 = z.object({id: z.string(), name: z.string(), path: z.array(escalationPathNodeV2), working_hours: z.array(weekdayIntervalConfigV2).optional()}); export const escalationsV2CreatePathResponseBody = z.object({escalation_path: escalationPathV2}); 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 }; /** * CreatePath Escalations V2 * * POST /v2/escalation_paths */ export const createApiV2EscalationPaths = async (args: CreateApiV2EscalationPathsArgs, options?: CreateApiV2EscalationPathsOptions) => { const {baseUrl = "https:////api.incident.io", 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(/\/+$/, '')}/v2/escalation_paths${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(escalationsV2CreatePathResponseBody, await res.json()) };