import {z} from 'zod' export type GetApiV2TasksLaborArgs = {tasks?: string | undefined, users?: number | undefined, start?: number | undefined, end?: number | undefined, limit?: number | undefined, cursor?: number | undefined, locations?: number | undefined, orderBy?: string | undefined}; export type GetApiV2TasksLaborOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2TasksLaborResponse = z.array(z.object({meta: z.object({task: z.string().optional(), user: z.string().optional()}).optional(), userID: z.number().int().optional(), timeSpent: z.number().int().optional(), userWage: z.number().int().optional(), taskID: z.number().int().optional(), taskName: z.string().optional(), tashPriorityID: z.number().int().optional(), description: z.unknown(), dateLogged: z.number().int().optional(), billableTime: z.unknown(), billableRate: z.unknown(), categoryID: z.unknown()})); 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 }; /** * Task Labor * * GET /v2/tasks/labor */ export const getApiV2TasksLabor = async (args: GetApiV2TasksLaborArgs, options: GetApiV2TasksLaborOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tasks != null) query.append("tasks", String(args.tasks)) if (args.users != null) query.append("users", String(args.users)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.locations != null) query.append("locations", String(args.locations)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/tasks/labor${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(getApiV2TasksLaborResponse, await res.json()) };