Skip to content

Commit

Permalink
feat(querying): add option to include query in response
Browse files Browse the repository at this point in the history
also refactor autocomplete
  • Loading branch information
evansiroky committed Feb 28, 2017
1 parent 1855cc4 commit 43b99d7
Show file tree
Hide file tree
Showing 6 changed files with 764 additions and 677 deletions.
62 changes: 23 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ Isomorphic Mapzen search for reuse across our JavaScript libraries. Get an API k
### `Autocomplete`

```js
import {Autocomplete} from 'isomorphic-mapzen-search'
import {autocomplete} from 'isomorphic-mapzen-search'

const autocomplete = new Autocomplete({
autocomplete({
apiKey: MAPZEN_API_KEY,
debounceTimeMs: 444,
minTextLength: 4,
resultsHandler: (result) => {
console.log(result)
}
})

autocomplete.query({
boundary: {
country: 'US',
rect: {
Expand All @@ -33,7 +25,11 @@ autocomplete.query({
},
focusPoint: {lat: 39.7691, lon: -86.1570},
layers: 'venue,coarse',
text: 'anywhere'
text: '1301 U Str'
}).then((result) => {
console.log(result)
}).catch((err) => {
console.error(err)
})
```

Expand Down Expand Up @@ -90,40 +86,26 @@ reverse({

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Autocomplete
### autocomplete

Class for making autocomplete requests.
Queries will be sent to
Search for and address using
Mapzen's [Autocomplete](https://mapzen.com/documentation/search/autocomplete/)
service.
All queries will be debounced to the specified debounce time parameter.

#### constructor

Create a new Autocomplete instance.

**Parameters**

- `$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.debounceTimeMs` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?= sep** default = 333
- `$0.formatResults` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?= https** default = false
- `$0.minTextLength` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?= a** default = 3
- `$0.resultsHandler` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** handler function that will receive geocode results

#### query

Send a new query to autocomplete.
Results will be sent to the resultsHandler defined upon initiation.

**Parameters**
- `$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.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'`)
- `$0.text` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** query text

- `config` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `config.boundary` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `config.focusPoint` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `config.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)
- `config.sources` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** default = 'gn,oa,osm,wof',
- `config.text` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** The address text to query for
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** A Promise that'll get resolved with the autocomplete result

### search

Expand All @@ -138,8 +120,9 @@ service.
- `$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)?= 10** default = 10
- `$0.sources` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?= 'gn,oa,osm,wof'** default ='gn,oa,osm,wof'
- `$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.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

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** A Promise that'll get resolved with search result
Expand All @@ -155,6 +138,7 @@ service.
- `$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.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
147 changes: 78 additions & 69 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -1,78 +1,87 @@
exports[`autocomplete should successfully autocomplete 1`] = `
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`autocomplete should successfully autocomplete and include query in response 1`] = `
Object {
"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,
"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,
],
"type": "Point",
"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",
},
"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",
],
"geocoding": Object {
"attribution": "https://search.mapzen.com/v1/attribution",
"engine": Object {
"author": "Mapzen",
"name": "Pelias",
"version": "1.0",
},
"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",
"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",
},
"timestamp": 1479272483487,
"version": "0.2",
"type": "FeatureCollection",
},
"query": Object {
"api_key": "test-key",
"sources": "gn,oa,osm,wof",
"text": "123 a",
},
"type": "FeatureCollection",
}
`;

Expand Down
31 changes: 10 additions & 21 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,22 @@ const mockSearchResult = require('./mock-search-result.json')
const mockKey = 'test-key'

describe('autocomplete', () => {
it('should successfully autocomplete', (done) => {
it('should successfully autocomplete', async () => {
nock('https://search.mapzen.com/')
.get(/v1\/autocomplete/)
.reply(200, mockSearchResult)

let numResultsHandled = 0

const resultsHandler = (result) => {
numResultsHandled++
expect(numResultsHandled).toEqual(1)
expect(result).toMatchSnapshot()
expect(result.features[0].properties.label).toEqual('Takoma, Takoma Park, MD, USA')
done()
}

const autocomplete = new geocoder.Autocomplete({
apiKey: mockKey,
resultsHandler
})
const result = await geocoder.autocomplete({apiKey: mockKey, text: '123 a'})
expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104)
})

autocomplete.query({
text: 'anywhere'
})
it('should successfully autocomplete and include query in response', async () => {
nock('https://search.mapzen.com/')
.get(/v1\/autocomplete/)
.reply(200, mockSearchResult)

autocomplete.query({
text: '123 abc st'
})
const result = await geocoder.autocomplete({apiKey: mockKey, includeQueryInResponse: true, text: '123 a'})
expect(result).toMatchSnapshot()
})
})

Expand Down
Loading

0 comments on commit 43b99d7

Please sign in to comment.