import {z} from 'zod' export type GetApiApiV1TimeOffRequestsArgs = {AcceptHeaderParameter?: ('application/xml' | 'application/json') | undefined, id?: string | undefined, action?: ('view' | 'approve' | 'myRequests') | undefined, employeeId?: string | undefined, start?: string | undefined, end?: string | undefined, updatedSince?: string | undefined, type?: string | undefined, status?: string | undefined, excludeNote?: string | undefined}; export type GetApiApiV1TimeOffRequestsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1TimeOffRequestsResponse = z.array(z.object({id: z.string().optional(), employeeId: z.string().optional(), name: z.string().optional(), start: z.string().optional(), end: z.string().optional(), created: z.string().optional(), updated: z.string().nullable().optional(), status: z.object({lastChanged: z.string().optional(), lastChangedByUserId: z.string().optional(), status: z.string().optional()}).optional(), type: z.object({id: z.string().optional(), name: z.string().optional(), icon: z.string().optional()}).optional(), amount: z.object({unit: z.enum(["hours", "days"]).optional(), amount: z.number().optional()}).optional(), actions: z.object({view: z.boolean().optional(), edit: z.boolean().optional(), cancel: z.boolean().optional(), approve: z.boolean().optional(), deny: z.boolean().optional(), bypass: z.boolean().optional()}).optional(), dates: z.record(z.string(), z.number()).optional(), notes: z.object({employee: z.string().optional(), manager: z.string().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.AcceptHeaderParameter != null) headers.set("AcceptHeaderParameter", String(args.AcceptHeaderParameter)) if (args.id != null) query.append("id", String(args.id)) if (args.action != null) query.append("action", String(args.action)) if (args.employeeId != null) query.append("employeeId", String(args.employeeId)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.updatedSince != null) query.append("updatedSince", String(args.updatedSince)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (args.excludeNote != null) query.append("excludeNote", String(args.excludeNote)) 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()) };