import {z} from 'zod' export type GetApiV2SchedulesArgs = {page_size?: number | undefined, after?: string | undefined}; export type GetApiV2SchedulesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationMetaResultWithTotal = z.object({after: z.string().optional(), page_size: z.number().int(), total_record_count: z.number().int().optional()}); export const scheduleRotationHandoverV2 = z.object({interval: z.number().int().optional(), interval_type: z.enum(["hourly", "daily", "weekly"]).optional()}); export const scheduleLayerV2 = z.object({id: z.string().optional(), name: z.string().optional()}); 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 scheduleRotationWorkingIntervalV2 = z.object({end_time: z.string(), start_time: z.string(), weekday: z.enum(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"])}); export const scheduleRotationV2 = z.object({effective_from: z.string().optional(), handover_start_at: z.string(), handovers: z.array(scheduleRotationHandoverV2), id: z.string(), layers: z.array(scheduleLayerV2), name: z.string(), users: z.array(userV2), working_interval: z.array(scheduleRotationWorkingIntervalV2).optional()}); export const scheduleConfigV2 = z.object({rotations: z.array(scheduleRotationV2)}); 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 scheduleHolidaysPublicConfigV2 = z.object({country_codes: z.array(z.string())}); export const scheduleV2 = z.object({annotations: z.record(z.string(), z.string()), config: scheduleConfigV2.optional(), created_at: z.string(), current_shifts: z.array(scheduleEntryV2).optional(), next_shifts: z.array(scheduleEntryV2).optional(), holidays_public_config: scheduleHolidaysPublicConfigV2.optional(), id: z.string(), name: z.string(), timezone: z.string(), updated_at: z.string()}); export const schedulesV2ListResponseBody = z.object({pagination_meta: paginationMetaResultWithTotal.optional(), schedules: z.array(scheduleV2)}); 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 Schedules V2 * * GET /v2/schedules */ export const getApiV2Schedules = async (args: GetApiV2SchedulesArgs, options?: GetApiV2SchedulesOptions) => { 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.page_size != null) query.append("page_size", String(args.page_size)) if (args.after != null) query.append("after", String(args.after)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/schedules${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(schedulesV2ListResponseBody, await res.json()) };