@wemap/routers 7.0.0 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable complexity */
2
2
  /* eslint-disable max-statements */
3
3
 
4
- import { Coordinates } from '@wemap/geo';
4
+ import { Coordinates, NoRouteFoundError } from '@wemap/geo';
5
5
  import Logger from '@wemap/logger';
6
6
 
7
7
  import IOMap from './IOMap.js';
@@ -81,12 +81,12 @@ class WemapMetaRouter {
81
81
  const { targetMaps } = options;
82
82
  if (targetMaps) {
83
83
 
84
- ioMapsToTest = this.maps.filter(map => targetMaps.includes(map));
84
+ ioMapsToTest = this.maps.filter(map => targetMaps.includes(map.name));
85
85
 
86
86
  // Send a warning if one of the request ioMap (from targetMaps) is not present in this.maps
87
87
  if (ioMapsToTest.length !== targetMaps.length) {
88
- targetMaps.forEach(map => {
89
- if (!ioMapsToTest.includes(map)) {
88
+ ioMapsToTest.forEach(map => {
89
+ if (!targetMaps.includes(map.name)) {
90
90
  Logger.warn(`IOMap "${map.name}" not found in WemapMetaRouter`);
91
91
  }
92
92
  });
@@ -182,7 +182,15 @@ class WemapMetaRouter {
182
182
  return routerResponse;
183
183
  }
184
184
 
185
- ioMapItinerary = mapWithStart.getBestItineraryFromStartToEntryPoints(start, end, wemapRouterOptions);
185
+ try {
186
+ ioMapItinerary = mapWithStart.getBestItineraryFromStartToEntryPoints(start, end, wemapRouterOptions);
187
+ } catch (e) {
188
+ if (!(e instanceof NoRouteFoundError)) {
189
+ throw e;
190
+ }
191
+ routerResponse.error = `${e.message} - on map ${mapWithStart.name}.`;
192
+ return routerResponse;
193
+ }
186
194
  remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
187
195
  remoteRouters, mode, [ioMapItinerary.to, end]
188
196
  );
@@ -230,7 +238,15 @@ class WemapMetaRouter {
230
238
  * calculate all the routes to entrypoints using local router than all the routes with the
231
239
  * remote router.
232
240
  */
233
- ioMapItinerary = mapWithEnd.getBestItineraryFromEntryPointsToEnd(start, end, wemapRouterOptions);
241
+ try {
242
+ ioMapItinerary = mapWithEnd.getBestItineraryFromEntryPointsToEnd(start, end, wemapRouterOptions);
243
+ } catch (e) {
244
+ if (!(e instanceof NoRouteFoundError)) {
245
+ throw e;
246
+ }
247
+ routerResponse.error = `${e.message} - on map ${mapWithEnd.name}.`;
248
+ return routerResponse;
249
+ }
234
250
  remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
235
251
  remoteRouters, mode, [start, ioMapItinerary.from]
236
252
  );
@@ -279,8 +295,25 @@ class WemapMetaRouter {
279
295
  return routerResponse;
280
296
  }
281
297
 
282
- const ioMapItinerary1 = mapWithStart.getBestItineraryFromStartToEntryPoints(start, end, wemapRouterOptions);
283
- const ioMapItinerary2 = mapWithEnd.getBestItineraryFromEntryPointsToEnd(start, end, wemapRouterOptions);
298
+ let ioMapItinerary1, ioMapItinerary2;
299
+ try {
300
+ ioMapItinerary1 = mapWithStart.getBestItineraryFromStartToEntryPoints(start, end, wemapRouterOptions);
301
+ } catch (e) {
302
+ if (!(e instanceof NoRouteFoundError)) {
303
+ throw e;
304
+ }
305
+ routerResponse.error = `${e.message} - on map ${mapWithStart.name}.`;
306
+ return routerResponse;
307
+ }
308
+ try {
309
+ ioMapItinerary2 = mapWithEnd.getBestItineraryFromEntryPointsToEnd(start, end, wemapRouterOptions);
310
+ } catch (e) {
311
+ if (!(e instanceof NoRouteFoundError)) {
312
+ throw e;
313
+ }
314
+ routerResponse.error = `${e.message} - on map ${mapWithEnd.name}.`;
315
+ return routerResponse;
316
+ }
284
317
  remoteRouterResponse = await RemoteRouterManager.getItinerariesWithFallback(
285
318
  remoteRouters, mode, [ioMapItinerary1.to, ioMapItinerary2.from]
286
319
  );
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable max-statements */
2
2
  import chai from 'chai';
3
- import fetch, { Headers } from 'node-fetch';
4
3
  import fs from 'fs';
5
4
  import path from 'path';
6
5
  import { fileURLToPath } from 'url';
@@ -12,15 +11,24 @@ import IOMap from './IOMap.js';
12
11
  import WemapMetaRouter from './WemapMetaRouter.js';
13
12
  import Constants from '../Constants.js';
14
13
  import WemapMetaRouterOptions from './WemapMetaRouterOptions.js';
15
- import { OsrmRemoteRouter } from '../../index.js';
14
+ import OsrmRemoteRouter from '../remote/osrm/OsrmRemoteRouter.js';
16
15
  import checkRouterResponseType from '../model/RouterResponse.type.spec.js';
17
16
 
18
17
  const { expect } = chai;
19
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
20
18
 
21
- // TODO: Remove with node 18
22
- global.fetch = fetch;
23
- global.Headers = Headers;
19
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
20
+ const assetsPath = path.resolve(__dirname, '../../assets');
21
+
22
+ function mockFetchWithJsonAsset(assetName) {
23
+ global.fetch = async () => ({
24
+ status: 200,
25
+ json: async () => {
26
+ const filePath = path.resolve(assetsPath, assetName);
27
+ const fileString = fs.readFileSync(filePath, 'utf8');
28
+ return JSON.parse(fileString);
29
+ }
30
+ });
31
+ }
24
32
 
25
33
  function createIOMapFromAsset(assetName) {
26
34
  const filePath = path.resolve(__dirname, '../../assets/' + assetName);
@@ -40,7 +48,7 @@ describe('WemapMetaRouter', () => {
40
48
 
41
49
  const mode = Constants.ROUTING_MODE.WALK;
42
50
  const options = new WemapMetaRouterOptions();
43
- options.targetMaps = [biocbonMap];
51
+ options.targetMaps = [biocbonMap.name];
44
52
  options.remoteRouters.push({
45
53
  name: OsrmRemoteRouter.rname,
46
54
  endpointUrl: 'https://routing.getwemap.com'
@@ -73,6 +81,7 @@ describe('WemapMetaRouter', () => {
73
81
  it('outdoor to outdoor', async () => {
74
82
  const start = new Coordinates(48.8726513, 2.343449);
75
83
  const end = new Coordinates(48.8726397, 2.3431657);
84
+ mockFetchWithJsonAsset('rr-wemap-meta-outdoor-outdoor.json');
76
85
 
77
86
  const routerResponse = await router.getItineraries(mode, [start, end], options);
78
87
  checkRouterResponseType(routerResponse);
@@ -84,6 +93,7 @@ describe('WemapMetaRouter', () => {
84
93
  it('outdoor to indoor', async () => {
85
94
  const start = new Coordinates(48.8726085, 2.3434289);
86
95
  const end = new Coordinates(48.8725694, 2.3433);
96
+ mockFetchWithJsonAsset('rr-wemap-meta-outdoor-indoor.json');
87
97
 
88
98
  const routerResponse = await router.getItineraries(mode, [start, end], options);
89
99
  checkRouterResponseType(routerResponse);
@@ -95,6 +105,7 @@ describe('WemapMetaRouter', () => {
95
105
  it('indoor to outdoor', async () => {
96
106
  const start = new Coordinates(48.8725992, 2.343431);
97
107
  const end = new Coordinates(48.8726513, 2.343449);
108
+ mockFetchWithJsonAsset('rr-wemap-meta-indoor-outdoor.json');
98
109
 
99
110
  const routerResponse = await router.getItineraries(mode, [start, end], options);
100
111
  checkRouterResponseType(routerResponse);
@@ -105,9 +116,10 @@ describe('WemapMetaRouter', () => {
105
116
 
106
117
  it('indoor to outdoor to indoor', async () => {
107
118
 
108
- options.targetMaps.push(garedelestMap);
119
+ options.targetMaps.push(garedelestMap.name);
109
120
  const start = new Coordinates(48.8725992, 2.343431);
110
121
  const end = new Coordinates(48.8772962, 2.3584458, null, new Level(0));
122
+ mockFetchWithJsonAsset('rr-wemap-meta-indoor-outdoor-indoor.json');
111
123
 
112
124
  const routerResponse = await router.getItineraries(mode, [start, end], options);
113
125
  checkRouterResponseType(routerResponse);
@@ -1,11 +1,9 @@
1
- import IOMap from './IOMap.js';
2
-
3
1
  class WemapMetaRouterOptions {
4
2
 
5
3
  /** @type {!boolean} */
6
4
  useStairs = true;
7
5
 
8
- /** @type {?(IOMap[])} */
6
+ /** @type {?(string[])} */
9
7
  targetMaps = null;
10
8
 
11
9
  /**