import {z} from 'zod' export type MaskTimeRange = {start?: ({hour?: number | undefined, minute?: number | undefined}) | undefined, end?: ({hour?: number | undefined, minute?: number | undefined}) | undefined}; export type RotationMask = {day?: ({su?: boolean | undefined, m?: boolean | undefined, t?: boolean | undefined, w?: boolean | undefined, th?: boolean | undefined, f?: boolean | undefined, sa?: boolean | undefined}) | undefined, time?: Array | undefined}; export type RotationShiftCreatePayload = {label: string, timezone: string, start: number, duration: number, shifttype: 'std' | 'fts' | 'cstm' | 'pho', mask: RotationMask, mask2?: RotationMask | undefined, mask3?: RotationMask | undefined, usernames?: Array | undefined}; export type RotationGroupCreatePayload = {label: string, shifts?: Array | undefined}; export type CreateApiApiPublicV1TeamsTeamRotationsArgs = {'X-VO-Api-Id': string, 'X-VO-Api-Key': string, team: string, skip?: number | undefined, count?: number | undefined, body: RotationGroupCreatePayload}; export type CreateApiApiPublicV1TeamsTeamRotationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 rotationShiftPeriod = z.object({start: z.number().int().optional(), end: z.number().int().optional(), username: z.string().optional(), isRoll: z.boolean().optional(), memberSlug: z.string().optional()}); export const shiftMember = z.object({slug: z.string().optional(), username: z.string().optional()}); export const rotationShiftResource = z.object({org_slug: z.string().optional(), team_slug: z.string().optional(), rot_id: z.number().int().optional(), group_id: z.number().int().optional(), label: z.string().optional(), timezone: z.string().optional(), start: z.number().int().optional(), duration: z.number().int().optional(), shifttype: z.enum(["std", "fts", "cstm", "pho"]).optional(), mask: rotationMask.optional(), mask2: rotationMask.optional(), mask3: rotationMask.optional(), periods: z.array(rotationShiftPeriod).optional(), oncall: z.string().optional(), current: rotationShiftPeriod.optional(), next: rotationShiftPeriod.optional(), usernames: z.array(z.string()).optional(), shiftMembers: z.array(shiftMember).optional()}); export const rotationGroupResponse = z.object({orgslug: z.string().optional(), teamslug: z.string().optional(), id: z.number().int().optional(), label: z.string().optional(), shifts: z.array(rotationShiftResource).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 }; /** * Create a rotation group * * POST /api-public/v1/teams/{team}/rotations */ export const createApiApiPublicV1TeamsTeamRotations = async (args: CreateApiApiPublicV1TeamsTeamRotationsArgs, options?: CreateApiApiPublicV1TeamsTeamRotationsOptions) => { const {baseUrl = "https://api.victorops.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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'])) if (args.skip != null) query.append("skip", String(args.skip)) if (args.count != null) query.append("count", String(args.count)) const url = `${baseUrl.replace(/\/+$/, '')}/api-public/v1/teams/${encodeURIComponent(String(args.team))}/rotations${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(rotationGroupResponse, await res.json()) };