import {z} from 'zod' export type EmployeeStatus = 'ONBOARDING' | 'ACTIVE' | 'OFFBOARDING' | 'ENDED' | 'DELETED' | 'ALL'; export type SortByField = 'startOn'; export type SortOrder = 'ASC' | 'DESC'; export type GetApiV1EmployeesArgs = {page?: number | undefined, pageSize?: number | undefined, status?: EmployeeStatus | undefined, employeeId?: string | undefined, employeeCode?: string | undefined, sortBy?: SortByField | undefined, sortOrder?: SortOrder | undefined}; export type GetApiV1EmployeesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const employeeData = z.object({id: z.string(), employeeCode: z.string().nullable().optional(), employeeId: z.string().nullable().optional(), firstName: z.string(), middleName: z.string().nullable().optional(), lastName: z.string(), fullName: z.string(), displayName: z.string(), workEmail: z.string().nullable().optional(), designation: z.string(), department: z.string().nullable().optional(), status: z.enum(["ONBOARDING", "ACTIVE", "OFFBOARDING", "ENDED", "DELETED", "UNKNOWN"]), type: z.enum(["EMPLOYEE", "CONTRACTOR", "FREELANCER", "HR_MEMBER", "UNKNOWN"]), startDate: z.string(), endDate: z.string().nullable().optional(), country: z.string(), customerId: z.string(), customerName: z.string(), entity: z.string().nullable().optional()}); export const pagination = z.object({page: z.number().int(), pageSize: z.number().int(), totalCount: z.number().int(), totalPages: z.number().int()}); export const v1EmployeesGetResponse = z.object({data: z.array(employeeData), pagination: pagination}); 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 }; /** * Search and list employees * * GET /v1/employees */ export const getApiV1Employees = async (args: GetApiV1EmployeesArgs, options?: GetApiV1EmployeesOptions) => { 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.status != null) query.append("status", String(args.status)) if (args.employeeId != null) query.append("employeeId", String(args.employeeId)) if (args.employeeCode != null) query.append("employeeCode", String(args.employeeCode)) 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/employees${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(v1EmployeesGetResponse, await res.json()) };