forked from derhuerst/query-overpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (57 loc) · 1.56 KB
/
index.js
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
'use strict'
const retry = require('p-retry')
const Promise = require('pinkie-promise')
const {fetch} = require('fetch-ponyfill')({Promise})
const {stringify} = require('query-string')
const defaults = {
fetchMode: null,
endpoint: 'https://overpass-api.de/api/interpreter',
retryOpts: {minTimeout: 500, retries: 3}
}
const queryOverpass = (query, opt = {}) => {
opt = Object.assign({}, defaults, opt)
if ('string' !== typeof opt.endpoint) {
throw new Error('opt.endpoint must be a string')
}
const url = opt.endpoint + '?' + stringify({data: query})
const fetchOpts = {
redirect: 'follow',
headers: {
accept: 'application/json',
'user-agent': 'http://github.com/derhuerst/vbb-osm-relations'
}
}
if (opt.fetchMode !== null) fetchOpts.mode = opt.fetchMode
const attempt = () => {
return fetch(url, fetchOpts)
.then((res) => {
if (!res.ok) {
const err = new Error(res.statusText)
err.statusCode = res.status
err.reponseBody = null
if (!res.headers.has('content-length') || parseInt(res.headers.get('content-length')) > 1024) {
throw err
}
return res.text()
.then((body) => {
err.reponseBody = body
throw err
}, () => {
throw err
})
}
return res.json()
})
.then((data) => {
if (!data || !Array.isArray(data.elements)) {
const err = new Error('invalid response')
err.responseBody = data
throw err
}
return data.elements
})
}
const retryOpts = Object.assign({}, defaults.retryOpts, opt.retryOpts || {})
return retry(attempt, retryOpts)
}
module.exports = queryOverpass