import {z} from 'zod' export type GetApiV2TimeSheetArgs = {pageSize?: number | undefined, page?: number | undefined, field?: 'startDate' | undefined, order?: ('ASC' | 'DESC') | undefined, techId?: string | undefined, jobId?: string | undefined, dateQuery?: string | undefined, withJob?: boolean | undefined, Authorization: string}; export type GetApiV2TimeSheetOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseTimesheetV2Dto = z.object({id: z.string().optional(), techId: z.string().optional(), techName: z.string().optional(), startDate: z.string().optional(), endDate: z.string().optional(), notes: z.string().optional(), jobId: z.string().optional(), laborCost: z.number().optional(), shiftLength: z.string().optional()}); export const responseTimesheetsV2Dto = z.object({pageSize: z.number().optional(), page: z.number().optional(), totalResults: z.number().optional(), hasMore: z.boolean().optional(), data: z.array(responseTimesheetV2Dto).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 }; /** * Retrieve a list of Time Sheets * * GET /v2/timeSheet */ export const getApiV2TimeSheet = async (args: GetApiV2TimeSheetArgs, options?: GetApiV2TimeSheetOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.field != null) query.append("field", String(args.field)) if (args.order != null) query.append("order", String(args.order)) if (args.techId != null) query.append("techId", String(args.techId)) if (args.jobId != null) query.append("jobId", String(args.jobId)) if (args.dateQuery != null) query.append("dateQuery", String(args.dateQuery)) if (args.withJob != null) query.append("withJob", String(args.withJob)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/timeSheet${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(responseTimesheetsV2Dto, await res.json()) };