import {z} from 'zod' export type GetApiPlacesV1PlaceHandleSuggestionsArgs = {place_handle: string, suggested_on_from?: (string | null) | undefined, suggested_on_to?: (string | null) | undefined, pagination_token?: (string | null) | undefined, limit?: (number | null) | undefined}; export type GetApiPlacesV1PlaceHandleSuggestionsOptions = {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 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 suggestionListResponse = z.object({suggestions: z.array(suggestionResponse), 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 suggestions for a place * * GET /places/v1/{place_handle}/suggestions/ * * Also accepts auth: TokenAuth */ export const getApiPlacesV1PlaceHandleSuggestions = async (args: GetApiPlacesV1PlaceHandleSuggestionsArgs, options?: GetApiPlacesV1PlaceHandleSuggestionsOptions) => { 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.suggested_on_from != null) query.append("suggested_on_from", String(args.suggested_on_from)) if (args.suggested_on_to != null) query.append("suggested_on_to", String(args.suggested_on_to)) if (args.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.limit != null) query.append("limit", String(args.limit)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/places/v1/${encodeURIComponent(String(args.place_handle))}/suggestions/${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(suggestionListResponse, await res.json()) };