import {z} from 'zod' export type GetApiV3SchedulesIdOverridesArgs = {id: string, since: string, until: string, time_zone?: string | undefined, overflow?: boolean | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV3SchedulesIdOverridesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const shiftMember = z.object({type: z.enum(["user_member", "empty_member"]), user_id: z.string().optional()}); export const overrideShift = z.object({id: z.string(), type: z.literal("override_shift"), rotation_id: z.string().optional(), custom_shift_id: z.string().optional(), start_time: z.string(), end_time: z.string(), overridden_member: shiftMember, overriding_member: shiftMember, self: z.string().optional(), html_url: z.string().optional()}); export const getApiV3SchedulesIdOverridesResponse = z.object({overrides: z.array(overrideShift), 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 overrides * * GET /v3/schedules/{id}/overrides */ export const getApiV3SchedulesIdOverrides = async (args: GetApiV3SchedulesIdOverridesArgs, options?: GetApiV3SchedulesIdOverridesOptions) => { 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.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (args.time_zone != null) query.append("time_zone", String(args.time_zone)) if (args.overflow != null) query.append("overflow", String(args.overflow)) 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))}/overrides${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(getApiV3SchedulesIdOverridesResponse, await res.json()) };