import {z} from 'zod' export type SuggestionApplyRequest = {applied_by: string}; export type CreateApiPlacesV1PlaceHandleSuggestionsSuggestionIdApplyArgs = {place_handle: string, suggestion_id: string, body: SuggestionApplyRequest}; export type CreateApiPlacesV1PlaceHandleSuggestionsSuggestionIdApplyOptions = {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 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 }; /** * Apply a suggestion to a place * * POST /places/v1/{place_handle}/suggestions/{suggestion_id}/apply * * Also accepts auth: TokenAuth */ export const createApiPlacesV1PlaceHandleSuggestionsSuggestionIdApply = async (args: CreateApiPlacesV1PlaceHandleSuggestionsSuggestionIdApplyArgs, options?: CreateApiPlacesV1PlaceHandleSuggestionsSuggestionIdApplyOptions) => { 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(/\/+$/, '')}/places/v1/${encodeURIComponent(String(args.place_handle))}/suggestions/${encodeURIComponent(String(args.suggestion_id))}/apply${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(suggestionResponse, await res.json()) };