import {z} from 'zod' export type GetApiEmployeesArgs = {$top?: number | undefined, $skip?: number | undefined, $filter?: string | undefined, $select?: Array | undefined, $orderby?: string | undefined, $count?: boolean | undefined}; export type GetApiEmployeesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ownership = z.enum(["Personal", "Organization"]); export const phoneType = z.enum(["Mobile", "Landline"]); export const phone = z.object({Ownership: ownership.optional(), Type: phoneType.optional(), Value: z.string().max(50).nullable().optional()}); export const email = z.object({Ownership: ownership.optional(), Value: z.string().max(320).nullable().optional()}); export const address = z.object({Ownership: ownership.optional(), Address1: z.string().max(50).nullable().optional(), Address2: z.string().max(50).nullable().optional(), Address3: z.string().max(50).nullable().optional(), Address4: z.string().max(50).nullable().optional(), Address5: z.string().max(50).nullable().optional(), PostCode: z.string().max(50).nullable().optional()}); export const assignedPaySchemes = z.object({SickPay: z.string().max(50).nullable().optional(), MaternityPay: z.string().max(50).nullable().optional(), PaternityPay: z.string().max(50).nullable().optional(), AdoptionPay: z.string().max(50).nullable().optional(), SharedParentalPay: z.string().max(50).nullable().optional(), AverageHolidayPay: z.string().max(50).nullable().optional(), BereavementPay: z.string().max(50).nullable().optional(), NeonatalPay: z.string().max(50).nullable().optional()}); export const linkModel = z.object({Ref: z.string().max(1000).nullable().optional(), Href: z.string().max(2000).nullable().optional()}); export const employeeModel = z.object({DisplayId: z.string().max(50).nullable().optional(), TitleHonorific: z.string().max(50).nullable().optional(), FirstName: z.string().max(50).nullable(), KnownAs: z.string().max(50).nullable().optional(), OtherName: z.string().max(50).nullable().optional(), LastName: z.string().max(50).nullable(), CostCentre: z.string().max(50).nullable().optional(), WorkingStatus: z.string().max(511).nullable().optional(), IsManager: z.boolean().nullable().optional(), NationalInsuranceNumber: z.string().max(50).nullable().optional(), PayrollId: z.string().max(50).nullable().optional(), TaxCode: z.string().max(5).nullable().optional(), IncludeInPayroll: z.boolean().nullable().optional(), EmploymentStartDate: z.string().nullable().optional(), EmploymentLeftDate: z.string().nullable().optional(), ContinuousServiceDate: z.string().nullable().optional(), DateOfBirth: z.string().nullable().optional(), LastWorkingDate: z.string().nullable().optional(), Gender: z.string().max(50).nullable().optional(), Ethnicity: z.string().max(50).nullable().optional(), Nationality: z.string().max(50).nullable().optional(), Religion: z.string().max(50).nullable().optional(), LeaverReason: z.string().max(50).nullable().optional(), MaritalStatus: z.string().max(50).nullable().optional(), Phones: z.array(phone).nullable().optional(), Emails: z.array(email).nullable().optional(), Addresses: z.array(address).nullable().optional(), GenderIdentity: z.string().max(50).nullable().optional(), WindowsUsername: z.string().max(50).nullable().optional(), PayrollCompanyName: z.string().max(50).nullable().optional(), Initials: z.string().max(50).nullable().optional(), AssignedPaySchemes: assignedPaySchemes.optional(), LengthOfServiceYears: z.number().int().nullable().optional(), LengthOfServiceMonths: z.number().int().nullable().optional(), SourceSystemId: z.string().max(36).nullable().optional(), Links: z.array(linkModel).nullable().optional(), SourceSystemCreatedOn: z.string().nullable().optional(), SourceSystemLastModifiedOn: z.string().nullable().optional(), Id: z.string().max(36).nullable().optional(), CreatedOn: z.string().optional(), CreatedBy: z.string().max(36).nullable().optional(), LastModifiedOn: z.string().nullable().optional(), LastModifiedBy: z.string().max(36).nullable().optional()}); export const employeeQueryModel = z.object({"@odata.context": z.string().nullable().optional(), "@odata.count": z.number().int().nullable().optional(), "@odata.nextlink": z.string().nullable().optional(), value: z.array(employeeModel).nullable().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 }; /** * Gets a list of Employees. * * GET /employees */ export const getApiEmployees = async (args: GetApiEmployeesArgs, options?: GetApiEmployeesOptions) => { const {baseUrl = "https://api.iris.co.uk/hr/v2", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.$top != null) query.append("$top", String(args.$top)) if (args.$skip != null) query.append("$skip", String(args.$skip)) if (args.$filter != null) query.append("$filter", String(args.$filter)) if (args.$select != null) query.append("$select", args.$select.join(',')) if (args.$orderby != null) query.append("$orderby", String(args.$orderby)) if (args.$count != null) query.append("$count", String(args.$count)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(employeeQueryModel, await res.json()) };