import {z} from 'zod' export type GetApiApiV1CalendarEventsArgs = {start: string, end: string, filter?: string | undefined, directReportsOnly?: boolean | undefined, includePersons?: boolean | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiApiV1CalendarEventsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const calendarTimeOffCalendarEventV1 = z.object({id: z.string(), type: z.literal("TIME_OFF"), start: z.string(), end: z.string(), employeeId: z.number().int(), timeOffTypeId: z.number().int().nullable()}); export const calendarHolidayCalendarEventV1 = z.object({id: z.string(), type: z.literal("HOLIDAY"), start: z.string(), end: z.string(), name: z.string()}); export const calendarBirthdayCalendarEventV1 = z.object({id: z.string(), type: z.literal("BIRTHDAY"), start: z.string(), end: z.string(), employeeId: z.number().int()}); export const calendarAnniversaryCalendarEventV1 = z.object({id: z.string(), type: z.literal("ANNIVERSARY"), start: z.string(), end: z.string(), employeeId: z.number().int(), years: z.number().int()}); export const calendarCalendarEventPersonV1 = z.object({id: z.number().int(), firstName: z.string(), lastName: z.string(), displayName: z.string(), jobTitle: z.string().nullable(), photoUrl: z.string().nullable()}); export const getApiApiV1CalendarEventsResponse = z.object({data: z.array(z.discriminatedUnion("type", [calendarTimeOffCalendarEventV1, calendarHolidayCalendarEventV1, calendarBirthdayCalendarEventV1, calendarAnniversaryCalendarEventV1])).optional(), persons: z.record(z.string(), calendarCalendarEventPersonV1).optional(), _links: z.object({prev: z.object({href: z.string().optional()}).optional(), next: z.object({href: z.string().optional()}).optional()}).optional(), meta: z.object({page: z.number().int(), pageSize: z.number().int(), totalPages: z.number().int(), totalItems: z.number().int()}).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 Calendar Events * * GET /api/v1/calendar-events * * Also accepts auth: oauth */ export const getApiApiV1CalendarEvents = async (args: GetApiApiV1CalendarEventsArgs, options?: GetApiApiV1CalendarEventsOptions) => { const {baseUrl = "https://companySubDomain.bamboohr.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.directReportsOnly != null) query.append("directReportsOnly", String(args.directReportsOnly)) if (args.includePersons != null) query.append("includePersons", String(args.includePersons)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/calendar-events${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(getApiApiV1CalendarEventsResponse, await res.json()) };