Skip to content

Commit 550f8bd

Browse files
feat: add custom endpoint parsing (#476)
this is a real basic implementation that makes custom endpoints work for local ors instances with isochrones and directions. Prob. not working for any other endpoint.
2 parents ee6a58e + 0982ee8 commit 550f8bd

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

src/support/ors-api-runner.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ import OrsApiClient from 'openrouteservice-js'
1616
// and uncomment the import line below to use a local and unpacked openrouteservice-js lib
1717
// import OrsApiClient from '@/ors-js/src'
1818

19+
function getCustomEndpointSettings(apiBaseUrl, endpoint) {
20+
let host
21+
let service
22+
if (!endpoint.startsWith('http')){
23+
return [apiBaseUrl, endpoint]
24+
} else {
25+
let parts = endpoint.split('/')
26+
if ( parts.includes('ors') ) {
27+
host = parts.slice(0,4).join('/')
28+
} else {
29+
host = parts.slice(0,3).join('/')
30+
}
31+
service = '/' + parts.slice(-1)
32+
return [host, service]
33+
}
34+
}
35+
1936
/**
2037
* Get the Directions function accessor
2138
* @param {Array} places
@@ -25,11 +42,12 @@ import OrsApiClient from 'openrouteservice-js'
2542
const Directions = (places, customArgs = null) => {
2643
const mapSettings = store.getters.mapSettings
2744

45+
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.directions)
2846
// Build the ors client object
2947
const directions = new OrsApiClient.Directions({
3048
api_key: mapSettings.apiKey,
31-
host: mapSettings.apiBaseUrl,
32-
service: mapSettings.endpoints.directions,
49+
host: host,
50+
service: service,
3351
timeout: constants.orsApiRequestTimeout
3452
})
3553

@@ -63,10 +81,11 @@ const Directions = (places, customArgs = null) => {
6381
*/
6482
const Geocode = (term, size = 10) => {
6583
const mapSettings = store.getters.mapSettings
84+
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.geocodeSearch)
6685
const client = new OrsApiClient.Geocode({
6786
api_key: mapSettings.apiKey,
68-
host: mapSettings.apiBaseUrl,
69-
service: mapSettings.endpoints.geocodeSearch
87+
host: host,
88+
service: service
7089
})
7190
const args = OrsParamsParser.buildPlaceSearchArgs(term)
7291
args.size = size
@@ -91,11 +110,12 @@ const Geocode = (term, size = 10) => {
91110
*/
92111
const ReverseGeocode = (lat, lng, size = 10) => {
93112
const mapSettings = store.getters.mapSettings
113+
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.reverseGeocode)
94114

95115
const client = new OrsApiClient.Geocode({
96116
api_key: mapSettings.apiKey,
97-
host: mapSettings.apiBaseUrl,
98-
service: mapSettings.endpoints.reverseGeocode
117+
host: host,
118+
service: service
99119
})
100120
const args = OrsParamsParser.buildReverseSearchArgs(lat, lng)
101121
args.size = size
@@ -124,11 +144,12 @@ const ReverseGeocode = (lat, lng, size = 10) => {
124144
const PlacesSearch = (term, quantity = 100, restrictArea = true) => {
125145
return new Promise((resolve, reject) => {
126146
const mapSettings = store.getters.mapSettings
147+
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.geocodeSearch)
127148

128149
const client = new OrsApiClient.Geocode({
129150
api_key: mapSettings.apiKey,
130-
host: mapSettings.apiBaseUrl,
131-
service: mapSettings.endpoints.geocodeSearch
151+
host: host,
152+
service: service
132153
})
133154

134155
let promises = []
@@ -293,11 +314,12 @@ const sortFeatures = (features) => {
293314
*/
294315
const Pois = (filters, limit = 100, distanceBuffer = 500) => {
295316
const mapSettings = store.getters.mapSettings
317+
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.pois)
296318

297319
const pois = new OrsApiClient.Pois({
298320
api_key: mapSettings.apiKey,
299-
host: mapSettings.apiBaseUrl,
300-
service: mapSettings.endpoints.pois
321+
host: host,
322+
service: service
301323
})
302324

303325
return new Promise((resolve, reject) => {
@@ -319,11 +341,12 @@ const Pois = (filters, limit = 100, distanceBuffer = 500) => {
319341
*/
320342
const Isochrones = (places) => {
321343
const mapSettings = store.getters.mapSettings
344+
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.isochrones)
322345

323346
const isochrones = new OrsApiClient.Isochrones({
324347
api_key: mapSettings.apiKey,
325-
host: mapSettings.apiBaseUrl,
326-
service: mapSettings.endpoints.isochrones,
348+
host: host,
349+
service: service,
327350
timeout: constants.orsApiRequestTimeout
328351
})
329352
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)