import {z} from 'zod' export type L0Mode = 'parallel' | 'gate' | 'only' | 'off'; export type L0Modes = {P1: L0Mode, P2: L0Mode, P3: L0Mode, P4: L0Mode}; export type L0Policy = {allow_downgrade: boolean, allow_promotion: boolean, allow_suppress: boolean, max_promotion_steps: number, mode: L0Modes, triage_budget_seconds: number}; export type Channel = 'email' | 'webhook' | 'sms' | 'voice' | 'slack'; export type RotationMode = 'on_call' | 'all'; export type EscalationTarget = {kind: 'rotation', mode?: RotationMode | undefined, rotation_id: string} | {email: string, kind: 'user'} | {kind: 'whole_team'}; export type LadderStep = {after_micros: number, targets: Array}; export type PriorityRung = {channels: Array, priority: number, steps: Array}; export type SetPolicyRequest = {destinations?: (Array | null) | undefined, l0?: (L0Policy | null) | undefined, rungs: Array, template?: (string | null) | undefined}; export type UpdateApiApiOrgIdOncallTeamsTeamIdPolicyArgs = {org_id: string, team_id: string, body: SetPolicyRequest}; export type UpdateApiApiOrgIdOncallTeamsTeamIdPolicyOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const l0Mode = z.enum(["parallel", "gate", "only", "off"]); export const l0Modes = z.object({P1: l0Mode, P2: l0Mode, P3: l0Mode, P4: l0Mode}); export const l0Policy = z.object({allow_downgrade: z.boolean(), allow_promotion: z.boolean(), allow_suppress: z.boolean(), max_promotion_steps: z.number().int().gte(0), mode: l0Modes, triage_budget_seconds: z.number().int()}); export const channel = z.enum(["email", "webhook", "sms", "voice", "slack"]); export const rotationMode = z.enum(["on_call", "all"]); export const escalationTarget = z.union([z.object({kind: z.literal("rotation"), mode: rotationMode.optional(), rotation_id: z.string()}), z.object({email: z.string(), kind: z.literal("user")}), z.object({kind: z.literal("whole_team")})]); export const ladderStep = z.object({after_micros: z.number().int(), targets: z.array(escalationTarget)}); export const priorityRung = z.object({channels: z.array(channel), priority: z.number().int().gte(0), steps: z.array(ladderStep)}); export const escalationPolicy = z.object({destinations: z.array(z.string()).optional(), id: z.string(), l0: l0Policy.optional(), org_id: z.string(), rungs: z.array(priorityRung), team_id: z.string(), template: z.string().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 }; /** * Replace a team's escalation policy * * PUT /api/{org_id}/oncall/teams/{team_id}/policy */ export const updateApiApiOrgIdOncallTeamsTeamIdPolicy = async (args: UpdateApiApiOrgIdOncallTeamsTeamIdPolicyArgs, options: UpdateApiApiOrgIdOncallTeamsTeamIdPolicyOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/oncall/teams/${encodeURIComponent(String(args.team_id))}/policy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(escalationPolicy, await res.json()) };