mobility-toolbox-js 3.0.0-beta.1 → 3.0.0-beta.10

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.
Files changed (90) hide show
  1. package/api/HttpAPI.d.ts +20 -0
  2. package/api/HttpAPI.js +1 -11
  3. package/api/RealtimeAPI.d.ts +365 -0
  4. package/api/RealtimeAPI.js +102 -89
  5. package/api/RoutingAPI.d.ts +37 -0
  6. package/api/StopsAPI.d.ts +44 -0
  7. package/api/StopsAPI.js +15 -9
  8. package/api/WebSocketAPI.d.ts +153 -0
  9. package/api/index.d.ts +3 -0
  10. package/api/typedefs.d.ts +178 -0
  11. package/common/controls/StopFinderControlCommon.d.ts +53 -0
  12. package/common/index.d.ts +2 -0
  13. package/common/mixins/RealtimeLayerMixin.d.ts +273 -0
  14. package/common/styles/index.d.ts +4 -0
  15. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  16. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  17. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  18. package/common/styles/realtimeSimpleStyle.d.ts +4 -0
  19. package/common/typedefs.d.ts +212 -0
  20. package/common/utils/compareDepartures.d.ts +10 -0
  21. package/common/utils/constants.d.ts +5 -0
  22. package/common/utils/createCanvas.d.ts +10 -0
  23. package/common/utils/createDefaultCopyrightElt.d.ts +5 -0
  24. package/common/utils/createDefaultStopFinderElt.d.ts +5 -0
  25. package/common/utils/createRealtimeFilters.d.ts +12 -0
  26. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  27. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  28. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  29. package/common/utils/getLayersAsFlatArray.js +5 -1
  30. package/common/utils/getMapGlCopyrights.d.ts +17 -0
  31. package/common/utils/getRealtimeModeSuffix.d.ts +9 -0
  32. package/common/utils/getUrlWithParams.d.ts +8 -0
  33. package/common/utils/getVehiclePosition.d.ts +16 -0
  34. package/common/utils/index.d.ts +16 -0
  35. package/common/utils/realtimeConfig.d.ts +64 -0
  36. package/common/utils/removeDuplicate.d.ts +9 -0
  37. package/common/utils/renderTrajectories.d.ts +16 -0
  38. package/common/utils/sortAndFilterDepartures.d.ts +15 -0
  39. package/common/utils/sortByDelay.d.ts +3 -0
  40. package/common/utils/timeUtils.d.ts +23 -0
  41. package/common/utils/toMercatorExtent.d.ts +5 -0
  42. package/iife.d.ts +2 -0
  43. package/index.d.ts +9 -0
  44. package/maplibre/controls/CopyrightControl.d.ts +35 -0
  45. package/maplibre/controls/index.d.ts +1 -0
  46. package/maplibre/index.d.ts +5 -0
  47. package/maplibre/layers/Layer.d.ts +28 -0
  48. package/maplibre/layers/RealtimeLayer.d.ts +160 -0
  49. package/maplibre/layers/RealtimeLayer.js +2 -2
  50. package/maplibre/layers/index.d.ts +2 -0
  51. package/maplibre/utils/getMercatorResolution.d.ts +7 -0
  52. package/maplibre/utils/getSourceCoordinates.d.ts +7 -0
  53. package/maplibre/utils/index.d.ts +2 -0
  54. package/mbt.js +1394 -1291
  55. package/mbt.js.map +4 -4
  56. package/mbt.min.js +42 -42
  57. package/mbt.min.js.map +4 -4
  58. package/ol/controls/CopyrightControl.d.ts +31 -0
  59. package/ol/controls/CopyrightControl.js +18 -8
  60. package/ol/controls/RoutingControl.d.ts +209 -0
  61. package/ol/controls/RoutingControl.js +3 -0
  62. package/ol/controls/StopFinderControl.d.ts +37 -0
  63. package/ol/controls/StopFinderControl.js +3 -0
  64. package/ol/controls/index.d.ts +3 -0
  65. package/ol/index.d.ts +6 -0
  66. package/ol/layers/MapGlLayer.d.ts +144 -0
  67. package/ol/layers/MapGlLayer.js +5 -3
  68. package/ol/layers/MaplibreLayer.d.ts +63 -0
  69. package/ol/layers/MaplibreStyleLayer.d.ts +235 -0
  70. package/ol/layers/MaplibreStyleLayer.js +24 -24
  71. package/ol/layers/RealtimeLayer.d.ts +285 -0
  72. package/ol/layers/RealtimeLayer.js +13 -7
  73. package/ol/layers/index.d.ts +3 -0
  74. package/ol/mixins/MobilityLayerMixin.d.ts +98 -0
  75. package/ol/mixins/MobilityLayerMixin.js +1 -4
  76. package/ol/mixins/PropertiesLayerMixin.d.ts +127 -0
  77. package/ol/mixins/PropertiesLayerMixin.js +6 -65
  78. package/ol/renderers/MaplibreLayerRenderer.d.ts +20 -0
  79. package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +20 -0
  80. package/ol/renderers/RealtimeLayerRenderer.d.ts +22 -0
  81. package/ol/renderers/RealtimeLayerRenderer.js +9 -9
  82. package/ol/styles/fullTrajectoryDelayStyle.d.ts +6 -0
  83. package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
  84. package/ol/styles/index.d.ts +3 -0
  85. package/ol/styles/routingStyle.d.ts +4 -0
  86. package/ol/utils/getFeatureInfoAtCoordinate.d.ts +8 -0
  87. package/ol/utils/getFeatureInfoAtCoordinate.js +2 -2
  88. package/ol/utils/index.d.ts +1 -0
  89. package/package.json +18 -19
  90. package/setupTests.d.ts +1 -0
@@ -0,0 +1,20 @@
1
+ export type HttpAPIOptions = {
2
+ url: string;
3
+ apiKey?: string;
4
+ };
5
+ /**
6
+ * Common class to access to a geOps api using http.
7
+ * @private
8
+ */
9
+ declare class HttpAPI {
10
+ url: string;
11
+ apiKey?: string;
12
+ constructor(options: HttpAPIOptions);
13
+ /**
14
+ * Append the apiKey before sending the request.
15
+ *
16
+ * @private
17
+ */
18
+ fetch(path: string, params: Object, config: RequestInit): Promise<any>;
19
+ }
20
+ export default HttpAPI;
package/api/HttpAPI.js CHANGED
@@ -10,17 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import getUrlWithParams from '../common/utils/getUrlWithParams';
11
11
  /**
12
12
  * Common class to access to a geOps api using http.
13
- *
14
- * @example
15
- * import { API } from 'mobility-toolbox-js/api';
16
- *
17
- * const api = new HttpApi({
18
- * url: [yourUrl],
19
- * apiKey: [yourApiKey]
20
- * });
21
- *
22
- * @classproperty {string} url Url of the service.
23
- * @classproperty {string} apiKey Api key to access the service.
24
13
  * @private
25
14
  */
26
15
  class HttpAPI {
@@ -47,6 +36,7 @@ class HttpAPI {
47
36
  // Clean requets parameters, removing undefined and null values.
48
37
  const searchParams = params || {};
49
38
  const url = getUrlWithParams(`${this.url}${path || ''}`, Object.assign({ key: this.apiKey }, searchParams));
39
+ fetch;
50
40
  const response = yield fetch(url.toString(), config);
51
41
  const data = yield response.json();
52
42
  if (data.error) {
@@ -0,0 +1,365 @@
1
+ import WebSocketAPI, { WebSocketAPIMessageCallback, WebSocketAPIMessageEventData, WebSocketAPIParameters } from './WebSocketAPI';
2
+ import type { RealtimeMode, RealtimeDeparture, RealtimeNews, RealtimeStation, RealtimeExtraGeom, RealtimeTrainId, RealtimeGeneralizationLevel, RealtimeFullTrajectory, RealtimeTrajectoryResponse, RealtimeStationId, RealtimeVersion, RealtimeTrajectory, RealtimeTenant, RealtimeBbox } from '../types';
3
+ import { StopSequence } from './typedefs';
4
+ /**
5
+ * @typedef RealtimeAPIOptions
6
+ */
7
+ export type RealtimeAPIOptions = {
8
+ url?: string;
9
+ apiKey?: string;
10
+ version?: RealtimeVersion;
11
+ bbox?: RealtimeBbox;
12
+ buffer?: number[];
13
+ pingIntervalMs?: number;
14
+ reconnectTimeoutMs?: number;
15
+ };
16
+ export declare type RealtimeAPIExtraGeomsById = {
17
+ [index: string]: RealtimeExtraGeom;
18
+ };
19
+ export type RealtimeAPIDeparturesById = {
20
+ [index: string]: RealtimeDeparture;
21
+ };
22
+ export type RealtimeModesType = {
23
+ RAW: RealtimeMode;
24
+ TOPOGRAPHIC: RealtimeMode;
25
+ SCHEMATIC: RealtimeMode;
26
+ };
27
+ /**
28
+ * Enum for Realtime modes.
29
+ * @readonly
30
+ * @typedef {string} RealtimeMode
31
+ * @property {string} RAW "raw"
32
+ * @property {string} SCHEMATIC "schematic"
33
+ * @property {string} TOPOGRAPHIC "topographic"
34
+ * @enum {RealtimeMode}
35
+ * @public
36
+ */
37
+ export declare const RealtimeModes: {
38
+ RAW: RealtimeMode;
39
+ TOPOGRAPHIC: RealtimeMode;
40
+ SCHEMATIC: RealtimeMode;
41
+ };
42
+ /**
43
+ * This class provides convenience methods to use to the [geOps realtime API](https://developer.geops.io/apis/realtime/).
44
+ *
45
+ * @example
46
+ * import { RealtimeAPI } from 'mobility-toolbox-js/api';
47
+ *
48
+ * const api = new RealtimeAPI({
49
+ * apiKey: "yourApiKey",
50
+ * bbox: [782001, 5888803, 923410, 5923660, 11, "mots=rail"],
51
+ * // url: "wss://api.geops.io/tracker-ws/v1/",
52
+ * });
53
+ *
54
+ * // Open the websocket connection
55
+ * api.open();
56
+ *
57
+ * // Subscribe to channel
58
+ * api.subscribeTrajectory('topographic', (data) => {
59
+ * console.log('Log trajectories:', JSON.stringify(data.content));
60
+ * });
61
+ *
62
+ * // Close the websocket connection
63
+ * // api.close();
64
+ *
65
+ * @public
66
+ */
67
+ declare class RealtimeAPI {
68
+ _url: string;
69
+ get url(): string;
70
+ set url(newUrl: string);
71
+ _bbox?: RealtimeBbox;
72
+ /**
73
+ * The bounding box to receive data from.\
74
+ * Example: [minX, minY, maxX, maxY, zoom, mots , gen_level, tenant, ...]\
75
+ * &nbsp;\
76
+ * Where:
77
+ * - **minX**: a string representation of an integer (not a float) representing the minimal X coordinate (in EPSG:3857) of a bounding box\
78
+ * &nbsp;
79
+ * - **minY**: a string representation of an integer (not a float) representing the minimal Y coordinate (in EPSG:3857) of a bounding box\
80
+ * &nbsp;
81
+ * - **maxX**: a string representation of an integer (not a float) representing the maximal X coordinate (in EPSG:3857) of a bounding box\
82
+ * &nbsp;
83
+ * - **maxY**: a string representation of an integer (not a float) representing the maximal Y coordinate (in EPSG:3857) of a bounding box\
84
+ * &nbsp;
85
+ * - **zoom**: a string representation of an integer representing the zoom level (from 4 to 22). When zoom < 8 only the trains are displayed for performance reasons.\
86
+ * &nbsp;
87
+ * - **mots**: A comma separated list of modes of transport. **Optional**.\
88
+ * Example: "mots=rail,subway".\
89
+ * &nbsp;
90
+ * - **gen_level**: An integer representing the generalization level. **Optional**.\
91
+ * Example: "gen_level=5"\
92
+ * &nbsp;
93
+ * - **tenant**: A string representing the tenant. **Optional**.\
94
+ * Example: "tenant=sbb"\
95
+ * &nbsp;
96
+ * - ...: Any other values added to the bbox will be send to the server
97
+ *
98
+ * @type {string[]}
99
+ *
100
+ * @public
101
+ */
102
+ get bbox(): RealtimeBbox | undefined;
103
+ set bbox(newBbox: RealtimeBbox | undefined);
104
+ _buffer?: number[];
105
+ get buffer(): number[] | undefined;
106
+ set buffer(newBuffer: number[] | undefined);
107
+ version: RealtimeVersion;
108
+ wsApi: WebSocketAPI;
109
+ private pingInterval;
110
+ private pingIntervalMs;
111
+ private reconnectTimeout?;
112
+ private reconnectTimeoutMs?;
113
+ /**
114
+ * Constructor
115
+ *
116
+ * @param {Object} options Options.
117
+ * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
118
+ * @param {string[]} options.bbox The bounding box to receive data from.
119
+ * @param {string} [options.url='wss://api.geops.io/tracker-ws/v1/'] Url of the [geOps realtime API](https://developer.geops.io/apis/realtime/).
120
+ * @public
121
+ */
122
+ constructor(options?: RealtimeAPIOptions);
123
+ /**
124
+ * Open the websocket connection.
125
+ *
126
+ * @public
127
+ */
128
+ open(): void;
129
+ /**
130
+ * Close the websocket connection without reconnection.
131
+ *
132
+ * @public
133
+ */
134
+ close(): void;
135
+ /**
136
+ * Unsubscribe trajectory and deleted_vehicles channels. To resubscribe you have to set a new BBOX.
137
+ */
138
+ reset(): void;
139
+ /**
140
+ * Callback when the websocket is opened and ready.
141
+ * It applies the bbox and the projection.
142
+ * @private
143
+ */
144
+ onOpen(): void;
145
+ /**
146
+ * Callback when the websocket is closed by the server.
147
+ * It auto reconnects after a timeout.
148
+ * @private
149
+ */
150
+ onClose(): void;
151
+ /**
152
+ * Send GET to a channel.
153
+ *
154
+ * @param {string | WebSocketAPIParameters} channelOrParams Name of the websocket channel to send GET or an object representing parameters to send
155
+ * @return {Promise<WebSocketAPIMessageEventData<?>>} A websocket response.
156
+ */
157
+ get(channelOrParams: string | WebSocketAPIParameters): Promise<WebSocketAPIMessageEventData<any>>;
158
+ /**
159
+ * Subscribe to a channel.
160
+ *
161
+ * @param {string} channel Name of the websocket channel to subscribe.
162
+ * @param {function} onSuccess Callback when the subscription succeeds.
163
+ * @param {function} onError Callback when the subscription fails.
164
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
165
+ * @private
166
+ */
167
+ subscribe(channel: string, onSuccess: WebSocketAPIMessageCallback<any>, onError?: EventListener, quiet?: boolean): void;
168
+ /**
169
+ * Unsubscribe both modes of a channel.
170
+ *
171
+ * @param {string} channel Name of the websocket channel to unsubscribe.
172
+ * @param {string} suffix Suffix to add to the channel name.
173
+ * @param {function} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
174
+ * @private
175
+ */
176
+ unsubscribe(channel: string, suffix?: string, onMessage?: WebSocketAPIMessageCallback<any>): void;
177
+ /**
178
+ * Subscribe to departures channel of a given station.
179
+ *
180
+ * @param {number} stationId UIC of the station.
181
+ * @param {Boolean} sortByMinArrivalTime Sort by minimum arrival time
182
+ * @param {function(departures:Departure[])} onMessage Function called on each message of the channel.
183
+ * @param {function} onError Callback when the subscription fails.
184
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
185
+ * @public
186
+ */
187
+ subscribeDepartures(stationId: number, onMessage: WebSocketAPIMessageCallback<RealtimeDeparture>, onError?: EventListener, quiet?: boolean): void;
188
+ /**
189
+ * Unsubscribe from current departures channel.
190
+ * @param {RealtimeStationId} id Station's id
191
+ * @param {function(data: { content: RealtimeDeparture[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
192
+ * @public
193
+ */
194
+ unsubscribeDepartures(id: RealtimeStationId, onMessage?: WebSocketAPIMessageCallback<RealtimeDeparture>): void;
195
+ /**
196
+ * Subscribe to the disruptions channel for tenant.
197
+ *
198
+ * @param {function(data: { content: RealtimeNews[] })} onMessage Function called on each message of the channel.
199
+ * @param {function} onError Callback when the subscription fails.
200
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
201
+ * @public
202
+ */
203
+ subscribeDisruptions(tenant: RealtimeTenant, onMessage: WebSocketAPIMessageCallback<RealtimeNews>, onError?: EventListener, quiet?: boolean): void;
204
+ /**
205
+ * Unsubscribe disruptions.
206
+ *
207
+ * @param {function(data: { content: RealtimeNews[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
208
+ * @public
209
+ */
210
+ unsubscribeDisruptions(tenant: RealtimeTenant, onMessage?: WebSocketAPIMessageCallback<RealtimeNews>): void;
211
+ /**
212
+ * Return a station with a given uic number and a mode.
213
+ *
214
+ * @param {number} uic UIC of the station.
215
+ * @param {RealtimeMode} mode Realtime mode.
216
+ * @return {Promise<{data: { content: RealtimeStation }}>} A station.
217
+ * @public
218
+ */
219
+ getStation(uic: RealtimeStationId, mode: RealtimeMode): Promise<WebSocketAPIMessageEventData<RealtimeStation>>;
220
+ /**
221
+ * Get the list of ststions available for a specifc mode. The promise is resolved every 100ms
222
+ * @param {RealtimeMode} mode Realtime mode.
223
+ * @param {number} timeout = 100 Duration in ms between each promise resolve calls.
224
+ * @return {Promise<RealtimeStation[]>} An array of stations.
225
+ * @public
226
+ */
227
+ getStations(mode: RealtimeMode, timeout?: number): Promise<RealtimeStation[]>;
228
+ /**
229
+ * Subscribe to stations channel.
230
+ * One message pro station.
231
+ *
232
+ * @param {RealtimeMode} mode Realtime mode.
233
+ * @param {function(data: { content: RealtimeStation })} onMessage Function called on each message of the channel.
234
+ * @param {function} onError Callback when the subscription fails.
235
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
236
+ * @public
237
+ */
238
+ subscribeStations(mode: RealtimeMode, onMessage: WebSocketAPIMessageCallback<RealtimeStation>, onError?: EventListener, quiet?: boolean): void;
239
+ /**
240
+ * Unsubscribe to stations channel.
241
+ * @param {function(data: { content: RealtimeStation })} onMessage The listener callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribe.
242
+ * @public
243
+ */
244
+ unsubscribeStations(onMessage?: WebSocketAPIMessageCallback<RealtimeStation>): void;
245
+ /**
246
+ * Subscribe to extra_geoms channel.
247
+ *
248
+ * @param {function(data: { content: RealtimeExtraGeom })} onMessage Function called on each message of the channel.
249
+ * @param {function} onError Callback when the subscription fails.
250
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
251
+ */
252
+ subscribeExtraGeoms(onMessage: WebSocketAPIMessageCallback<RealtimeExtraGeom>, onError?: EventListener, quiet?: boolean): void;
253
+ /**
254
+ * Unsubscribe to extra_geoms channel.
255
+ * @param {function(data: { content: RealtimeExtraGeom })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
256
+ */
257
+ unsubscribeExtraGeoms(onMessage: WebSocketAPIMessageCallback<RealtimeExtraGeom>): void;
258
+ /**
259
+ * Return a partial trajectory with a given id and a mode.
260
+ *
261
+ * @param {number} trainId The identifier of a trajectory.
262
+ * @param {RealtimeMode} mode Realtime mode.
263
+ * @return {Promise<{data: { content: RealtimeTrajectory }}>} A trajectory.
264
+ * @public
265
+ */
266
+ getTrajectory(id: RealtimeTrainId, mode: RealtimeMode): Promise<WebSocketAPIMessageEventData<RealtimeTrajectory>>;
267
+ /**
268
+ * Subscribe to trajectory channel.
269
+ *
270
+ * @param {RealtimeMode} mode Realtime mode.
271
+ * @param {function(data: { content: RealtimeTrajectoryResponse[] })} onMessage Function called on each message of the channel.
272
+ * @param {function} onError Callback when the subscription fails.
273
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
274
+ * @public
275
+ */
276
+ subscribeTrajectory(mode: RealtimeMode, onMessage: WebSocketAPIMessageCallback<RealtimeTrajectoryResponse[] | RealtimeTrajectoryResponse>, onError?: EventListener, quiet?: boolean): void;
277
+ /**
278
+ * Unsubscribe to trajectory channels.
279
+ * @param {function(data: { content: RealtimeTrajectoryResponse[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
280
+ * @public
281
+ */
282
+ unsubscribeTrajectory(onMessage: WebSocketAPIMessageCallback<RealtimeTrajectoryResponse[]>): void;
283
+ /**
284
+ * Subscribe to deleted_vhicles channel.
285
+ *
286
+ * @param {RealtimeMode} mode Realtime mode.
287
+ * @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
288
+ * @param {function} onError Callback when the subscription fails.
289
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
290
+ */
291
+ subscribeDeletedVehicles(mode: RealtimeMode, onMessage: WebSocketAPIMessageCallback<RealtimeTrainId>, onError?: EventListener, quiet?: boolean): void;
292
+ /**
293
+ * Unsubscribe to deleted_vhicles channels.
294
+ * @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
295
+ */
296
+ unsubscribeDeletedVehicles(onMessage: WebSocketAPIMessageCallback<RealtimeTrainId>): void;
297
+ /**
298
+ * Get a full trajectory of a vehicule .
299
+ *
300
+ * @param {string} id A vehicle id.
301
+ * @param {RealtimeMode} mode Realtime mode.
302
+ * @param {string} generalizationLevel The generalization level to request. Can be one of 5 (more generalized), 10, 30, 100, undefined (less generalized).
303
+ * @return {Promise<{ data: { content: RealtimeFullTrajectory } }>} Return a full trajectory.
304
+ * @public
305
+ */
306
+ getFullTrajectory(id: RealtimeTrainId, mode: RealtimeMode, generalizationLevel: RealtimeGeneralizationLevel | undefined): Promise<WebSocketAPIMessageEventData<RealtimeFullTrajectory>>;
307
+ /**
308
+ * Subscribe to full_trajectory channel of a given vehicle.
309
+ *
310
+ * @param {string} id A vehicle id.
311
+ * @param {RealtimeMode} mode Realtime mode.
312
+ * @param {function(data: { content: RealtimeFullTrajectory })} onMessage Function called on each message of the channel.
313
+ * @param {function} onError Callback when the subscription fails.
314
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
315
+ * @public
316
+ */
317
+ subscribeFullTrajectory(id: RealtimeTrainId, mode: RealtimeMode, onMessage: WebSocketAPIMessageCallback<RealtimeFullTrajectory>, onError?: EventListener, quiet?: boolean): void;
318
+ /**
319
+ * Unsubscribe from full_trajectory channel
320
+ *
321
+ * @param {string} id A vehicle id.
322
+ * @param {function(data: { content: RealtimeFullTrajectory })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
323
+ * @public
324
+ */
325
+ unsubscribeFullTrajectory(id: RealtimeTrainId, onMessage?: WebSocketAPIMessageCallback<RealtimeFullTrajectory>): void;
326
+ /**
327
+ * Get the list of stops for this vehicle.
328
+ *
329
+ * @param {string} id A vehicle id.
330
+ * @return {Promise<{ data: { content: StopSequence[] } }>} Returns a stop sequence object.
331
+ * @public
332
+ */
333
+ getStopSequence(id: RealtimeTrainId): Promise<WebSocketAPIMessageEventData<StopSequence[]>>;
334
+ /**
335
+ * Subscribe to stopsequence channel of a given vehicle.
336
+ *
337
+ * @param {string} id A vehicle id.
338
+ * @param {function(data: { content: StopSequence[] })} onMessage Function called on each message of the channel.
339
+ * @param {function} onError Callback when the subscription fails.
340
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
341
+ * @public
342
+ */
343
+ subscribeStopSequence(id: RealtimeTrainId, onMessage: WebSocketAPIMessageCallback<StopSequence[]>, onError?: EventListener, quiet?: boolean): void;
344
+ /**
345
+ * Unsubscribe from stopsequence channel
346
+ *
347
+ * @param {string} id A vehicle id.
348
+ * @param {function(data: { content: StopSequence[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
349
+ * @public
350
+ */
351
+ unsubscribeStopSequence(id: RealtimeTrainId, onMessage?: WebSocketAPIMessageCallback<StopSequence[]>): void;
352
+ /**
353
+ * Subscribe to healthcheck channel.
354
+ * @param {function(data: { content: string })} onMessage Callback when the subscribe to healthcheck channel succeeds.
355
+ * @param {function} onError Callback when the subscription fails.
356
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
357
+ */
358
+ subscribeHealthCheck(onMessage: WebSocketAPIMessageCallback<string>, onError?: EventListener, quiet?: boolean): void;
359
+ /**
360
+ * Unsubscribe to healthcheck channel.
361
+ * @param {function(data: { content: string })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
362
+ */
363
+ unsubscribeHealthCheck(onMessage?: WebSocketAPIMessageCallback<string>): void;
364
+ }
365
+ export default RealtimeAPI;
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-underscore-dangle */
1
2
  /* eslint-disable class-methods-use-this */
2
3
  import WebSocketAPI from './WebSocketAPI';
3
4
  import debounceWebsocketMessages from '../common/utils/debounceWebsocketMessages';
@@ -10,6 +11,7 @@ import getModeSuffix from '../common/utils/getRealtimeModeSuffix';
10
11
  * @property {string} SCHEMATIC "schematic"
11
12
  * @property {string} TOPOGRAPHIC "topographic"
12
13
  * @enum {RealtimeMode}
14
+ * @public
13
15
  */
14
16
  export const RealtimeModes = {
15
17
  RAW: 'raw',
@@ -17,123 +19,135 @@ export const RealtimeModes = {
17
19
  SCHEMATIC: 'schematic',
18
20
  };
19
21
  /**
20
- * This class provides convenience methods to use to the [geOps Realtime API](https://developer.geops.io/apis/realtime/).
22
+ * This class provides convenience methods to use to the [geOps realtime API](https://developer.geops.io/apis/realtime/).
21
23
  *
22
24
  * @example
23
25
  * import { RealtimeAPI } from 'mobility-toolbox-js/api';
24
26
  *
25
27
  * const api = new RealtimeAPI({
26
28
  * apiKey: "yourApiKey",
27
- * bbox: [782001, 5888803, 923410, 5923660, 11, "mots=rail],
29
+ * bbox: [782001, 5888803, 923410, 5923660, 11, "mots=rail"],
28
30
  * // url: "wss://api.geops.io/tracker-ws/v1/",
29
31
  * });
30
32
  *
33
+ * // Open the websocket connection
31
34
  * api.open();
32
35
  *
36
+ * // Subscribe to channel
33
37
  * api.subscribeTrajectory('topographic', (data) => {
34
38
  * console.log('Log trajectories:', JSON.stringify(data.content));
35
39
  * });
36
40
  *
37
- * @classproperty {string} apiKey - Access key for [geOps APIs](https://developer.geops.io/)
38
- * @classproperty {RealtimeBbox} bbox - The bounding box to receive data from. \ Example: \ [ minX, minY, maxX, maxY, zoom, "tenant=tenant1", "gen_level=5", "mots=mot1,mot2" ]. \ tenant, gen_level and mots are optional.
39
- * @classproperty {string} url - The [geOps Realtime API](https://developer.geops.io/apis/realtime/) url.
41
+ * // Close the websocket connection
42
+ * // api.close();
43
+ *
40
44
  * @public
41
45
  */
42
46
  class RealtimeAPI {
47
+ get url() {
48
+ return this._url;
49
+ }
50
+ set url(newUrl) {
51
+ if (this._url !== newUrl) {
52
+ this._url = newUrl;
53
+ // Update the websocket only if the url has changed and the websocket is already open or is opening.
54
+ if (this.wsApi.open || this.wsApi.connecting) {
55
+ this.open();
56
+ }
57
+ }
58
+ }
59
+ /**
60
+ * The bounding box to receive data from.\
61
+ * Example: [minX, minY, maxX, maxY, zoom, mots , gen_level, tenant, ...]\
62
+ * &nbsp;\
63
+ * Where:
64
+ * - **minX**: a string representation of an integer (not a float) representing the minimal X coordinate (in EPSG:3857) of a bounding box\
65
+ * &nbsp;
66
+ * - **minY**: a string representation of an integer (not a float) representing the minimal Y coordinate (in EPSG:3857) of a bounding box\
67
+ * &nbsp;
68
+ * - **maxX**: a string representation of an integer (not a float) representing the maximal X coordinate (in EPSG:3857) of a bounding box\
69
+ * &nbsp;
70
+ * - **maxY**: a string representation of an integer (not a float) representing the maximal Y coordinate (in EPSG:3857) of a bounding box\
71
+ * &nbsp;
72
+ * - **zoom**: a string representation of an integer representing the zoom level (from 4 to 22). When zoom < 8 only the trains are displayed for performance reasons.\
73
+ * &nbsp;
74
+ * - **mots**: A comma separated list of modes of transport. **Optional**.\
75
+ * Example: "mots=rail,subway".\
76
+ * &nbsp;
77
+ * - **gen_level**: An integer representing the generalization level. **Optional**.\
78
+ * Example: "gen_level=5"\
79
+ * &nbsp;
80
+ * - **tenant**: A string representing the tenant. **Optional**.\
81
+ * Example: "tenant=sbb"\
82
+ * &nbsp;
83
+ * - ...: Any other values added to the bbox will be send to the server
84
+ *
85
+ * @type {string[]}
86
+ *
87
+ * @public
88
+ */
89
+ get bbox() {
90
+ return this._bbox;
91
+ }
92
+ set bbox(newBbox) {
93
+ if (JSON.stringify(newBbox) !== JSON.stringify(this._bbox)) {
94
+ this._bbox = newBbox;
95
+ if (this.wsApi && this._bbox) {
96
+ this.wsApi.send(`BBOX ${this._bbox.join(' ')}`);
97
+ }
98
+ }
99
+ }
100
+ get buffer() {
101
+ return this._buffer;
102
+ }
103
+ set buffer(newBuffer) {
104
+ if (JSON.stringify(newBuffer) !== JSON.stringify(this._buffer)) {
105
+ this._buffer = newBuffer;
106
+ if (this.wsApi && this._buffer) {
107
+ this.wsApi.send(`BUFFER ${this._buffer.join(' ')}`);
108
+ }
109
+ }
110
+ }
43
111
  /**
44
112
  * Constructor
45
113
  *
46
- * @param {RealtimeAPIOptions} options A string representing the url of the service or an object containing the url and the apiKey.
47
- * @param {string} options.url Url to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
114
+ * @param {Object} options Options.
48
115
  * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
49
- * @param {number[5]} [options.bbox=[minX, minY, maxX, maxY, zoom, tenant]] The bounding box to receive data from.
116
+ * @param {string[]} options.bbox The bounding box to receive data from.
117
+ * @param {string} [options.url='wss://api.geops.io/tracker-ws/v1/'] Url of the [geOps realtime API](https://developer.geops.io/apis/realtime/).
118
+ * @public
50
119
  */
51
120
  constructor(options = {}) {
52
121
  this.version = '2';
53
- this.defineProperties(options);
54
- this.onOpen = this.onOpen.bind(this);
55
- }
56
- defineProperties(options = {}) {
57
- let opt = options || {};
122
+ let opt = options;
58
123
  if (typeof options === 'string') {
59
124
  opt = { url: options };
60
125
  }
61
- const { apiKey, version } = opt;
62
- let { url, bbox, buffer = [100, 100] } = opt;
126
+ const { apiKey } = opt;
127
+ const { url } = opt;
63
128
  const wsApi = new WebSocketAPI();
64
- if (!url) {
65
- url = 'wss://api.geops.io/tracker-ws/v1/';
66
- }
67
- if (apiKey) {
68
- url = `${url}?key=${apiKey}`;
129
+ let suffix = '';
130
+ if (apiKey && !(url === null || url === void 0 ? void 0 : url.includes('key='))) {
131
+ suffix = `?key=${apiKey}`;
69
132
  }
70
- Object.defineProperties(this, {
71
- url: {
72
- get: () => url,
73
- set: (newUrl) => {
74
- if (url !== newUrl) {
75
- url = newUrl;
76
- // Update the websocket only if the url has changed and the websocket is already open or is opening.
77
- if (this.wsApi.open || this.wsApi.connecting) {
78
- this.open();
79
- }
80
- }
81
- },
82
- },
83
- bbox: {
84
- get: () => bbox,
85
- set: (newBbox) => {
86
- if (JSON.stringify(newBbox) !== JSON.stringify(bbox)) {
87
- bbox = newBbox;
88
- if (this.wsApi && bbox) {
89
- this.wsApi.send(`BBOX ${bbox.join(' ')}`);
90
- }
91
- }
92
- },
93
- },
94
- buffer: {
95
- get: () => buffer,
96
- set: (newBuffer) => {
97
- if (JSON.stringify(newBuffer) !== JSON.stringify(buffer)) {
98
- buffer = newBuffer;
99
- if (this.wsApi) {
100
- this.wsApi.send(`BUFFER ${buffer.join(' ')}`);
101
- }
102
- }
103
- },
104
- },
105
- version: {
106
- value: version,
107
- writable: true,
108
- },
109
- /**
110
- * The websocket helper class to connect the websocket.
111
- *
112
- * @private
113
- */
114
- wsApi: {
115
- value: wsApi,
116
- writable: true,
117
- },
118
- /**
119
- * Interval between PING request in ms.
120
- * If equal to 0, no PING request are sent.
121
- * @type {number}
122
- * @private
123
- */
124
- pingIntervalMs: {
125
- value: options.pingIntervalMs || 10000,
126
- writable: true,
127
- },
128
- /**
129
- * Timeout in ms after an automatic reconnection when the websoscket has been closed by the server.
130
- * @type {number}
131
- */
132
- reconnectTimeoutMs: {
133
- value: options.pingIntervalMs || 100,
134
- writable: true,
135
- },
136
- });
133
+ this._url = (url || 'wss://api.geops.io/tracker-ws/v1/') + suffix;
134
+ this._buffer = opt.buffer || [100, 100];
135
+ this.version = opt.version || '2';
136
+ /**
137
+ * Interval between PING request in ms.
138
+ * If equal to 0, no PING request are sent.
139
+ * @type {number}
140
+ */
141
+ this.pingIntervalMs = opt.pingIntervalMs || 10000;
142
+ /**
143
+ * Timeout in ms after an automatic reconnection when the websoscket has been closed by the server.
144
+ * @type {number}
145
+ */
146
+ this.reconnectTimeoutMs = opt.reconnectTimeoutMs || 100;
147
+ /**
148
+ * The websocket helper class to connect the websocket.
149
+ */
150
+ this.wsApi = wsApi;
137
151
  }
138
152
  /**
139
153
  * Open the websocket connection.
@@ -141,7 +155,7 @@ class RealtimeAPI {
141
155
  * @public
142
156
  */
143
157
  open() {
144
- this.wsApi.connect(this.url, this.onOpen);
158
+ this.wsApi.connect(this.url, this.onOpen.bind(this));
145
159
  // Register reconnection on close.
146
160
  if (this.wsApi.websocket) {
147
161
  this.wsApi.websocket.onclose = () => {
@@ -160,7 +174,6 @@ class RealtimeAPI {
160
174
  /**
161
175
  * Unsubscribe trajectory and deleted_vehicles channels. To resubscribe you have to set a new BBOX.
162
176
  */
163
- // eslint-disable-next-line class-methods-use-this
164
177
  reset() {
165
178
  this.wsApi.send('RESET');
166
179
  }