import isPlainObject from 'lodash/isPlainObject' import axios from 'axios' import get from 'lodash/get' import { schema } from 'normalizr' import * as types from './types/sdk' interface MakeGeo { googleApiKey: string } const makeGeo = function({ googleApiKey }: { googleApiKey: MakeGeo }) { if (!googleApiKey) { throw new Error('Please provide a google api key') } const baseURL = `https://maps.googleapis.com/maps/api` const geoReq = axios.create({ baseURL, maxRedirects: 5, // default method: 'post', // default params: { key: googleApiKey, }, }) return { getClient() { return geoReq }, async getDirections( start: types.GeoGoogleMapsCoords, end: types.GeoGoogleMapsCoords, ) { try { const url = '/directions/json' const response = await geoReq.get(url, { params: { origin: `${start.lat},${start.lng}`, destination: `${end.lat},${end.lng}`, }, }) return response.data } catch (error) { throw error } }, async fetchGeo() { try { console.log('fetching geo') } catch (error) { throw error } }, schemas: { directions: new schema.Entity('directions'), }, } } export default makeGeo