import {z} from 'zod' export type GetApiTimeTimesheetEntriesArgs = {starts_on?: string | undefined, ends_on?: string | undefined, 'employee_ids[]'?: number | undefined, 'position_ids[]'?: number | undefined, 'team_ids[]'?: number | undefined, 'department_ids[]'?: number | undefined, 'location_ids[]'?: number | undefined, 'division_ids[]'?: number | undefined, type?: string | undefined, 'project_ids[]'?: number | undefined, 'status[]'?: string | undefined}; export type GetApiTimeTimesheetEntriesOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const timesheetEntry = z.object({id: z.number().int(), employee_id: z.number().int(), starts_at: z.string(), ends_at: z.string(), date: z.string(), minutes: z.number().int(), type: z.enum(["working", "break"]), project_id: z.number().int().optional(), comment: z.string().optional()}); export const getApiTimeTimesheetEntriesResponse = z.object({data: z.array(timesheetEntry).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 timesheet entries * * GET /time/timesheet_entries */ export const getApiTimeTimesheetEntries = async (args: GetApiTimeTimesheetEntriesArgs, options?: GetApiTimeTimesheetEntriesOptions) => { const {baseUrl = "https://app.peopleforce.io/api/public/v3", fetch: fetchFn = fetch, XAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.starts_on != null) query.append("starts_on", String(args.starts_on)) if (args.ends_on != null) query.append("ends_on", String(args.ends_on)) if (args['employee_ids[]'] != null) query.append("employee_ids[]", String(args['employee_ids[]'])) if (args['position_ids[]'] != null) query.append("position_ids[]", String(args['position_ids[]'])) if (args['team_ids[]'] != null) query.append("team_ids[]", String(args['team_ids[]'])) if (args['department_ids[]'] != null) query.append("department_ids[]", String(args['department_ids[]'])) if (args['location_ids[]'] != null) query.append("location_ids[]", String(args['location_ids[]'])) if (args['division_ids[]'] != null) query.append("division_ids[]", String(args['division_ids[]'])) if (args.type != null) query.append("type", String(args.type)) if (args['project_ids[]'] != null) query.append("project_ids[]", String(args['project_ids[]'])) if (args['status[]'] != null) query.append("status[]", String(args['status[]'])) if (XAPIKEY !== undefined) headers.set("X-API-KEY", XAPIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/time/timesheet_entries${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(getApiTimeTimesheetEntriesResponse, await res.json()) };