Skip to content

Commit

Permalink
refactor(response): always include query in response
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Feb 28, 2017
1 parent 43b99d7 commit 47070ae
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 102 deletions.
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`)
Expand All @@ -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
Expand All @@ -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
140 changes: 69 additions & 71 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
`;

Expand Down
22 changes: 4 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -34,7 +33,6 @@ export function autocomplete ({
boundary,
focusPoint,
format,
includeQueryInResponse,
layers,
sources = 'gn,oa,osm,wof',
text
Expand Down Expand Up @@ -76,7 +74,6 @@ export function autocomplete ({

return run({
format,
includeQueryInResponse,
query,
url: autocompleteUrl
})
Expand All @@ -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
Expand All @@ -103,7 +99,6 @@ export function search ({
boundary,
focusPoint,
format,
includeQueryInResponse,
size = 10,
sources = 'gn,oa,osm,wof',
text
Expand Down Expand Up @@ -139,7 +134,7 @@ export function search ({
}
}

return run({format, includeQueryInResponse, query})
return run({format, query})
}

/**
Expand All @@ -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,
Expand All @@ -176,7 +168,6 @@ export function reverse ({
function run ({
format = false,
query,
includeQueryInResponse,
url = searchUrl
}) {
return fetch(`${url}?${qs.stringify(query)}`)
Expand All @@ -187,14 +178,9 @@ function run ({
jsonResponse = json.features.map(split)
}

if (includeQueryInResponse) {
return {
query,
jsonResponse
}
} else {
return jsonResponse
}
jsonResponse.isomorphicMapzenSearchQuery = query

return jsonResponse
})
}

Expand Down

0 comments on commit 47070ae

Please sign in to comment.