import config from './../config' import axios from 'axios' import { LatLng } from './common' export interface GetNearbyRequestOptions { currentLocation: LatLng serviceType: string maxCount?: number searchRadius?: number } export interface GetNearbyResponse { status: string currentLocation: { latitude: number longitude: number } maxCount: number msg: string searchRadius: number serviceType: string results: Array } interface GetNearbyResponseResult { distance: number eta: number id: string location: { latitude: number longitude: number } } export const requestGetNearby = async (opt: GetNearbyRequestOptions): 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 (!opt.currentLocation.lat && opt.currentLocation.lat !== 0) { throw new Error('Invalid currentLocation lat') } if (!opt.currentLocation.lng && opt.currentLocation.lng !== 0) { throw new Error('Invalid currentLocation lng') } const params: any = { key: config.API_KEY, currentlocation: `${opt.currentLocation.lat},${opt.currentLocation.lng}`, servicetype: opt.serviceType, } if (opt.maxCount !== undefined && !isNaN(opt.maxCount)) { params['maxcount'] = opt.maxCount } if (opt.searchRadius !== undefined && !isNaN(opt.searchRadius)) { params['searchradius'] = opt.searchRadius } return await axios .get(`https://${config.API_HOST}/getnearby`, { params, }) .then((resp) => resp.data as GetNearbyResponse) }