-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
238 lines (214 loc) · 6.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { stringify } from 'qs'
import type { LonLatInput, LonLatOutput } from '@conveyal/lonlat'
import { normalize, fromCoordinates } from '@conveyal/lonlat'
if (typeof fetch === 'undefined') {
require('isomorphic-fetch')
}
const mapzenUrl = 'https://search.mapzen.com/v1'
const autocompleteUrl = `${mapzenUrl}/autocomplete`
const reverseUrl = `${mapzenUrl}/reverse`
const searchUrl = `${mapzenUrl}/search`
type Rect = {
maxLat: number
maxLon: number
minLat: number
minLon: number
}
type Boundary = {
country: string
rect: Rect
}
type Query = {
// apiKey is renamed depending on if it is being passed around or into fetch
// OK to disable this rule as api_key is a hack to conform with api
// eslint-disable-next-line camelcase
api_key: string
apiKey?: string
boundary?: Boundary
focusPoint?: LonLatInput
format?: boolean
layers?: string
options?: RequestInit // Built-in Typing
point?: GeoJSON.Point | LonLatOutput
size?: number
sources?: string
text?: string
url?: string
}
type PeliasFetchArgs = {
format: boolean
options: RequestInit // Built-in Typing
query: Query
url: string
}
type JSONArrayPromise = Promise<Array<JSON>>
/**
* Search for and address using
* Mapzen's {@link https://mapzen.com/documentation/search/autocomplete/|Autocomplete}
* service.
*
* @param {Object} $0
* @param {string} $0.apiKey The Mapzen API key
* @param {Object} $0.boundary
* @param {Object} $0.focusPoint
* @param {boolean} $0.format
* @param {string} $0.layers a comma-separated list of
* {@link https://mapzen.com/documentation/search/autocomplete/#layers|layer types}
* @param {Object} $0.options options to pass to fetch (e.g., custom headers)
* @param {string} [$0.sources='gn,oa,osm,wof']
* @param {string} $0.text query text
* @param {string} [$0.url='https://search.mapzen.com/v1/autocomplete'] optional URL to override Mapzen autocomplete endpoint
* @return {Promise} A Promise that'll get resolved with the autocomplete result
*/
export function autocomplete({
apiKey,
boundary,
focusPoint,
format,
layers,
options,
sources = 'gn,oa,osm,wof',
text,
url = autocompleteUrl
}: Query): JSONArrayPromise {
// build query
const query: Query = { api_key: apiKey, text }
if (sources && sources.length > 0) query.sources = sources
if (layers) {
query.layers = layers
}
if (focusPoint) {
const { lat, lon }: LonLatOutput = normalize(focusPoint)
query['focus.point.lat'] = lat
query['focus.point.lon'] = lon
}
if (boundary) {
if (boundary.country) query['boundary.country'] = boundary.country
if (boundary.rect) {
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
}
}
return run({
format,
options,
query,
url
})
}
/**
* Search for an address using
* Mapzen's {@link https://mapzen.com/documentation/search/search/|Search}
* service.
*
* @param {Object} $0
* @param {string} $0.apiKey The Mapzen API key
* @param {Object} $0.boundary
* @param {Object} $0.focusPoint
* @param {boolean} $0.format
* @param {Object} $0.options options to pass to fetch (e.g., custom headers)
* @param {number} [$0.size=10]
* @param {string} [$0.sources='gn,oa,osm,wof']
* @param {string} $0.text The address text to query for
* @param {string} [$0.url='https://search.mapzen.com/v1/search'] optional URL to override Mapzen search endpoint
* @return {Promise} A Promise that'll get resolved with search result
*/
export function search({
apiKey,
boundary,
focusPoint,
format,
options,
size = 10,
sources = 'gn,oa,osm,wof',
text,
url = searchUrl
}: Query): JSONArrayPromise {
if (!text) return Promise.resolve([])
const query: Query = {
api_key: apiKey,
size,
text
}
if (sources && sources.length > 0) query.sources = sources
if (focusPoint) {
const { lat, lon }: LonLatOutput = normalize(focusPoint)
query['focus.point.lat'] = lat
query['focus.point.lon'] = lon
}
if (boundary) {
if (boundary.country) query['boundary.country'] = boundary.country
if (boundary.rect) {
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
}
}
return run({ format, options, query, url })
}
/**
* Reverse geocode using
* Mapzen's {@link https://mapzen.com/documentation/search/reverse/|Reverse geocoding}
* service.
*
* @param {Object} $0
* @param {string} $0.apiKey The Mapzen API key
* @param {boolean} $0.format
* @param {Object} $0.options options to pass to fetch (e.g., custom headers)
* @param {{lat: number, lon: number}} $0.point Point to reverse geocode
* @param {string} [$0.url='https://search.mapzen.com/v1/reverse'] optional URL to override Mapzen reverse endpoint
* @return {Promise} A Promise that'll get resolved with reverse geocode result
*/
export function reverse({
apiKey,
format,
options,
point,
url = reverseUrl
}: Query): JSONArrayPromise {
const { lat, lon }: LonLatOutput = normalize(point)
return run({
format,
options,
query: {
api_key: apiKey,
point: {
lat,
lon
}
},
url
})
}
// TODO: turn this into one large async function?
// TODO: replace Array<Object> with more specific output once tests work
function run({
format = false,
options,
query,
url = searchUrl
}: PeliasFetchArgs): JSONArrayPromise {
return fetch(`${url}?${stringify(query, { allowDots: true })}`, options)
.then((res) => res.json())
.then((json) => {
let jsonResponse = json
if (json && json.features && format) {
jsonResponse = json.features.map(split)
}
jsonResponse.isomorphicMapzenSearchQuery = query
return jsonResponse
})
}
function split({ geometry, properties }): Record<string, string | number> {
return Object.assign({}, properties, {
address: `${properties.label}${
properties.postalcode ? ' ' + properties.postalcode : ''
}`,
latlng: fromCoordinates(geometry.coordinates)
})
}
const toExport = { autocomplete, reverse, search }
export default toExport