import {z} from 'zod' export type ScheduleRotationHandoverV2 = {interval?: number | undefined, interval_type?: ('hourly' | 'daily' | 'weekly') | undefined}; export type ScheduleLayerCreatePayloadV2 = {id?: string | undefined, name: string}; export type UserReferencePayloadV2 = {email?: string | undefined, id?: string | undefined, slack_user_id?: string | undefined}; export type ScheduleRotationWorkingIntervalCreatePayloadV2 = {end_time: string, start_time: string, weekday: 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday'}; export type ScheduleRotationCreatePayloadV2 = {effective_from?: string | undefined, handover_start_at?: string | undefined, handovers?: Array | undefined, id?: string | undefined, layers?: Array | undefined, name: string, users?: Array | undefined, working_interval?: Array | undefined}; export type ScheduleConfigCreatePayloadV2 = {rotations?: Array | undefined}; export type ScheduleHolidaysPublicConfigPayloadV2 = {country_codes: Array}; export type ScheduleCreatePayloadV2 = {annotations?: {[key: string]: string} | undefined, config?: ScheduleConfigCreatePayloadV2 | undefined, holidays_public_config?: ScheduleHolidaysPublicConfigPayloadV2 | undefined, name?: string | undefined, timezone?: string | undefined}; export type SchedulesV2CreateRequestBody = {schedule: ScheduleCreatePayloadV2}; export type CreateApiV2SchedulesArgs = {body: SchedulesV2CreateRequestBody}; export type CreateApiV2SchedulesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 schedulesV2CreateResponseBody = z.object({schedule: 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 }; /** * Create Schedules V2 * * POST /v2/schedules */ export const createApiV2Schedules = async (args: CreateApiV2SchedulesArgs, options?: CreateApiV2SchedulesOptions) => { const {baseUrl = "https:////api.incident.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/schedules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(schedulesV2CreateResponseBody, await res.json()) };