import {z} from 'zod' export type GetApiV1TimesheetsArgs = {page?: number | undefined, pageSize?: number | undefined, employeeIds?: Array | undefined, status?: ('OPEN' | 'PARTIALLY_SUBMITTED' | 'SUBMITTED' | 'APPROVED' | 'REJECTED') | undefined, startDateFrom?: string | undefined, startDateTo?: string | undefined, memberName?: string | undefined, companyIds?: Array | undefined, country?: string | undefined, sortBy?: ('startDate' | 'endDate' | 'updatedOn' | 'submittedOn' | 'approvedOn') | undefined, sortOrder?: ('ASC' | 'DESC') | undefined}; export type GetApiV1TimesheetsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v1TimesheetsGetResponseTimesheet = z.object({timesheetId: z.string(), employeeId: z.string().optional(), startDate: z.string(), endDate: z.string(), status: z.enum(["OPEN", "PARTIALLY_SUBMITTED", "SUBMITTED", "APPROVED", "REJECTED", "UNKNOWN"]), totalDays: z.number().int().optional(), totalSubmittedDays: z.number().int().optional(), totalApprovedDays: z.number().int().optional(), totalRejectedDays: z.number().int().optional(), totalTimeInMinutes: z.number().int().optional(), submittedOn: z.string().optional(), approvedOn: z.string().optional(), updatedOn: z.string().optional()}); export const pagination = z.object({page: z.number().int(), pageSize: z.number().int(), totalCount: z.number().int(), totalPages: z.number().int()}); export const v1TimesheetsGetResponse = z.object({data: z.array(v1TimesheetsGetResponseTimesheet), pagination: pagination}); 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 timesheets with filtering and pagination * * GET /v1/timesheets */ export const getApiV1Timesheets = async (args: GetApiV1TimesheetsArgs, options?: GetApiV1TimesheetsOptions) => { const {baseUrl = "https://api.usemultiplier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) for (const item of args.employeeIds ?? []) query.append("employeeIds", String(item)) if (args.status != null) query.append("status", String(args.status)) if (args.startDateFrom != null) query.append("startDateFrom", String(args.startDateFrom)) if (args.startDateTo != null) query.append("startDateTo", String(args.startDateTo)) if (args.memberName != null) query.append("memberName", String(args.memberName)) for (const item of args.companyIds ?? []) query.append("companyIds", String(item)) if (args.country != null) query.append("country", String(args.country)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(v1TimesheetsGetResponse, await res.json()) };