import {z} from 'zod' export type GetApiApiV1TimeOffRequestsArgs = {filter?: string | undefined, orderBy?: string | undefined, page?: number | undefined, pageSize?: number | undefined, returnActions?: boolean | undefined}; export type GetApiApiV1TimeOffRequestsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const timeOffRequestDailyAmountV1 = z.object({date: z.string().optional(), amount: z.number().optional()}); export const availableAction = z.object({href: z.string().optional(), fields: z.array(z.object({})).nullable().optional(), method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]).nullable().optional()}); export const timeOffRequestV1 = z.object({id: z.number().int().optional(), employeeId: z.number().int().optional(), categoryId: z.number().int().optional(), startDate: z.string().optional(), endDate: z.string().optional(), amount: z.number().optional(), unit: z.enum(["HOURS", "DAYS"]).optional(), status: z.enum(["REQUESTED", "APPROVED", "DENIED", "CANCELED"]).optional(), requestedAt: z.string().optional(), dailyAmounts: z.array(timeOffRequestDailyAmountV1).optional(), employeeNote: z.string().nullable().optional(), managerNote: z.string().nullable().optional(), statusUpdatedAt: z.string().nullable().optional(), statusUpdatedBy: z.number().int().nullable().optional(), _links: z.record(z.string(), availableAction).optional()}); export const timeOffRequestCollectionV1 = z.array(timeOffRequestV1); export const getApiApiV1TimeOffRequestsResponse = z.object({data: timeOffRequestCollectionV1.optional(), meta: z.object({page: z.number().int().optional(), pageSize: z.number().int().optional(), totalPages: z.number().int().optional(), totalItems: z.number().int().optional()}).optional(), _links: z.object({prev: z.object({href: z.string().optional()}).nullable().optional(), next: z.object({href: z.string().optional()}).nullable().optional()}).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 Time Off Requests * * GET /api/v1/time-off/requests * * Also accepts auth: oauth */ export const getApiApiV1TimeOffRequests = async (args: GetApiApiV1TimeOffRequestsArgs, options?: GetApiApiV1TimeOffRequestsOptions) => { 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.filter != null) query.append("filter", String(args.filter)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.returnActions != null) query.append("returnActions", String(args.returnActions)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/time-off/requests${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(getApiApiV1TimeOffRequestsResponse, await res.json()) };