import {z} from 'zod' export type GetApiLangPlacesAutosuggestArgs = {lang: string, name: string, radius: number, lon: number, lat: number, src_geom?: ('osm' | 'wikidata' | 'snow' | 'cultura.ru' | 'rosnedra') | undefined, src_attr?: ('osm' | 'wikidata' | 'snow' | 'cultura.ru' | 'rosnedra' | 'user') | undefined, kinds?: string | undefined, rate?: ('1' | '2' | '3' | '1h' | '2h' | '3h') | undefined, format?: ('json' | 'geojson' | 'count') | undefined, props?: ('base' | 'address') | undefined, limit?: number | undefined}; export type GetApiLangPlacesAutosuggestOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const simpleSuggestFeature = z.object({xid: z.string(), name: z.string(), highlighted_name: z.string(), kinds: z.string(), osm: z.string().optional(), wikidata: z.string().optional(), dist: z.number().optional(), point: z.object({lon: z.number().optional(), lat: z.number().optional()})}); export const getApiLangPlacesAutosuggestResponse = z.array(simpleSuggestFeature); 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 }; /** * Method returns suggestions for search term closest to the selected point optionally filtered by parameters. Only basic information is include in response: xid, name, kinds, osm, wikidata of each object. Depending on the chosen format, the response is either a simple array of objects (with a smaller volume) or an object in GeoJSON format. * * GET /{lang}/places/autosuggest */ export const getApiLangPlacesAutosuggest = async (args: GetApiLangPlacesAutosuggestArgs, options?: GetApiLangPlacesAutosuggestOptions) => { const {baseUrl = "https://api.opentripmap.com/0.1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.lon != null) query.append("lon", String(args.lon)) if (args.lat != null) query.append("lat", String(args.lat)) if (args.src_geom != null) query.append("src_geom", String(args.src_geom)) if (args.src_attr != null) query.append("src_attr", String(args.src_attr)) if (args.kinds != null) query.append("kinds", String(args.kinds)) if (args.rate != null) query.append("rate", String(args.rate)) if (args.format != null) query.append("format", String(args.format)) if (args.props != null) query.append("props", String(args.props)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) query.set("apikey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.lang))}/places/autosuggest${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(getApiLangPlacesAutosuggestResponse, await res.json()) };