From 47070ae8b18eb244b3cd129d56c13af8fd506182 Mon Sep 17 00:00:00 2001 From: Evan Siroky Date: Tue, 28 Feb 2017 10:32:24 -0800 Subject: [PATCH] refactor(response): always include query in response --- README.md | 23 ++--- __tests__/__snapshots__/index.js.snap | 140 +++++++++++++------------- index.js | 22 +--- 3 files changed, 83 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index e7c4a2d..51caa48 100644 --- a/README.md +++ b/README.md @@ -94,12 +94,11 @@ service. **Parameters** -- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** +- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - `$0.apiKey` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The Mapzen API key - - `$0.boundary` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - - `$0.focusPoint` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - - `$0.format` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** - - `$0.includeQueryInResponse` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** If true, return query object in output of resolved Promise + - `$0.boundary` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** + - `$0.focusPoint` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** + - `$0.format` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** - `$0.layers` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** a comma-separated list of [layer types](https://mapzen.com/documentation/search/autocomplete/#layers) - `$0.sources` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** (optional, default `'gn,oa,osm,wof'`) @@ -115,12 +114,11 @@ service. **Parameters** -- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** +- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - `$0.apiKey` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The Mapzen API key - - `$0.boundary` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - - `$0.focusPoint` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - - `$0.format` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** - - `$0.includeQueryInResponse` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** If true, return query object in output of resolved Promise + - `$0.boundary` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** + - `$0.focusPoint` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** + - `$0.format` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** - `$0.size` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** (optional, default `10`) - `$0.sources` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** (optional, default `'gn,oa,osm,wof'`) - `$0.text` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The address text to query for @@ -135,10 +133,9 @@ service. **Parameters** -- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** +- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** - `$0.apiKey` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The Mapzen API key - - `$0.format` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** - - `$0.includeQueryInResponse` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** If true, return query object in output of resolved Promise + - `$0.format` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** - `$0.point` **{lat: [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), lon: [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)}** Point to reverse geocode Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** A Promise that'll get resolved with reverse geocode result diff --git a/__tests__/__snapshots__/index.js.snap b/__tests__/__snapshots__/index.js.snap index 83e7977..36d8010 100644 --- a/__tests__/__snapshots__/index.js.snap +++ b/__tests__/__snapshots__/index.js.snap @@ -2,86 +2,84 @@ exports[`autocomplete should successfully autocomplete and include query in response 1`] = ` Object { - "jsonResponse": Object { - "bbox": Array [ - -77.017897, - 38.904, - -76.978642, - 39.001084, - ], - "features": Array [ - Object { - "bbox": Array [ - -77.017242, - 38.973896, - -77.012062, - 38.980594, + "bbox": Array [ + -77.017897, + 38.904, + -76.978642, + 39.001084, + ], + "features": Array [ + Object { + "bbox": Array [ + -77.017242, + 38.973896, + -77.012062, + 38.980594, + ], + "geometry": Object { + "coordinates": Array [ + -77.023104, + 38.976745, ], - "geometry": Object { - "coordinates": Array [ - -77.023104, - 38.976745, - ], - "type": "Point", - }, - "properties": Object { - "accuracy": "centroid", - "confidence": 0.965, - "country": "United States", - "country_a": "USA", - "country_gid": "whosonfirst:country:85633793", - "county": "Montgomery County", - "county_gid": "whosonfirst:county:102082719", - "gid": "whosonfirst:neighbourhood:85851759", - "id": "85851759", - "label": "Takoma, Takoma Park, MD, USA", - "layer": "neighbourhood", - "locality": "Takoma Park", - "locality_gid": "whosonfirst:locality:85949501", - "name": "Takoma", - "neighbourhood": "Takoma", - "neighbourhood_gid": "whosonfirst:neighbourhood:85851759", - "region": "Maryland", - "region_a": "MD", - "region_gid": "whosonfirst:region:85688501", - "source": "whosonfirst", - "source_id": "85851759", - }, - "type": "Feature", + "type": "Point", }, - ], - "geocoding": Object { - "attribution": "https://search.mapzen.com/v1/attribution", - "engine": Object { - "author": "Mapzen", - "name": "Pelias", - "version": "1.0", + "properties": Object { + "accuracy": "centroid", + "confidence": 0.965, + "country": "United States", + "country_a": "USA", + "country_gid": "whosonfirst:country:85633793", + "county": "Montgomery County", + "county_gid": "whosonfirst:county:102082719", + "gid": "whosonfirst:neighbourhood:85851759", + "id": "85851759", + "label": "Takoma, Takoma Park, MD, USA", + "layer": "neighbourhood", + "locality": "Takoma Park", + "locality_gid": "whosonfirst:locality:85949501", + "name": "Takoma", + "neighbourhood": "Takoma", + "neighbourhood_gid": "whosonfirst:neighbourhood:85851759", + "region": "Maryland", + "region_a": "MD", + "region_gid": "whosonfirst:region:85688501", + "source": "whosonfirst", + "source_id": "85851759", }, - "query": Object { - "boundary.circle.lat": 38.8886, - "boundary.circle.lon": -77.043, - "boundary.circle.radius": 25, - "private": false, - "querySize": 20, - "size": 10, - "sources": Array [ - "geonames", - "openaddresses", - "openstreetmap", - "whosonfirst", - ], - "text": "takoma", - }, - "timestamp": 1479272483487, - "version": "0.2", + "type": "Feature", + }, + ], + "geocoding": Object { + "attribution": "https://search.mapzen.com/v1/attribution", + "engine": Object { + "author": "Mapzen", + "name": "Pelias", + "version": "1.0", + }, + "query": Object { + "boundary.circle.lat": 38.8886, + "boundary.circle.lon": -77.043, + "boundary.circle.radius": 25, + "private": false, + "querySize": 20, + "size": 10, + "sources": Array [ + "geonames", + "openaddresses", + "openstreetmap", + "whosonfirst", + ], + "text": "takoma", }, - "type": "FeatureCollection", + "timestamp": 1479272483487, + "version": "0.2", }, - "query": Object { + "isomorphicMapzenSearchQuery": Object { "api_key": "test-key", "sources": "gn,oa,osm,wof", "text": "123 a", }, + "type": "FeatureCollection", } `; diff --git a/index.js b/index.js index 17a765f..614165f 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,6 @@ const searchUrl = `${mapzenUrl}/search` * @param {Object} $0.boundary * @param {Object} $0.focusPoint * @param {boolean} $0.format - * @param {boolean} $0.includeQueryInResponse If true, return query object in output of resolved Promise * @param {string} $0.layers a comma-separated list of * {@link https://mapzen.com/documentation/search/autocomplete/#layers|layer types} * @param {string} [$0.sources='gn,oa,osm,wof'] @@ -34,7 +33,6 @@ export function autocomplete ({ boundary, focusPoint, format, - includeQueryInResponse, layers, sources = 'gn,oa,osm,wof', text @@ -76,7 +74,6 @@ export function autocomplete ({ return run({ format, - includeQueryInResponse, query, url: autocompleteUrl }) @@ -92,7 +89,6 @@ export function autocomplete ({ * @param {Object} $0.boundary * @param {Object} $0.focusPoint * @param {boolean} $0.format - * @param {boolean} $0.includeQueryInResponse If true, return query object in output of resolved Promise * @param {number} [$0.size=10] * @param {string} [$0.sources='gn,oa,osm,wof'] * @param {string} $0.text The address text to query for @@ -103,7 +99,6 @@ export function search ({ boundary, focusPoint, format, - includeQueryInResponse, size = 10, sources = 'gn,oa,osm,wof', text @@ -139,7 +134,7 @@ export function search ({ } } - return run({format, includeQueryInResponse, query}) + return run({format, query}) } /** @@ -150,20 +145,17 @@ export function search ({ * @param {Object} $0 * @param {string} $0.apiKey The Mapzen API key * @param {boolean} $0.format - * @param {boolean} $0.includeQueryInResponse If true, return query object in output of resolved Promise * @param {{lat: number, lon: number}} $0.point Point to reverse geocode * @return {Promise} A Promise that'll get resolved with reverse geocode result */ export function reverse ({ apiKey, format, - includeQueryInResponse, point }) { const {lon, lat} = lonlat(point) return run({ format, - includeQueryInResponse, query: { api_key: apiKey, 'point.lat': lat, @@ -176,7 +168,6 @@ export function reverse ({ function run ({ format = false, query, - includeQueryInResponse, url = searchUrl }) { return fetch(`${url}?${qs.stringify(query)}`) @@ -187,14 +178,9 @@ function run ({ jsonResponse = json.features.map(split) } - if (includeQueryInResponse) { - return { - query, - jsonResponse - } - } else { - return jsonResponse - } + jsonResponse.isomorphicMapzenSearchQuery = query + + return jsonResponse }) }