From 5aa5fe0224c3fbe614a3aafcd529443823a9881d Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Tue, 31 Jan 2017 18:11:48 +0100 Subject: [PATCH 1/2] refactor(lonlat): Remove latlng usage and switch to all options objects. BREAKING CHANGE: all apis are different. --- .travis.yml | 11 ++++----- README.md | 8 +++---- __tests__/index.js | 8 +++---- index.js | 60 +++++++++++++++++++++++++++------------------- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/.travis.yml b/.travis.yml index bdcbd17..5e80173 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,22 +1,19 @@ sudo: false language: node_js cache: - directories: - - node_modules + yarn: true notifications: email: false node_js: - '6' before_install: - npm i -g codecov -before_script: - - npm prune script: - - npm run lint - - npm run cover + - yarn run lint + - yarn run cover - codecov after_success: - - npm run semantic-release + - yarn run semantic-release branches: except: - /^v\d+\.\d+\.\d+$/ diff --git a/README.md b/README.md index 8b7b79a..996807c 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,14 @@ import {search} from 'isomorphic-mapzen-search' search(MAPZEN_API_KEY, '1301 U Street NW, Washington, DC', { boundary: { country: 'US', - maxLatlng: maxLatlng, - minLatlng: minLatlng + maxLonLat: maxLonLat, + minLonLat: minLonLat }, circle: { - latlng: centerLatlng, + center: centerLonLat, radius: 35 // kilometers }, - focusLatlng: {lat: 39.7691, lng: -86.1570}, + focusLonLat: {lat: 39.7691, lon: -86.1570}, format: false // keep as returned GeoJSON }).then(geojson => { console.log(geojson) diff --git a/__tests__/index.js b/__tests__/index.js index 4ba40f4..94ea702 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -15,7 +15,7 @@ describe('search', () => { .get(/v1\/search/) .reply(200, mockSearchResult) - const result = await geocoder.search(mockKey, searchQuery) + const result = await geocoder.search({apiKey: mockKey, text: searchQuery}) expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104) }) @@ -24,7 +24,7 @@ describe('search', () => { .get(/v1\/search/) .reply(200, mockSearchResult) - const result = await geocoder.search(mockKey, searchQuery, { format: true }) + const result = await geocoder.search({apiKey: mockKey, format: true, text: searchQuery}) expect(result).toMatchSnapshot() expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA') }) @@ -38,7 +38,7 @@ describe('reverse', () => { .get(/v1\/reverse/) .reply(200, mockReverseResult) - const result = await geocoder.reverse(mockKey, reverseQuery) + const result = await geocoder.reverse({apiKey: mockKey, point: reverseQuery}) expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104) }) @@ -47,7 +47,7 @@ describe('reverse', () => { .get(/v1\/reverse/) .reply(200, mockReverseResult) - const result = await geocoder.reverse(mockKey, reverseQuery, { format: true }) + const result = await geocoder.reverse({apiKey: mockKey, format: true, point: reverseQuery}) expect(result).toMatchSnapshot() expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA') }) diff --git a/index.js b/index.js index 37003fe..e00a131 100644 --- a/index.js +++ b/index.js @@ -6,13 +6,15 @@ const mapzenUrl = 'https://search.mapzen.com/v1' const searchUrl = `${mapzenUrl}/search` const reverseUrl = `${mapzenUrl}/reverse` -export function search (apiKey, text, { +export function search ({ + apiKey, boundary, - focusLatlng, + focusPoint, format, size = 10, - sources = 'gn,oa,osm,wof' -} = {}) { + sources = 'gn,oa,osm,wof', + text +}) { if (!text) return Promise.resolve([]) const query = { @@ -22,8 +24,8 @@ export function search (apiKey, text, { text } - if (focusLatlng) { - const {lat, lon} = lonlat(focusLatlng) + if (focusPoint) { + const {lat, lon} = lonlat(focusPoint) query['focus.point.lat'] = lat query['focus.point.lon'] = lon } @@ -31,37 +33,47 @@ export function search (apiKey, text, { if (boundary) { if (boundary.country) query['boundary.country'] = boundary.country if (boundary.rect) { - const min = lonlat(boundary.rect.minLatlng) - const max = lonlat(boundary.rect.maxLatlng) - query['boundary.rect.min_lat'] = min.lat - query['boundary.rect.min_lon'] = min.lon - query['boundary.rect.max_lat'] = max.lat - query['boundary.rect.max_lon'] = max.lon + query['boundary.rect.min_lat'] = boundary.rect.minLat + query['boundary.rect.min_lon'] = boundary.rect.minLon + query['boundary.rect.max_lat'] = boundary.rect.maxLat + query['boundary.rect.max_lon'] = boundary.rect.maxLon } if (boundary.circle) { - const {lat, lon} = lonlat(boundary.circle.latlng) + const {lat, lon} = lonlat(boundary.circle.centerPoint) query['boundary.circle.lat'] = lat query['boundary.circle.lon'] = lon query['boundary.circle.radius'] = boundary.circle.radius } } - return run(searchUrl, query, format) + return run({format, query}) } -export function reverse (apiKey, latlon, {format} = {}) { - const {lon, lat} = lonlat(latlon) - return run(reverseUrl, { - api_key: apiKey, - 'point.lat': lat, - 'point.lon': lon - }, format) +export function reverse ({ + apiKey, + format, + point +}) { + const {lon, lat} = lonlat(point) + return run({ + format, + query: { + api_key: apiKey, + 'point.lat': lat, + 'point.lon': lon + }, + url: reverseUrl + }) } -function run (url, query, format) { +function run ({ + format = false, + query, + url = searchUrl +}) { return fetch(`${url}?${qs.stringify(query)}`) - .then(res => res.json()) - .then(json => { return json && json.features && format ? json.features.map(split) : json }) + .then((res) => res.json()) + .then((json) => { return json && json.features && format ? json.features.map(split) : json }) } function split ({ From 043b455fa64b07d15601a4e39c40f22914014fa1 Mon Sep 17 00:00:00 2001 From: Trevor Gerhardt Date: Tue, 31 Jan 2017 18:16:58 +0100 Subject: [PATCH 2/2] docs(readme): Update readme to reflect new api --- README.md | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 996807c..7a51600 100644 --- a/README.md +++ b/README.md @@ -3,41 +3,49 @@ Isomorphic Mapzen search for reuse across our JavaScript libraries. Get an API k ## API -### `search(apiKey, searchText, options = {})` +### `search({apiKey, text, ...options})` ```js import {search} from 'isomorphic-mapzen-search' -search(MAPZEN_API_KEY, '1301 U Street NW, Washington, DC', { +search({ + apiKey: MAPZEN_API_KEY, + text: '1301 U Street NW, Washington, DC', + focusPoint: {lat: 39.7691, lon: -86.1570}, boundary: { country: 'US', - maxLonLat: maxLonLat, - minLonLat: minLonLat + rect: { + minLon: minLon, + minLat: minLat, + maxLon: maxLon, + maxLat: maxLat + }, + circle: { + centerPoint: centerLonLat, + radius: 35 // kilometers + } }, - circle: { - center: centerLonLat, - radius: 35 // kilometers - }, - focusLonLat: {lat: 39.7691, lon: -86.1570}, format: false // keep as returned GeoJSON -}).then(geojson => { +}).then((geojson) => { console.log(geojson) -}).catch(err => { +}).catch((err) => { console.error(err) }) ``` -### `reverse(apiKey, latlng, options = {})` +### `reverse({apiKey, point, format})` ```js import {reverse} from 'isomorphic-mapzen-search' -reverse(MAPZEN_API_KEY, { - lat: 39.7691, - lng: -86.1570 -}, { +reverse({ + apiKey: MAPZEN_API_KEY, + point: { + lat: 39.7691, + lng: -86.1570 + }, format: true -}).then(json => { +}).then((json) => { console.log(json[0].address) -}).catch(err => { +}).catch((err) => { console.error(err) }) ```