import {z} from 'zod' export type GetApiApiV1TimeOffPoliciesArgs = {filter?: string | undefined, orderBy?: string | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiApiV1TimeOffPoliciesOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const timeOffPolicyMilestoneV1 = z.object({id: z.number().int().optional(), accrualBeginsInterval: z.number().int().optional(), accrualBeginsType: z.enum(["IMMEDIATELY", "DAYS", "WEEKS", "MONTHS", "YEARS"]).optional(), accrualHours: z.number().nullable().optional(), accrualInterval: z.enum(["HOURLY", "DAILY", "WEEKLY", "BIWEEKLY", "SEMIMONTHLY", "MONTHLY", "QUARTERLY", "SEMIANNUALLY", "ANNUALLY", "ANNIVERSARY"]).optional(), maximumHours: z.number().nullable().optional(), maximumCarryOver: z.number().nullable().optional(), minimumCarryOver: z.number().nullable().optional(), accruesPerHourWorked: z.boolean().optional(), preaccruing: z.boolean().optional()}); export const timeOffPolicyVersionV1 = z.object({id: z.number().int().optional(), policyId: z.number().int().optional(), effectiveDate: z.string().nullable().optional(), status: z.enum(["ACTIVE", "ARCHIVED"]).optional(), rateTransitions: z.enum(["IMMEDIATELY", "NEXT_PERIOD"]).optional(), firstAccrual: z.enum(["PRORATE", "FULL"]).optional(), carryOverYmd: z.string().nullable().optional(), negativeBalanceResetOnCarryOver: z.boolean().optional(), milestones: z.array(timeOffPolicyMilestoneV1).optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional()}); export const timeOffPolicyV1 = z.object({id: z.number().int().optional(), name: z.string().optional(), categoryId: z.number().int().optional(), type: z.enum(["ACCRUING", "UNLIMITED", "MANUAL"]).optional(), currentVersion: timeOffPolicyVersionV1.nullable().optional(), assignedEmployeeCount: z.number().int().optional(), createdAt: z.string().optional(), updatedAt: z.string().nullable().optional(), deletedAt: z.string().nullable().optional()}); export const timeOffPolicyCollectionV1 = z.array(timeOffPolicyV1); export const getApiApiV1TimeOffPoliciesResponse = z.object({data: timeOffPolicyCollectionV1.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()}).optional(), next: z.object({href: z.string().optional()}).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 Policies * * GET /api/v1/time-off/policies * * Also accepts auth: oauth */ export const getApiApiV1TimeOffPolicies = async (args: GetApiApiV1TimeOffPoliciesArgs, options?: GetApiApiV1TimeOffPoliciesOptions) => { 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 (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/time-off/policies${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(getApiApiV1TimeOffPoliciesResponse, await res.json()) };