import {z} from 'zod' export type GetApiLeaveRequestsIdOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const employeeShort = z.object({id: z.number().int(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional()}); export const leaveRequest = z.object({id: z.number().int(), employee_id: z.number().int(), employee: employeeShort.optional(), leave_type_id: z.number().int().optional(), leave_type: z.string(), state: z.enum(["pending", "approved", "rejected", "withdrawn"]), amount: z.number(), tracking_time_in: z.enum(["hours", "days"]), on_demand: z.boolean().optional(), ends_on: z.string(), starts_on: z.string(), comment: z.string().optional(), attachment_url: z.string().optional(), attachments: z.array(z.object({filename: z.string().optional(), url: z.string().optional(), expires_at: z.string().optional()})).optional(), entries: z.array(z.object({date: z.string(), amount: z.number(), starts_at: z.string().optional(), ends_at: z.string().optional()})).optional(), approvals: z.array(z.object({state: z.string().optional(), assigned_to: z.object({id: z.number().int().optional(), full_name: z.string().optional()}).optional()})).optional(), leave_type_policy: z.object({name: z.string(), visibility: z.enum(["private", "public"]), activity_type: z.enum(["not_working_paid", "not_working_unpaid", "working_paid"])}).optional(), created_at: z.string(), updated_at: z.string()}); export const getApiLeaveRequestsIdResponse = z.object({data: leaveRequest.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 }; /** * Get a leave request * * GET /leave_requests/{id} */ export const getApiLeaveRequestsId = async (options?: GetApiLeaveRequestsIdOptions) => { const {baseUrl = "https://app.peopleforce.io/api/public/v3", fetch: fetchFn = fetch, XAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (XAPIKEY !== undefined) headers.set("X-API-KEY", XAPIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/leave_requests/${encodeURIComponent(String(args.id))}${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(getApiLeaveRequestsIdResponse, await res.json()) };