import {z} from 'zod' export type GetApiV3SchedulesIdRotationsRotationIdEventsArgs = {id: string, rotation_id: string, limit?: number | undefined, offset?: number | undefined}; export type GetApiV3SchedulesIdRotationsRotationIdEventsOptions = {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 getApiV3SchedulesIdRotationsRotationIdEventsResponse = z.object({events: z.array(event), limit: z.number().int().optional(), offset: z.number().int().optional(), more: z.boolean().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 }; /** * List events * * GET /v3/schedules/{id}/rotations/{rotation_id}/events */ export const getApiV3SchedulesIdRotationsRotationIdEvents = async (args: GetApiV3SchedulesIdRotationsRotationIdEventsArgs, options?: GetApiV3SchedulesIdRotationsRotationIdEventsOptions) => { const {baseUrl = "https://api.pagerduty.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/schedules/${encodeURIComponent(String(args.id))}/rotations/${encodeURIComponent(String(args.rotation_id))}/events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV3SchedulesIdRotationsRotationIdEventsResponse, await res.json()) };