mobility-toolbox-js 2.0.0 → 2.1.0

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 (271) hide show
  1. package/api/RealtimeAPI.d.ts +279 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RealtimeAPI.js +466 -0
  4. package/api/RoutingAPI.d.ts +37 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/RoutingAPI.js +35 -0
  7. package/api/StopsAPI.d.ts +38 -0
  8. package/api/StopsAPI.d.ts.map +1 -0
  9. package/api/StopsAPI.js +36 -0
  10. package/api/index.d.ts +4 -0
  11. package/api/index.d.ts.map +1 -0
  12. package/api/index.js +3 -3
  13. package/api/typedefs.d.ts +179 -0
  14. package/api/typedefs.d.ts.map +1 -0
  15. package/api/{tralis/typedefs.js → typedefs.js} +4 -10
  16. package/common/api/HttpAPI.d.ts +31 -0
  17. package/common/api/HttpAPI.d.ts.map +1 -0
  18. package/common/api/HttpAPI.js +57 -0
  19. package/common/api/WebSocketAPI.d.ts +153 -0
  20. package/common/api/WebSocketAPI.d.ts.map +1 -0
  21. package/common/api/WebSocketAPI.js +341 -0
  22. package/common/controls/ControlCommon.d.ts +76 -0
  23. package/common/controls/ControlCommon.d.ts.map +1 -0
  24. package/common/controls/ControlCommon.js +150 -0
  25. package/common/controls/CopyrightControlCommon.d.ts +13 -0
  26. package/common/controls/CopyrightControlCommon.d.ts.map +1 -0
  27. package/common/controls/CopyrightControlCommon.js +34 -0
  28. package/common/controls/StopFinderControlCommon.d.ts +55 -0
  29. package/common/controls/StopFinderControlCommon.d.ts.map +1 -0
  30. package/common/controls/StopFinderControlCommon.js +144 -0
  31. package/common/index.d.ts +3 -0
  32. package/common/index.d.ts.map +1 -0
  33. package/common/index.js +2 -0
  34. package/common/layers/LayerCommon.d.ts +94 -0
  35. package/common/layers/LayerCommon.d.ts.map +1 -0
  36. package/common/layers/LayerCommon.js +244 -0
  37. package/common/mixins/RealtimeLayerMixin.d.ts +286 -0
  38. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  39. package/common/mixins/RealtimeLayerMixin.js +776 -0
  40. package/common/mixins/UserInteractionsLayerMixin.d.ts +60 -0
  41. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  42. package/common/mixins/UserInteractionsLayerMixin.js +241 -0
  43. package/common/styles/index.d.ts +5 -0
  44. package/common/styles/index.d.ts.map +1 -0
  45. package/common/styles/index.js +4 -4
  46. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  47. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  48. package/common/styles/realtimeDefaultStyle.js +275 -0
  49. package/common/styles/{trackerDelayStyle.js → realtimeDelayStyle.d.ts} +4 -9
  50. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  51. package/common/styles/realtimeDelayStyle.js +13 -0
  52. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  53. package/common/styles/realtimeHeadingStyle.d.ts.map +1 -0
  54. package/common/styles/realtimeHeadingStyle.js +85 -0
  55. package/common/styles/realtimeSimpleStyle.d.ts +4 -0
  56. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  57. package/common/styles/realtimeSimpleStyle.js +23 -0
  58. package/common/typedefs.d.ts +111 -0
  59. package/common/typedefs.d.ts.map +1 -0
  60. package/common/typedefs.js +35 -6
  61. package/common/utils/compareDepartures.d.ts +11 -0
  62. package/common/utils/compareDepartures.d.ts.map +1 -0
  63. package/common/utils/compareDepartures.js +35 -0
  64. package/common/utils/createCanvas.d.ts +10 -0
  65. package/common/utils/createCanvas.d.ts.map +1 -0
  66. package/common/utils/createCanvas.js +27 -0
  67. package/common/utils/createRealtimeFilters.d.ts +13 -0
  68. package/common/utils/createRealtimeFilters.d.ts.map +1 -0
  69. package/common/utils/createRealtimeFilters.js +74 -0
  70. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  71. package/common/utils/debounceDeparturesMessages.d.ts.map +1 -0
  72. package/common/utils/debounceDeparturesMessages.js +24 -0
  73. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  74. package/common/utils/debounceWebsocketMessages.d.ts.map +1 -0
  75. package/common/utils/debounceWebsocketMessages.js +29 -0
  76. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  77. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  78. package/common/utils/getLayersAsFlatArray.js +15 -0
  79. package/common/utils/getMapboxMapCopyrights.d.ts +18 -0
  80. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  81. package/common/utils/getMapboxMapCopyrights.js +21 -23
  82. package/common/utils/getMapboxRender.d.ts +7 -0
  83. package/common/utils/getMapboxRender.d.ts.map +1 -0
  84. package/common/utils/getMapboxRender.js +87 -0
  85. package/common/utils/getMaplibreRender.d.ts +7 -0
  86. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  87. package/common/utils/getMaplibreRender.js +38 -0
  88. package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
  89. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  90. package/common/utils/getRealtimeModeSuffix.js +7 -0
  91. package/common/utils/getUrlWithParams.d.ts +9 -0
  92. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  93. package/common/utils/getUrlWithParams.js +18 -0
  94. package/common/utils/getVehiclePosition.d.ts +16 -0
  95. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  96. package/common/utils/getVehiclePosition.js +60 -60
  97. package/common/utils/index.d.ts +17 -0
  98. package/common/utils/index.d.ts.map +1 -0
  99. package/common/utils/index.js +13 -2
  100. package/common/utils/realtimeConfig.d.ts +49 -0
  101. package/common/utils/realtimeConfig.d.ts.map +1 -0
  102. package/common/utils/realtimeConfig.js +173 -0
  103. package/common/utils/removeDuplicate.d.ts +10 -0
  104. package/common/utils/removeDuplicate.d.ts.map +1 -0
  105. package/common/utils/removeDuplicate.js +5 -12
  106. package/common/utils/renderTrajectories.d.ts +17 -0
  107. package/common/utils/renderTrajectories.d.ts.map +1 -0
  108. package/common/utils/renderTrajectories.js +110 -0
  109. package/common/utils/sortAndFilterDepartures.d.ts +16 -0
  110. package/common/utils/sortAndFilterDepartures.d.ts.map +1 -0
  111. package/common/utils/sortAndFilterDepartures.js +58 -0
  112. package/common/utils/sortByDelay.d.ts +3 -0
  113. package/common/utils/sortByDelay.d.ts.map +1 -0
  114. package/common/utils/sortByDelay.js +17 -20
  115. package/common/utils/timeUtils.d.ts +24 -0
  116. package/common/utils/timeUtils.d.ts.map +1 -0
  117. package/common/utils/timeUtils.js +13 -18
  118. package/iife.d.ts +3 -0
  119. package/iife.d.ts.map +1 -0
  120. package/iife.js +5 -0
  121. package/index.d.ts +10 -0
  122. package/index.d.ts.map +1 -0
  123. package/index.js +10 -2
  124. package/mapbox/controls/CopyrightControl.d.ts +29 -0
  125. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  126. package/mapbox/controls/CopyrightControl.js +25 -35
  127. package/mapbox/controls/index.d.ts +2 -0
  128. package/mapbox/controls/index.d.ts.map +1 -0
  129. package/mapbox/controls/index.js +2 -0
  130. package/mapbox/index.d.ts +6 -0
  131. package/mapbox/index.d.ts.map +1 -0
  132. package/mapbox/index.js +5 -3
  133. package/mapbox/layers/Layer.d.ts +59 -0
  134. package/mapbox/layers/Layer.d.ts.map +1 -0
  135. package/mapbox/layers/Layer.js +78 -95
  136. package/mapbox/layers/RealtimeLayer.d.ts +181 -0
  137. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  138. package/mapbox/layers/RealtimeLayer.js +276 -0
  139. package/mapbox/layers/index.d.ts +3 -0
  140. package/mapbox/layers/index.d.ts.map +1 -0
  141. package/mapbox/layers/index.js +2 -0
  142. package/mapbox/utils/getMercatorResolution.d.ts +9 -0
  143. package/mapbox/utils/getMercatorResolution.d.ts.map +1 -0
  144. package/mapbox/utils/getMercatorResolution.js +18 -0
  145. package/mapbox/utils/getSourceCoordinates.d.ts +9 -0
  146. package/mapbox/utils/getSourceCoordinates.d.ts.map +1 -0
  147. package/mapbox/utils/getSourceCoordinates.js +27 -0
  148. package/mapbox/utils/index.d.ts +3 -0
  149. package/mapbox/utils/index.d.ts.map +1 -0
  150. package/mapbox/utils/index.js +2 -0
  151. package/mbt.js +60005 -0
  152. package/mbt.js.map +7 -0
  153. package/mbt.min.js +1084 -0
  154. package/mbt.min.js.map +7 -0
  155. package/ol/controls/CopyrightControl.d.ts +31 -0
  156. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  157. package/ol/controls/CopyrightControl.js +44 -56
  158. package/ol/controls/RoutingControl.d.ts +193 -0
  159. package/ol/controls/RoutingControl.d.ts.map +1 -0
  160. package/ol/controls/RoutingControl.js +546 -667
  161. package/ol/controls/StopFinderControl.d.ts +30 -0
  162. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  163. package/ol/controls/StopFinderControl.js +10 -16
  164. package/ol/controls/index.d.ts +4 -0
  165. package/ol/controls/index.d.ts.map +1 -0
  166. package/ol/controls/index.js +3 -0
  167. package/ol/index.d.ts +6 -0
  168. package/ol/index.d.ts.map +1 -0
  169. package/ol/index.js +4 -12
  170. package/ol/layers/Layer.d.ts +86 -0
  171. package/ol/layers/Layer.d.ts.map +1 -0
  172. package/ol/layers/Layer.js +147 -166
  173. package/ol/layers/MapGlLayer.d.ts +67 -0
  174. package/ol/layers/MapGlLayer.d.ts.map +1 -0
  175. package/ol/layers/MapGlLayer.js +218 -0
  176. package/ol/layers/MapboxLayer.d.ts +50 -0
  177. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  178. package/ol/layers/MapboxLayer.js +79 -348
  179. package/ol/layers/MapboxStyleLayer.d.ts +129 -0
  180. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  181. package/ol/layers/MapboxStyleLayer.js +329 -368
  182. package/ol/layers/MaplibreLayer.d.ts +28 -0
  183. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  184. package/ol/layers/MaplibreLayer.js +13 -259
  185. package/ol/layers/RealtimeLayer.d.ts +202 -0
  186. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  187. package/ol/layers/RealtimeLayer.js +340 -0
  188. package/ol/layers/RoutingLayer.d.ts +34 -0
  189. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  190. package/ol/layers/RoutingLayer.js +62 -68
  191. package/ol/layers/VectorLayer.d.ts +25 -0
  192. package/ol/layers/VectorLayer.d.ts.map +1 -0
  193. package/ol/layers/VectorLayer.js +27 -32
  194. package/ol/layers/WMSLayer.d.ts +42 -0
  195. package/ol/layers/WMSLayer.d.ts.map +1 -0
  196. package/ol/layers/WMSLayer.js +74 -66
  197. package/ol/layers/index.d.ts +9 -0
  198. package/ol/layers/index.d.ts.map +1 -0
  199. package/ol/layers/index.js +8 -0
  200. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  201. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  202. package/ol/styles/fullTrajectoryDelayStyle.js +23 -25
  203. package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
  204. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  205. package/ol/styles/fullTrajectoryStyle.js +37 -44
  206. package/ol/styles/index.d.ts +3 -0
  207. package/ol/styles/index.d.ts.map +1 -0
  208. package/package.json +98 -177
  209. package/setupTests.d.ts +2 -0
  210. package/setupTests.d.ts.map +1 -0
  211. package/setupTests.js +26 -0
  212. package/types/common.d.ts +122 -0
  213. package/types/index.d.ts +11 -0
  214. package/types/realtime.d.ts +320 -0
  215. package/types/routing.d.ts +206 -0
  216. package/types/stops.d.ts +143 -0
  217. package/README.md +0 -23
  218. package/api/routing/RoutingAPI.js +0 -44
  219. package/api/routing/RoutingAPI.test.js +0 -41
  220. package/api/stops/StopsAPI.js +0 -41
  221. package/api/stops/StopsAPI.test.js +0 -34
  222. package/api/tralis/TralisAPI.js +0 -731
  223. package/api/tralis/TralisAPI.test.js +0 -75
  224. package/api/tralis/TralisAPIUtils.js +0 -73
  225. package/api/tralis/WebSocketConnector.js +0 -338
  226. package/api/tralis/WebSocketConnector.test.js +0 -356
  227. package/common/Tracker.js +0 -197
  228. package/common/api/api.js +0 -64
  229. package/common/api/api.test.js +0 -68
  230. package/common/controls/Control.js +0 -146
  231. package/common/controls/Control.test.js +0 -98
  232. package/common/layers/Layer.js +0 -404
  233. package/common/layers/Layer.test.js +0 -585
  234. package/common/mixins/CopyrightMixin.js +0 -48
  235. package/common/mixins/SearchMixin.js +0 -176
  236. package/common/mixins/TralisLayerMixin.js +0 -930
  237. package/common/styles/trackerDefaultStyle.js +0 -333
  238. package/common/styles/trackerSimpleStyle.js +0 -22
  239. package/common/trackerConfig.js +0 -190
  240. package/common/trackerConfig.test.js +0 -25
  241. package/common/utils/createTrackerFilters.js +0 -87
  242. package/common/utils/createTrackerFilters.test.js +0 -95
  243. package/common/utils/getMapboxMapCopyrights.test.js +0 -47
  244. package/common/utils/getMapboxStyleUrl.js +0 -32
  245. package/common/utils/removeDuplicate.test.js +0 -22
  246. package/common/utils/timeUtils.test.js +0 -16
  247. package/index.js.map +0 -1
  248. package/mapbox/layers/Layer.test.js +0 -202
  249. package/mapbox/layers/TralisLayer.js +0 -329
  250. package/mapbox/layers/TralisLayer.test.js +0 -40
  251. package/mapbox/utils.js +0 -46
  252. package/module.js +0 -23
  253. package/ol/README.md +0 -0
  254. package/ol/controls/CopyrightControl.test.js +0 -211
  255. package/ol/controls/RoutingControl.test.js +0 -216
  256. package/ol/controls/StopFinderControl.test.js +0 -59
  257. package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  258. package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  259. package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  260. package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  261. package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  262. package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  263. package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
  264. package/ol/layers/Layer.test.js +0 -197
  265. package/ol/layers/MapboxLayer.test.js +0 -186
  266. package/ol/layers/MapboxStyleLayer.test.js +0 -262
  267. package/ol/layers/RoutingLayer.test.js +0 -49
  268. package/ol/layers/TralisLayer.js +0 -359
  269. package/ol/layers/TralisLayer.test.js +0 -97
  270. package/ol/layers/VectorLayer.test.js +0 -98
  271. package/ol/layers/WMSLayer.test.js +0 -84
@@ -0,0 +1,466 @@
1
+ import WebSocketAPI from '../common/api/WebSocketAPI';
2
+ import debounceWebsocketMessages from '../common/utils/debounceWebsocketMessages';
3
+ import getModeSuffix from '../common/utils/getRealtimeModeSuffix';
4
+ /**
5
+ * Enum for Realtime modes.
6
+ * @readonly
7
+ * @typedef {string} RealtimeMode
8
+ * @property {string} RAW "raw"
9
+ * @property {string} SCHEMATIC "schematic"
10
+ * @property {string} TOPOGRAPHIC "topographic"
11
+ * @enum {RealtimeMode}
12
+ */
13
+ export const RealtimeModes = {
14
+ RAW: 'raw',
15
+ TOPOGRAPHIC: 'topographic',
16
+ SCHEMATIC: 'schematic',
17
+ };
18
+ /**
19
+ * This class provides convenience methods to access to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
20
+ *
21
+ * @example
22
+ * import { RealtimeAPI } from 'mobility-toolbox-js/api';
23
+ *
24
+ * const api = new RealtimeAPI({
25
+ * url: "yourUrl",
26
+ * apiKey: "yourApiKey"
27
+ * });
28
+ *
29
+ * @example
30
+ * import { RealtimeAPI } from 'mobility-toolbox-js/api';
31
+ *
32
+ * const api = new RealtimeAPI("yourUrl");
33
+ */
34
+ class RealtimeAPI {
35
+ /**
36
+ * Constructor
37
+ *
38
+ * @param {Object|string} options A string representing the url of the service or an object containing the url and the apiKey.
39
+ * @param {string} options.url Url to the [geOps realtime api](https://developer.geops.io/apis/realtime/).
40
+ * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
41
+ * @param {string} [options.prefix=''] Service prefix to specify tenant.
42
+ * @param {string} [options.projection] The epsg code of the projection for features. Default to EPSG:3857.
43
+ * @param {number[4]} [options.bbox=[minX, minY, maxX, maxY, zoom, tenant] The bounding box to receive data from.
44
+ */
45
+ constructor(options = {}) {
46
+ this.defineProperties(options);
47
+ /** @ignore */
48
+ this.prefix = options.prefix || '';
49
+ /** @ignore */
50
+ this.onOpen = this.onOpen.bind(this);
51
+ }
52
+ /* @private */
53
+ defineProperties(options) {
54
+ let opt = options;
55
+ if (typeof options === 'string') {
56
+ opt = { url: options };
57
+ }
58
+ const { apiKey } = opt;
59
+ let { url, projection, bbox, buffer = [100, 100] } = opt;
60
+ const wsApi = new WebSocketAPI();
61
+ if (!url) {
62
+ url = 'wss://api.geops.io/tracker-ws/v1/';
63
+ }
64
+ if (apiKey) {
65
+ url = `${url}?key=${apiKey}`;
66
+ }
67
+ Object.defineProperties(this, {
68
+ url: {
69
+ get: () => url,
70
+ set: (newUrl) => {
71
+ if (url !== newUrl) {
72
+ url = newUrl;
73
+ // Update the websocket only if the url has changed and the websocket is already open or is opening.
74
+ if (this.wsApi.open || this.wsApi.connecting) {
75
+ this.open();
76
+ }
77
+ }
78
+ },
79
+ },
80
+ projection: {
81
+ get: () => projection,
82
+ set: (newProjection) => {
83
+ if (newProjection !== projection) {
84
+ projection = newProjection;
85
+ if (this.wsApi) {
86
+ this.wsApi.send(`PROJECTION ${projection}`);
87
+ }
88
+ }
89
+ },
90
+ },
91
+ bbox: {
92
+ get: () => bbox,
93
+ set: (newBbox) => {
94
+ if (JSON.stringify(newBbox) !== JSON.stringify(bbox)) {
95
+ bbox = newBbox;
96
+ if (this.wsApi && bbox) {
97
+ this.wsApi.send(`BBOX ${bbox.join(' ')}`);
98
+ }
99
+ }
100
+ },
101
+ },
102
+ buffer: {
103
+ get: () => buffer,
104
+ set: (newBuffer) => {
105
+ if (JSON.stringify(newBuffer) !== JSON.stringify(buffer)) {
106
+ buffer = newBuffer;
107
+ if (this.wsApi) {
108
+ this.wsApi.send(`BUFFER ${buffer.join(' ')}`);
109
+ }
110
+ }
111
+ },
112
+ },
113
+ /**
114
+ * The websocket helper class to connect the websocket.
115
+ *
116
+ * @private
117
+ */
118
+ wsApi: {
119
+ value: wsApi,
120
+ writable: true,
121
+ },
122
+ /**
123
+ * Interval between PING request in ms.
124
+ * If equal to 0, no PING request are sent.
125
+ * @type {number}
126
+ * @private
127
+ */
128
+ pingIntervalMs: {
129
+ value: options.pingIntervalMs || 10000,
130
+ writable: true,
131
+ },
132
+ /**
133
+ * Timeout in ms after an automatic reconnection when the websoscket has been closed by the server.
134
+ * @type {number}
135
+ */
136
+ reconnectTimeoutMs: {
137
+ value: options.pingIntervalMs || 100,
138
+ writable: true,
139
+ },
140
+ });
141
+ }
142
+ open() {
143
+ // Register BBOX and PROJECTION messages must be send before previous subscriptions.
144
+ this.wsApi.connect(this.url, this.onOpen);
145
+ // Register reconnection on close.
146
+ if (this.wsApi.websocket) {
147
+ this.wsApi.websocket.onclose = () => {
148
+ this.onClose();
149
+ };
150
+ }
151
+ }
152
+ /**
153
+ * Close the websocket connection without reconnection.
154
+ */
155
+ close() {
156
+ this.wsApi.close();
157
+ }
158
+ /**
159
+ * Unsubscribe trajectory and deleted_vehicles channels. To resubscribe you have to set a new BBOX.
160
+ */
161
+ // eslint-disable-next-line class-methods-use-this
162
+ reset() {
163
+ this.wsApi.send('RESET');
164
+ }
165
+ /**
166
+ * Callback when the websocket is opened and ready.
167
+ * It applies the bbox and the projection.
168
+ */
169
+ onOpen() {
170
+ if (this.projection) {
171
+ this.wsApi.send(`PROJECTION ${this.projection}`);
172
+ }
173
+ if (this.bbox) {
174
+ this.wsApi.send(`BBOX ${this.bbox.join(' ')}`);
175
+ }
176
+ if (this.buffer) {
177
+ this.wsApi.send(`BUFFER ${this.buffer.join(' ')}`);
178
+ }
179
+ /**
180
+ * Keep websocket alive
181
+ */
182
+ if (this.pingIntervalMs) {
183
+ window.clearInterval(this.pingInterval);
184
+ /** @ignore */
185
+ this.pingInterval = window.setInterval(() => {
186
+ this.wsApi.send('PING');
187
+ }, this.pingIntervalMs);
188
+ }
189
+ }
190
+ /**
191
+ * Callback when the websocket is closed by the server.
192
+ * It auto reconnects after a timeout.
193
+ */
194
+ onClose() {
195
+ window.clearTimeout(this.pingInterval);
196
+ window.clearTimeout(this.reconnectTimeout);
197
+ if (this.reconnectTimeoutMs) {
198
+ /** @ignore */
199
+ this.reconnectTimeout = window.setTimeout(() => this.open(), this.reconnectTimeoutMs);
200
+ }
201
+ }
202
+ /**
203
+ * Subscribe to a channel.
204
+ *
205
+ * @param {string} channel Name of the websocket channel to subscribe.
206
+ * @param {function} onSuccess Callback when the subscription succeeds.
207
+ * @param {function} onError Callback when the subscription fails.
208
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
209
+ * @private
210
+ */
211
+ subscribe(channel, onSuccess, onError = () => { }, quiet = false) {
212
+ if (!channel || !onSuccess) {
213
+ return;
214
+ }
215
+ this.wsApi.subscribe({ channel }, onSuccess, onError, quiet);
216
+ }
217
+ /**
218
+ * Unsubscribe both modes of a channel.
219
+ *
220
+ * @param {string} channel Name of the websocket channel to unsubscribe.
221
+ * @param {string} suffix Suffix to add to the channel name.
222
+ * @param {function} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
223
+ * @private
224
+ */
225
+ unsubscribe(channel, suffix = '', onMessage) {
226
+ const suffixSchenatic = getModeSuffix(RealtimeModes.SCHEMATIC, RealtimeModes);
227
+ const suffixTopographic = getModeSuffix(RealtimeModes.TOPOGRAPHIC, RealtimeModes);
228
+ this.wsApi.unsubscribe(`${channel}${suffixSchenatic}${suffix || ''}`, onMessage);
229
+ this.wsApi.unsubscribe(`${channel}${suffixTopographic}${suffix || ''}`, onMessage);
230
+ }
231
+ /**
232
+ * Subscribe to departures channel of a given station.
233
+ *
234
+ * @param {number} stationId UIC of the station.
235
+ * @param {Boolean} sortByMinArrivalTime Sort by minimum arrival time
236
+ * @param {function(departures:Departure[])} onMessage Function called on each message of the channel.
237
+ * @param {function} onError Callback when the subscription fails.
238
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
239
+ */
240
+ subscribeDepartures(stationId, onMessage, onError = () => { }, quiet = false) {
241
+ this.subscribe(`timetable_${stationId}`, onMessage, onError, quiet);
242
+ }
243
+ /**
244
+ * Unsubscribe from current departures channel.
245
+ * @param {RealtimeStationId} id Station's id
246
+ * @param {function(data: { content: RealtimeDeparture[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
247
+ */
248
+ unsubscribeDepartures(id, onMessage) {
249
+ this.unsubscribe(`timetable_${id}`, '', onMessage);
250
+ }
251
+ /**
252
+ * Subscribe to the disruptions channel for tenant.
253
+ *
254
+ * @param {function(data: { content: RealtimeNews[] })} onMessage Function called on each message of the channel.
255
+ * @param {function} onError Callback when the subscription fails.
256
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
257
+ */
258
+ subscribeDisruptions(onMessage, onError = () => { }, quiet = false) {
259
+ this.subscribe(`${this.prefix}newsticker`, onMessage, onError, quiet);
260
+ }
261
+ /**
262
+ * Unsubscribe disruptions.
263
+ *
264
+ * @param {function(data: { content: RealtimeNews[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
265
+ */
266
+ unsubscribeDisruptions(onMessage) {
267
+ this.unsubscribe(`${this.prefix}newsticker`, '', onMessage);
268
+ }
269
+ /**
270
+ * Return a station with a given uic number and a mode.
271
+ *
272
+ * @param {number} uic UIC of the station.
273
+ * @param {RealtimeMode} mode Realtime mode.
274
+ * @return {Promise<{data: { content: RealtimeStation }}>} A station.
275
+ */
276
+ getStation(uic, mode) {
277
+ const params = {
278
+ channel: `station${getModeSuffix(mode, RealtimeModes)}`,
279
+ args: uic,
280
+ };
281
+ return new Promise((resolve, reject) => {
282
+ this.wsApi.get(params, resolve, reject);
283
+ });
284
+ }
285
+ /**
286
+ * Get the list of ststions available for a specifc mode. The promise is resolved every 100ms
287
+ * @param {RealtimeMode} mode Realtime mode.
288
+ * @param {number} timeout = 100 Duration in ms between each promise resolve calls.
289
+ * @return {Promise<RealtimeStation[]>} An array of stations.
290
+ */
291
+ getStations(mode, timeout = 100) {
292
+ return new Promise((resolve) => {
293
+ this.wsApi.get({
294
+ channel: `station${getModeSuffix(mode, RealtimeModes)}`,
295
+ }, debounceWebsocketMessages(resolve, undefined, timeout));
296
+ });
297
+ }
298
+ /**
299
+ * Subscribe to stations channel.
300
+ * One message pro station.
301
+ *
302
+ * @param {RealtimeMode} mode Realtime mode.
303
+ * @param {function(data: { content: RealtimeStation })} onMessage Function called on each message of the channel.
304
+ * @param {function} onError Callback when the subscription fails.
305
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
306
+ */
307
+ subscribeStations(mode, onMessage, onError = () => { }, quiet = false) {
308
+ this.subscribe(`station${getModeSuffix(mode, RealtimeModes)}`, onMessage, onError, quiet);
309
+ }
310
+ /**
311
+ * Unsubscribe to stations channel.
312
+ * @param {function(data: { content: RealtimeStation })} onMessage The listener callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribe.
313
+ */
314
+ unsubscribeStations(onMessage) {
315
+ this.unsubscribe('station', '', onMessage);
316
+ }
317
+ /**
318
+ * Subscribe to extra_geoms channel.
319
+ *
320
+ * @param {function(data: { content: RealtimeExtraGeom })} onMessage Function called on each message of the channel.
321
+ * @param {function} onError Callback when the subscription fails.
322
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
323
+ */
324
+ subscribeExtraGeoms(onMessage, onError = () => { }, quiet = false) {
325
+ this.subscribe('extra_geoms', onMessage, onError, quiet);
326
+ }
327
+ /**
328
+ * Unsubscribe to extra_geoms channel.
329
+ * @param {function(data: { content: RealtimeExtraGeom })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
330
+ */
331
+ unsubscribeExtraGeoms(onMessage) {
332
+ this.unsubscribe('extra_geoms', '', onMessage);
333
+ }
334
+ /**
335
+ * Subscribe to trajectory channel.
336
+ *
337
+ * @param {RealtimeMode} mode Realtime mode.
338
+ * @param {function(data: { content: RealtimeTrajectoryResponse[] })} 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
+ */
342
+ subscribeTrajectory(mode, onMessage, onError = () => { }, quiet = false) {
343
+ this.unsubscribeTrajectory(onMessage);
344
+ this.subscribe(`trajectory${getModeSuffix(mode, RealtimeModes)}`, onMessage, onError, quiet);
345
+ }
346
+ /**
347
+ * Unsubscribe to trajectory channels.
348
+ * @param {function(data: { content: RealtimeTrajectoryResponse[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
349
+ */
350
+ unsubscribeTrajectory(onMessage) {
351
+ this.unsubscribe(`trajectory`, '', onMessage);
352
+ }
353
+ /**
354
+ * Subscribe to deleted_vhicles channel.
355
+ *
356
+ * @param {RealtimeMode} mode Realtime mode.
357
+ * @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
358
+ * @param {function} onError Callback when the subscription fails.
359
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
360
+ */
361
+ subscribeDeletedVehicles(mode, onMessage, onError = () => { }, quiet = false) {
362
+ this.unsubscribeDeletedVehicles(onMessage);
363
+ this.subscribe(`deleted_vehicles${getModeSuffix(mode, RealtimeModes)}`, onMessage, onError, quiet);
364
+ }
365
+ /**
366
+ * Unsubscribe to deleted_vhicles channels.
367
+ * @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
368
+ */
369
+ unsubscribeDeletedVehicles(onMessage) {
370
+ this.unsubscribe('deleted_vehicles', '', onMessage);
371
+ }
372
+ /**
373
+ * Get a full trajectory of a vehicule .
374
+ *
375
+ * @param {string} id A vehicle id.
376
+ * @param {RealtimeMode} mode Realtime mode.
377
+ * @param {string} generalizationLevel The generalization level to request. Can be one of 5 (more generalized), 10, 30, 100, undefined (less generalized).
378
+ * @return {Promise<{ data: { content: FullTrajectory } }>} Return a full trajectory.
379
+ */
380
+ getFullTrajectory(id, mode, generalizationLevel) {
381
+ const channel = [`full_trajectory${getModeSuffix(mode, RealtimeModes)}`];
382
+ if (id) {
383
+ channel.push(id);
384
+ }
385
+ if ((!mode || mode === RealtimeModes.TOPOGRAPHIC) && generalizationLevel) {
386
+ channel.push(`gen${generalizationLevel}`);
387
+ }
388
+ const params = {
389
+ channel: channel.join('_'),
390
+ };
391
+ return new Promise((resolve, reject) => {
392
+ this.wsApi.get(params, resolve, reject);
393
+ });
394
+ }
395
+ /**
396
+ * Subscribe to full_trajectory channel of a given vehicle.
397
+ *
398
+ * @param {string} id A vehicle id.
399
+ * @param {RealtimeMode} mode Realtime mode.
400
+ * @param {function(data: { content: RealtimeFullTrajectory })} onMessage Function called on each message of the channel.
401
+ * @param {function} onError Callback when the subscription fails.
402
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
403
+ */
404
+ subscribeFullTrajectory(id, mode, onMessage, onError = () => { }, quiet = false) {
405
+ this.subscribe(`full_trajectory${getModeSuffix(mode, RealtimeModes)}_${id}`, onMessage, onError, quiet);
406
+ }
407
+ /**
408
+ * Unsubscribe from full_trajectory channel
409
+ *
410
+ * @param {string} id A vehicle id.
411
+ * @param {function(data: { content: RealtimeFullTrajectory })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
412
+ */
413
+ unsubscribeFullTrajectory(id, onMessage) {
414
+ this.unsubscribe('full_trajectory', `_${id}`, onMessage);
415
+ }
416
+ /**
417
+ * Get the list of stops for this vehicle.
418
+ *
419
+ * @param {string} id A vehicle id.
420
+ * @return {Promise<{ data: { content: StopSequence[] } }>} Returns a stop sequence object.
421
+ */
422
+ getStopSequence(id) {
423
+ return new Promise((resolve, reject) => {
424
+ this.wsApi.get({
425
+ channel: `stopsequence_${id}`,
426
+ }, resolve, reject);
427
+ });
428
+ }
429
+ /**
430
+ * Subscribe to stopsequence channel of a given vehicle.
431
+ *
432
+ * @param {string} id A vehicle id.
433
+ * @param {function(data: { content: StopSequence[] })} onMessage Function called on each message of the channel.
434
+ * @param {function} onError Callback when the subscription fails.
435
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
436
+ */
437
+ subscribeStopSequence(id, onMessage, onError = () => { }, quiet = false) {
438
+ this.subscribe(`stopsequence_${id}`, onMessage, onError, quiet);
439
+ }
440
+ /**
441
+ * Unsubscribe from stopsequence channel
442
+ *
443
+ * @param {string} id A vehicle id.
444
+ * @param {function(data: { content: StopSequence[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
445
+ */
446
+ unsubscribeStopSequence(id, onMessage) {
447
+ this.unsubscribe(`stopsequence`, `_${id}`, onMessage);
448
+ }
449
+ /**
450
+ * Subscribe to healthcheck channel.
451
+ * @param {function(data: { content: string })} onMessage Callback when the subscribe to healthcheck channel succeeds.
452
+ * @param {function} onError Callback when the subscription fails.
453
+ * @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
454
+ */
455
+ subscribeHealthCheck(onMessage, onError = () => { }, quiet = false) {
456
+ this.subscribe('healthcheck', onMessage, onError, quiet);
457
+ }
458
+ /**
459
+ * Unsubscribe to healthcheck channel.
460
+ * @param {function(data: { content: string })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
461
+ */
462
+ unsubscribeHealthCheck(onMessage) {
463
+ this.unsubscribe('healthcheck', '', onMessage);
464
+ }
465
+ }
466
+ export default RealtimeAPI;
@@ -0,0 +1,37 @@
1
+ import HttpAPI from '../common/api/HttpAPI';
2
+ import { RoutingParameters, RoutingResponse } from '../types';
3
+ export type RoutingAPIOptions = {
4
+ url?: string;
5
+ apiKey?: string;
6
+ };
7
+ /**
8
+ * Access to the [geOps Routing service](https://developer.geops.io/apis/routing).
9
+ *
10
+ * @example
11
+ * import { RoutingAPI } from 'mobility-toolbox-js';
12
+ *
13
+ * const api = new RoutingAPI({
14
+ * apiKey: [yourApiKey]
15
+ * });
16
+ *
17
+ */
18
+ declare class RoutingAPI extends HttpAPI {
19
+ /**
20
+ * Constructor
21
+ *
22
+ * @param {RoutingAPIOptions} options Options.
23
+ * @param {string} [options.url='https://api.geops.io/routing/v1/'] Service url.
24
+ * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
25
+ */
26
+ constructor(options?: RoutingAPIOptions);
27
+ /**
28
+ * Route.
29
+ *
30
+ * @param {RoutingParameters} params Request parameters. See [Routing service documentation](https://developer.geops.io/apis/routing/).
31
+ * @param {RequestInit} config Options for the fetch request.
32
+ * @return {Promise<RoutingResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326).
33
+ */
34
+ route(params: RoutingParameters, config: RequestInit): Promise<RoutingResponse>;
35
+ }
36
+ export default RoutingAPI;
37
+ //# sourceMappingURL=RoutingAPI.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RoutingAPI.d.ts","sourceRoot":"","sources":["../../src/api/RoutingAPI.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE9D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;;;;;;GAUG;AACH,cAAM,UAAW,SAAQ,OAAO;IAC9B;;;;;;OAMG;gBACS,OAAO,GAAE,iBAAsB;IAI3C;;;;;;OAMG;IACH,KAAK,CACH,MAAM,EAAE,iBAAiB,EACzB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,eAAe,CAAC;CAG5B;AAED,eAAe,UAAU,CAAC"}
@@ -0,0 +1,35 @@
1
+ import HttpAPI from '../common/api/HttpAPI';
2
+ /**
3
+ * Access to the [geOps Routing service](https://developer.geops.io/apis/routing).
4
+ *
5
+ * @example
6
+ * import { RoutingAPI } from 'mobility-toolbox-js';
7
+ *
8
+ * const api = new RoutingAPI({
9
+ * apiKey: [yourApiKey]
10
+ * });
11
+ *
12
+ */
13
+ class RoutingAPI extends HttpAPI {
14
+ /**
15
+ * Constructor
16
+ *
17
+ * @param {RoutingAPIOptions} options Options.
18
+ * @param {string} [options.url='https://api.geops.io/routing/v1/'] Service url.
19
+ * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
20
+ */
21
+ constructor(options = {}) {
22
+ super(Object.assign({ url: 'https://api.geops.io/routing/v1/' }, options));
23
+ }
24
+ /**
25
+ * Route.
26
+ *
27
+ * @param {RoutingParameters} params Request parameters. See [Routing service documentation](https://developer.geops.io/apis/routing/).
28
+ * @param {RequestInit} config Options for the fetch request.
29
+ * @return {Promise<RoutingResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326).
30
+ */
31
+ route(params, config) {
32
+ return this.fetch('', params, config);
33
+ }
34
+ }
35
+ export default RoutingAPI;
@@ -0,0 +1,38 @@
1
+ import HttpAPI from '../common/api/HttpAPI';
2
+ import { StopsParameters, StopsResponse } from '../types';
3
+ export type StopsAPIOptions = {
4
+ url?: string;
5
+ apiKey?: string;
6
+ };
7
+ /**
8
+ * Access to the [Stops service](https://developer.geops.io/apis/5dcbd702a256d90001cf1361/).
9
+ *
10
+ * @example
11
+ * import { StopsAPI } from 'mobility-toolbox-js/api';
12
+ *
13
+ * const api = new StopsAPI({
14
+ * url: 'https://api.geops.io/stops/v1/',
15
+ * apiKey: [yourApiKey]
16
+ * });
17
+ *
18
+ */
19
+ declare class StopsAPI extends HttpAPI {
20
+ /**
21
+ * Constructor
22
+ *
23
+ * @param {StopsAPIOptions} options Options.
24
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] Service url.
25
+ * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
26
+ */
27
+ constructor(options?: StopsAPIOptions);
28
+ /**
29
+ * Search fo stops.
30
+ *
31
+ * @param {StopsParameters} params Request parameters. See [Stops service documentation](https://developer.geops.io/apis/stops).
32
+ * @param {RequestInit} config Options for the fetch request.
33
+ * @return {Promise<StopsResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326).
34
+ */
35
+ search(params: StopsParameters, config: RequestInit): Promise<StopsResponse>;
36
+ }
37
+ export default StopsAPI;
38
+ //# sourceMappingURL=StopsAPI.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StopsAPI.d.ts","sourceRoot":"","sources":["../../src/api/StopsAPI.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE1D,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,cAAM,QAAS,SAAQ,OAAO;IAC5B;;;;;;OAMG;gBACS,OAAO,GAAE,eAAoB;IAIzC;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC;CAG7E;AAED,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,36 @@
1
+ import HttpAPI from '../common/api/HttpAPI';
2
+ /**
3
+ * Access to the [Stops service](https://developer.geops.io/apis/5dcbd702a256d90001cf1361/).
4
+ *
5
+ * @example
6
+ * import { StopsAPI } from 'mobility-toolbox-js/api';
7
+ *
8
+ * const api = new StopsAPI({
9
+ * url: 'https://api.geops.io/stops/v1/',
10
+ * apiKey: [yourApiKey]
11
+ * });
12
+ *
13
+ */
14
+ class StopsAPI extends HttpAPI {
15
+ /**
16
+ * Constructor
17
+ *
18
+ * @param {StopsAPIOptions} options Options.
19
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] Service url.
20
+ * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
21
+ */
22
+ constructor(options = {}) {
23
+ super(Object.assign({ url: 'https://api.geops.io/stops/v1/' }, options));
24
+ }
25
+ /**
26
+ * Search fo stops.
27
+ *
28
+ * @param {StopsParameters} params Request parameters. See [Stops service documentation](https://developer.geops.io/apis/stops).
29
+ * @param {RequestInit} config Options for the fetch request.
30
+ * @return {Promise<StopsResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326).
31
+ */
32
+ search(params, config) {
33
+ return this.fetch('', params, config);
34
+ }
35
+ }
36
+ export default StopsAPI;
package/api/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { default as RoutingAPI } from "./RoutingAPI";
2
+ export { default as StopsAPI } from "./StopsAPI";
3
+ export { default as RealtimeAPI, RealtimeModes } from "./RealtimeAPI";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.js"],"names":[],"mappings":""}
package/api/index.js CHANGED
@@ -1,3 +1,3 @@
1
- // eslint-disable-next-line import/prefer-default-export
2
- export { default as StopsAPI } from './stops/StopsAPI';
3
- export { default as TralisAPI, TralisModes } from './tralis/TralisAPI';
1
+ export { default as RoutingAPI } from './RoutingAPI';
2
+ export { default as StopsAPI } from './StopsAPI';
3
+ export { default as RealtimeAPI, RealtimeModes } from './RealtimeAPI';