@qikdev/sdk 0.2.2 → 0.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qikdev/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "Promise based Javascript SDK",
6
6
  "main": "./dist/main.js",
@@ -2,22 +2,7 @@ import axios from "axios";
2
2
  import qs from "qs";
3
3
  import { version } from "../version.js";
4
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
- }
5
+
21
6
 
22
7
  /**
23
8
  * Creates a new instance of Axios, with automatic authentication and token refreshing functionality.
@@ -192,71 +177,6 @@ var QikAPI = function (qik) {
192
177
 
193
178
  //////////////////////////////////////////////////////////////////////////////
194
179
 
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
180
  function createNewAxios(adapter) {
261
181
  var instance = axios.create({
262
182
  paramsSerializer: (params) =>
@@ -370,8 +290,6 @@ var QikAPI = function (qik) {
370
290
  //Not found
371
291
  break;
372
292
  default:
373
- //Some other error, likely a problem connecting to the server
374
- // console.log('qik.api > connection error', status, err);
375
293
  break;
376
294
  }
377
295
 
@@ -387,20 +305,9 @@ var QikAPI = function (qik) {
387
305
  /////////////////////////////////////////////////////
388
306
 
389
307
  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)];
308
+ const text = typeof data === "string" ? data : JSON.stringify(data);
309
+ const matches = text.match(/\b[0-9a-f]{24}\b/gi);
310
+ return matches ? [...new Set(matches)] : [];
404
311
  }
405
312
 
406
313
  ///////////////////////////////////////
@@ -276,19 +276,19 @@ var QikAuth = function(qik) {
276
276
 
277
277
  if (!credentials) {
278
278
  return reject({
279
- message: 'Missing credentials!',
279
+ message: 'Login requires an email and password',
280
280
  })
281
281
  }
282
282
 
283
283
  if (!credentials.email || !credentials.email.length) {
284
284
  return reject({
285
- message: 'Username was not provided',
285
+ message: 'An email address is required to log in',
286
286
  })
287
287
  }
288
288
 
289
289
  if (!credentials.password || !credentials.password.length) {
290
290
  return reject({
291
- message: 'Password was not provided',
291
+ message: 'A password is required to log in',
292
292
  })
293
293
  }
294
294
 
@@ -362,44 +362,44 @@ var QikAuth = function(qik) {
362
362
 
363
363
  if (!credentials) {
364
364
  return reject({
365
- message: 'No details provided',
365
+ message: 'Signup details are required',
366
366
  })
367
367
  }
368
368
 
369
369
  if (!credentials.firstName || !credentials.firstName.length) {
370
370
  return reject({
371
- message: 'First Name was not provided',
371
+ message: 'Please provide a first name',
372
372
  })
373
373
  }
374
374
 
375
375
  if (!credentials.lastName || !credentials.lastName.length) {
376
376
  return reject({
377
- message: 'Last Name was not provided',
377
+ message: 'Please provide a last name',
378
378
  })
379
379
  }
380
380
 
381
381
  if (!credentials.email || !credentials.email.length) {
382
382
  return reject({
383
- message: 'Email/Username was not provided',
383
+ message: 'An email address is required to sign up',
384
384
  })
385
385
  }
386
386
 
387
387
 
388
388
  if (!credentials.password || !credentials.password.length) {
389
389
  return reject({
390
- message: 'Password was not provided',
390
+ message: 'A password is required to sign up',
391
391
  })
392
392
  }
393
393
 
394
394
  if (!credentials.confirmPassword || !credentials.confirmPassword.length) {
395
395
  return reject({
396
- message: 'Confirm Password was not provided',
396
+ message: 'Please confirm your password',
397
397
  })
398
398
  }
399
399
 
400
400
  if (credentials.confirmPassword != credentials.password) {
401
401
  return reject({
402
- message: 'Your passwords do not match',
402
+ message: 'Password and confirmation do not match',
403
403
  })
404
404
  }
405
405
 
@@ -568,13 +568,13 @@ var QikAuth = function(qik) {
568
568
 
569
569
  if (!details) {
570
570
  return Promise.reject({
571
- message: 'No details were provided for password reset request',
571
+ message: 'Please provide your account details to reset your password',
572
572
  })
573
573
  }
574
574
 
575
575
  if (!details.email || !details.email.length) {
576
576
  return Promise.reject({
577
- message: 'Email is required but was not provided',
577
+ message: 'An email address is required for password reset',
578
578
  })
579
579
  }
580
580
 
@@ -738,8 +738,6 @@ var QikAuth = function(qik) {
738
738
 
739
739
  ///////////////////////////////////////////////////
740
740
 
741
- var retryCount = 0;
742
-
743
741
  /**
744
742
  * @alias auth.sync
745
743
  * @description A useful function to sync the current user session with the server.
@@ -756,7 +754,6 @@ var QikAuth = function(qik) {
756
754
  if (res.data) {
757
755
  const currentUser = storageAdapter.getUser();
758
756
  if (currentUser) {
759
- // Update the session data within the current user object
760
757
  const updatedUser = { ...currentUser };
761
758
  if (updatedUser.session) {
762
759
  Object.assign(updatedUser.session, res.data);
@@ -765,28 +762,17 @@ var QikAuth = function(qik) {
765
762
  }
766
763
  service.set(updatedUser);
767
764
  } else {
768
- // No current user, set the response data as new user
769
765
  service.set(res.data);
770
766
  }
771
767
  } else {
772
768
  service.set(null);
773
769
  }
774
770
 
775
- retryCount = 0;
776
771
  dispatch();
777
772
  })
778
773
  .catch(function(err) {
779
-
780
- // if (retryCount > 2) {
781
774
  service.set(null);
782
- retryCount = 0;
783
775
  dispatch();
784
- // } else {
785
-
786
- // retryCount++;
787
- // service.sync();
788
- // }
789
-
790
776
  });
791
777
  }
792
778
 
@@ -23,13 +23,11 @@ var QikCache = {
23
23
  */
24
24
  reset() {
25
25
  Object.entries(caches).forEach(([key, cache]) => {
26
- // console.log(`Reset ${key} cache`, cache);
27
26
  if(cache.clear) {
28
27
  cache.clear()
29
28
  } else if(cache.reset) {
30
29
  cache.reset()
31
30
  }
32
-
33
31
  })
34
32
  },
35
33
 
@@ -48,10 +46,7 @@ var QikCache = {
48
46
  return caches[key];
49
47
  }
50
48
  caches[key] = new Cache(options)
51
-
52
- // console.log('Created new cache', key);
53
49
  return caches[key];
54
-
55
50
  }
56
51
  }
57
52
 
@@ -1,5 +1,3 @@
1
- // if (process.browser) {
2
-
3
1
  import QikAPI from './qik.api.js';
4
2
  import QikAuth from './qik.auth.js';
5
3
  import QikUtils from './qik.utils.js';
@@ -87,8 +85,6 @@ export default function(options) {
87
85
  ///////////////////////////////////////
88
86
 
89
87
  const core = Object.assign(options, {
90
- //Other options
91
- // domain:'',
92
88
  version,
93
89
  global:{},
94
90
  })
@@ -797,12 +797,8 @@ service.mapFields = function (fields, options) {
797
797
  mapped.trail.push(fieldKey);
798
798
 
799
799
  mapped.path = mapped.trail.join(".");
800
- if (field.titles && field.titles.length) {
801
- mapped.titles = field.titles.slice();
802
- } else {
803
- mapped.titles = titles.slice();
804
- mapped.titles.push(field.filterTitle || field.title || "");
805
- }
800
+ mapped.titles = titles.slice();
801
+ mapped.titles.push(field.filterTitle || field.title || "");
806
802
 
807
803
  const isNotLayoutOrIsAllowed = !isLayout || options.includeLayout;
808
804
 
@@ -1016,79 +1012,37 @@ service.getTypeFromID = function (id) {
1016
1012
  * sdk.utils.currencySymbol('usd');
1017
1013
  *
1018
1014
  */
1019
- service.currencySymbol = function (currency) {
1020
- //Ensure lowercase currency
1021
- currency = String(currency).toLowerCase();
1015
+ const CURRENCY_SYMBOLS = { gbp: "\u00A3", eur: "\u20AC" };
1022
1016
 
1023
- switch (String(currency).toLowerCase()) {
1024
- case "gbp":
1025
- return "£";
1026
- break;
1027
- case "eur":
1028
- return "€";
1029
- break;
1030
- default:
1031
- return "$";
1032
- break;
1033
- }
1017
+ service.currencySymbol = function (currency) {
1018
+ return CURRENCY_SYMBOLS[String(currency).toLowerCase()] || "$";
1034
1019
  };
1035
1020
 
1036
- service.getAvailableCurrencies = function (DefaultCountryID) {
1037
- var array = [];
1038
-
1039
- array.push({
1040
- name: `USD (${service.currencySymbol("usd")})`,
1041
- value: "usd",
1042
- countryCode: { US: true },
1043
- });
1044
-
1045
- array.push({
1046
- name: `GBP (${service.currencySymbol("gbp")})`,
1047
- value: "gbp",
1048
- countryCode: { GB: true, UK: true },
1049
- });
1050
-
1051
- array.push({
1052
- name: `CAD (${service.currencySymbol("cad")})`,
1053
- value: "cad",
1054
- countryCode: { CA: true },
1055
- });
1056
-
1057
- array.push({
1058
- name: `AUD (${service.currencySymbol("aud")})`,
1059
- value: "aud",
1060
- countryCode: { AU: true },
1061
- });
1062
-
1063
- array.push({
1064
- name: `NZD (${service.currencySymbol("nzd")})`,
1065
- value: "nzd",
1066
- countryCode: { NZ: true },
1067
- });
1068
-
1069
- array.push({
1070
- name: `SGD (${service.currencySymbol("sgd")})`,
1071
- value: "sgd",
1072
- countryCode: { SG: true },
1073
- });
1074
-
1075
- if (DefaultCountryID) {
1076
- var findMatch = array.findIndex(function (currency) {
1077
- return currency.countryCode[DefaultCountryID];
1078
- });
1079
-
1080
- const moveArrayItem = (array, fromIndex, toIndex) => {
1081
- const arr = [...array];
1082
- arr.splice(toIndex, 0, ...arr.splice(fromIndex, 1));
1083
- return arr;
1084
- };
1085
-
1086
- if (findMatch != -1) {
1087
- array = moveArrayItem(array, findMatch, 0);
1021
+ const SUPPORTED_CURRENCIES = [
1022
+ { code: "usd", countries: ["US"] },
1023
+ { code: "gbp", countries: ["GB", "UK"] },
1024
+ { code: "cad", countries: ["CA"] },
1025
+ { code: "aud", countries: ["AU"] },
1026
+ { code: "nzd", countries: ["NZ"] },
1027
+ { code: "sgd", countries: ["SG"] },
1028
+ ];
1029
+
1030
+ service.getAvailableCurrencies = function (defaultCountryID) {
1031
+ let currencies = SUPPORTED_CURRENCIES.map(({ code, countries }) => ({
1032
+ name: `${code.toUpperCase()} (${service.currencySymbol(code)})`,
1033
+ value: code,
1034
+ countryCode: Object.fromEntries(countries.map((c) => [c, true])),
1035
+ }));
1036
+
1037
+ if (defaultCountryID) {
1038
+ const idx = currencies.findIndex((c) => c.countryCode[defaultCountryID]);
1039
+ if (idx > 0) {
1040
+ const [match] = currencies.splice(idx, 1);
1041
+ currencies.unshift(match);
1088
1042
  }
1089
1043
  }
1090
1044
 
1091
- return array;
1045
+ return currencies;
1092
1046
  };
1093
1047
 
1094
1048
  ///////////////////////////////////////////////////////////////////////////////
@@ -1396,34 +1350,18 @@ service.errorMessage = function (err) {
1396
1350
  err = err[0];
1397
1351
  }
1398
1352
 
1399
- ////////////////////////////////////
1400
-
1401
- var candidates = ["response.data.message", "response.data", "message"];
1402
-
1403
- var message;
1404
-
1405
- candidates.forEach(function (path) {
1406
- if (message) {
1407
- return;
1408
- }
1409
-
1410
- var m = _get(err, path);
1411
- if (m) {
1412
- message = m;
1413
- }
1414
- });
1353
+ // Walk through the error object to find the most useful message
1354
+ let message =
1355
+ err?.response?.data?.message ||
1356
+ (typeof err?.response?.data === "string" ? err.response.data : null) ||
1357
+ err?.message ||
1358
+ null;
1415
1359
 
1416
1360
  if (Array.isArray(message)) {
1417
1361
  message = message[0];
1418
1362
  }
1419
1363
 
1420
- ////////////////////////////////////
1421
-
1422
- message = message || JSON.stringify(err);
1423
-
1424
- ////////////////////////////////////
1425
-
1426
- return message;
1364
+ return message || JSON.stringify(err);
1427
1365
  };
1428
1366
 
1429
1367
  ////////////////////////////////////
@@ -1437,19 +1375,14 @@ service.errorMessage = function (err) {
1437
1375
  * sdk.utils.guid()
1438
1376
  */
1439
1377
  service.guid = function () {
1440
- var u =
1441
- new Date().getTime().toString(16) +
1442
- Math.random().toString(16).substring(2) +
1443
- "0".repeat(16);
1444
- return (
1445
- u.substr(0, 8) +
1446
- "-" +
1447
- u.substr(8, 4) +
1448
- "-4000-8" +
1449
- u.substr(12, 3) +
1450
- "-" +
1451
- u.substr(15, 12)
1452
- );
1378
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
1379
+ return crypto.randomUUID();
1380
+ }
1381
+ // Fallback for environments without crypto.randomUUID
1382
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
1383
+ const r = (Math.random() * 16) | 0;
1384
+ return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
1385
+ });
1453
1386
  };
1454
1387
 
1455
1388
  ////////////////////////////////////
@@ -1483,87 +1416,59 @@ export default service;
1483
1416
 
1484
1417
  /////////////////////////////////////////////
1485
1418
 
1486
- //Export the event dispatcher
1419
+ /**
1420
+ * A lightweight event emitter that can be attached to any service object
1421
+ * to provide pub/sub event capabilities. Can be called with or without `new`.
1422
+ */
1487
1423
  export function EventDispatcher() {
1488
- var listeners = {};
1489
-
1490
- /////////////////////////////////////////////
1491
-
1492
- var dispatcher = {};
1493
-
1494
- /////////////////////////////////////////////
1495
-
1496
- //Remove all listeners
1497
- dispatcher.removeAllListeners = function () {
1498
- listeners = {};
1499
- };
1500
-
1501
- /////////////////////////////////////////////
1502
-
1503
- dispatcher.dispatch = function (event, details) {
1504
- if (listeners[event]) {
1505
- //For each listener
1506
- listeners[event].forEach(function (callback) {
1507
- return callback(details);
1508
- });
1509
- }
1510
- };
1511
-
1512
- /////////////////////////////////////////////
1513
-
1514
- dispatcher.addEventListener = function (event, callback) {
1515
- if (!listeners[event]) {
1516
- listeners[event] = [];
1517
- }
1518
-
1519
- const type = typeof callback;
1520
- if (type !== "function") {
1521
- throw new Error(
1522
- `addEventListener() requires a function but ${type} was added`,
1523
- );
1524
- } else {
1525
- if (listeners[event].indexOf(callback) == -1) {
1526
- //Add to the listeners
1527
- listeners[event].push(callback);
1528
- } else {
1529
- //A
1530
- //lready listening
1424
+ const handlers = new Map();
1425
+
1426
+ const emitter = {
1427
+ dispatch(event, details) {
1428
+ const fns = handlers.get(event);
1429
+ if (fns) {
1430
+ for (const fn of fns) {
1431
+ fn(details);
1432
+ }
1531
1433
  }
1532
- }
1533
- };
1534
-
1535
- /////////////////////////////////////////////
1536
-
1537
- dispatcher.removeEventListener = function (event, callback) {
1538
- if (!listeners[event]) {
1539
- listeners[event] = [];
1540
- }
1541
-
1542
- //Get the index of the listener
1543
- var index = listeners[event].indexOf(callback);
1434
+ },
1544
1435
 
1545
- if (index != -1) {
1546
- //Remove from the listeners
1547
- listeners[event].splice(index, 1);
1548
- }
1549
- };
1436
+ addEventListener(event, callback) {
1437
+ if (typeof callback !== "function") {
1438
+ throw new TypeError(
1439
+ `Expected a function for addEventListener, received ${typeof callback}`,
1440
+ );
1441
+ }
1550
1442
 
1551
- /////////////////////////////////////////////
1443
+ if (!handlers.has(event)) {
1444
+ handlers.set(event, new Set());
1445
+ }
1552
1446
 
1553
- //Wrap the event listener functionality
1554
- dispatcher.bootstrap = function (service) {
1555
- if (!service) {
1556
- // console.log('No service to bootstrap to')
1557
- return;
1558
- }
1447
+ handlers.get(event).add(callback);
1448
+ },
1559
1449
 
1560
- service.dispatch = dispatcher.dispatch;
1561
- service.addEventListener = dispatcher.addEventListener;
1562
- service.removeEventListener = dispatcher.removeEventListener;
1563
- service.removeAllListeners = dispatcher.removeAllListeners;
1450
+ removeEventListener(event, callback) {
1451
+ const fns = handlers.get(event);
1452
+ if (fns) {
1453
+ fns.delete(callback);
1454
+ if (fns.size === 0) {
1455
+ handlers.delete(event);
1456
+ }
1457
+ }
1458
+ },
1459
+
1460
+ removeAllListeners() {
1461
+ handlers.clear();
1462
+ },
1463
+
1464
+ bootstrap(target) {
1465
+ if (!target) return;
1466
+ target.dispatch = emitter.dispatch;
1467
+ target.addEventListener = emitter.addEventListener;
1468
+ target.removeEventListener = emitter.removeEventListener;
1469
+ target.removeAllListeners = emitter.removeAllListeners;
1470
+ },
1564
1471
  };
1565
1472
 
1566
- /////////////////////////////////////////////
1567
-
1568
- return dispatcher;
1473
+ return emitter;
1569
1474
  }
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.2.2";
1
+ export const version = "0.3.0";