import {z} from 'zod' export type GetApiTimeOffStatusArgs = {Authorization: string, _userId: string, _timeOffTypeId: string}; export type GetApiTimeOffStatusOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTimeOffStatusResponse = z.object({_userId: z.string().optional(), _timeOffTypeId: z.string().optional(), timeOffTypeName: z.string().optional(), policyName: z.string().optional(), _policyType: z.enum(["Day", "Hour"]).optional(), unlimitedAllowance: z.boolean().optional(), totalAllowance: z.number().optional(), cycleAllowance: z.number().optional(), prevCycleCarryOver: z.number().optional(), alreadyTaken: z.number().optional(), planned: z.number().optional(), available: z.number().optional(), carryOverEnabled: z.boolean().optional(), plannedFromCarryOver: z.number().optional(), takenFromCarryOver: z.number().optional(), expiredCarryOver: z.number().optional(), carryOverExpiresAt: 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 }; /** * Return a list of time-off statuses. * * GET /time-off/status */ export const getApiTimeOffStatus = async (args: GetApiTimeOffStatusArgs, options?: GetApiTimeOffStatusOptions) => { const {baseUrl = "https://sandbox-api.kenjo.io/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args._userId != null) query.append("_userId", String(args._userId)) if (args._timeOffTypeId != null) query.append("_timeOffTypeId", String(args._timeOffTypeId)) const url = `${baseUrl.replace(/\/+$/, '')}/time-off/status${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(getApiTimeOffStatusResponse, await res.json()) };