Kenny Jue

home  about
      

MapWrap

April 23, 2019

Project Cover

npm version npm bundle size

About This Project

MapWrap is a simple wrapper around several Google Maps API services such as Directions, Places, and Geocoding. It provides simple methods to access data members in the API response with error checking.

MapWrap also implements a simple LRU cache to speed up requests and decrease the amount of unnecessary calls to the server. It is fully compliant with Google’s restriction of not caching data for over 30 days. It also includes typings written in Typescript to provide intellisense and autocomplete support in popular editors such as VS Code.

MapWrap methods essentially serve as factory methods and return smaller wrappers around the data retrieved from Google Map API calls. These wrappers provide methods that allow you to easily access properties within the JSON without having to access deeply nested keys and indices. This eliminates tedious error checking in the application.

Here’s an example:

const data = response.data[routeNum].legs[0].start_address;

Without MapWrap, if you want to access the address from the Google Directions API, you would be forced to do this after making a Get request. Not only is it verbose, it is also easy to get wrong! Additionally, in some API responses there may be missing properties. MapWrap handles this error checking.

With MapWrap:

const data = wrapperInstance.getStartAddress();

MapWrap also has other features, such as Joi to validate object schemas. This reduces the logic needed by the user to make sure that the request form is valid, such as having to check for missing required parameters (e.g. trying to make a Directions API request without an origin or destination). All the methods have been unit tested with Mocha and Chai.

I created MapWrap to abstract logic away from my server code for The Wayfarer. The first release was on April 23, 2019. The package is publicly available on npmjs and has been built for production with Babel.

Links

Npmjs link

MapWrap

Installation

npm install --save mapwrap

Usage

See documentation.