@schibsted/account-sdk-browser 5.2.3 → 5.2.6

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/identity.js CHANGED
@@ -1,4 +1,4 @@
1
- /* Copyright 2024 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
1
+ /* Copyright 2026 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
2
2
  * See LICENSE.md in the project root.
3
3
  */
4
4
 
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /* Copyright 2024 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
1
+ /* Copyright 2026 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
2
2
  * See LICENSE.md in the project root.
3
3
  */
4
4
 
package/monetization.js CHANGED
@@ -1,4 +1,4 @@
1
- /* Copyright 2024 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
1
+ /* Copyright 2026 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
2
2
  * See LICENSE.md in the project root.
3
3
  */
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schibsted/account-sdk-browser",
3
- "version": "5.2.3",
3
+ "version": "5.2.6",
4
4
  "description": "Schibsted account SDK for browsers",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/payment.js CHANGED
@@ -1,4 +1,4 @@
1
- /* Copyright 2024 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
1
+ /* Copyright 2026 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
2
2
  * See LICENSE.md in the project root.
3
3
  */
4
4
 
@@ -0,0 +1,20 @@
1
+
2
+ /**
3
+ * Registers a component as a property on the provided global object, if not already registered, and dispatches an event to notify listeners.
4
+ * The event is dispatched on `document` and will have the name `$sch${componentClassName}:ready` and a `detail` property with the instance.
5
+ *
6
+ * @param {any} global typically `window`
7
+ * @param {string} componentClassName the name of the component to register, 'identity', 'monetization' or 'payment'
8
+ * @param {any} instance the instance of the component to register
9
+ * @returns {void}
10
+ */
11
+ export const registerGlobal = (global, componentClassName, instance) => {
12
+ const prefixedName = `sch${componentClassName}`;
13
+ if (!(global)[prefixedName]) {
14
+ (global)[prefixedName] = instance;
15
+ }
16
+ if (typeof global.dispatchEvent === 'function') {
17
+ global.dispatchEvent(new CustomEvent(`${prefixedName}:ready`, { detail: { instance } }));
18
+ }
19
+ }
20
+
package/src/identity.js CHANGED
@@ -15,6 +15,7 @@ import RESTClient from './RESTClient.js';
15
15
  import SDKError from './SDKError.js';
16
16
  import * as spidTalk from './spidTalk.js';
17
17
  import version from './version.js';
18
+ import { registerGlobal } from './global-registry.js';
18
19
 
19
20
  /**
20
21
  * @typedef {object} LoginOptions
@@ -218,6 +219,9 @@ export class Identity extends EventEmitter {
218
219
  this._setGlobalSessionServiceUrl(env);
219
220
 
220
221
  this._unblockSessionCall();
222
+
223
+ registerGlobal(window, 'Identity', this);
224
+
221
225
  }
222
226
 
223
227
  /**
@@ -13,6 +13,7 @@ import Cache from './cache.js';
13
13
  import * as spidTalk from './spidTalk.js';
14
14
  import SDKError from './SDKError.js';
15
15
  import version from './version.js';
16
+ import { registerGlobal } from './global-registry.js';
16
17
 
17
18
  const globalWindow = () => window;
18
19
 
@@ -39,12 +40,14 @@ export class Monetization extends EventEmitter {
39
40
  this.clientId = clientId;
40
41
  this.env = env;
41
42
  this.redirectUri = redirectUri;
43
+ this.pendingHasAccessRequests = {};
42
44
  this._setSpidServerUrl(env);
43
45
 
44
46
  if (sessionDomain) {
45
47
  assert(isUrl(sessionDomain), 'sessionDomain parameter is not a valid URL');
46
48
  this._setSessionServiceUrl(sessionDomain);
47
49
  }
50
+ registerGlobal(window, 'Monetization', this);
48
51
  }
49
52
 
50
53
  /**
@@ -97,13 +100,24 @@ export class Monetization extends EventEmitter {
97
100
  throw new SDKError(`'productIds' must be an array`);
98
101
  }
99
102
 
100
- const sortedIds = productIds.sort();
101
- const cacheKey = this._accessCacheKey(productIds, userId);
103
+ const sortedIds = [...productIds].sort();
104
+ const cacheKey = this._accessCacheKey(sortedIds, userId);
102
105
  let data = this.cache.get(cacheKey);
103
106
  if (!data) {
104
- data = await this._sessionService.get(`/hasAccess/${sortedIds.join(',')}`);
105
- const expiresSeconds = data.ttl;
106
- this.cache.set(cacheKey, data, expiresSeconds * 1000);
107
+ if (!this.pendingHasAccessRequests[cacheKey]) {
108
+ this.pendingHasAccessRequests[cacheKey] = this._sessionService.get(`/hasAccess/${sortedIds.join(',')}`);
109
+ }
110
+ const promise = this.pendingHasAccessRequests[cacheKey];
111
+ try {
112
+ data = await promise;
113
+ const expiresSeconds = data.ttl;
114
+ this.cache.set(cacheKey, data, expiresSeconds * 1000);
115
+ } finally {
116
+ // If it rejects, we still want to clear the pending request
117
+ if (this.pendingHasAccessRequests[cacheKey] === promise) {
118
+ delete this.pendingHasAccessRequests[cacheKey];
119
+ }
120
+ }
107
121
  }
108
122
 
109
123
  if (!data.entitled) {
@@ -131,7 +145,7 @@ export class Monetization extends EventEmitter {
131
145
  * @private
132
146
  */
133
147
  _accessCacheKey(productIds, userId) {
134
- return `prd_${productIds.sort()}_${userId}`;
148
+ return `prd_${[...productIds].sort()}_${userId}`;
135
149
  }
136
150
 
137
151
  /**
package/src/payment.js CHANGED
@@ -10,6 +10,7 @@ import { ENDPOINTS } from './config.js';
10
10
  import * as popup from './popup.js';
11
11
  import RESTClient from './RESTClient.js';
12
12
  import * as spidTalk from './spidTalk.js';
13
+ import { registerGlobal } from './global-registry.js';
13
14
 
14
15
  const globalWindow = () => window;
15
16
 
@@ -37,6 +38,7 @@ export class Payment {
37
38
  this.publisher = publisher;
38
39
  this._setSpidServerUrl(env);
39
40
  this._setBffServerUrl(env);
41
+ registerGlobal(window, 'Payment', this);
40
42
  }
41
43
 
42
44
  /**
package/src/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Automatically generated in 'npm version' by scripts/genversion.js
2
2
 
3
3
  'use strict'
4
- const version = '5.2.3';
4
+ const version = '5.2.6';
5
5
  export default version;