import {z} from 'zod' export type GetApiV2ScheduleEntriesArgs = {schedule_id: string, entry_window_start?: string | undefined, entry_window_end?: string | undefined}; export type GetApiV2ScheduleEntriesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const afterPaginationMetaResultV2 = z.object({after: z.string(), after_url: z.string()}); export const userV2 = z.object({email: z.string().optional(), id: z.string(), name: z.string(), role: z.enum(["viewer", "responder", "administrator", "owner", "unset"]), slack_user_id: z.string().optional()}); export const scheduleEntryV2 = z.object({end_at: z.string(), entry_id: z.string().optional(), fingerprint: z.string().optional(), layer_id: z.string().optional(), rotation_id: z.string().optional(), start_at: z.string(), user: userV2.optional()}); export const scheduleEntriesListPayloadV2 = z.object({final: z.array(scheduleEntryV2), overrides: z.array(scheduleEntryV2), scheduled: z.array(scheduleEntryV2)}); export const schedulesV2ListScheduleEntriesResponseBody = z.object({pagination_meta: afterPaginationMetaResultV2.optional(), schedule_entries: scheduleEntriesListPayloadV2}); 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 }; /** * ListScheduleEntries Schedules V2 * * GET /v2/schedule_entries */ export const getApiV2ScheduleEntries = async (args: GetApiV2ScheduleEntriesArgs, options?: GetApiV2ScheduleEntriesOptions) => { const {baseUrl = "https:////api.incident.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.schedule_id != null) query.append("schedule_id", String(args.schedule_id)) if (args.entry_window_start != null) query.append("entry_window_start", String(args.entry_window_start)) if (args.entry_window_end != null) query.append("entry_window_end", String(args.entry_window_end)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/schedule_entries${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(schedulesV2ListScheduleEntriesResponseBody, await res.json()) };