import {z} from 'zod' export type ZonedDateTime = {date_time: string, time_zone: string}; export type ShiftMember = {type: 'user_member' | 'empty_member', user_id?: string | undefined}; export type EventAssignmentStrategy = {type: 'rotating_member_assignment_strategy' | 'every_member_assignment_strategy', shifts_per_member?: number | undefined, members: Array}; export type UpdateEventRequest = {event: {name?: string | undefined, start_time?: ZonedDateTime | undefined, end_time?: ZonedDateTime | undefined, effective_since?: string | undefined, effective_until?: (string | null) | undefined, recurrence?: Array | undefined, assignment_strategy?: EventAssignmentStrategy | undefined}}; export type UpdateApiV3SchedulesIdRotationsRotationIdEventsEventIdArgs = {id: string, rotation_id: string, event_id: string, body: UpdateEventRequest}; export type UpdateApiV3SchedulesIdRotationsRotationIdEventsEventIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.object({date_time: z.string(), time_zone: z.string()}); export const shiftMember = z.object({type: z.enum(["user_member", "empty_member"]), user_id: z.string().optional()}); export const eventAssignmentStrategy = z.object({type: z.enum(["rotating_member_assignment_strategy", "every_member_assignment_strategy"]), shifts_per_member: z.number().int().gte(1).optional(), members: z.array(shiftMember)}); export const event = z.object({id: z.string(), type: z.literal("schedule_event"), name: z.string(), start_time: zonedDateTime, end_time: zonedDateTime, effective_since: z.string(), effective_until: z.string().nullable().optional(), recurrence: z.array(z.string()), assignment_strategy: eventAssignmentStrategy, self: z.string().optional(), html_url: z.string().optional()}); export const eventResponse = z.object({event: event}); 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 }; /** * Update an event * * PUT /v3/schedules/{id}/rotations/{rotation_id}/events/{event_id} */ export const updateApiV3SchedulesIdRotationsRotationIdEventsEventId = async (args: UpdateApiV3SchedulesIdRotationsRotationIdEventsEventIdArgs, options?: UpdateApiV3SchedulesIdRotationsRotationIdEventsEventIdOptions) => { const {baseUrl = "https://api.pagerduty.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/schedules/${encodeURIComponent(String(args.id))}/rotations/${encodeURIComponent(String(args.rotation_id))}/events/${encodeURIComponent(String(args.event_id))}${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(eventResponse, await res.json()) };