import config from './../config' import axios from 'axios' import { LatLng } from './common' export interface SnapToRoadsRequestOptions { path: LatLng[] timestamps?: number[] radiuses?: number[] interpolate?: boolean debug?: boolean } export interface SnapToRoadsResponse { status: string snappedPoints: Array distance: number geometry: string[] } interface SnapToRoadsResponseRow { location: { latitude: number longitude: number } originalIndex: number bearing: number distance: number name: string } export const requestSnapToRoads = async (opt: SnapToRoadsRequestOptions): Promise => { if (!config.API_KEY) { throw new Error('In order to call the NextBillion.ai API, you must provide a valid API Key.') } if (!config.API_HOST) { throw new Error('An API host is required to make request to NextBillion.ai API.') } if (!Array.isArray(opt.path) || opt.path.length === 0) { throw new Error('Invalid path') } const params: any = { key: config.API_KEY, path: opt.path.map((point) => `${point.lat},${point.lng}`).join('|'), interpolate: opt.interpolate || 'false', debug: opt.debug || 'false', } if (Array.isArray(opt.timestamps) && opt.timestamps.length > 0) { params.timestamps = opt.timestamps.join('|') } if (Array.isArray(opt.radiuses) && opt.radiuses.length > 0) { params.radiuses = opt.radiuses.join('|') } return await axios .get(`https://${config.API_HOST}/snapToRoads/json`, { params, }) .then((resp) => resp.data as SnapToRoadsResponse) }