mobility-toolbox-js 3.0.0-beta.0 → 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 (92) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -0
  3. package/api/HttpAPI.d.ts +20 -0
  4. package/api/HttpAPI.js +1 -11
  5. package/api/RealtimeAPI.d.ts +365 -0
  6. package/api/RealtimeAPI.js +102 -89
  7. package/api/RoutingAPI.d.ts +37 -0
  8. package/api/StopsAPI.d.ts +44 -0
  9. package/api/StopsAPI.js +15 -9
  10. package/api/WebSocketAPI.d.ts +153 -0
  11. package/api/index.d.ts +3 -0
  12. package/api/typedefs.d.ts +178 -0
  13. package/common/controls/StopFinderControlCommon.d.ts +53 -0
  14. package/common/index.d.ts +2 -0
  15. package/common/mixins/RealtimeLayerMixin.d.ts +273 -0
  16. package/common/styles/index.d.ts +4 -0
  17. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  18. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  19. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  20. package/common/styles/realtimeSimpleStyle.d.ts +4 -0
  21. package/common/typedefs.d.ts +212 -0
  22. package/common/utils/compareDepartures.d.ts +10 -0
  23. package/common/utils/constants.d.ts +5 -0
  24. package/common/utils/createCanvas.d.ts +10 -0
  25. package/common/utils/createDefaultCopyrightElt.d.ts +5 -0
  26. package/common/utils/createDefaultStopFinderElt.d.ts +5 -0
  27. package/common/utils/createRealtimeFilters.d.ts +12 -0
  28. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  29. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  30. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  31. package/common/utils/getLayersAsFlatArray.js +5 -1
  32. package/common/utils/getMapGlCopyrights.d.ts +17 -0
  33. package/common/utils/getRealtimeModeSuffix.d.ts +9 -0
  34. package/common/utils/getUrlWithParams.d.ts +8 -0
  35. package/common/utils/getVehiclePosition.d.ts +16 -0
  36. package/common/utils/index.d.ts +16 -0
  37. package/common/utils/realtimeConfig.d.ts +64 -0
  38. package/common/utils/removeDuplicate.d.ts +9 -0
  39. package/common/utils/renderTrajectories.d.ts +16 -0
  40. package/common/utils/sortAndFilterDepartures.d.ts +15 -0
  41. package/common/utils/sortByDelay.d.ts +3 -0
  42. package/common/utils/timeUtils.d.ts +23 -0
  43. package/common/utils/toMercatorExtent.d.ts +5 -0
  44. package/iife.d.ts +2 -0
  45. package/index.d.ts +9 -0
  46. package/maplibre/controls/CopyrightControl.d.ts +35 -0
  47. package/maplibre/controls/index.d.ts +1 -0
  48. package/maplibre/index.d.ts +5 -0
  49. package/maplibre/layers/Layer.d.ts +28 -0
  50. package/maplibre/layers/RealtimeLayer.d.ts +160 -0
  51. package/maplibre/layers/RealtimeLayer.js +2 -2
  52. package/maplibre/layers/index.d.ts +2 -0
  53. package/maplibre/utils/getMercatorResolution.d.ts +7 -0
  54. package/maplibre/utils/getSourceCoordinates.d.ts +7 -0
  55. package/maplibre/utils/index.d.ts +2 -0
  56. package/mbt.js +1395 -1293
  57. package/mbt.js.map +4 -4
  58. package/mbt.min.js +42 -42
  59. package/mbt.min.js.map +4 -4
  60. package/ol/controls/CopyrightControl.d.ts +31 -0
  61. package/ol/controls/CopyrightControl.js +18 -8
  62. package/ol/controls/RoutingControl.d.ts +209 -0
  63. package/ol/controls/RoutingControl.js +3 -0
  64. package/ol/controls/StopFinderControl.d.ts +37 -0
  65. package/ol/controls/StopFinderControl.js +3 -0
  66. package/ol/controls/index.d.ts +3 -0
  67. package/ol/index.d.ts +6 -0
  68. package/ol/layers/MapGlLayer.d.ts +144 -0
  69. package/ol/layers/MapGlLayer.js +7 -7
  70. package/ol/layers/MaplibreLayer.d.ts +63 -0
  71. package/ol/layers/MaplibreStyleLayer.d.ts +235 -0
  72. package/ol/layers/MaplibreStyleLayer.js +24 -24
  73. package/ol/layers/RealtimeLayer.d.ts +285 -0
  74. package/ol/layers/RealtimeLayer.js +13 -7
  75. package/ol/layers/index.d.ts +3 -0
  76. package/ol/mixins/MobilityLayerMixin.d.ts +98 -0
  77. package/ol/mixins/MobilityLayerMixin.js +1 -4
  78. package/ol/mixins/PropertiesLayerMixin.d.ts +127 -0
  79. package/ol/mixins/PropertiesLayerMixin.js +6 -65
  80. package/ol/renderers/MaplibreLayerRenderer.d.ts +20 -0
  81. package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +20 -0
  82. package/ol/renderers/RealtimeLayerRenderer.d.ts +22 -0
  83. package/ol/renderers/RealtimeLayerRenderer.js +9 -9
  84. package/ol/styles/fullTrajectoryDelayStyle.d.ts +6 -0
  85. package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
  86. package/ol/styles/index.d.ts +3 -0
  87. package/ol/styles/routingStyle.d.ts +4 -0
  88. package/ol/utils/getFeatureInfoAtCoordinate.d.ts +8 -0
  89. package/ol/utils/getFeatureInfoAtCoordinate.js +2 -2
  90. package/ol/utils/index.d.ts +1 -0
  91. package/package.json +19 -20
  92. package/setupTests.d.ts +1 -0
@@ -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
+ *  \
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
+ *  
66
+ * - **minY**: a string representation of an integer (not a float) representing the minimal Y coordinate (in EPSG:3857) of a bounding box\
67
+ *  
68
+ * - **maxX**: a string representation of an integer (not a float) representing the maximal X coordinate (in EPSG:3857) of a bounding box\
69
+ *  
70
+ * - **maxY**: a string representation of an integer (not a float) representing the maximal Y coordinate (in EPSG:3857) of a bounding box\
71
+ *  
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
  }
@@ -0,0 +1,37 @@
1
+ import HttpAPI from './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 API](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
+ * @public
18
+ */
19
+ declare class RoutingAPI extends HttpAPI {
20
+ /**
21
+ * Constructor
22
+ *
23
+ * @param {RoutingAPIOptions} options Options.
24
+ * @param {string} [options.url='https://api.geops.io/routing/v1/'] Service url.
25
+ * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
26
+ */
27
+ constructor(options?: RoutingAPIOptions);
28
+ /**
29
+ * Route.
30
+ *
31
+ * @param {RoutingParameters} params Request parameters. See [Routing service documentation](https://developer.geops.io/apis/routing/).
32
+ * @param {RequestInit} config Options for the fetch request.
33
+ * @return {Promise<RoutingResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326).
34
+ */
35
+ route(params: RoutingParameters, config: RequestInit): Promise<RoutingResponse>;
36
+ }
37
+ export default RoutingAPI;
@@ -0,0 +1,44 @@
1
+ import HttpAPI from './HttpAPI';
2
+ import { StopsParameters, StopsResponse } from '../types';
3
+ export type StopsAPIOptions = {
4
+ url?: string;
5
+ apiKey?: string;
6
+ };
7
+ /**
8
+ * This class provides convenience methods to use to the [geOps Stops api](https://developer.geops.io/apis/stops/).
9
+ *
10
+ * @example
11
+ * import { StopsAPI } from 'mobility-toolbox-js/api';
12
+ *
13
+ * const api = new StopsAPI({
14
+ * apiKey: [yourApiKey],
15
+ * // url: 'https://api.geops.io/stops/v1/',
16
+ * });
17
+ *
18
+ * const stops = await api.search({ q:"Bern" });
19
+ *
20
+ * console.log('Log stops:', JSON.stringify(stops));
21
+ *
22
+ * @public
23
+ */
24
+ declare class StopsAPI extends HttpAPI {
25
+ /**
26
+ * Constructor
27
+ *
28
+ * @param {Object} options Options.
29
+ * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
30
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] Url of the [geOps stops API](https://developer.geops.io/apis/stops/).
31
+ * @public
32
+ */
33
+ constructor(options?: StopsAPIOptions);
34
+ /**
35
+ * Search fo stops.
36
+ *
37
+ * @param {StopsAPIParameters} params Request parameters. See [Stops API documentation](https://developer.geops.io/apis/stops).
38
+ * @param {FetchOptions} config Options for the fetch request.
39
+ * @returns {Promise<StopsAPIResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326). See [Stops API documentation](https://developer.geops.io/apis/stops).
40
+ * @public
41
+ */
42
+ search(params: StopsParameters, config: RequestInit): Promise<StopsResponse>;
43
+ }
44
+ export default StopsAPI;
package/api/StopsAPI.js CHANGED
@@ -1,24 +1,29 @@
1
1
  import HttpAPI from './HttpAPI';
2
2
  /**
3
- * Access to the [geOps Stops api](https://developer.geops.io/apis/5dcbd702a256d90001cf1361/).
3
+ * This class provides convenience methods to use to the [geOps Stops api](https://developer.geops.io/apis/stops/).
4
4
  *
5
5
  * @example
6
6
  * import { StopsAPI } from 'mobility-toolbox-js/api';
7
7
  *
8
8
  * const api = new StopsAPI({
9
- * url: 'https://api.geops.io/stops/v1/',
10
- * apiKey: [yourApiKey]
9
+ * apiKey: [yourApiKey],
10
+ * // url: 'https://api.geops.io/stops/v1/',
11
11
  * });
12
12
  *
13
+ * const stops = await api.search({ q:"Bern" });
14
+ *
15
+ * console.log('Log stops:', JSON.stringify(stops));
16
+ *
13
17
  * @public
14
18
  */
15
19
  class StopsAPI extends HttpAPI {
16
20
  /**
17
21
  * Constructor
18
22
  *
19
- * @param {StopsAPIOptions} options Options.
20
- * @param {string} [options.url='https://api.geops.io/stops/v1/'] Service url.
21
- * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
23
+ * @param {Object} options Options.
24
+ * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
25
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] Url of the [geOps stops API](https://developer.geops.io/apis/stops/).
26
+ * @public
22
27
  */
23
28
  constructor(options = {}) {
24
29
  super(Object.assign({ url: 'https://api.geops.io/stops/v1/' }, options));
@@ -26,9 +31,10 @@ class StopsAPI extends HttpAPI {
26
31
  /**
27
32
  * Search fo stops.
28
33
  *
29
- * @param {StopsParameters} params Request parameters. See [Stops service documentation](https://developer.geops.io/apis/stops).
30
- * @param {RequestInit} config Options for the fetch request.
31
- * @return {Promise<StopsResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326).
34
+ * @param {StopsAPIParameters} params Request parameters. See [Stops API documentation](https://developer.geops.io/apis/stops).
35
+ * @param {FetchOptions} config Options for the fetch request.
36
+ * @returns {Promise<StopsAPIResponse>} An GeoJSON feature collection with coordinates in [EPSG:4326](http://epsg.io/4326). See [Stops API documentation](https://developer.geops.io/apis/stops).
37
+ * @public
32
38
  */
33
39
  search(params, config) {
34
40
  return this.fetch('', params, config);
@@ -0,0 +1,153 @@
1
+ import { RealtimeTrajectoryResponse } from '../types';
2
+ export declare type WebSocketAPIParameters = {
3
+ channel?: string;
4
+ args?: string | number;
5
+ id?: string | number;
6
+ };
7
+ export declare type WebSocketAPIMessageEventData<T> = {
8
+ timestamp: number;
9
+ source: string;
10
+ content: T;
11
+ client_reference: string | number | null;
12
+ };
13
+ export type WebSocketAPIBufferMessageEventData = WebSocketAPIMessageEventData<RealtimeTrajectoryResponse[]> & {
14
+ source: 'buffer';
15
+ };
16
+ export type WebSocketAPIMessageEvent = Event & {
17
+ data: string;
18
+ };
19
+ export interface WebSocketAPIMessageEventListener {
20
+ (evt: WebSocketAPIMessageEvent): void;
21
+ }
22
+ /**
23
+ * This type represents a function that has been call with each feature returned by the websocket.
24
+ */
25
+ export interface WebSocketAPIMessageCallback<T> {
26
+ (data: WebSocketAPIMessageEventData<T>): void;
27
+ }
28
+ export declare type WebSocketAPISubscription = {
29
+ params: WebSocketAPIParameters;
30
+ cb: WebSocketAPIMessageCallback<any>;
31
+ errorCb?: EventListener;
32
+ onMessageCb: WebSocketAPIMessageEventListener;
33
+ onErrorCb?: EventListener;
34
+ quiet: boolean;
35
+ };
36
+ export declare type WebSocketAPISubscribed = {
37
+ [index: string]: boolean;
38
+ };
39
+ export declare type WebSocketAPIRequest = {
40
+ params: WebSocketAPIParameters;
41
+ cb: WebSocketAPIMessageCallback<any>;
42
+ errorCb?: EventListener;
43
+ onMessageCb: WebSocketAPIMessageEventListener;
44
+ onErrorCb?: EventListener;
45
+ requestString: String;
46
+ };
47
+ /**
48
+ * Class used to facilitate connection to a WebSocketAPI and
49
+ * also to manage properly messages send to the WebSocketAPI.
50
+ * This class must not contain any specific implementation.
51
+ * @private
52
+ */
53
+ declare class WebSocketAPI {
54
+ websocket?: WebSocket;
55
+ closed?: boolean;
56
+ closing?: boolean;
57
+ connecting?: boolean;
58
+ open?: boolean;
59
+ messagesOnOpen: Array<string>;
60
+ subscriptions: Array<WebSocketAPISubscription>;
61
+ subscribed: WebSocketAPISubscribed;
62
+ requests: Array<WebSocketAPIRequest>;
63
+ constructor();
64
+ defineProperties(): void;
65
+ /**
66
+ * Get the websocket request string.
67
+ *
68
+ * @param {string} method Request mehtod {GET, SUB}.
69
+ * @param {WebSocketParameters} params Request parameters.
70
+ * @param {string} params.channel Channel name
71
+ * @param {string} [params.args] Request arguments
72
+ * @param {Number|string} [params.id] Request identifier
73
+ * @return {string} request string
74
+ * @private
75
+ */
76
+ static getRequestString(method: string, params?: WebSocketAPIParameters): string;
77
+ /**
78
+ * (Re)connect the websocket.
79
+ *
80
+ * @param {string} url Websocket url.
81
+ * @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
82
+ * @private
83
+ */
84
+ connect(url: string, onOpen?: () => void): void;
85
+ /**
86
+ * Close the websocket definitively.
87
+ *
88
+ * @private
89
+ */
90
+ close(): void;
91
+ /**
92
+ * Sends a message to the websocket.
93
+ *
94
+ * @param {message} message Message to send.
95
+ * @private
96
+ */
97
+ send(message: string): void;
98
+ addEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void;
99
+ removeEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void;
100
+ /**
101
+ * Listen to websocket messages.
102
+ *
103
+ * @param {WebSocketParameters} params Parameters for the websocket get request
104
+ * @param {function} cb callback on listen
105
+ * @param {function} errorCb Callback on error
106
+ * @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks
107
+ * @private
108
+ */
109
+ listen(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>, errorCb?: EventListener): {
110
+ onMessageCb: WebSocketAPIMessageEventListener;
111
+ onErrorCb?: EventListener;
112
+ };
113
+ /**
114
+ * Unlisten websocket messages.
115
+ *
116
+ * @param {Object} params Parameters for the websocket get request.
117
+ * @param {function} cb Callback used when listen.
118
+ * @private
119
+ */
120
+ unlisten(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>): void;
121
+ /**
122
+ * Sends a get request to the websocket.
123
+ * The callback is called only once, when the response is received or when the call returns an error.
124
+ *
125
+ * @param {Object} params Parameters for the websocket get request
126
+ * @param {function} onMessage callback on message event
127
+ * @param {function} onError Callback on error and close event
128
+ * @private
129
+ */
130
+ get(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>, errorCb?: EventListener): void;
131
+ /**
132
+ * Subscribe to a given channel.
133
+ *
134
+ * @param {Object} params Parameters for the websocket get request
135
+ * @param {function} cb callback on listen
136
+ * @param {function} errorCb Callback on error
137
+ * @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
138
+ * @private
139
+ */
140
+ subscribe(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>, errorCb?: EventListener, quiet?: boolean): void;
141
+ /**
142
+ * Unsubscribe from a channel.
143
+ * @param {string} source source to unsubscribe from
144
+ * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
145
+ * @private
146
+ */
147
+ unsubscribe(source: string, cb?: WebSocketAPIMessageCallback<any>): void;
148
+ /**
149
+ * After an auto reconnection we need to re-subscribe to the channels.
150
+ */
151
+ subscribePreviousSubscriptions(): void;
152
+ }
153
+ export default WebSocketAPI;
package/api/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { default as RoutingAPI } from "./RoutingAPI";
2
+ export { default as StopsAPI } from "./StopsAPI";
3
+ export { default as RealtimeAPI, RealtimeModes } from "./RealtimeAPI";