import {z} from 'zod' export type GetApiApiPublicV2TeamTeamRotationsArgs = {'X-VO-Api-Id': string, 'X-VO-Api-Key': string, team: string}; export type GetApiApiPublicV2TeamTeamRotationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const onCallPeriod = z.object({start: z.string().optional(), end: z.string().optional(), username: z.string().optional()}); export const shiftMember = z.object({slug: z.string().optional(), username: z.string().optional()}); export const maskTimeRange = z.object({start: z.object({hour: z.number().optional(), minute: z.number().optional()}).optional(), end: z.object({hour: z.number().optional(), minute: z.number().optional()}).optional()}); export const rotationMask = z.object({day: z.object({su: z.boolean().optional(), m: z.boolean().optional(), t: z.boolean().optional(), w: z.boolean().optional(), th: z.boolean().optional(), f: z.boolean().optional(), sa: z.boolean().optional()}).optional(), time: z.array(maskTimeRange).optional()}); export const shiftDetails = z.object({shiftId: z.number().int().optional(), label: z.string().optional(), duration: z.number().optional(), current: onCallPeriod.optional(), next: onCallPeriod.optional(), periods: z.array(onCallPeriod).optional(), shiftMembers: z.array(shiftMember).optional(), shifttype: z.unknown(), start: z.string().optional(), timezone: z.string().optional(), mask: rotationMask.optional(), mask2: rotationMask.optional(), mast3: rotationMask.optional()}); export const rotation = z.object({groupId: z.number().int().optional(), label: z.string().optional(), totalMembersInRotation: z.number().int().optional(), shifts: z.array(shiftDetails).optional()}); export const getApiApiPublicV2TeamTeamRotationsResponse = z.object({rotations: z.array(rotation).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 }; /** * Get all rotations and some rotation details for a team * * GET /api-public/v2/team/{team}/rotations */ export const getApiApiPublicV2TeamTeamRotations = async (args: GetApiApiPublicV2TeamTeamRotationsArgs, options?: GetApiApiPublicV2TeamTeamRotationsOptions) => { const {baseUrl = "https://api.victorops.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-VO-Api-Id'] != null) headers.set("X-VO-Api-Id", String(args['X-VO-Api-Id'])) if (args['X-VO-Api-Key'] != null) headers.set("X-VO-Api-Key", String(args['X-VO-Api-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api-public/v2/team/${encodeURIComponent(String(args.team))}/rotations${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(getApiApiPublicV2TeamTeamRotationsResponse, await res.json()) };