@schibsted/account-sdk-browser 5.2.7 → 6.0.0-alpha.10
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/README.md +46 -301
- package/dist/account-sdk.d.ts +22 -0
- package/dist/account.d.ts +179 -0
- package/dist/cache.d.ts +65 -0
- package/dist/config.d.ts +86 -0
- package/dist/globals.d.ts +11 -0
- package/dist/has-access-response.d.ts +2 -0
- package/dist/identity.d.ts +302 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +828 -0
- package/dist/index.js.map +1 -0
- package/dist/monetization.d.ts +39 -0
- package/{src → dist}/object.d.ts +4 -9
- package/dist/popup.d.ts +9 -0
- package/dist/resolve-browser-window.d.ts +1 -0
- package/dist/rest-client.d.ts +102 -0
- package/dist/rest-clients.d.ts +10 -0
- package/dist/sdk-error.d.ts +29 -0
- package/dist/session-response.d.ts +54 -0
- package/{src/spidTalk.d.ts → dist/spid-talk.d.ts} +4 -6
- package/dist/types/account.d.ts +48 -0
- package/dist/types/common.d.ts +28 -0
- package/dist/types/identity.d.ts +23 -0
- package/dist/types/login.d.ts +125 -0
- package/dist/types/monetization.d.ts +26 -0
- package/dist/types/session.d.ts +146 -0
- package/dist/url.d.ts +8 -0
- package/dist/validate.d.ts +50 -0
- package/dist/version.d.ts +2 -0
- package/package.json +34 -52
- package/src/account-sdk.ts +92 -0
- package/src/account.ts +332 -0
- package/src/{cache.js → cache.ts} +53 -38
- package/src/{config.js → config.ts} +7 -32
- package/src/globals.ts +8 -0
- package/src/has-access-response.ts +35 -0
- package/src/identity.ts +1023 -0
- package/src/index.ts +40 -0
- package/src/monetization.ts +128 -0
- package/src/{object.js → object.ts} +9 -16
- package/src/popup.ts +74 -0
- package/src/resolve-browser-window.ts +11 -0
- package/src/rest-client.ts +253 -0
- package/src/rest-clients.ts +30 -0
- package/src/sdk-error.ts +61 -0
- package/src/session-response.ts +100 -0
- package/src/{spidTalk.js → spid-talk.ts} +10 -12
- package/src/types/account.ts +55 -0
- package/src/types/common.ts +32 -0
- package/src/types/identity.ts +26 -0
- package/src/types/login.ts +150 -0
- package/src/types/monetization.ts +29 -0
- package/src/types/session.ts +169 -0
- package/src/{url.js → url.ts} +6 -10
- package/src/{validate.js → validate.ts} +27 -43
- package/src/{version.js → version.ts} +1 -2
- package/es5/global.js +0 -12968
- package/es5/global.js.map +0 -1
- package/es5/global.min.js +0 -2
- package/es5/global.min.js.map +0 -1
- package/es5/identity.js +0 -12212
- package/es5/identity.js.map +0 -1
- package/es5/identity.min.js +0 -2
- package/es5/identity.min.js.map +0 -1
- package/es5/index.js +0 -12940
- package/es5/index.js.map +0 -1
- package/es5/index.min.js +0 -2
- package/es5/index.min.js.map +0 -1
- package/es5/monetization.js +0 -9176
- package/es5/monetization.js.map +0 -1
- package/es5/monetization.min.js +0 -2
- package/es5/monetization.min.js.map +0 -1
- package/es5/payment.js +0 -8853
- package/es5/payment.js.map +0 -1
- package/es5/payment.min.js +0 -2
- package/es5/payment.min.js.map +0 -1
- package/identity.d.ts +0 -1
- package/identity.js +0 -5
- package/index.d.ts +0 -4
- package/index.js +0 -9
- package/monetization.d.ts +0 -1
- package/monetization.js +0 -5
- package/payment.d.ts +0 -1
- package/payment.js +0 -5
- package/src/RESTClient.d.ts +0 -89
- package/src/RESTClient.js +0 -193
- package/src/SDKError.d.ts +0 -16
- package/src/SDKError.js +0 -55
- package/src/__mocks__/.eslintrc.yml +0 -8
- package/src/__mocks__/RESTClient.js +0 -54
- package/src/cache.d.ts +0 -64
- package/src/config.d.ts +0 -34
- package/src/es5/global.js +0 -13
- package/src/es5/identity.js +0 -10
- package/src/es5/index.js +0 -13
- package/src/es5/monetization.js +0 -10
- package/src/es5/payment.js +0 -10
- package/src/global-registry.js +0 -20
- package/src/identity.d.ts +0 -679
- package/src/identity.js +0 -1157
- package/src/monetization.d.ts +0 -80
- package/src/monetization.js +0 -172
- package/src/payment.d.ts +0 -115
- package/src/payment.js +0 -211
- package/src/popup.d.ts +0 -10
- package/src/popup.js +0 -59
- package/src/url.d.ts +0 -10
- package/src/validate.d.ts +0 -64
- package/src/version.d.ts +0 -2
package/src/account.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/* Copyright 2026 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
|
|
2
|
+
* See LICENSE.md in the project root.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import Identity from './identity.js';
|
|
6
|
+
import Monetization from './monetization.js';
|
|
7
|
+
import { resolveBrowserWindow } from './resolve-browser-window.js';
|
|
8
|
+
import SDKError from './sdk-error.js';
|
|
9
|
+
import type { AccountConfig, AccountEventListener, AccountEventName } from './types/account.js';
|
|
10
|
+
import type {
|
|
11
|
+
LoginOptions,
|
|
12
|
+
SimplifiedLoginData,
|
|
13
|
+
SimplifiedLoginWidgetLoginOptions,
|
|
14
|
+
SimplifiedLoginWidgetOptions,
|
|
15
|
+
} from './types/login.js';
|
|
16
|
+
import type { HasAccessResult } from './types/monetization.js';
|
|
17
|
+
import type { ConnectedSessionResponse, SessionResponse, SessionUserId } from './types/session.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Unified client-side interface for a configured Schibsted Account integration.
|
|
21
|
+
*
|
|
22
|
+
* Create an instance with `createAccountSdk()` unless you explicitly need to construct it
|
|
23
|
+
* yourself. The instance coordinates identity, login, session, and access-check features for one
|
|
24
|
+
* configured client.
|
|
25
|
+
*
|
|
26
|
+
* @group Core API
|
|
27
|
+
*/
|
|
28
|
+
export class Account {
|
|
29
|
+
#identity: Identity;
|
|
30
|
+
#monetization: Monetization;
|
|
31
|
+
#defaultProductIds?: string[];
|
|
32
|
+
|
|
33
|
+
constructor(config: AccountConfig) {
|
|
34
|
+
const browserWindow = resolveBrowserWindow(config.window);
|
|
35
|
+
|
|
36
|
+
if (!browserWindow) {
|
|
37
|
+
throw new SDKError('Schibsted Account SDK is not available in this browser context');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (browserWindow.schAccount) {
|
|
41
|
+
throw new SDKError('Schibsted Account SDK has already been initialized');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const { log, callbackBeforeRedirect, defaultProductIds, varnish } = config;
|
|
45
|
+
const sharedConfig = {
|
|
46
|
+
clientId: config.clientId,
|
|
47
|
+
redirectUri: config.redirectUri,
|
|
48
|
+
sessionDomain: config.sessionDomain,
|
|
49
|
+
env: config.env,
|
|
50
|
+
window: browserWindow,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
this.#identity = new Identity({
|
|
54
|
+
...sharedConfig,
|
|
55
|
+
log,
|
|
56
|
+
callbackBeforeRedirect,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (defaultProductIds !== undefined && !Array.isArray(defaultProductIds)) {
|
|
60
|
+
throw new SDKError('Account defaultProductIds config must be an array');
|
|
61
|
+
}
|
|
62
|
+
this.#defaultProductIds = defaultProductIds;
|
|
63
|
+
|
|
64
|
+
if (varnish) {
|
|
65
|
+
if (typeof varnish !== 'object' || Array.isArray(varnish)) {
|
|
66
|
+
throw new SDKError('Account varnish config must be an object');
|
|
67
|
+
}
|
|
68
|
+
this.#identity.enableVarnishCookie(varnish);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.#monetization = new Monetization(sharedConfig);
|
|
72
|
+
|
|
73
|
+
browserWindow.schAccount = this;
|
|
74
|
+
browserWindow.dispatchEvent(
|
|
75
|
+
new CustomEvent('schAccount:ready', { detail: { instance: this } }),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Queries Session Service and returns the current browser session state.
|
|
81
|
+
*
|
|
82
|
+
* This call may trigger a full-page session refresh redirect. Configure
|
|
83
|
+
* `callbackBeforeRedirect` if your app needs to persist state before that navigation.
|
|
84
|
+
*
|
|
85
|
+
* @throws {SDKError} If the Session Service call fails.
|
|
86
|
+
*/
|
|
87
|
+
hasSession(): Promise<SessionResponse> {
|
|
88
|
+
return this.#identity.hasSession();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Checks whether the browser currently has a Schibsted Account session.
|
|
93
|
+
*
|
|
94
|
+
* Returns `false` instead of throwing when the session lookup fails.
|
|
95
|
+
*/
|
|
96
|
+
isLoggedIn(): Promise<boolean> {
|
|
97
|
+
return this.#identity.isLoggedIn();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Checks whether the current user is connected to the configured client.
|
|
102
|
+
*
|
|
103
|
+
* A user can be logged in to Schibsted Account without being connected to your client, for
|
|
104
|
+
* example before accepting required terms.
|
|
105
|
+
*/
|
|
106
|
+
isConnected(): Promise<boolean> {
|
|
107
|
+
return this.#identity.isConnected();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Returns the connected user session data.
|
|
112
|
+
*
|
|
113
|
+
* @throws {SDKError} If the user is not connected to the configured client.
|
|
114
|
+
*/
|
|
115
|
+
getUser(): Promise<ConnectedSessionResponse> {
|
|
116
|
+
return this.#identity.getUser();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns the realm-specific Schibsted Account user id as a number.
|
|
121
|
+
*
|
|
122
|
+
* Use `getUserUuid()` when you need the globally unique user id.
|
|
123
|
+
*
|
|
124
|
+
* @throws {SDKError} If the user is not connected to the configured client.
|
|
125
|
+
*/
|
|
126
|
+
getUserId(): Promise<number> {
|
|
127
|
+
return this.#identity.getUserId();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Returns an external identifier for the authenticated user, scoped to the configured client
|
|
132
|
+
* and external party.
|
|
133
|
+
*
|
|
134
|
+
* @param externalParty - External party for which the identifier is generated.
|
|
135
|
+
* @param optionalSuffix - Optional suffix included when generating the identifier.
|
|
136
|
+
* @throws {SDKError} If the session lookup fails, the session does not contain a `pairId`, or
|
|
137
|
+
* `externalParty` is empty.
|
|
138
|
+
* @deprecated Retained for compatibility with existing integrations. Avoid new usage.
|
|
139
|
+
*/
|
|
140
|
+
getExternalId(externalParty: string, optionalSuffix: string = ''): Promise<string> {
|
|
141
|
+
return this.#identity.getExternalId(externalParty, optionalSuffix);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns the globally unique user id from the current session.
|
|
146
|
+
*
|
|
147
|
+
* @throws {SDKError} If the user is not connected to the configured client.
|
|
148
|
+
*/
|
|
149
|
+
getUserUuid(): Promise<string> {
|
|
150
|
+
return this.#identity.getUserUuid();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Returns the user SDRN from the current session.
|
|
155
|
+
*
|
|
156
|
+
* @throws {SDKError} If the SDRN is missing from the session response.
|
|
157
|
+
*/
|
|
158
|
+
getUserSDRN(): Promise<string> {
|
|
159
|
+
return this.#identity.getUserSDRN();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Returns minimal user context for simplified login prompts.
|
|
164
|
+
*
|
|
165
|
+
* Returns `null` if the global account session cannot be read.
|
|
166
|
+
*/
|
|
167
|
+
getUserContextData(): Promise<SimplifiedLoginData | null> {
|
|
168
|
+
return this.#identity.getUserContextData();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Returns the Varnish `sp_id` value from the current session when present.
|
|
173
|
+
*/
|
|
174
|
+
getSpId(): Promise<string | null> {
|
|
175
|
+
return this.#identity.getSpId();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Starts the login flow with either a full-page redirect or a popup.
|
|
180
|
+
*
|
|
181
|
+
* Call this from a user gesture when `preferPopup` is enabled, otherwise browser popup blockers
|
|
182
|
+
* may block the popup and the SDK will fall back to redirect navigation.
|
|
183
|
+
*
|
|
184
|
+
* @param options - Login URL parameters.
|
|
185
|
+
* @returns A popup window reference when a popup is opened; otherwise `null`.
|
|
186
|
+
*/
|
|
187
|
+
login(options: LoginOptions): Window | null {
|
|
188
|
+
return this.#identity.login(options);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Logs the user out from the brand Session Service session and redirects the browser.
|
|
193
|
+
*
|
|
194
|
+
* @param redirectUri - Where Session Service should redirect the browser after logout.
|
|
195
|
+
*/
|
|
196
|
+
logout(redirectUri?: string): void {
|
|
197
|
+
this.#identity.logout(redirectUri);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Builds the login URL without navigating the browser.
|
|
202
|
+
*
|
|
203
|
+
* @param options - Login URL parameters.
|
|
204
|
+
*/
|
|
205
|
+
loginUrl(options: LoginOptions): string {
|
|
206
|
+
return this.#identity.loginUrl(options);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Builds the logout URL without navigating the browser.
|
|
211
|
+
*
|
|
212
|
+
* @param redirectUri - Where Session Service should redirect the browser after logout.
|
|
213
|
+
*/
|
|
214
|
+
logoutUrl(redirectUri?: string): string {
|
|
215
|
+
return this.#identity.logoutUrl(redirectUri);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Builds the Schibsted Account profile page URL.
|
|
220
|
+
*
|
|
221
|
+
* @param redirectUri - Where the account page should redirect the browser afterwards.
|
|
222
|
+
*/
|
|
223
|
+
accountUrl(redirectUri?: string): string {
|
|
224
|
+
return this.#identity.accountUrl(redirectUri);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Loads and displays the simplified login widget when user context is available.
|
|
229
|
+
*
|
|
230
|
+
* Your application decides how often to call this method. The `state` field in `loginParams`
|
|
231
|
+
* may be a string or a function that resolves to a string.
|
|
232
|
+
* Widget open and cancel events can be observed with `on()`.
|
|
233
|
+
*
|
|
234
|
+
* @returns `true` when the widget was displayed.
|
|
235
|
+
* @throws {SDKError} If user context data is missing or the widget script cannot be loaded.
|
|
236
|
+
*/
|
|
237
|
+
showSimplifiedLoginWidget(
|
|
238
|
+
loginParams: SimplifiedLoginWidgetLoginOptions,
|
|
239
|
+
options?: SimplifiedLoginWidgetOptions,
|
|
240
|
+
): Promise<boolean> {
|
|
241
|
+
return this.#identity.showSimplifiedLoginWidget(loginParams, options);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Enables or disables caching for subsequent session lookups.
|
|
246
|
+
*
|
|
247
|
+
* Changing this setting does not remove an existing cached session.
|
|
248
|
+
*/
|
|
249
|
+
setSessionCachingEnabled(enabled: boolean): void {
|
|
250
|
+
this.#identity.setSessionCachingEnabled(enabled);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Removes the cached Session Service user session response.
|
|
255
|
+
*/
|
|
256
|
+
clearCachedUserSession(): void {
|
|
257
|
+
this.#identity.clearCachedUserSession();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Checks whether the current user has access to any of the requested products or features.
|
|
262
|
+
*
|
|
263
|
+
* When `productIds` is omitted, the ids configured as `defaultProductIds` are used.
|
|
264
|
+
* Results are cached according to the TTL returned by Session Service.
|
|
265
|
+
*
|
|
266
|
+
* @param productIds - Product or feature ids to check instead of the configured defaults.
|
|
267
|
+
* @returns Access data, or `null` when the user is not entitled.
|
|
268
|
+
* @throws {SDKError} If product ids are not provided or configured, `sessionDomain` is missing,
|
|
269
|
+
* the current user cannot be resolved, input is invalid, or the request fails.
|
|
270
|
+
*/
|
|
271
|
+
async hasAccess(productIds = this.#defaultProductIds): Promise<HasAccessResult | null> {
|
|
272
|
+
if (!productIds) {
|
|
273
|
+
throw new SDKError(
|
|
274
|
+
'hasAccess requires productIds or defaultProductIds in the Account config',
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
if (productIds.length === 0) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const userId = await this.#identity.getUserId();
|
|
282
|
+
return this.#monetization.hasAccess(productIds, userId);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Removes a cached access-check result.
|
|
287
|
+
*
|
|
288
|
+
* @param productIds - Product or feature ids used for the cached lookup.
|
|
289
|
+
* @param userId - User id used for the cached lookup.
|
|
290
|
+
*/
|
|
291
|
+
clearCachedAccessResult(productIds: string[], userId: SessionUserId): void {
|
|
292
|
+
this.#monetization.clearCachedAccessResult(productIds, userId);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Subscribes to Account events.
|
|
297
|
+
*
|
|
298
|
+
* Supported event names are defined by {@link AccountEventName}.
|
|
299
|
+
*
|
|
300
|
+
* @param event - Event name.
|
|
301
|
+
* @param listener - Listener that receives an `Event` or `CustomEvent`.
|
|
302
|
+
*/
|
|
303
|
+
on<EventName extends AccountEventName>(
|
|
304
|
+
event: EventName,
|
|
305
|
+
listener: AccountEventListener<EventName>,
|
|
306
|
+
): this;
|
|
307
|
+
on(event: AccountEventName, listener: EventListener): this {
|
|
308
|
+
this.#identity.addEventListener(event, listener);
|
|
309
|
+
|
|
310
|
+
return this;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Unsubscribes from Account events.
|
|
315
|
+
*
|
|
316
|
+
* Supported event names are defined by {@link AccountEventName}.
|
|
317
|
+
*
|
|
318
|
+
* @param event - Event name.
|
|
319
|
+
* @param listener - Listener to remove.
|
|
320
|
+
*/
|
|
321
|
+
off<EventName extends AccountEventName>(
|
|
322
|
+
event: EventName,
|
|
323
|
+
listener: AccountEventListener<EventName>,
|
|
324
|
+
): this;
|
|
325
|
+
off(event: AccountEventName, listener: EventListener): this {
|
|
326
|
+
this.#identity.removeEventListener(event, listener);
|
|
327
|
+
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export default Account;
|
|
@@ -2,45 +2,47 @@
|
|
|
2
2
|
* See LICENSE.md in the project root.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import SDKError from './SDKError.js';
|
|
5
|
+
import SDKError from './sdk-error.js';
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* Check whether we are able to use web storage
|
|
11
|
-
* @param
|
|
9
|
+
* @param storeProvider - A function to return a WebStorage instance (either
|
|
12
10
|
* `sessionStorage` or `localStorage` from a `Window` object)
|
|
13
11
|
* @private
|
|
14
|
-
* @returns {boolean}
|
|
15
12
|
*/
|
|
16
|
-
function webStorageWorks(storeProvider) {
|
|
13
|
+
function webStorageWorks(storeProvider: (() => Storage) | undefined): boolean {
|
|
17
14
|
if (!storeProvider) {
|
|
18
15
|
return false;
|
|
19
16
|
}
|
|
20
17
|
try {
|
|
21
18
|
const store = storeProvider();
|
|
22
|
-
const randomKey = 'x-x-x-x'.replace(/x/g, () => Math.random());
|
|
19
|
+
const randomKey = 'x-x-x-x'.replace(/x/g, () => String(Math.random()));
|
|
23
20
|
const testValue = 'TEST-VALUE';
|
|
24
21
|
store.setItem(randomKey, testValue);
|
|
25
22
|
const val = store.getItem(randomKey);
|
|
26
23
|
store.removeItem(randomKey);
|
|
27
|
-
return
|
|
28
|
-
} catch
|
|
24
|
+
return val === testValue;
|
|
25
|
+
} catch {
|
|
29
26
|
return false;
|
|
30
27
|
}
|
|
31
28
|
}
|
|
32
29
|
|
|
33
30
|
/**
|
|
34
|
-
*
|
|
31
|
+
* Cache implementation used when web storage is available. Wraps a `Storage` instance.
|
|
35
32
|
* @private
|
|
36
33
|
*/
|
|
37
34
|
class WebStorageCache {
|
|
35
|
+
store: Storage;
|
|
36
|
+
get: (key: string) => string | null;
|
|
37
|
+
set: (key: string, value: string) => void;
|
|
38
|
+
delete: (key: string) => void;
|
|
39
|
+
|
|
38
40
|
/**
|
|
39
41
|
* Create web storage cache object
|
|
40
|
-
* @param
|
|
42
|
+
* @param store - A reference to either `sessionStorage` or `localStorage` from a
|
|
41
43
|
* `Window` object
|
|
42
44
|
*/
|
|
43
|
-
constructor(store) {
|
|
45
|
+
constructor(store: Storage) {
|
|
44
46
|
this.store = store;
|
|
45
47
|
this.get = (key) => this.store.getItem(key);
|
|
46
48
|
this.set = (key, value) => this.store.setItem(key, value);
|
|
@@ -49,35 +51,49 @@ class WebStorageCache {
|
|
|
49
51
|
}
|
|
50
52
|
|
|
51
53
|
/**
|
|
52
|
-
*
|
|
54
|
+
* Cache implementation used when web storage is not available. Stores entries in an in-memory object.
|
|
53
55
|
* @private
|
|
54
56
|
*/
|
|
55
57
|
class LiteralCache {
|
|
58
|
+
store: Record<string, string>;
|
|
59
|
+
get: (key: string) => string | undefined;
|
|
60
|
+
set: (key: string, value: string) => void;
|
|
61
|
+
delete: (key: string) => void;
|
|
62
|
+
|
|
56
63
|
/**
|
|
57
64
|
* Create JS object literal cache object
|
|
58
65
|
*/
|
|
59
66
|
constructor() {
|
|
60
67
|
this.store = {};
|
|
61
68
|
this.get = (key) => this.store[key];
|
|
62
|
-
this.set = (key, value) =>
|
|
63
|
-
|
|
69
|
+
this.set = (key, value) => {
|
|
70
|
+
this.store[key] = value;
|
|
71
|
+
};
|
|
72
|
+
this.delete = (key) => {
|
|
73
|
+
delete this.store[key];
|
|
74
|
+
};
|
|
64
75
|
}
|
|
65
76
|
}
|
|
66
77
|
|
|
67
|
-
|
|
78
|
+
type CacheEntry = { expiresOn: number; value: unknown };
|
|
79
|
+
|
|
80
|
+
const maxExpiresIn = 2 ** 31 - 1;
|
|
68
81
|
|
|
69
82
|
/**
|
|
70
83
|
* Cache class that attempts WebStorage (session/local storage), and falls back to JS object literal
|
|
71
84
|
* @private
|
|
72
85
|
*/
|
|
73
86
|
export default class Cache {
|
|
87
|
+
cache: WebStorageCache | LiteralCache;
|
|
88
|
+
type: string;
|
|
89
|
+
|
|
74
90
|
/**
|
|
75
|
-
* @param
|
|
91
|
+
* @param storeProvider - A function to return a WebStorage instance (either
|
|
76
92
|
* `sessionStorage` or `localStorage` from a `Window` object)
|
|
77
93
|
* @throws {SDKError} - If sessionStorage or localStorage are not accessible
|
|
78
94
|
*/
|
|
79
|
-
constructor(storeProvider) {
|
|
80
|
-
if (webStorageWorks(storeProvider)) {
|
|
95
|
+
constructor(storeProvider?: () => Storage) {
|
|
96
|
+
if (storeProvider && webStorageWorks(storeProvider)) {
|
|
81
97
|
this.cache = new WebStorageCache(storeProvider());
|
|
82
98
|
this.type = 'WebStorage';
|
|
83
99
|
} else {
|
|
@@ -88,46 +104,46 @@ export default class Cache {
|
|
|
88
104
|
|
|
89
105
|
/**
|
|
90
106
|
* Get a value from cache (checks that the object has not expired)
|
|
91
|
-
* @param
|
|
107
|
+
* @param key
|
|
92
108
|
* @private
|
|
93
|
-
* @returns {*} - The value if it exists, otherwise null
|
|
94
109
|
*/
|
|
95
|
-
get(key) {
|
|
110
|
+
get(key: string): unknown {
|
|
96
111
|
/**
|
|
97
112
|
* JSON.parse safe wrapper
|
|
98
|
-
* @param
|
|
99
|
-
* @returns {*} parsed value or null if failed to parse
|
|
113
|
+
* @param raw
|
|
100
114
|
*/
|
|
101
|
-
function getObj(raw) {
|
|
115
|
+
function getObj(raw: string | null | undefined): CacheEntry | null {
|
|
116
|
+
if (raw == null) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
102
119
|
try {
|
|
103
120
|
return JSON.parse(raw);
|
|
104
|
-
} catch
|
|
121
|
+
} catch {
|
|
105
122
|
return null;
|
|
106
123
|
}
|
|
107
124
|
}
|
|
108
125
|
|
|
109
126
|
try {
|
|
110
127
|
const raw = this.cache.get(key);
|
|
111
|
-
|
|
128
|
+
const obj = getObj(raw);
|
|
112
129
|
if (obj && Number.isInteger(obj.expiresOn) && obj.expiresOn > Date.now()) {
|
|
113
130
|
return obj.value;
|
|
114
131
|
}
|
|
115
132
|
this.delete(key);
|
|
116
133
|
return null;
|
|
117
134
|
} catch (e) {
|
|
118
|
-
throw new SDKError(e);
|
|
135
|
+
throw new SDKError(String(e));
|
|
119
136
|
}
|
|
120
137
|
}
|
|
121
138
|
|
|
122
139
|
/**
|
|
123
140
|
* Set a cache entry
|
|
124
|
-
* @param
|
|
125
|
-
* @param
|
|
126
|
-
* @param
|
|
141
|
+
* @param key
|
|
142
|
+
* @param value
|
|
143
|
+
* @param expiresIn - Value in milliseconds until the entry expires
|
|
127
144
|
* @private
|
|
128
|
-
* @returns {void}
|
|
129
145
|
*/
|
|
130
|
-
set(key, value, expiresIn = 0) {
|
|
146
|
+
set(key: string, value: unknown, expiresIn = 0): void {
|
|
131
147
|
if (expiresIn <= 0) {
|
|
132
148
|
return;
|
|
133
149
|
}
|
|
@@ -138,21 +154,20 @@ export default class Cache {
|
|
|
138
154
|
this.cache.set(key, JSON.stringify({ expiresOn, value }));
|
|
139
155
|
setTimeout(() => this.delete(key), expiresIn);
|
|
140
156
|
} catch (e) {
|
|
141
|
-
throw new SDKError(e);
|
|
157
|
+
throw new SDKError(String(e));
|
|
142
158
|
}
|
|
143
159
|
}
|
|
144
160
|
|
|
145
161
|
/**
|
|
146
162
|
* Delete a cache entry
|
|
147
|
-
* @param
|
|
163
|
+
* @param key
|
|
148
164
|
* @private
|
|
149
|
-
* @returns {void}
|
|
150
165
|
*/
|
|
151
|
-
delete(key) {
|
|
166
|
+
delete(key: string): void {
|
|
152
167
|
try {
|
|
153
168
|
this.cache.delete(key);
|
|
154
169
|
} catch (e) {
|
|
155
|
-
throw new SDKError(e);
|
|
170
|
+
throw new SDKError(String(e));
|
|
156
171
|
}
|
|
157
172
|
}
|
|
158
173
|
}
|
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
* See LICENSE.md in the project root.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
'use strict';
|
|
6
|
-
|
|
7
5
|
/*
|
|
8
6
|
* This file declares configs that are essentially part of how the SDK works and interacts with our
|
|
9
7
|
* backend servers.
|
|
@@ -18,34 +16,11 @@
|
|
|
18
16
|
*/
|
|
19
17
|
|
|
20
18
|
/**
|
|
21
|
-
* Core configuration used by the SDK
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @prop {string} ENDPOINTS.SPiD.LOCAL - Local endpoint (for Identity team)
|
|
27
|
-
* @prop {string} ENDPOINTS.SPiD.DEV - Dev environment (for Identity team)
|
|
28
|
-
* @prop {string} ENDPOINTS.SPiD.PRE - Staging environment
|
|
29
|
-
* @prop {string} ENDPOINTS.SPiD.PRO - Production environment Sweden
|
|
30
|
-
* @prop {string} ENDPOINTS.SPiD.PRO_NO - Production environment Norway
|
|
31
|
-
* @prop {string} ENDPOINTS.SPiD.PRO_FI - Production environment Finland
|
|
32
|
-
* @prop {string} ENDPOINTS.SPiD.PRO_DK - Production environment Denmark
|
|
33
|
-
* @prop {object} ENDPOINTS.BFF - Endpoints used with new GDPR-compliant web flows
|
|
34
|
-
* @prop {string} ENDPOINTS.BFF.LOCAL - Local endpoint (for Identity team)
|
|
35
|
-
* @prop {string} ENDPOINTS.BFF.DEV - Dev environment (for Identity team)
|
|
36
|
-
* @prop {string} ENDPOINTS.BFF.PRE - Staging environment
|
|
37
|
-
* @prop {string} ENDPOINTS.BFF.PRO - Production environment Sweden
|
|
38
|
-
* @prop {string} ENDPOINTS.BFF.PRO_NO - Production environment Norway
|
|
39
|
-
* @prop {string} ENDPOINTS.BFF.PRO_FI - Production environment Finland
|
|
40
|
-
* @prop {string} ENDPOINTS.BFF.PRO_DK - Production environment Denmark
|
|
41
|
-
* @prop {object} ENDPOINTS.SESSION_SERVICE - Endpoints to check global user session data
|
|
42
|
-
* @prop {string} ENDPOINTS.SESSION_SERVICE.LOCAL - Local endpoint (for Identity team)
|
|
43
|
-
* @prop {string} ENDPOINTS.SESSION_SERVICE.DEV - Dev environment (for Identity team)
|
|
44
|
-
* @prop {string} ENDPOINTS.SESSION_SERVICE.PRE - Staging environment
|
|
45
|
-
* @prop {string} ENDPOINTS.SESSION_SERVICE.PRO - Production environment Sweden
|
|
46
|
-
* @prop {string} ENDPOINTS.SESSION_SERVICE.PRO_NO - Production environment Norway
|
|
47
|
-
* @prop {string} ENDPOINTS.SESSION_SERVICE.PRO_FI - Production environment Finland
|
|
48
|
-
* @prop {string} ENDPOINTS.SESSION_SERVICE.PRO_DK - Production environment Denmark
|
|
19
|
+
* Core configuration used by the SDK.
|
|
20
|
+
* - `ENDPOINTS.SPiD` — SPiD endpoints per environment
|
|
21
|
+
* - `ENDPOINTS.BFF` — Endpoints used with new GDPR-compliant web flows
|
|
22
|
+
* - `ENDPOINTS.SESSION_SERVICE` — Endpoints to check global user session data
|
|
23
|
+
*
|
|
49
24
|
*/
|
|
50
25
|
const config = {
|
|
51
26
|
ENDPOINTS: {
|
|
@@ -85,8 +60,8 @@ const config = {
|
|
|
85
60
|
PRO_NO: 'spid.no',
|
|
86
61
|
PRO_FI: 'schibsted.fi',
|
|
87
62
|
PRO_DK: 'schibsted.dk',
|
|
88
|
-
}
|
|
89
|
-
};
|
|
63
|
+
},
|
|
64
|
+
} as const;
|
|
90
65
|
|
|
91
66
|
export default config;
|
|
92
67
|
export const ENDPOINTS = config.ENDPOINTS;
|
package/src/globals.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/* Copyright 2026 Schibsted Products & Technology AS. Licensed under the terms of the MIT license.
|
|
2
|
+
* See LICENSE.md in the project root.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import SDKError from './sdk-error.js';
|
|
6
|
+
import type { HasAccessResult } from './types/monetization.js';
|
|
7
|
+
import { isObject, isStr } from './validate.js';
|
|
8
|
+
|
|
9
|
+
export function parseHasAccessResult(value: unknown): HasAccessResult {
|
|
10
|
+
if (!isObject(value)) {
|
|
11
|
+
throw new SDKError('Invalid hasAccess response');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { entitled, ttl, allowedFeatures, userId, uuid, sig } = value;
|
|
15
|
+
|
|
16
|
+
if (
|
|
17
|
+
typeof entitled !== 'boolean' ||
|
|
18
|
+
typeof ttl !== 'number' ||
|
|
19
|
+
!Array.isArray(allowedFeatures) ||
|
|
20
|
+
typeof userId !== 'number' ||
|
|
21
|
+
!isStr(uuid) ||
|
|
22
|
+
!isStr(sig)
|
|
23
|
+
) {
|
|
24
|
+
throw new SDKError('Invalid hasAccess response');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
entitled,
|
|
29
|
+
ttl,
|
|
30
|
+
allowedFeatures: allowedFeatures.filter(isStr),
|
|
31
|
+
userId,
|
|
32
|
+
uuid,
|
|
33
|
+
sig,
|
|
34
|
+
};
|
|
35
|
+
}
|