import {z} from 'zod' export type GetApiV1TimeoffsArgs = {page?: number | undefined, pageSize?: number | undefined, employeeId?: string | undefined, status?: (Array<'DRAFT' | 'APPROVAL_IN_PROGRESS' | 'APPROVED' | 'REJECTED' | 'TAKEN' | 'DELETED' | 'REVOKED'>) | undefined, startDateFrom?: string | undefined, startDateTo?: string | undefined, typeId?: Array | undefined, sortBy?: ('startDate' | 'createdOn' | 'updatedOn' | 'noOfDays') | undefined, sortOrder?: ('ASC' | 'DESC') | undefined}; export type GetApiV1TimeoffsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const timeOffDate = z.object({date: z.string().optional(), session: z.enum(["MORNING", "AFTERNOON", "FULL_DAY"]).optional()}); export const timeOffTypeInfo = z.object({typeId: z.string().optional(), key: z.string().optional()}); export const timeOffItem = z.object({timeOffId: z.string().optional(), status: z.enum(["DRAFT", "APPROVAL_IN_PROGRESS", "APPROVED", "REJECTED", "TAKEN", "DELETED", "REVOKED"]).optional(), startDate: timeOffDate.optional(), endDate: timeOffDate.optional(), noOfDays: z.number().optional(), description: z.string().optional(), type: timeOffTypeInfo.optional(), createdOn: z.string().optional(), updatedOn: z.string().optional()}); export const pagination = z.object({page: z.number().int(), pageSize: z.number().int(), totalCount: z.number().int(), totalPages: z.number().int()}); export const v1TimeoffsGetResponse = z.object({timeOffs: z.array(timeOffItem), page: pagination, allCount: z.number().int().optional(), pendingCount: z.number().int().optional(), approvedCount: z.number().int().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 /v1/timeoffs */ export const getApiV1Timeoffs = async (args: GetApiV1TimeoffsArgs, options?: GetApiV1TimeoffsOptions) => { const {baseUrl = "https://api.usemultiplier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.employeeId != null) query.append("employeeId", String(args.employeeId)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.startDateFrom != null) query.append("startDateFrom", String(args.startDateFrom)) if (args.startDateTo != null) query.append("startDateTo", String(args.startDateTo)) for (const item of args.typeId ?? []) query.append("typeId", String(item)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/timeoffs${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(v1TimeoffsGetResponse, await res.json()) };