UNPKG

google-places-web

Version:

A simple wrapper for the Google Places Web API

156 lines 6.31 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GooglePlaces = exports.GOOGLE_MAPS_API_TARGET = void 0; const superagent = require("superagent"); const constants_1 = require("./constants"); const search_1 = require("../search"); exports.GOOGLE_MAPS_API_TARGET = 'https://maps.googleapis.com/maps/api/place'; class GooglePlaces { constructor(opts = { debug: false }) { this._debug = false; this._permitParams = ({ requiredKeys, optionalKeys }, params) => { if (!requiredKeys || !requiredKeys.length) { throw new Error('No required params defined'); } else if (!params || Object.keys(params).length === 0) { throw new Error('No parameters provided'); } const missingKeys = []; const filteredRequiredParams = requiredKeys.reduce((p, key) => { const param = params[key]; if (param) { p[key] = param; } else { missingKeys.push(key); } return p; }, {}); if (missingKeys.length > 0) { throw new Error(`Missing required params: [${missingKeys.join(', ')}]`); } const filteredOptionalParams = optionalKeys.reduce((p, key) => { const param = params[key]; if (param) { p[key] = param; } return p; }, {}); this._log('GPW:PARAMS', JSON.stringify(params)); return Object.assign(Object.assign({}, filteredOptionalParams), filteredRequiredParams); }; const { apiKey, debug } = opts; this.apiKey = apiKey; this.debug = debug; this._query = this._query.bind(this); } query(type, opts) { return __awaiter(this, void 0, void 0, function* () { if (!opts || Object.keys(opts).length === 0) { throw new Error('no parameters'); } const search = new search_1.BaseSearch(opts); return yield this._query(type, search.toRequestJSON()); }); } autocomplete(opts) { return __awaiter(this, void 0, void 0, function* () { const config = constants_1.API.AUTOCOMPLETE(opts); const params = this._permitParams(config, opts); const res = yield this._query(config.path, params); return res.body; }); } queryautocomplete(opts) { return __awaiter(this, void 0, void 0, function* () { const config = constants_1.API.AUTOCOMPLETE(opts); const params = this._permitParams(config, opts); const res = yield this._query(config.path, params); return res.body; }); } details(opts) { return __awaiter(this, void 0, void 0, function* () { const config = constants_1.API.DETAILS(opts); const params = this._permitParams(config, opts); const res = yield this._query(config.path, params); return res.body; }); } nearbysearch(opts) { return __awaiter(this, void 0, void 0, function* () { if (opts && opts.rankby && opts.rankby.toUpperCase() === 'DISTANCE') { opts.radius = undefined; } const config = constants_1.API.NEARBY_SEARCH(opts); const params = this._permitParams(config, opts); const res = yield this._query(config.path, params); return res.body; }); } textsearch(opts) { return __awaiter(this, void 0, void 0, function* () { const config = constants_1.API.TEXT_SEARCH(opts); const params = this._permitParams(config, opts); const res = yield this._query(config.path, params); return res.body; }); } set apiKey(apiKey) { if (apiKey && (typeof apiKey !== 'string' || !apiKey.match(/^[^\s]+$/gi))) { throw new Error('Invalid API Key'); } this._apiKey = apiKey; } get apiKey() { return this._apiKey; } set debug(isDebug) { this._debug = isDebug; } _googleApiRequest(url, params) { return __awaiter(this, void 0, void 0, function* () { const target = `${exports.GOOGLE_MAPS_API_TARGET}${url}`; this._log(`GPW:REQ ${target}`, JSON.stringify(Object.assign({}, params))); return yield superagent.get(target).query(Object.assign({ key: this.apiKey }, params)); }); } _log(title, message) { if (this._debug) { console.log(title, message); } } _query(path, params) { return __awaiter(this, void 0, void 0, function* () { if (!this._apiKey) { throw new Error('Invalid API key'); } else if (!path) { throw new Error('Google API path is required'); } else if (!params) { throw new Error('Missing params'); } params.key = this.apiKey; const response = yield this._googleApiRequest(`/${path}/json`, params); const body = response.body; this._log('GPW:RES', body); if (body.status !== 'OK') { throw new Error(body.status); } return response; }); } } exports.GooglePlaces = GooglePlaces; exports.default = new GooglePlaces(); //# sourceMappingURL=Places.js.map