import {z} from 'zod' export type GetApiPlacesV1PlaceHandleHistoryArgs = {place_handle: string, pagination_token?: (string | null) | undefined, limit?: (number | null) | undefined, field_name?: ('place_handle' | 'name' | 'geofence' | 'metadata' | 'parkings' | 'checkins' | 'timings' | 'closed_on' | 'expected_service_time' | 'locked_from_suggestions' | 'null' | null) | undefined}; export type GetApiPlacesV1PlaceHandleHistoryOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const source = z.object({source: z.string().nullable().optional(), timestamp: z.string().nullable().optional(), "ip-address": z.string().nullable().optional(), device_id: z.string().nullable().optional(), driver_handle: z.string().nullable().optional(), user_id: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const fieldChange = z.object({old: z.unknown(), new: z.unknown()}); export const placeHistoryEntry = z.object({audit_id: z.string(), changed_at: z.string(), changed_by: z.union([source, z.unknown()]).optional(), operation: z.string(), changes: z.record(z.string(), fieldChange)}); export const placeHistoryResponse = z.object({place_id: z.string(), place_handle: z.string(), history: z.array(placeHistoryEntry), pagination_token: z.string().nullable().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 }; /** * Get place audit history * * GET /places/v1/{place_handle}/history * * Also accepts auth: TokenAuth */ export const getApiPlacesV1PlaceHandleHistory = async (args: GetApiPlacesV1PlaceHandleHistoryArgs, options?: GetApiPlacesV1PlaceHandleHistoryOptions) => { 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.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.field_name != null) query.append("field_name", String(args.field_name)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/places/v1/${encodeURIComponent(String(args.place_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(placeHistoryResponse, await res.json()) };