nimbu-js-sdk 2.0.0-beta.3 → 2.0.0-beta.4

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/dist/index.d.ts CHANGED
@@ -169,5 +169,7 @@ declare const Nimbu: {
169
169
  useSSO: (sso?: boolean) => boolean;
170
170
  };
171
171
  export type { ChannelConfig, Select, MultiSelect };
172
+ export type { IStorage, SyncStorage, AsyncStorage, IAjax, RequestMethod, RequestOptions, IProgressHandler, IOptions, AjaxHeaders, AjaxResponse, ISetOptions, IGetOptions, ICommonFields, Reference, NimbuObjectLike, PersistedNimbuCustomer, NimbuGalleryImageJSON, AuthOptions, InitialOAuth2AccessToken, APIRequestOptions, NimbuQueryOptions, IStateManager, ILoginOptions, ISessionManager, AttributeMap, AtomicsMap, ObjectCache, State, ObjectIdentifier, EncodeOptions, } from './internal';
172
173
  export { Atomic, NimbuAtomic, NimbuACL, NimbuAPI, NimbuCloud, NimbuCollection, NimbuCoupon, NimbuCustomer, NimbuDevice, NimbuFile, NimbuGallery, NimbuGalleryImage, NimbuObject, NimbuOrder, NimbuPage, NimbuProduct, NimbuProductAggregate, NimbuPush, NimbuQuery, NimbuRelation, NimbuRole, NimbuSelectOption, NimbuSelectOptionList, NimbuError, };
174
+ export { OAuth2, TaskQueue, LocalStorage, MemoryStorage, BrowserSessionStorage } from './internal';
173
175
  export default Nimbu;
@@ -31,12 +31,14 @@ export * from './sdk/role';
31
31
  export * from './sdk/oauth2';
32
32
  export * from './sdk/select-option';
33
33
  export * from './sdk/select-option-list';
34
+ export * from './sdk/default-storages';
34
35
  export * from './sdk/storage';
35
36
  export * from './sdk/state';
36
37
  export * from './utils/array-contains-object';
37
38
  export * from './utils/decode';
38
39
  export * from './utils/encode';
39
40
  export * from './utils/promise-utils';
41
+ export * from './utils/sdk-type-guards';
40
42
  export * from './utils/traverse';
41
43
  export * from './utils/unique';
42
44
  export * from './utils/task-queue';
@@ -6,7 +6,7 @@ var _ = require('underscore');
6
6
  var cryptoJs = require('crypto-js');
7
7
  var AES = require('crypto-js/aes');
8
8
 
9
- var VERSION = '2.0.0-beta.3';
9
+ var VERSION = '2.0.0-beta.4';
10
10
 
11
11
  function _arrayLikeToArray(r, a) {
12
12
  (null == a || a > r.length) && (a = r.length);
@@ -2516,6 +2516,92 @@ var StateManager = {
2516
2516
  setServerData: setServerData
2517
2517
  };
2518
2518
 
2519
+ var memMap = {};
2520
+ var MemoryStorage = {
2521
+ async: 0,
2522
+ getItem: function getItem(path) {
2523
+ if (Object.prototype.hasOwnProperty.call(memMap, path)) {
2524
+ return memMap[path];
2525
+ }
2526
+ return null;
2527
+ },
2528
+ setItem: function setItem(path, value) {
2529
+ memMap[path] = String(value);
2530
+ return memMap[path];
2531
+ },
2532
+ removeItem: function removeItem(path) {
2533
+ delete memMap[path];
2534
+ },
2535
+ getAllKeys: function getAllKeys() {
2536
+ return Object.keys(memMap);
2537
+ },
2538
+ clear: function clear() {
2539
+ for (var key in memMap) {
2540
+ if (Object.prototype.hasOwnProperty.call(memMap, key)) {
2541
+ delete memMap[key];
2542
+ }
2543
+ }
2544
+ }
2545
+ };
2546
+ var BrowserSessionStorage = {
2547
+ async: 0,
2548
+ getItem: function getItem(path) {
2549
+ return window.sessionStorage.getItem(path);
2550
+ },
2551
+ setItem: function setItem(path, value) {
2552
+ window.sessionStorage.setItem(path, value);
2553
+ return value;
2554
+ },
2555
+ removeItem: function removeItem(path) {
2556
+ window.sessionStorage.removeItem(path);
2557
+ },
2558
+ getAllKeys: function getAllKeys() {
2559
+ var keys = [];
2560
+ for (var i = 0; i < window.sessionStorage.length; i++) {
2561
+ var key = window.sessionStorage.key(i);
2562
+ if (key !== null) keys.push(key);
2563
+ }
2564
+ return keys;
2565
+ },
2566
+ clear: function clear() {
2567
+ window.sessionStorage.clear();
2568
+ }
2569
+ };
2570
+ var LocalStorage = {
2571
+ async: 0,
2572
+ getItem: function getItem(path) {
2573
+ return window.localStorage.getItem(path);
2574
+ },
2575
+ setItem: function setItem(path, value) {
2576
+ window.localStorage.setItem(path, value);
2577
+ return value;
2578
+ },
2579
+ removeItem: function removeItem(path) {
2580
+ window.localStorage.removeItem(path);
2581
+ },
2582
+ getAllKeys: function getAllKeys() {
2583
+ var keys = [];
2584
+ for (var i = 0; i < window.localStorage.length; i++) {
2585
+ var key = window.localStorage.key(i);
2586
+ if (key !== null) keys.push(key);
2587
+ }
2588
+ return keys;
2589
+ },
2590
+ clear: function clear() {
2591
+ window.localStorage.clear();
2592
+ }
2593
+ };
2594
+ function pickDefaultStorage() {
2595
+ try {
2596
+ if (typeof window !== 'undefined' && window.localStorage) {
2597
+ return LocalStorage;
2598
+ }
2599
+ } catch (_unused) {
2600
+ // window.localStorage access can throw in privacy/sandbox modes — fall through.
2601
+ }
2602
+ return MemoryStorage;
2603
+ }
2604
+
2519
2605
  function requireMethods(name, methods, controller) {
2520
2606
  methods.forEach(function (func) {
2521
2607
  if (typeof controller[func] !== 'function') {
@@ -2705,6 +2791,12 @@ var Config = {
2705
2791
  config['Storage'] = storage;
2706
2792
  },
2707
2793
  getStorage: function getStorage() {
2794
+ if (!config['Storage']) {
2795
+ // Lazy default: pick LocalStorage in browsers, MemoryStorage otherwise.
2796
+ // Deferred from module load so SSR-rendered modules that never read
2797
+ // storage on the server don't bind MemoryStorage permanently.
2798
+ config['Storage'] = pickDefaultStorage();
2799
+ }
2708
2800
  return config['Storage'];
2709
2801
  },
2710
2802
  setSessionManager: function setSessionManager(manager) {
@@ -5413,82 +5505,10 @@ var Storage = {
5413
5505
  }))();
5414
5506
  }
5415
5507
  };
5416
- var memMap = {};
5417
- var MemoryStorage = {
5418
- async: 0,
5419
- getItem: function getItem(path) {
5420
- if (Object.prototype.hasOwnProperty.call(memMap, path)) {
5421
- return memMap[path];
5422
- }
5423
- return null;
5424
- },
5425
- setItem: function setItem(path, value) {
5426
- memMap[path] = String(value);
5427
- return memMap[path];
5428
- },
5429
- removeItem: function removeItem(path) {
5430
- delete memMap[path];
5431
- },
5432
- getAllKeys: function getAllKeys() {
5433
- return Object.keys(memMap);
5434
- },
5435
- clear: function clear() {
5436
- for (var key in memMap) {
5437
- if (Object.prototype.hasOwnProperty.call(memMap, key)) {
5438
- delete memMap[key];
5439
- }
5440
- }
5441
- }
5442
- };
5443
- var BrowserSessionStorage = {
5444
- async: 0,
5445
- getItem: function getItem(path) {
5446
- return window.sessionStorage.getItem(path);
5447
- },
5448
- setItem: function setItem(path, value) {
5449
- window.sessionStorage.setItem(path, value);
5450
- return value;
5451
- },
5452
- removeItem: function removeItem(path) {
5453
- window.sessionStorage.removeItem(path);
5454
- },
5455
- getAllKeys: function getAllKeys() {
5456
- return Object.keys(window.sessionStorage);
5457
- },
5458
- clear: function clear() {
5459
- window.sessionStorage.clear();
5460
- }
5461
- };
5462
- var LocalStorage = {
5463
- async: 0,
5464
- getItem: function getItem(path) {
5465
- return window.localStorage.getItem(path);
5466
- },
5467
- setItem: function setItem(path, value) {
5468
- window.localStorage.setItem(path, value);
5469
- return value;
5470
- },
5471
- removeItem: function removeItem(path) {
5472
- window.localStorage.removeItem(path);
5473
- },
5474
- getAllKeys: function getAllKeys() {
5475
- return Object.keys(window.localStorage);
5476
- },
5477
- clear: function clear() {
5478
- window.localStorage.clear();
5479
- }
5480
- };
5481
5508
 
5482
- // detect if we have localStorage available
5483
- try {
5484
- if (typeof window !== 'undefined' && window.localStorage) {
5485
- Config.setStorage(LocalStorage);
5486
- } else {
5487
- Config.setStorage(MemoryStorage);
5488
- }
5489
- } catch (e) {
5490
- Config.setStorage(MemoryStorage);
5491
- }
5509
+ // Back-compat name kept for callers that imported `SessionStorage` directly
5510
+ // (e.g. oauth2.ts uses BrowserSessionStorage when localStorage is available,
5511
+ // MemoryStorage otherwise).
5492
5512
  var SessionStorage = typeof window !== 'undefined' && window.localStorage ? BrowserSessionStorage : MemoryStorage;
5493
5513
 
5494
5514
  var _NimbuCustomer;
@@ -6597,6 +6617,14 @@ var NimbuCoupon = /*#__PURE__*/function (_NimbuObject) {
6597
6617
  return NimbuCoupon;
6598
6618
  }(NimbuObject);
6599
6619
 
6620
+ // Ruby's Base64.encode64 wraps base64 output at 60 chars with newlines.
6621
+ // CryptoJS's base64 parser doesn't strip whitespace and silently produces
6622
+ // garbage bytes when those newlines reach AES.decrypt — yielding intermittent
6623
+ // "Malformed UTF-8 data" errors depending on whether the garbage happens to
6624
+ // be UTF-8 valid. Normalize the input.
6625
+ var stripBase64Whitespace = function stripBase64Whitespace(s) {
6626
+ return s.replace(/\s+/g, '');
6627
+ };
6600
6628
  var OAuth2SessionManager = {
6601
6629
  logIn: (/*#__PURE__*/function () {
6602
6630
  var _logIn = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(email, password, options) {
@@ -6861,7 +6889,7 @@ var BasicSessionManager = {
6861
6889
  case 12:
6862
6890
  if (sso_data != null && sso_url != null) {
6863
6891
  try {
6864
- sso_id = AES.decrypt(decodeURIComponent(sso_data), Config.accessToken).toString(cryptoJs.enc.Utf8);
6892
+ sso_id = AES.decrypt(stripBase64Whitespace(decodeURIComponent(sso_data)), Config.accessToken).toString(cryptoJs.enc.Utf8);
6865
6893
  } catch (e) {
6866
6894
  sso_id = 'unauthenticated';
6867
6895
  }
@@ -6898,7 +6926,7 @@ var BasicSessionManager = {
6898
6926
  case 17:
6899
6927
  _yield$_request4 = _context6.v;
6900
6928
  body = _yield$_request4.body;
6901
- sharedSSOSecret = AES.decrypt(body.shared_secret, Config.accessToken).toString(cryptoJs.enc.Utf8);
6929
+ sharedSSOSecret = AES.decrypt(stripBase64Whitespace(body.shared_secret), Config.accessToken).toString(cryptoJs.enc.Utf8);
6902
6930
  headers = {};
6903
6931
  headers._clientVersion = VERSION;
6904
6932
  headers._accessToken = Config.accessToken;
@@ -6913,7 +6941,7 @@ var BasicSessionManager = {
6913
6941
  xhr = _yield$Config$ajax.xhr;
6914
6942
  model = NimbuObject._create('customers');
6915
6943
  serverAttrs = model.parse(customerBody, status, xhr);
6916
- serverAttrs.session_token = AES.decrypt(serverAttrs.session_token, sharedSSOSecret).toString(cryptoJs.enc.Utf8);
6944
+ serverAttrs.session_token = AES.decrypt(stripBase64Whitespace(serverAttrs.session_token), sharedSSOSecret).toString(cryptoJs.enc.Utf8);
6917
6945
  sharedSSOSecret = null;
6918
6946
  model._finishFetch(serverAttrs);
6919
6947
  NimbuCustomer.setCurrent(model);
@@ -9018,6 +9046,9 @@ var Nimbu = {
9018
9046
  };
9019
9047
 
9020
9048
  exports.Atomic = Atomic;
9049
+ exports.BrowserSessionStorage = BrowserSessionStorage;
9050
+ exports.LocalStorage = LocalStorage;
9051
+ exports.MemoryStorage = MemoryStorage;
9021
9052
  exports.NimbuACL = NimbuACL;
9022
9053
  exports.NimbuAPI = NimbuAPI;
9023
9054
  exports.NimbuAtomic = NimbuAtomic;
@@ -9041,5 +9072,7 @@ exports.NimbuRelation = NimbuRelation;
9041
9072
  exports.NimbuRole = NimbuRole;
9042
9073
  exports.NimbuSelectOption = NimbuSelectOption;
9043
9074
  exports.NimbuSelectOptionList = NimbuSelectOptionList;
9075
+ exports.OAuth2 = OAuth2;
9076
+ exports.TaskQueue = TaskQueue;
9044
9077
  exports.default = Nimbu;
9045
9078
  //# sourceMappingURL=nimbu-js-sdk.cjs.development.js.map