import {z} from 'zod' export type GetApiJobsArgs = {$top?: number | undefined, $skip?: number | undefined, $filter?: string | undefined, $select?: Array | undefined, $orderby?: string | undefined, $count?: boolean | undefined}; export type GetApiJobsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const valueWithCurrency = z.object({Value: z.number().nullable().optional(), CurrencyCode: z.string().max(50).nullable().optional(), CurrencySymbol: z.string().max(3).nullable().optional()}); export const linkModel = z.object({Ref: z.string().max(1000).nullable().optional(), Href: z.string().max(2000).nullable().optional()}); export const jobModel = z.object({EmployeeId: z.string().min(1).max(36), JobTitle: z.string().max(100).nullable().optional(), Classification: z.string().max(50).nullable().optional(), StartDate: z.string(), EndDate: z.string().nullable().optional(), WorkingCalendar: z.string().max(50).nullable().optional(), LineManagerId: z.string().max(36).nullable().optional(), HierarchyNodeId: z.string().max(36).nullable().optional(), Active: z.boolean().optional(), Salary: z.number().nullable().optional(), SalaryWithCurrency: valueWithCurrency.optional(), Contract: z.string().max(50).nullable().optional(), PayFrequency: z.string().max(50).nullable().optional(), PayBasis: z.string().max(50).nullable().optional(), FullTimeEquivalent: z.number().nullable().optional(), Position: z.string().max(200).nullable().optional(), PositionId: z.string().max(36).nullable().optional(), ChangeReason: z.string().max(50).nullable().optional(), PublicHolidayId: z.string().max(36).nullable().optional(), PublicHoliday: z.string().max(50).nullable().optional(), WorkingPatternId: z.string().max(50).nullable().optional(), PayGrade: z.string().max(50).nullable().optional(), SpinalPoint: z.number().int().nullable().optional(), NextIncrementDate: z.string().nullable().optional(), TimesheetLocation: z.string().max(50).nullable().optional(), TimesheetRuleSet: z.string().max(50).nullable().optional(), TimesheetLunchDuration: z.string().nullable().optional(), AutoEnrolmentProfile: z.string().max(200).nullable().optional(), ExpenseSubmissionFrequency: z.string().max(50).nullable().optional(), CostCentre: z.string().max(50).nullable().optional(), JobFamily: z.string().max(50).nullable().optional(), ApprenticeUnder25: z.boolean().nullable().optional(), ApprenticeshipEndDate: z.string().nullable().optional(), ContractEndDate: z.string().nullable().optional(), ReportsToPost: z.string().max(200).nullable().optional(), NormalHours: z.number().nullable().optional(), RealTimeInformationIrregularFrequency: z.string().max(50).nullable().optional(), NoticePeriod: z.string().max(50).nullable().optional(), SourceSystemId: z.string().max(36).nullable().optional(), SourceSystemCreatedOn: z.string().nullable().optional(), SourceSystemLastModifiedOn: z.string().nullable().optional(), Links: z.array(linkModel).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 jobQueryModel = z.object({"@odata.context": z.string().nullable().optional(), "@odata.count": z.number().int().nullable().optional(), "@odata.nextlink": z.string().nullable().optional(), value: z.array(jobModel).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 Jobs. * * GET /jobs */ export const getApiJobs = async (args: GetApiJobsArgs, options?: GetApiJobsOptions) => { 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(/\/+$/, '')}/jobs${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(jobQueryModel, await res.json()) };