import {z} from 'zod' export type GetApiEmployeesPersonalsArgs = {Authorization: string, firstName?: string | undefined, lastName?: string | undefined, displayName?: string | undefined, gender?: ('Male' | 'Female' | 'Other') | undefined, birthdate?: string | undefined, nationality?: string | undefined}; export type GetApiEmployeesPersonalsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiEmployeesPersonalsResponse = z.object({data: z.array(z.object({_id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), displayName: z.string().optional(), gender: z.string().optional(), birthdate: z.string().optional(), nationality: z.string().optional(), delegatedApproverId: z.string().optional()})).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 employee personals. * * GET /employees/personals */ export const getApiEmployeesPersonals = async (args: GetApiEmployeesPersonalsArgs, options?: GetApiEmployeesPersonalsOptions) => { 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.firstName != null) query.append("firstName", String(args.firstName)) if (args.lastName != null) query.append("lastName", String(args.lastName)) if (args.displayName != null) query.append("displayName", String(args.displayName)) if (args.gender != null) query.append("gender", String(args.gender)) if (args.birthdate != null) query.append("birthdate", String(args.birthdate)) if (args.nationality != null) query.append("nationality", String(args.nationality)) const url = `${baseUrl.replace(/\/+$/, '')}/employees/personals${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(getApiEmployeesPersonalsResponse, await res.json()) };