|
1 | 1 | import { get } from "./helper.js"; |
2 | 2 |
|
| 3 | +function escapeCqlString(value) { |
| 4 | + return String(value).replace(/'/g, "''"); |
| 5 | +} |
| 6 | + |
| 7 | +function sanitizeForILikePrefix(value) { |
| 8 | + return String(value).replace(/%/g, "").replace(/_/g, ""); |
| 9 | +} |
| 10 | + |
| 11 | +function normalizeRoutableLocationFeature(feature) { |
| 12 | + const props = feature.properties || {}; |
| 13 | + const displayName = props.name != null ? String(props.name) : ""; |
| 14 | + return { |
| 15 | + ...feature, |
| 16 | + properties: { |
| 17 | + ...props, |
| 18 | + fullAddress: displayName, |
| 19 | + }, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +export function getExtraLocations(addressInput) { |
| 24 | + // return empty promise if address input is less than 2 characters |
| 25 | + const trimmed = (addressInput || "").trim(); |
| 26 | + if (trimmed.length < 2) { |
| 27 | + return Promise.resolve({ type: "FeatureCollection", features: [] }); |
| 28 | + } |
| 29 | + |
| 30 | + // escape CQL string and sanitize for ILike prefix |
| 31 | + const literal = escapeCqlString(sanitizeForILikePrefix(trimmed)); |
| 32 | + return get( |
| 33 | + `${window.ROUTABLE_LOCATIONS_HOST}`, |
| 34 | + { |
| 35 | + service: "WFS", |
| 36 | + version: "1.0.0", |
| 37 | + request: "GetFeature", |
| 38 | + typeName: "public:routable-locations", |
| 39 | + outputFormat: "application/json", |
| 40 | + maxFeatures: 5, |
| 41 | + cql_filter: `authority='DriveBC' and name ilike '${literal}%'`, |
| 42 | + }, |
| 43 | + {}, |
| 44 | + false, |
| 45 | + ); |
| 46 | +} |
| 47 | + |
3 | 48 | export function getLocations(addressInput) { |
4 | | - return get(`${window.GEOCODER_HOST}/addresses.json`, { |
5 | | - minScore: 50, |
6 | | - maxResults: 7, |
7 | | - echo: 'false', |
8 | | - brief: true, |
9 | | - autoComplete: true, |
10 | | - addressString: addressInput, |
11 | | - exactSpelling: false, |
12 | | - locationDescriptor: 'routingPoint', |
13 | | - fuzzyMatch: true, |
14 | | - }, { |
15 | | - 'apiKey': `${window.GEOCODER_API_CLIENT_ID}`, |
16 | | - } |
17 | | - ).then((data) => data); |
| 49 | + const geocoderPromise = get(`${window.GEOCODER_HOST}/addresses.json`, { |
| 50 | + minScore: 50, |
| 51 | + maxResults: 7, |
| 52 | + echo: "false", |
| 53 | + brief: true, |
| 54 | + autoComplete: true, |
| 55 | + addressString: addressInput, |
| 56 | + exactSpelling: false, |
| 57 | + locationDescriptor: "routingPoint", |
| 58 | + fuzzyMatch: true, |
| 59 | + }, { |
| 60 | + apiKey: `${window.GEOCODER_API_CLIENT_ID}`, |
| 61 | + }); |
| 62 | + |
| 63 | + const extraPromise = getExtraLocations(addressInput).catch(() => ({ |
| 64 | + type: "FeatureCollection", |
| 65 | + features: [], |
| 66 | + })); |
| 67 | + |
| 68 | + // return all features from geocoder and extra locations |
| 69 | + return Promise.all([geocoderPromise, extraPromise]).then( |
| 70 | + ([geoData, extraData]) => { |
| 71 | + const extraFeatures = (extraData.features || []).map( |
| 72 | + normalizeRoutableLocationFeature, |
| 73 | + ); |
| 74 | + const geoFeatures = geoData.features || []; |
| 75 | + return { |
| 76 | + ...geoData, |
| 77 | + features: [...extraFeatures, ...geoFeatures], |
| 78 | + }; |
| 79 | + }, |
| 80 | + ); |
18 | 81 | } |
0 commit comments