import {z} from 'zod' export type WorkerExportRequest = {from_date?: (string | null) | undefined, to_date?: (string | null) | undefined, limit?: (number | null) | undefined, include_schedule?: boolean | undefined, include_summary?: boolean | undefined, ops_group_handle?: (string | null) | undefined, profile?: (string | null) | undefined, worker_handle?: (string | null) | undefined, search_term?: (string | null) | undefined, pagination_token?: (string | null) | undefined, device_id?: (string | null) | undefined, include_deleted?: boolean | undefined, from_time?: (string | null) | undefined, to_time?: (string | null) | undefined, filter_outage?: (string | null) | undefined, filter_outage_category?: (string | null) | undefined, filter_geofence_metadata?: (string | null) | undefined, aggregate?: boolean | undefined, work_status?: (string | null) | undefined, filter_status?: ('active' | 'inactive' | 'disconnected' | 'null' | null) | undefined, tracked?: (boolean | null) | undefined, worked?: (boolean | null) | undefined, intended_to_track?: (boolean | null) | undefined, include_lp?: (boolean | null) | undefined, region_filter?: (string | null) | undefined, email_recipients?: Array | undefined}; export type CreateApiWorkersExportArgs = {body: WorkerExportRequest}; export type CreateApiWorkersExportOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const workerExportResponse = z.object({job_id: z.string(), status: z.string(), message: z.string()}); 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 }; /** * Create a worker export job * * POST /workers/export * * Also accepts auth: TokenAuth */ export const createApiWorkersExport = async (args: CreateApiWorkersExportArgs, options?: CreateApiWorkersExportOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/workers/export${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(workerExportResponse, await res.json()) };