@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,431 @@
1
+ import axios from "axios";
2
+ import qs from "qs";
3
+ import { version } from "../version.js";
4
+
5
+ import {
6
+ cacheAdapterEnhancer,
7
+ throttleAdapterEnhancer,
8
+ Cache,
9
+ } from "axios-extensions";
10
+
11
+ function isString(value) {
12
+ const type = typeof value;
13
+ return (
14
+ type === "string" ||
15
+ (type === "object" &&
16
+ value != null &&
17
+ !Array.isArray(value) &&
18
+ getTag(value) == "[object String]")
19
+ );
20
+ }
21
+
22
+ /**
23
+ * Creates a new instance of Axios, with automatic authentication and token refreshing functionality.
24
+ * This module provides all the underlying functions for making http requests and interacting with the REST API.
25
+ * It is a wrapper around the axios library, a popular promise based javascript request package. The api module is used by most other modules in the SDK and will handle authentication, token refreshing and other headers automatically.
26
+ * @alias api
27
+ * @constructor
28
+ * @hideconstructor
29
+ * @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.
30
+ */
31
+
32
+ const CancelToken = axios.CancelToken;
33
+
34
+ ///////////////////////////////////////
35
+
36
+ var QikAPI = function (qik) {
37
+ var defaultCache;
38
+
39
+ if (typeof window !== "undefined") {
40
+ defaultCache = qik.cache.get("api");
41
+ }
42
+
43
+ const defaultAdapter = axios.defaults.adapter;
44
+
45
+ /////////////////////////////////////////////////////
46
+
47
+ function getRequestCacheKey(config) {
48
+ var key = [
49
+ config.method,
50
+ config.url,
51
+ config.headers?.Authorization,
52
+ config.headers.Accept,
53
+ JSON.stringify({
54
+ params: config.params,
55
+ data: config.data,
56
+ }),
57
+ ]
58
+ .filter(Boolean)
59
+ .join("-");
60
+
61
+ return key;
62
+ }
63
+
64
+ ///////////////////////////////////////
65
+
66
+ const inflightRequests = {};
67
+
68
+ let cacheAdapter = function (config) {
69
+ var cacheKey = getRequestCacheKey(config);
70
+
71
+ // If there is already an identical request being made
72
+ if (!inflightRequests[cacheKey]) {
73
+ // Be more efficient and wait for the existing request
74
+ inflightRequests[cacheKey] = new Promise(function (resolve, reject) {
75
+ var useCache;
76
+ var cachedResponse;
77
+
78
+ ///////////////////////////////////////
79
+
80
+ switch (String(config.method).toLowerCase()) {
81
+ case "post":
82
+ case "patch":
83
+ case "put":
84
+ case "delete":
85
+ //Unless we've specified we want a cache
86
+ if (!config.cache) {
87
+ //Don't use the cache
88
+ config.cache = false;
89
+ }
90
+ break;
91
+ }
92
+
93
+ ///////////////////////////////////////
94
+
95
+ if (config.cache === false) {
96
+ //No cache so make new request
97
+ } else {
98
+ //Use the cache specified or the default cache
99
+ useCache = config.cache || defaultCache;
100
+
101
+ //If there is a cache
102
+ if (useCache) {
103
+ //If we have the cachedResponse version
104
+ cachedResponse = useCache.get(cacheKey);
105
+ }
106
+ }
107
+
108
+ ///////////////////////////////////////
109
+
110
+ if (cachedResponse) {
111
+ return resolve(cachedResponse);
112
+ }
113
+
114
+ var copy = Object.assign(config, { adapter: defaultAdapter });
115
+
116
+ return axios.request(config).then(
117
+ function (res) {
118
+ delete inflightRequests[cacheKey];
119
+ resolve(res);
120
+ },
121
+ function (err) {
122
+ delete inflightRequests[cacheKey];
123
+ reject(err);
124
+ },
125
+ );
126
+ });
127
+ }
128
+ return inflightRequests[cacheKey];
129
+ };
130
+
131
+ //////////////////////////////////////////////////////////////////////////////
132
+
133
+ const service = createNewAxios(cacheAdapter);
134
+
135
+ //////////////////////////////////////////////////////////////////////////////
136
+
137
+ /**
138
+ *
139
+ * Generate an Endpoint URL, complete with authentication.
140
+ * this function is helpful for generating authenticated links for the user to open in another window
141
+ * @alias api.generateEndpointURL
142
+ * @param {String} endpoint The endpoint to generate a url for
143
+ * @param {Object} params Extra parameters for the url
144
+ * @param {Object} options Additional request configuration
145
+ * @param {Object} options.withoutToken Don't append the current user's access token to the url
146
+ * @param {Object} options.file Whether the response for this request is expected to be a binary file type
147
+ * @example
148
+ *
149
+ *
150
+ * const result = sdk.api.generateEndpointURL('/image/61eca4746971e75c1fc670cf', {
151
+ * w:100,
152
+ * h:50,
153
+ * }, {file:true})
154
+ *
155
+ * // Would return something similar to:
156
+ * // https://api.qik.dev/image/61eca4746971e75c1fc670cf?w=100&h=50&access_token=XXXX...
157
+ */
158
+ service.generateEndpointURL = function (endpoint, params, options) {
159
+ options = options || {};
160
+ params = params || {};
161
+
162
+ var token;
163
+ const apiURL = options.file ? qik.fileAPI || qik.apiURL : qik.apiURL;
164
+
165
+ //Append the access token to the url
166
+ if (!options.withoutToken) {
167
+ delete params.access_token;
168
+ token = qik.auth.getCurrentToken();
169
+ }
170
+
171
+ var stripLeadingTag = endpoint[0] == "/" ? endpoint.slice(1) : endpoint;
172
+ var parameterString = qik.utils.mapParameters(params);
173
+
174
+ if (token) {
175
+ parameterString = parameterString
176
+ ? `?access_token=${token}&${parameterString}`
177
+ : `?access_token=${token}`;
178
+ } else {
179
+ parameterString = parameterString ? `?${parameterString}` : "";
180
+ }
181
+
182
+ var url = `${apiURL}${endpoint}${parameterString}`;
183
+
184
+ return url;
185
+ };
186
+
187
+ //////////////////////////////////////////////////////////////////////////////
188
+
189
+ service.wasCancelled = function (err) {
190
+ return axios.isCancel(err);
191
+ };
192
+
193
+ //////////////////////////////////////////////////////////////////////////////
194
+
195
+ /**
196
+ *
197
+ * Make a GET request to the API
198
+ * @alias api.get
199
+ * @param {Object} endpoint The endpoint to request
200
+ * @param {Object} options The options to pass to axios for making the request
201
+ * @example
202
+ *
203
+ * const result = await sdk.api.get('/user');
204
+ */
205
+ var f = function () {};
206
+
207
+ /**
208
+ *
209
+ *
210
+ * @alias api.post
211
+ * @description Make a POST request to the API
212
+ * @param {Object} endpoint The endpoint to request
213
+ * @param {Object} body The post body to send with the request
214
+ * @param {Object} options The options to pass to axios for making the request
215
+ * @example
216
+ *
217
+ * const result = await sdk.api.post('/login', {username:'', password:''});
218
+ */
219
+ var f = function () {};
220
+
221
+ /**
222
+ *
223
+ * Make a PUT request to the API
224
+ * @alias api.put
225
+ * @param {Object} endpoint The endpoint to request
226
+ * @param {Object} body The post body to send with the request
227
+ * @param {Object} options The options to pass to axios for making the request
228
+ * @example
229
+ *
230
+ * const result = await sdk.api.put('/content/61eca4746971e75c1fc670cf', {title:'A new title'});
231
+ */
232
+ var f = function () {};
233
+
234
+ /**
235
+ *
236
+ * Make a PATCH request to the API
237
+ * @alias api.patch
238
+ * @param {Object} endpoint The endpoint to request
239
+ * @param {Object} body The post body to send with the request
240
+ * @param {Object} options The options to pass to axios for making the request
241
+ * @example
242
+ *
243
+ * const result = await sdk.api.patch('/content/61eca4746971e75c1fc670cf', {title:'A new title'});
244
+ */
245
+ var f = function () {};
246
+
247
+ /**
248
+ *
249
+ * Make a DELETE request to the API
250
+ * @alias api.delete
251
+ * @param {Object} endpoint The endpoint to request
252
+ * @param {Object} body The post body to send with the request
253
+ * @param {Object} options The options to pass to axios for making the request
254
+ * @example
255
+ *
256
+ * const result = await sdk.api.delete('/content/61eca4746971e75c1fc670cf');
257
+ */
258
+ var f = function () {};
259
+
260
+ function createNewAxios(adapter) {
261
+ var instance = axios.create({
262
+ paramsSerializer: (params) =>
263
+ qs.stringify(params, { arrayFormat: "repeat" }),
264
+ adapter,
265
+ });
266
+
267
+ ///////////////////////////////////////
268
+
269
+ instance.defaults.baseURL = qik.apiURL;
270
+ instance.defaults.headers.common.Accept = "application/json";
271
+
272
+ /////////////////////////////////////////////////////
273
+
274
+ // Add relative date and timezone to every request
275
+ instance.interceptors.request.use(function (config) {
276
+ const inflightKey = getRequestCacheKey(config);
277
+
278
+ if (config.withoutToken) {
279
+ return config;
280
+ }
281
+
282
+ config.headers["qik-request-date"] = new Date().getTime();
283
+ if (Intl) {
284
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
285
+ if (timezone) {
286
+ config.headers["qik-request-timezone"] = timezone;
287
+ }
288
+ }
289
+
290
+ config.headers["qik-api-version"] = version;
291
+
292
+ // Include the socket window id in the request
293
+ if (qik?.socket?.windowID) {
294
+ config.headers["qik-socket-window"] = qik.socket.windowID;
295
+ }
296
+
297
+ var token = qik.auth.getCurrentToken();
298
+
299
+ if (token) {
300
+ config.headers["Authorization"] = `Bearer ${token}`;
301
+
302
+ if (config.params && config.params.access_token) {
303
+ delete config.params.access_token;
304
+ }
305
+ }
306
+
307
+ return config;
308
+ });
309
+
310
+ /////////////////////////////////////////////////////
311
+
312
+ let retryCount = 0;
313
+
314
+ instance.interceptors.response.use(
315
+ function (response) {
316
+ var config = response.config;
317
+
318
+ //Get the response status
319
+ var status = response.status;
320
+ switch (status) {
321
+ case 204:
322
+ // No content give it another try
323
+ if (retryCount < 5) {
324
+ retryCount++;
325
+ // Wait a second and try again
326
+ return new Promise(function (resolve, reject) {
327
+ setTimeout(function () {
328
+ return instance.request(config).then(resolve, reject);
329
+ }, 800);
330
+ });
331
+ } else {
332
+ console.log("Failed after 5 retries");
333
+ retryCount = 0;
334
+ }
335
+ break;
336
+ }
337
+
338
+ return response;
339
+ },
340
+ function (err) {
341
+ if (axios.isCancel(err)) {
342
+ return Promise.reject(err);
343
+ }
344
+
345
+ //Get the response status
346
+ var status = err?.response?.status || err.status;
347
+
348
+ //Check the status
349
+ switch (status) {
350
+ case 401:
351
+ //Ignore let QikAuth handle it
352
+ break;
353
+
354
+ case 502:
355
+ case 504:
356
+ if (retryCount < 5) {
357
+ retryCount++;
358
+ // Wait a second and try again
359
+ return new Promise(function (resolve, reject) {
360
+ setTimeout(function () {
361
+ return instance.request(err.config).then(resolve, reject);
362
+ }, 800);
363
+ });
364
+ } else {
365
+ console.log("Failed after 5 retries");
366
+ retryCount = 0;
367
+ }
368
+ break;
369
+ case 404:
370
+ //Not found
371
+ break;
372
+ default:
373
+ //Some other error, likely a problem connecting to the server
374
+ // console.log('qik.api > connection error', status, err);
375
+ break;
376
+ }
377
+
378
+ /////////////////////////////////////////////////////
379
+
380
+ return Promise.reject(err);
381
+ },
382
+ );
383
+
384
+ return instance;
385
+ }
386
+
387
+ /////////////////////////////////////////////////////
388
+
389
+ function retrieveIDs(data) {
390
+ var dataString;
391
+
392
+ if (_isString(data)) {
393
+ dataString = data;
394
+ } else {
395
+ dataString = JSON.stringify(data);
396
+ }
397
+
398
+ //Find all mongo ids included in the object
399
+ var myregexp = /[0-9a-fA-F]{24}/g;
400
+ var matches = dataString.match(myregexp);
401
+
402
+ //Make sure the matches are unique
403
+ return [...new Set(matches)];
404
+ }
405
+
406
+ ///////////////////////////////////////
407
+
408
+ /**
409
+ *
410
+ * Reference to the underlying axios package (https://www.npmjs.com/package/axios)
411
+ * Useful for creating new instances of axios if you want to create http requests to
412
+ * external APIs and not send tokens etc..
413
+ * @name api.axios
414
+ * @example
415
+ * const newRequest = await sdk.api.axios.get('https://otherapi.com/content/61eca4746971e75c1fc670cf');
416
+ */
417
+
418
+ service.CancelToken = CancelToken;
419
+ service.axios = axios;
420
+
421
+ ///////////////////////////////////////
422
+
423
+ return service;
424
+ };
425
+
426
+ ///////////////////////////////////////
427
+ ///////////////////////////////////////
428
+ ///////////////////////////////////////
429
+
430
+ export { CancelToken as CancelToken };
431
+ export default QikAPI;