import {z} from 'zod' export type GetApiWorkersWorkerHandleHistoryArgs = {worker_handle: string, from_time?: (string | null) | undefined, to_time?: (string | null) | undefined, date?: (string | null) | undefined}; export type GetApiWorkersWorkerHandleHistoryOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const lineStringLocation1 = z.object({type: z.string(), coordinates: z.array(z.array(z.unknown()))}); export const getWorkerHistoryResponse = z.object({worker_handle: z.string(), locations: lineStringLocation1, markers: z.array(z.record(z.string(), z.unknown())), started_at: z.string(), completed_at: z.string(), distance: z.number().int(), duration: z.number().int(), steps: z.number().int()}); 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 Worker History * * GET /workers/{worker_handle}/history * * Also accepts auth: TokenAuth */ export const getApiWorkersWorkerHandleHistory = async (args: GetApiWorkersWorkerHandleHistoryArgs, options?: GetApiWorkersWorkerHandleHistoryOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_time != null) query.append("from_time", String(args.from_time)) if (args.to_time != null) query.append("to_time", String(args.to_time)) if (args.date != null) query.append("date", String(args.date)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/workers/${encodeURIComponent(String(args.worker_handle))}/history${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(getWorkerHistoryResponse, await res.json()) };