@qikdev/sdk 0.1.169 → 0.1.170

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 (71) hide show
  1. package/.gitlab-ci.yml +24 -0
  2. package/.husky/pre-commit +5 -0
  3. package/eslint.config.js +6 -0
  4. package/jsdoc.conf +10 -0
  5. package/package.json +2 -12
  6. package/public/docs/access.html +3826 -0
  7. package/public/docs/api.html +1356 -0
  8. package/public/docs/auth.html +2180 -0
  9. package/public/docs/cache.html +351 -0
  10. package/public/docs/content.html +2560 -0
  11. package/public/docs/files.html +779 -0
  12. package/public/docs/filter.html +1355 -0
  13. package/public/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
  14. package/public/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
  15. package/public/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
  16. package/public/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
  17. package/public/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
  18. package/public/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
  19. package/public/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
  20. package/public/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
  21. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  22. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
  23. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  24. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  25. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  26. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  27. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
  28. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  29. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  30. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  31. package/public/docs/index.html +96 -0
  32. package/public/docs/qik.access.js.html +790 -0
  33. package/public/docs/qik.api.js.html +511 -0
  34. package/public/docs/qik.auth.js.html +1006 -0
  35. package/public/docs/qik.cache.js.html +131 -0
  36. package/public/docs/qik.content.js.html +918 -0
  37. package/public/docs/qik.core.js.html +301 -0
  38. package/public/docs/qik.files.js.html +219 -0
  39. package/public/docs/qik.filter.js.html +611 -0
  40. package/public/docs/qik.socket.js.html +416 -0
  41. package/public/docs/qik.system.js.html +117 -0
  42. package/public/docs/qik.utils.js.html +1467 -0
  43. package/public/docs/scripts/collapse.js +20 -0
  44. package/public/docs/scripts/linenumber.js +25 -0
  45. package/public/docs/scripts/nav.js +12 -0
  46. package/public/docs/scripts/polyfill.js +4 -0
  47. package/public/docs/scripts/prettify/Apache-License-2.0.txt +202 -0
  48. package/public/docs/scripts/prettify/lang-css.js +2 -0
  49. package/public/docs/scripts/prettify/prettify.js +28 -0
  50. package/public/docs/scripts/search.js +83 -0
  51. package/public/docs/socket.html +810 -0
  52. package/public/docs/styles/jsdoc.css +765 -0
  53. package/public/docs/styles/prettify.css +79 -0
  54. package/public/docs/system.html +197 -0
  55. package/src/api/qik.access.js +825 -0
  56. package/src/api/qik.api.js +431 -0
  57. package/src/api/qik.auth.js +881 -0
  58. package/src/api/qik.cache.js +63 -0
  59. package/src/api/qik.content.js +839 -0
  60. package/src/api/qik.core.js +245 -0
  61. package/src/api/qik.files.js +186 -0
  62. package/src/api/qik.filter.js +543 -0
  63. package/src/api/qik.geo.js +895 -0
  64. package/src/api/qik.socket.js +360 -0
  65. package/src/api/qik.system.js +49 -0
  66. package/src/api/qik.utils.js +1412 -0
  67. package/src/index.js +20 -0
  68. package/src/version.js +1 -0
  69. package/test/index.spec.js +41 -0
  70. package/test/mocha.opts +0 -0
  71. package/yarn-error.log +3598 -0
@@ -0,0 +1,360 @@
1
+ import { EventDispatcher } from "./qik.utils.js";
2
+
3
+ ///////////////////////////////////////////////////
4
+
5
+ /**
6
+ * Creates a new instance of QikSocket a module of the SDK
7
+ * that contains all helper functions to do with realtime sockets
8
+ * @name socket
9
+ * @constructor
10
+ * @hideconstructor
11
+ * @param {QikAPI} qik A reference to the parent instance of the QikCore module. This module is usually created by a QikCore instance that passes itself in as the first argument.
12
+ */
13
+
14
+ const QikSocket = function (qik, mode) {
15
+ mode = mode || "production";
16
+
17
+ if (!qik.auth) {
18
+ throw new Error(`Can't Instantiate QikSocket before QikAuth exists`);
19
+ }
20
+
21
+ const windowID = qik.utils.guid();
22
+
23
+ ///////////////////////////////////////////////////
24
+
25
+ let service = {
26
+ debug: false,
27
+ url: `wss://iqtm6zjz3l.execute-api.ap-southeast-2.amazonaws.com/${mode}`,
28
+ connected: false,
29
+ windowID,
30
+ autoreconnect: true,
31
+ };
32
+
33
+ let socket;
34
+ let timer;
35
+ let buckets = {};
36
+ const channels = {};
37
+
38
+ //Create a new dispatcher
39
+ const dispatcher = new EventDispatcher();
40
+ dispatcher.bootstrap(service);
41
+
42
+ ///////////////////////////////////////////////////
43
+
44
+ function getSubscriptions() {
45
+ return Object.keys(buckets);
46
+ }
47
+
48
+ ///////////////////////////////////////////////////
49
+
50
+ /**
51
+ * @description Create an event dispatcher instance and subscribe to a new socket channel.
52
+ * This can then be used to receive socket events for this channel
53
+ * @alias socket.channel
54
+ * @param {String} key The key or name of the channel to subscribe to
55
+ * @example
56
+ * const socketChannel = await sdk.socket.channel('awesome:notifications')
57
+ * socketChannel.addEventListener('message', function(message) {
58
+ * console.log('Received a new message from the socket')
59
+ * })
60
+ *
61
+ * // Disconnect from this channel and remove all listeners
62
+ * socketChannel.destroy();
63
+ *
64
+ */
65
+ service.channel = async function (key) {
66
+ if (!buckets[key]) {
67
+ // Create a new channel instance
68
+ const bucket = new EventDispatcher();
69
+ buckets[key] = bucket;
70
+
71
+ // If the user isn't online
72
+ if (!service.connected) {
73
+ // Connect them now
74
+ await service.connect();
75
+ }
76
+
77
+ // Subscribe to this key
78
+ service.subscribe(key);
79
+
80
+ // Listen for all relevant events
81
+ service.addEventListener(key, dispatchMessage);
82
+
83
+ function dispatchMessage(message) {
84
+ bucket.dispatch("message", message);
85
+ }
86
+
87
+ // Destroy the service
88
+ bucket.destroy = function () {
89
+ if (buckets[key]) {
90
+ buckets[key].removeAllListeners();
91
+ service.unsubscribe(key);
92
+ buckets[key] = null;
93
+ }
94
+ };
95
+ }
96
+
97
+ // Return the channel instance
98
+ return buckets[key];
99
+ };
100
+
101
+ ///////////////////////////////////////////////////
102
+
103
+ function ping() {
104
+ broadcast({
105
+ action: "ping",
106
+ });
107
+ }
108
+
109
+ function startHeartbeat() {
110
+ if (timer) {
111
+ return;
112
+ }
113
+ timer = setInterval(ping, 10000);
114
+ }
115
+
116
+ function stopHeartbeat() {
117
+ if (!timer) {
118
+ return;
119
+ }
120
+
121
+ clearInterval(timer);
122
+ timer = null;
123
+ }
124
+
125
+ ///////////////////////////////////////////////////
126
+
127
+ function socketOpened(event) {
128
+ service.debug ? console.log("[socket] Connection open", event) : null;
129
+ service.connected = true;
130
+
131
+ dispatcher.dispatch("connected", event);
132
+
133
+ // startHeartbeat();
134
+ }
135
+
136
+ async function socketClosed(event) {
137
+ if (event.wasClean) {
138
+ service.debug
139
+ ? console.log(
140
+ `[socket] Connection closed cleanly, code=${event.code} reason=${event.reason}`,
141
+ )
142
+ : null;
143
+ } else {
144
+ // e.g. server process killed or network down
145
+ // event.code is usually 1006 in this case
146
+ service.debug
147
+ ? console.log("[socket] - connection closed due to error", event)
148
+ : null;
149
+ }
150
+
151
+ service.connected = false;
152
+ dispatcher.dispatch("disconnected", event);
153
+ socket = null;
154
+
155
+ if (service.autoreconnect) {
156
+ service.reconnect();
157
+ }
158
+ }
159
+
160
+ function socketError(error) {
161
+ console.log("[event] socketError", event);
162
+ service.debug ? console.log("[socket] Error", error) : null;
163
+ dispatcher.dispatch("error", error);
164
+ }
165
+
166
+ function socketMessageReceived(event) {
167
+ const eventData = JSON.parse(event.data);
168
+ service.debug ? console.log("[socket] message received", eventData) : null;
169
+
170
+ // Dispatch a generic message
171
+ dispatcher.dispatch("message", eventData);
172
+
173
+ if (eventData.channel) {
174
+ dispatcher.dispatch(eventData.channel, eventData);
175
+ }
176
+ }
177
+
178
+ async function broadcast(data) {
179
+ socket.send(JSON.stringify(data));
180
+ }
181
+
182
+ ///////////////////////////////////////////////////
183
+
184
+ function wait() {
185
+ return new Promise(function (resolve) {
186
+ function check() {
187
+ if (service.connected) {
188
+ return resolve(socket);
189
+ }
190
+
191
+ setTimeout(check, 1000);
192
+ }
193
+
194
+ check();
195
+ });
196
+ }
197
+
198
+ ///////////////////////////////////////////////////
199
+
200
+ /**
201
+ * @description Connect to the socket service
202
+ * @alias socket.connect
203
+ * @example
204
+ * const connected = await sdk.socket.connect()
205
+ *
206
+ */
207
+ service.connect = async function () {
208
+ if (socket) {
209
+ if (!socket.connected) {
210
+ return wait();
211
+ }
212
+
213
+ console.log("[socket] - Socket is already connected");
214
+ return socket;
215
+ }
216
+
217
+ const accessToken = qik.auth.getCurrentToken();
218
+
219
+ if (!accessToken) {
220
+ service.debug
221
+ ? console.log("[socket] - Must be authenticated to connect to socket")
222
+ : null;
223
+ return;
224
+ }
225
+
226
+ socket = new WebSocket(
227
+ `${service.url}?access_token=${accessToken}&windowid=${windowID}`,
228
+ );
229
+ socket.addEventListener("close", socketClosed);
230
+ socket.addEventListener("error", socketError);
231
+ socket.addEventListener("open", socketOpened);
232
+ socket.addEventListener("message", socketMessageReceived);
233
+ service.debug
234
+ ? console.log("[socket] - Connected with event listeners")
235
+ : null;
236
+
237
+ return wait();
238
+ };
239
+
240
+ /**
241
+ * @description Reconnect to the socket service
242
+ * @alias socket.reconnect
243
+ * @example
244
+ * const connected = await sdk.socket.reconnect()
245
+ *
246
+ */
247
+ service.reconnect = async function () {
248
+ if (service.connected) {
249
+ return;
250
+ }
251
+
252
+ service.debug ? console.log(`[socket] reconnecting...`) : null;
253
+
254
+ // Reconnect all listeners
255
+ const result = await service.connect();
256
+
257
+ // Resubscribe to all previously connected channels
258
+ const subscribedChannels = Object.keys(channels);
259
+ subscribedChannels.forEach(function (channel) {
260
+ service.subscribe(channel);
261
+ });
262
+
263
+ return result;
264
+ };
265
+
266
+ ///////////////////////////////////////////////////
267
+
268
+ /**
269
+ * @description Close the connection to the socket service
270
+ * @alias socket.close
271
+ * @example
272
+ * const closed = await sdk.socket.close()
273
+ *
274
+ */
275
+ service.close = async function () {
276
+ if (!socket) {
277
+ console.log("[socket] - Socket is not connected");
278
+ return;
279
+ }
280
+
281
+ const previousSetting = service.autoreconnect;
282
+ service.autoreconnect = false;
283
+ socket.close();
284
+ socket.removeEventListener("close", socketClosed);
285
+ socket.removeEventListener("error", socketError);
286
+ socket.removeEventListener("open", socketOpened);
287
+ socket.removeEventListener("message", socketMessageReceived);
288
+ socket = null;
289
+ socketClosed({ wasClean: true });
290
+ service.autoreconnect = previousSetting;
291
+ };
292
+
293
+ service.disconnect = service.close;
294
+
295
+ ///////////////////////////////////////////////////
296
+
297
+ /**
298
+ * @description Subscribe to a socket channel
299
+ * @alias socket.subscribe
300
+ * @param {String} key The name of the channel to subscribe to
301
+ * @example
302
+ * const subscribed = await sdk.socket.subscribe('some:cool:alert')
303
+ *
304
+ */
305
+ service.subscribe = async function (channel) {
306
+ if (!socket) {
307
+ console.log(
308
+ `[socket] - Can't subscribe to channel as socket is not connected`,
309
+ );
310
+ return;
311
+ }
312
+
313
+ broadcast({
314
+ action: "subscribe",
315
+ channel,
316
+ });
317
+
318
+ channels[channel] = true;
319
+
320
+ dispatcher.dispatch("subscribe", channel);
321
+ service.debug ? console.log(`[socket] - subscribed to ${channel}`) : null;
322
+ };
323
+
324
+ ///////////////////////////////////////////////////
325
+
326
+ /**
327
+ * @description Unsubscribe from a socket channel
328
+ * @alias socket.unsubscribe
329
+ * @param {String} key The name of the channel to unsubscribe from
330
+ * @example
331
+ * const unsubscribed = await sdk.socket.unsubscribe('some:cool:alert')
332
+ *
333
+ */
334
+ service.unsubscribe = async function (channel) {
335
+ if (!socket) {
336
+ console.log(
337
+ `[socket] - Can't unsubscribe from channel as socket is not connected`,
338
+ );
339
+ return;
340
+ }
341
+
342
+ broadcast({
343
+ action: "unsubscribe",
344
+ channel,
345
+ });
346
+
347
+ delete channels[channel];
348
+
349
+ dispatcher.dispatch("unsubscribe", channel);
350
+ service.debug
351
+ ? console.log(`[socket] - unsubscribed from ${channel}`)
352
+ : null;
353
+ };
354
+
355
+ ///////////////////////////////////////////////////
356
+
357
+ return service;
358
+ };
359
+
360
+ export default QikSocket;
@@ -0,0 +1,49 @@
1
+ ///////////////////////////////////////////////////
2
+
3
+ /**
4
+ * Creates a new QikSystem instance.
5
+ * This module provides a number of helper functions for the system
6
+ * @alias system
7
+ * @constructor
8
+ * @hideconstructor
9
+ * @param {QikCore} qik A reference to the parent instance of the QikCore module. This module is usually created by a QikCore instance that passes itself in as the first argument.
10
+ */
11
+ export default function(qik) {
12
+
13
+ ///////////////////////////////////////////////////
14
+
15
+ var service = {}
16
+
17
+
18
+ ///////////////////////////////////////////////////////////////////////////////
19
+
20
+ var inflightCountries;
21
+
22
+ /**
23
+ * @alias system.countries
24
+ * @description Helper function for retrieving a full list of all countries, their ISO codes
25
+ * and phone number prefixes. Helpful for populating dropdown lists.
26
+ * @example
27
+ * const countries = await sdk.system.countries()
28
+ */
29
+ service.countries = async function() {
30
+
31
+ if (!inflightCountries) {
32
+
33
+ inflightCountries = new Promise(async function(resolve, reject) {
34
+ qik.api.get(`/system/countries`)
35
+ .then(function(res) {
36
+ return resolve(res.data);
37
+ })
38
+ .catch(reject);
39
+ })
40
+ }
41
+
42
+ return inflightCountries;
43
+ }
44
+
45
+ ///////////////////////////////////////////////////
46
+
47
+ return service;
48
+
49
+ }