import {z} from 'zod' export type ShiftMember = {type: 'user_member' | 'empty_member', user_id?: string | undefined}; export type UpdateCustomShiftRequest = {custom_shift: {start_time?: string | undefined, end_time?: string | undefined, assignments?: Array<{type: 'shift_assignment', member: ShiftMember}> | undefined}}; export type UpdateApiV3SchedulesIdCustomShiftsCustomShiftIdArgs = {id: string, custom_shift_id: string, body: UpdateCustomShiftRequest}; export type UpdateApiV3SchedulesIdCustomShiftsCustomShiftIdOptions = {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 shiftAssignment = z.object({id: z.string(), type: z.literal("shift_assignment"), member: shiftMember}); export const customShift = z.object({id: z.string(), type: z.literal("custom_shift"), start_time: z.string(), end_time: z.string(), assignments: z.array(shiftAssignment), self: z.string().optional(), html_url: z.string().optional()}); export const updateApiV3SchedulesIdCustomShiftsCustomShiftIdResponse = z.object({custom_shift: customShift}); 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 }; /** * Update a custom shift * * PUT /v3/schedules/{id}/custom_shifts/{custom_shift_id} */ export const updateApiV3SchedulesIdCustomShiftsCustomShiftId = async (args: UpdateApiV3SchedulesIdCustomShiftsCustomShiftIdArgs, options?: UpdateApiV3SchedulesIdCustomShiftsCustomShiftIdOptions) => { const {baseUrl = "https://api.pagerduty.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/schedules/${encodeURIComponent(String(args.id))}/custom_shifts/${encodeURIComponent(String(args.custom_shift_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV3SchedulesIdCustomShiftsCustomShiftIdResponse, await res.json()) };