Skip to content

feat: add custom endpoint parsing #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/support/ors-api-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ import OrsApiClient from 'openrouteservice-js'
// and uncomment the import line below to use a local and unpacked openrouteservice-js lib
// import OrsApiClient from '@/ors-js/src'

function getCustomEndpointSettings(apiBaseUrl, endpoint) {
let host
let service
if (!endpoint.startsWith('http')){
return [apiBaseUrl, endpoint]
} else {
let parts = endpoint.split('/')
if ( parts.includes('ors') ) {
host = parts.slice(0,4).join('/')
} else {
host = parts.slice(0,3).join('/')
}
service = '/' + parts.slice(-1)
return [host, service]
}
}

/**
* Get the Directions function accessor
* @param {Array} places
Expand All @@ -25,11 +42,12 @@ import OrsApiClient from 'openrouteservice-js'
const Directions = (places, customArgs = null) => {
const mapSettings = store.getters.mapSettings

const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.directions)
// Build the ors client object
const directions = new OrsApiClient.Directions({
api_key: mapSettings.apiKey,
host: mapSettings.apiBaseUrl,
service: mapSettings.endpoints.directions,
host: host,
service: service,
timeout: constants.orsApiRequestTimeout
})

Expand Down Expand Up @@ -63,10 +81,11 @@ const Directions = (places, customArgs = null) => {
*/
const Geocode = (term, size = 10) => {
const mapSettings = store.getters.mapSettings
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.geocodeSearch)
const client = new OrsApiClient.Geocode({
api_key: mapSettings.apiKey,
host: mapSettings.apiBaseUrl,
service: mapSettings.endpoints.geocodeSearch
host: host,
service: service
})
const args = OrsParamsParser.buildPlaceSearchArgs(term)
args.size = size
Expand All @@ -91,11 +110,12 @@ const Geocode = (term, size = 10) => {
*/
const ReverseGeocode = (lat, lng, size = 10) => {
const mapSettings = store.getters.mapSettings
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.reverseGeocode)

const client = new OrsApiClient.Geocode({
api_key: mapSettings.apiKey,
host: mapSettings.apiBaseUrl,
service: mapSettings.endpoints.reverseGeocode
host: host,
service: service
})
const args = OrsParamsParser.buildReverseSearchArgs(lat, lng)
args.size = size
Expand Down Expand Up @@ -124,11 +144,12 @@ const ReverseGeocode = (lat, lng, size = 10) => {
const PlacesSearch = (term, quantity = 100, restrictArea = true) => {
return new Promise((resolve, reject) => {
const mapSettings = store.getters.mapSettings
const [host, service] = getCustomEndpointSettings(mapSettings.apiBaseUrl, mapSettings.endpoints.geocodeSearch)

const client = new OrsApiClient.Geocode({
api_key: mapSettings.apiKey,
host: mapSettings.apiBaseUrl,
service: mapSettings.endpoints.geocodeSearch
host: host,
service: service
})

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

const pois = new OrsApiClient.Pois({
api_key: mapSettings.apiKey,
host: mapSettings.apiBaseUrl,
service: mapSettings.endpoints.pois
host: host,
service: service
})

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

const isochrones = new OrsApiClient.Isochrones({
api_key: mapSettings.apiKey,
host: mapSettings.apiBaseUrl,
service: mapSettings.endpoints.isochrones,
host: host,
service: service,
timeout: constants.orsApiRequestTimeout
})
return new Promise((resolve, reject) => {
Expand Down