import {z} from 'zod' export type GetApiApiV1TimeOffWhosOutArgs = {AcceptHeaderParameter?: ('application/xml' | 'application/json') | undefined, start?: string | undefined, end?: string | undefined, filter?: 'off' | undefined}; export type GetApiApiV1TimeOffWhosOutOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1TimeOffWhosOutResponse = z.array(z.object({id: z.number().int().optional(), type: z.enum(["timeOff", "holiday"]).optional(), employeeId: z.number().int().optional(), name: z.string().optional(), start: z.string().optional(), end: z.string().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 Who’s Out * * GET /api/v1/time_off/whos_out * * Also accepts auth: oauth */ export const getApiApiV1TimeOffWhosOut = async (args: GetApiApiV1TimeOffWhosOutArgs, options?: GetApiApiV1TimeOffWhosOutOptions) => { 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.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 (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/time_off/whos_out${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(getApiApiV1TimeOffWhosOutResponse, await res.json()) };