import {z} from 'zod' export type GetHrisTimesheetsParameterCursor = string; export type GetHrisTimesheetsParameterPageSize = number; export type GetHrisTimesheetsParameterUpdatedAfter = string; export type GetHrisTimesheetsParameterIncludeDeleted = 'true' | 'false'; export type GetHrisTimesheetsParameterIgnoreUnsupportedFilters = 'true' | 'false'; export type GetHrisTimesheetsParameterIds = string; export type GetHrisTimesheetsParameterRemoteIds = string; export type GetHrisTimesheetsParameterEmployeeId = string; export type GetHrisTimesheetsParameterStartedBefore = string; export type GetHrisTimesheetsParameterStartedAfter = string; export type GetHrisTimesheetsParameterEndedBefore = string; export type GetHrisTimesheetsParameterEndedAfter = string; export type GetApiHrisTimesheetsArgs = {'X-Integration-Id': string, cursor?: GetHrisTimesheetsParameterCursor | undefined, page_size?: GetHrisTimesheetsParameterPageSize | undefined, updated_after?: GetHrisTimesheetsParameterUpdatedAfter | undefined, include_deleted?: GetHrisTimesheetsParameterIncludeDeleted | undefined, ignore_unsupported_filters?: GetHrisTimesheetsParameterIgnoreUnsupportedFilters | undefined, ids?: GetHrisTimesheetsParameterIds | undefined, remote_ids?: GetHrisTimesheetsParameterRemoteIds | undefined, employee_id?: GetHrisTimesheetsParameterEmployeeId | undefined, started_before?: GetHrisTimesheetsParameterStartedBefore | undefined, started_after?: GetHrisTimesheetsParameterStartedAfter | undefined, ended_before?: GetHrisTimesheetsParameterEndedBefore | undefined, ended_after?: GetHrisTimesheetsParameterEndedAfter | undefined}; export type GetApiHrisTimesheetsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getHrisTimesheetsPositiveResponse = z.object({status: z.literal("success"), data: z.object({next: z.string().nullable(), results: z.array(z.object({id: z.string(), remote_id: z.string().nullable(), employee_id: z.string(), started_at: z.string().nullable(), ended_at: z.string().nullable(), timezone: z.string().nullable(), payable_hours: z.number().nullable(), unpaid_break_minutes: z.number().nullable(), breaks: z.array(z.object({ended_at: z.union([z.string(), z.string()]), paid: z.boolean(), started_at: z.union([z.string(), z.string()])})).nullable().optional(), approval_status: z.string().nullable(), approved_at: z.string().nullable(), comment: z.string().nullable(), custom_fields: z.record(z.string(), z.unknown()).nullable(), integration_fields: z.array(z.object({id: z.string(), key: z.string(), type: z.enum(["DEFAULT", "CUSTOM"]), value: z.unknown(), label: z.string().nullable()})), changed_at: z.string(), remote_deleted_at: z.string().nullable(), remote_data: z.record(z.string(), z.unknown()).nullable()}))})}); 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 }; /** * Get timesheets * * GET /hris/timesheets */ export const getApiHrisTimesheets = async (args: GetApiHrisTimesheetsArgs, options?: GetApiHrisTimesheetsOptions) => { const {baseUrl = "https://api.kombo.dev/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Integration-Id'] != null) headers.set("X-Integration-Id", String(args['X-Integration-Id'])) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.include_deleted != null) query.append("include_deleted", String(args.include_deleted)) if (args.ignore_unsupported_filters != null) query.append("ignore_unsupported_filters", String(args.ignore_unsupported_filters)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.remote_ids != null) query.append("remote_ids", String(args.remote_ids)) if (args.employee_id != null) query.append("employee_id", String(args.employee_id)) if (args.started_before != null) query.append("started_before", String(args.started_before)) if (args.started_after != null) query.append("started_after", String(args.started_after)) if (args.ended_before != null) query.append("ended_before", String(args.ended_before)) if (args.ended_after != null) query.append("ended_after", String(args.ended_after)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/hris/timesheets${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(getHrisTimesheetsPositiveResponse, await res.json()) };