import {z} from 'zod' export type PointGeometry = {coordinates: Array, type?: 'Point' | undefined}; export type PolygonGeometry = {type: 'Polygon', coordinates: Array>>}; export type ScheduleItem = {day_of_week: 'MONDAY' | 'TUESDAY' | 'WEDNESDAY' | 'THURSDAY' | 'FRIDAY' | 'SATURDAY' | 'SUNDAY', start_time: string, end_time: string}; export type AddressComponents = {country?: (string | null) | undefined, state?: (string | null) | undefined, city?: (string | null) | undefined, postal_code?: (string | null) | undefined, street?: (string | null) | undefined}; export type PointLocationGeometryResponse = {type: 'Point', coordinates: Array}; export type PlaceLocation = {geometry: PointLocationGeometryResponse}; export type VisitDetectionConfig = {worker_handle?: (string | null) | undefined, metadata_filter?: ({[key: string]: unknown} | null) | undefined}; export type PlacePatchRequest = {place_handle?: (string | null) | undefined, name?: (string | null) | undefined, geometry?: (PointGeometry | PolygonGeometry | unknown) | undefined, radius?: (number | null) | undefined, address?: (string | null) | undefined, timings?: (Array | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, closed_on?: (Array | null) | undefined, expected_service_time?: (number | null) | undefined, detect_visits?: (boolean | null) | undefined, geofence_id?: (string | null) | undefined, address_components?: (AddressComponents | unknown) | undefined, place_type?: ('shop' | 'house' | 'restaurant' | 'office' | 'warehouse' | 'hospital' | 'mall' | 'university' | 'golf_course' | 'stadium' | 'airport' | 'null' | null) | undefined, parkings?: (Array | null) | undefined, checkins?: (Array | null) | undefined, visit_detection_config?: (VisitDetectionConfig | unknown) | undefined, suggestion_used?: (boolean | null) | undefined, suggestion_id?: (string | null) | undefined, locked_from_suggestions?: (boolean | null) | undefined, suggestions_reviewed_on?: (string | null) | undefined}; export type PatchApiPlacesV1PlaceHandleArgs = {place_handle: string, update_associated_orders?: (boolean | null) | undefined, update_geometry_using_address?: (boolean | null) | undefined, body: PlacePatchRequest}; export type PatchApiPlacesV1PlaceHandleOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pointGeometry = z.object({coordinates: z.array(z.number()), type: z.literal("Point").optional()}); export const polygonGeometry = z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number())))}); export const scheduleItem = z.object({day_of_week: z.enum(["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]), start_time: z.string(), end_time: z.string()}); export const addressComponents = z.object({country: z.string().nullable().optional(), state: z.string().nullable().optional(), city: z.string().nullable().optional(), postal_code: z.string().nullable().optional(), street: z.string().nullable().optional()}); export const polygonLocationGeometryResponse = z.object({type: z.literal("Polygon"), coordinates: z.array(z.array(z.array(z.number())))}); export const inferredGeofence = z.object({geometry: polygonLocationGeometryResponse, source: z.enum(["customer_provided", "ht_generated"]), inclusivity: z.string(), skew: z.number(), area_ratio: z.number().nullable().optional(), centroid_shift: z.number().nullable().optional()}); export const visitDetectionConfig = z.object({worker_handle: z.string().nullable().optional(), metadata_filter: z.record(z.string(), z.unknown()).nullable().optional()}); export const boundingBox = z.object({geometry: polygonLocationGeometryResponse, source: z.enum(["customer_provided", "ht_generated"]), skew: z.number(), area_ratio: z.number().nullable().optional(), centroid_shift: z.number().nullable().optional()}); 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 pointLocationGeometryResponse = z.object({type: z.literal("Point"), coordinates: z.array(z.number())}); export const placeLocationScore = z.object({geometry: pointLocationGeometryResponse, score: z.number(), source: z.enum(["customer_provided", "ht_generated"])}); export const area = z.object({geometry: polygonLocationGeometryResponse, source: z.enum(["customer_provided", "ht_generated"])}); export const suggestionResponse = z.object({suggestion_id: z.string(), geometry: z.union([pointGeometry, polygonGeometry]), radius: z.number().int().nullable().optional(), hex_id: z.string(), applied_by: z.string().nullable().optional(), applied_on: z.string().nullable().optional(), suggested_by: z.string().nullable().optional(), suggested_on: z.string(), order_handle: z.string().nullable().optional(), source: z.enum(["ht_generated", "customer_provided"]).optional(), type: z.string().optional(), skew: z.number().nullable().optional(), area_ratio: z.number().nullable().optional(), centroid_shift: z.number().nullable().optional(), inclusivity: z.string().nullable().optional()}); export const placeResponse = z.object({place_handle: z.string().nullable().optional(), name: z.string().nullable().optional(), geometry: z.union([pointGeometry, polygonGeometry]), radius: z.number().int().gte(1).nullable().optional(), address: z.string().nullable().optional(), timings: z.array(scheduleItem).nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), closed_on: z.array(z.string()).nullable().optional(), expected_service_time: z.number().int().nullable().optional(), detect_visits: z.boolean().nullable().optional(), geofence_id: z.string().nullable().optional(), address_components: z.union([addressComponents, z.unknown()]).optional(), place_type: z.enum(["shop", "house", "restaurant", "office", "warehouse", "hospital", "mall", "university", "golf_course", "stadium", "airport", "null"]).nullable().optional(), place_id: z.string().nullable().optional(), last_updated: z.string().nullable().optional(), order_count: z.number().int().nullable().optional(), inferred_geofences: z.array(inferredGeofence).nullable().optional(), inferred_geofences_skew: z.number().nullable().optional(), visit_detection_config: z.union([visitDetectionConfig, z.unknown()]).optional(), inferred_geofence: z.union([inferredGeofence, z.unknown()]).optional(), bounding_box: z.union([boundingBox, z.unknown()]).optional(), created_by: z.union([source, z.unknown()]).optional(), updated_by: z.union([source, z.unknown()]).optional(), locked_from_suggestions: z.boolean().nullable().optional(), suggestion_applied: z.boolean().nullable().optional(), suggestion_applied_on: z.string().nullable().optional(), has_pending_suggestion: z.boolean().nullable().optional(), suggestion_not_applied_reason: z.string().nullable().optional(), suggestions_reviewed_on: z.string().nullable().optional(), parkings: z.array(placeLocationScore).nullable().optional(), checkins: z.array(placeLocationScore).nullable().optional(), service_areas: z.array(area).nullable().optional(), completions: z.array(placeLocationScore).nullable().optional(), suggestions: z.array(suggestionResponse).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 }; /** * Updates a place * * PATCH /places/v1/{place_handle} * * Also accepts auth: TokenAuth */ export const patchApiPlacesV1PlaceHandle = async (args: PatchApiPlacesV1PlaceHandleArgs, options?: PatchApiPlacesV1PlaceHandleOptions) => { 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 (args.update_associated_orders != null) query.append("update_associated_orders", String(args.update_associated_orders)) if (args.update_geometry_using_address != null) query.append("update_geometry_using_address", String(args.update_geometry_using_address)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/places/v1/${encodeURIComponent(String(args.place_handle))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(placeResponse, await res.json()) };