@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
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AccountBaseConfig, LogFunction } from './common';
|
|
2
|
+
import { IdentityBeforeRedirectCallback, VarnishCookieOptions } from './identity';
|
|
3
|
+
import { SessionResponse } from './session';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration used to create an {@link Account} instance.
|
|
6
|
+
*
|
|
7
|
+
* @group Configuration
|
|
8
|
+
*/
|
|
9
|
+
export type AccountConfig = AccountBaseConfig & {
|
|
10
|
+
/** Receives debug log information. If not set, no logging will be done. */
|
|
11
|
+
log?: LogFunction;
|
|
12
|
+
/** Callback triggered before a session refresh redirect happens. */
|
|
13
|
+
callbackBeforeRedirect?: IdentityBeforeRedirectCallback;
|
|
14
|
+
/** Product or feature ids used by `hasAccess()` when no ids are passed to the method. */
|
|
15
|
+
defaultProductIds?: string[];
|
|
16
|
+
/** Optional Varnish cookie setup applied during Account initialization. */
|
|
17
|
+
varnish?: VarnishCookieOptions;
|
|
18
|
+
};
|
|
19
|
+
type LoginRequestedEventDetail = {
|
|
20
|
+
url: string;
|
|
21
|
+
method: 'popup' | 'default';
|
|
22
|
+
};
|
|
23
|
+
type LogoutRequestedEventDetail = {
|
|
24
|
+
url: string;
|
|
25
|
+
};
|
|
26
|
+
export type AccountEventMap = {
|
|
27
|
+
/** Emitted with the login URL and method when `login()` initiates a login flow. */
|
|
28
|
+
loginRequested: CustomEvent<LoginRequestedEventDetail>;
|
|
29
|
+
/** Emitted with the logout URL when `logout()` is called. */
|
|
30
|
+
logoutRequested: CustomEvent<LogoutRequestedEventDetail>;
|
|
31
|
+
/** Emitted when `hasSession()` detects a change in the stored session state. */
|
|
32
|
+
sessionUpdated: CustomEvent<SessionResponse>;
|
|
33
|
+
/** Emitted when the simplified login widget is displayed. */
|
|
34
|
+
simplifiedLoginOpened: Event;
|
|
35
|
+
/** Emitted when the simplified login widget is closed. */
|
|
36
|
+
simplifiedLoginCancelled: Event;
|
|
37
|
+
};
|
|
38
|
+
/** Account event names emitted by the SDK. */
|
|
39
|
+
export type AccountEventName = keyof AccountEventMap;
|
|
40
|
+
/**
|
|
41
|
+
* Native event listener used by Account event subscriptions.
|
|
42
|
+
*
|
|
43
|
+
* Payload-bearing events are `CustomEvent` instances and expose their payload through `detail`.
|
|
44
|
+
*
|
|
45
|
+
* @group Events
|
|
46
|
+
*/
|
|
47
|
+
export type AccountEventListener<EventName extends AccountEventName = AccountEventName> = (event: AccountEventMap[EventName]) => void;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Log callback used by the SDK for debug output.
|
|
3
|
+
* Receives a pre-formatted log line.
|
|
4
|
+
*
|
|
5
|
+
* @group Configuration
|
|
6
|
+
*/
|
|
7
|
+
export type LogFunction = (message: string) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Supported Schibsted account environment keys.
|
|
10
|
+
*
|
|
11
|
+
* @group Configuration
|
|
12
|
+
*/
|
|
13
|
+
export type AccountEnvironment = 'LOCAL' | 'DEV' | 'PRE' | 'PRO' | 'PRO_NO' | 'PRO_FI' | 'PRO_DK';
|
|
14
|
+
/**
|
|
15
|
+
* Shared Account configuration fields.
|
|
16
|
+
*/
|
|
17
|
+
export type AccountBaseConfig = {
|
|
18
|
+
/** Mandatory client id. Example: "1234567890abcdef12345678" */
|
|
19
|
+
clientId: string;
|
|
20
|
+
/** Redirect uri. Example: "https://site.com" */
|
|
21
|
+
redirectUri: string;
|
|
22
|
+
/** Session Service domain. Example: "https://id.site.com" */
|
|
23
|
+
sessionDomain: string;
|
|
24
|
+
/** Schibsted account environment. Defaults to PRE. */
|
|
25
|
+
env?: AccountEnvironment;
|
|
26
|
+
/** Window object to use. Defaults to the global `window`. */
|
|
27
|
+
window?: Window;
|
|
28
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AccountBaseConfig, LogFunction } from './common';
|
|
2
|
+
/**
|
|
3
|
+
* Callback invoked before the SDK performs a full-page session refresh redirect.
|
|
4
|
+
*/
|
|
5
|
+
export type IdentityBeforeRedirectCallback = () => void | Promise<void>;
|
|
6
|
+
/**
|
|
7
|
+
* Options accepted by the {@link Identity} constructor.
|
|
8
|
+
*/
|
|
9
|
+
export type IdentityOptions = AccountBaseConfig & {
|
|
10
|
+
/** Receives debug log information. If not set, no logging will be done. */
|
|
11
|
+
log?: LogFunction;
|
|
12
|
+
/** Callback triggered before a session refresh redirect happens. */
|
|
13
|
+
callbackBeforeRedirect?: IdentityBeforeRedirectCallback;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Options for {@link Identity#enableVarnishCookie}.
|
|
17
|
+
*/
|
|
18
|
+
export type VarnishCookieOptions = {
|
|
19
|
+
/** Override number of seconds before the Varnish cookie expires. */
|
|
20
|
+
expiresIn?: number;
|
|
21
|
+
/** Override cookie domain. E.g. "vg.no" instead of "www.vg.no". */
|
|
22
|
+
domain?: string;
|
|
23
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options accepted by {@link Account#login} and related login URL helpers.
|
|
3
|
+
*
|
|
4
|
+
* @group Login & Widget
|
|
5
|
+
*/
|
|
6
|
+
export type LoginOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* An opaque value used by the client to maintain state between the request and callback.
|
|
9
|
+
* It's also recommended to prevent CSRF {@link https://tools.ietf.org/html/rfc6749#section-10.12}
|
|
10
|
+
*/
|
|
11
|
+
state: string;
|
|
12
|
+
/**
|
|
13
|
+
* Authentication Context Class Reference values. If omitted, the default account flow is used.
|
|
14
|
+
* Supported values include `password`, `otp`, `sms`, `otp-email`, `eid`, `eid-no`, `eid-se`,
|
|
15
|
+
* `eid-fi`, and `eid-dk`. Values other than `otp-email` may be combined as a space-separated
|
|
16
|
+
* string. Verify the AMR (Authentication Methods References) claim in the ID token on your
|
|
17
|
+
* backend if you need to enforce a completed authentication method.
|
|
18
|
+
*/
|
|
19
|
+
acrValues?: 'password' | 'otp' | 'sms' | 'eid-dk' | 'eid-no' | 'eid-se' | 'eid-fi' | 'eid' | 'otp-email' | (string & {});
|
|
20
|
+
/**
|
|
21
|
+
* The OAuth scopes for the tokens. This is a list of scopes, separated by space. If the list
|
|
22
|
+
* of scopes contains `openid`, the generated tokens includes the id token which can be useful
|
|
23
|
+
* for getting information about the user. Omitting scope is allowed, while `invalid_scope` is
|
|
24
|
+
* returned when the client asks for a scope you aren't allowed to request.
|
|
25
|
+
* {@link https://tools.ietf.org/html/rfc6749#section-3.3}
|
|
26
|
+
* Defaults to `openid`.
|
|
27
|
+
*/
|
|
28
|
+
scope?: 'openid' | (string & {});
|
|
29
|
+
/**
|
|
30
|
+
* Redirect uri that will receive the code. Must exactly match a redirectUri from your client
|
|
31
|
+
* in self-service
|
|
32
|
+
*/
|
|
33
|
+
redirectUri?: string;
|
|
34
|
+
/** Should we try to open a popup window? Defaults to `false`. */
|
|
35
|
+
preferPopup?: boolean;
|
|
36
|
+
/** user email or UUID hint */
|
|
37
|
+
loginHint?: string;
|
|
38
|
+
/** Pulse tag */
|
|
39
|
+
tag?: string;
|
|
40
|
+
/** Teaser slug. Teaser with given slug will be displayed in place of default teaser */
|
|
41
|
+
teaser?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Specifies the allowable elapsed time in seconds since the last time the End-User was actively
|
|
44
|
+
* authenticated. If last authentication time is more than maxAge seconds in the past,
|
|
45
|
+
* re-authentication will be required. See the OpenID Connect spec section 3.1.2.1 for more
|
|
46
|
+
* information. Prefer a number of seconds; strings remain accepted for legacy callers.
|
|
47
|
+
*/
|
|
48
|
+
maxAge?: number | string;
|
|
49
|
+
/**
|
|
50
|
+
* Optional parameter to overwrite client locale setting.
|
|
51
|
+
* New flows supports nb_NO, fi_FI, sv_SE, en_US
|
|
52
|
+
*/
|
|
53
|
+
locale?: 'nb_NO' | 'fi_FI' | 'sv_SE' | 'en_US';
|
|
54
|
+
/** display username and password on one screen. Defaults to `false`. */
|
|
55
|
+
oneStepLogin?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* String that specifies whether the Authorization Server prompts the End-User for
|
|
58
|
+
* reauthentication or confirm account screen. Supported values: `select_account` or `login`.
|
|
59
|
+
* Defaults to `select_account`.
|
|
60
|
+
*/
|
|
61
|
+
prompt?: 'select_account' | 'login';
|
|
62
|
+
/** Identifier for cross-domain tracking in Pulse */
|
|
63
|
+
xDomainId?: string;
|
|
64
|
+
/** Environment for cross-domain tracking in Pulse */
|
|
65
|
+
xEnvironmentId?: string;
|
|
66
|
+
/** Campaign identifier for tracking in Pulse */
|
|
67
|
+
originCampaign?: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Identical to {@link LoginOptions} except that `state` may also be a (possibly async) function;
|
|
71
|
+
* all other fields reuse the `LoginOptions` documentation.
|
|
72
|
+
*
|
|
73
|
+
* @group Login & Widget
|
|
74
|
+
*/
|
|
75
|
+
export type SimplifiedLoginWidgetLoginOptions = Omit<LoginOptions, 'state'> & {
|
|
76
|
+
/**
|
|
77
|
+
* An opaque value used by the client to maintain state between the request and callback.
|
|
78
|
+
* It's also recommended to prevent CSRF {@link https://tools.ietf.org/html/rfc6749#section-10.12}
|
|
79
|
+
*/
|
|
80
|
+
state: string | (() => string | Promise<string>);
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Minimal user information used by the simplified login widget flow.
|
|
84
|
+
*
|
|
85
|
+
* @group Login & Widget
|
|
86
|
+
*/
|
|
87
|
+
export type SimplifiedLoginData = {
|
|
88
|
+
/** Deprecated: User UUID used as `loginHint` for {@link Account#login}. */
|
|
89
|
+
identifier: string;
|
|
90
|
+
/** Human-readable user identifier */
|
|
91
|
+
display_text: string;
|
|
92
|
+
/** Client name */
|
|
93
|
+
client_name: string;
|
|
94
|
+
/** Provider id used by the simplified login widget when supplied by the backend. */
|
|
95
|
+
provider_id?: string;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Configuration options for {@link Account#showSimplifiedLoginWidget}.
|
|
99
|
+
*
|
|
100
|
+
* @group Login & Widget
|
|
101
|
+
*/
|
|
102
|
+
export type SimplifiedLoginWidgetOptions = {
|
|
103
|
+
/** expected encoding of simplified login widget. Could be utf-8 (default), iso-8859-1 or iso-8859-15 */
|
|
104
|
+
encoding?: string;
|
|
105
|
+
/**
|
|
106
|
+
* expected locale of simplified login widget. Should be provided in a short format like 'nb',
|
|
107
|
+
* 'sv'. If not set, a value from the env variable is used.
|
|
108
|
+
*/
|
|
109
|
+
locale?: 'nb' | 'sv' | 'fi' | 'da' | 'en';
|
|
110
|
+
};
|
|
111
|
+
export type SimplifiedLoginWidgetInitialParams = {
|
|
112
|
+
displayText: string;
|
|
113
|
+
env: string;
|
|
114
|
+
clientName: string;
|
|
115
|
+
clientId: string;
|
|
116
|
+
providerId?: string;
|
|
117
|
+
locale?: SimplifiedLoginWidgetOptions['locale'];
|
|
118
|
+
windowWidth: () => number;
|
|
119
|
+
windowOnResize: (handler: () => void) => void;
|
|
120
|
+
};
|
|
121
|
+
export type SimplifiedLoginWidgetHandler = () => void | Promise<void>;
|
|
122
|
+
export type OpenSimplifiedLoginWidget = (initialParams: SimplifiedLoginWidgetInitialParams, loginHandler: SimplifiedLoginWidgetHandler, loginNotYouHandler: SimplifiedLoginWidgetHandler, initHandler: SimplifiedLoginWidgetHandler, cancelLoginHandler: SimplifiedLoginWidgetHandler) => unknown;
|
|
123
|
+
export declare const hasOpenSimplifiedLoginWidget: (targetWindow: Window) => targetWindow is Window & {
|
|
124
|
+
openSimplifiedLoginWidget: OpenSimplifiedLoginWidget;
|
|
125
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AccountBaseConfig } from './common';
|
|
2
|
+
import { SessionUserId } from './session';
|
|
3
|
+
/**
|
|
4
|
+
* Result returned by the Session Service `/hasAccess` endpoint, describing whether the user
|
|
5
|
+
* is entitled to a requested set of products/features.
|
|
6
|
+
*
|
|
7
|
+
* @group Access Checks
|
|
8
|
+
*/
|
|
9
|
+
export interface HasAccessResult {
|
|
10
|
+
/** Whether the user is entitled to at least one of the requested products/features. */
|
|
11
|
+
entitled: boolean;
|
|
12
|
+
/** How long this result stays valid, in seconds, before the cache entry expires. */
|
|
13
|
+
ttl: number;
|
|
14
|
+
/** Feature ids the user is allowed to access. */
|
|
15
|
+
allowedFeatures: string[];
|
|
16
|
+
/** User id the access result belongs to. */
|
|
17
|
+
userId: SessionUserId;
|
|
18
|
+
/** Globally unique user id the access result belongs to. */
|
|
19
|
+
uuid: string;
|
|
20
|
+
/** Signed response payload. */
|
|
21
|
+
sig: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Options accepted by the {@link Monetization} constructor.
|
|
25
|
+
*/
|
|
26
|
+
export type MonetizationOptions = Pick<AccountBaseConfig, 'clientId' | 'env' | 'window'> & Partial<Pick<AccountBaseConfig, 'redirectUri' | 'sessionDomain'>>;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numeric Schibsted Account user id.
|
|
3
|
+
*
|
|
4
|
+
* @group Session
|
|
5
|
+
*/
|
|
6
|
+
export type SessionUserId = number;
|
|
7
|
+
/**
|
|
8
|
+
* Successful response returned from {@link Account#hasSession}.
|
|
9
|
+
*
|
|
10
|
+
* `result` means the has-session call itself succeeded. `result: true` means the user is
|
|
11
|
+
* connected to the merchant; `result: false` means a session may exist, but the user is not
|
|
12
|
+
* connected to this client.
|
|
13
|
+
*
|
|
14
|
+
* @group Session
|
|
15
|
+
*/
|
|
16
|
+
export type SessionSuccessResponse = {
|
|
17
|
+
/** Response status code. Example: 200. */
|
|
18
|
+
code?: number;
|
|
19
|
+
/** Response type. Example: 'OK'. */
|
|
20
|
+
type?: string;
|
|
21
|
+
/** Human-readable response description. Example: 'OK'. */
|
|
22
|
+
description?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Is the user connected to the merchant? This means the merchant id is in the list of
|
|
25
|
+
* merchants connected to this user.
|
|
26
|
+
*/
|
|
27
|
+
result: boolean;
|
|
28
|
+
/** Connection status string. Deprecated; use `Account.isConnected()`. Example: 'connected'. */
|
|
29
|
+
userStatus?: string;
|
|
30
|
+
/** Cookie base domain used as the `SP_ID` cookie domain fallback. Example: 'spid.no'. */
|
|
31
|
+
baseDomain?: string;
|
|
32
|
+
/** Legacy session id. Obsolete. Example: '58eca10fdbb9f6df72c3368f'. */
|
|
33
|
+
id?: string;
|
|
34
|
+
/** A numeric user id which is unique per realm (country). Example: 37162 */
|
|
35
|
+
userId?: SessionUserId;
|
|
36
|
+
/** Globally unique user id. Example: 'b3b23aa7-34f2-5d02-a10e-5a3455c6ab2c' */
|
|
37
|
+
uuid?: string;
|
|
38
|
+
/** Session token used for the `SP_ID` cookie. Example: 'eyJjbGllbnRfaWQ...'. */
|
|
39
|
+
sp_id?: string;
|
|
40
|
+
/** Session response TTL in seconds. Example: 300. */
|
|
41
|
+
expiresIn?: number;
|
|
42
|
+
/** Server time as a Unix timestamp in seconds. Example: 1506285759. */
|
|
43
|
+
serverTime?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Example: 'NCdzXaz4ZRb7...' The sig parameter is a concatenation of an HMAC SHA-256
|
|
46
|
+
* signature string, a dot (.) and a base64url encoded JSON object (session).
|
|
47
|
+
* {@link http://techdocs.spid.no/sdks/js/response-signature-and-validation/}
|
|
48
|
+
*/
|
|
49
|
+
sig?: string;
|
|
50
|
+
/** User display name. Example: 'batman'. */
|
|
51
|
+
displayName?: string;
|
|
52
|
+
/** User given name. Example: 'Bruce'. */
|
|
53
|
+
givenName?: string;
|
|
54
|
+
/** User family name. Example: 'Wayne'. */
|
|
55
|
+
familyName?: string;
|
|
56
|
+
/** User gender value. Example: 'male', 'female', 'undisclosed'. */
|
|
57
|
+
gender?: string;
|
|
58
|
+
/** User profile photo URL. Example: 'http://www.srv.com/some/picture.jpg' . */
|
|
59
|
+
photo?: string;
|
|
60
|
+
/** Whether tracking is enabled for this session. Example: true. */
|
|
61
|
+
tracking?: boolean;
|
|
62
|
+
/** Whether the client agreement is accepted. Example: true. */
|
|
63
|
+
clientAgreementAccepted?: boolean;
|
|
64
|
+
/** Whether the default agreement is accepted. Example: true. */
|
|
65
|
+
defaultAgreementAccepted?: boolean;
|
|
66
|
+
/** Pairwise user id for this client. Example: 'b2a23caa...'. */
|
|
67
|
+
pairId?: string;
|
|
68
|
+
/** User SDRN. Example: 'sdrn:spid.no:user:12345'. */
|
|
69
|
+
sdrn?: string;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Successful has-session response for a user connected to the merchant.
|
|
73
|
+
*
|
|
74
|
+
* @group Session
|
|
75
|
+
*/
|
|
76
|
+
export type ConnectedSessionResponse = SessionSuccessResponse & {
|
|
77
|
+
result: true;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Connected session response that also carries a usable user id.
|
|
81
|
+
*
|
|
82
|
+
* @group Session
|
|
83
|
+
*/
|
|
84
|
+
export type ConnectedSessionWithUserIdResponse = ConnectedSessionResponse & {
|
|
85
|
+
userId: SessionUserId;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Failure payload returned by Session Service.
|
|
89
|
+
*
|
|
90
|
+
* API and network failures usually reject with {@link SDKError}; this shape represents a failure
|
|
91
|
+
* payload when one is returned by Session Service and reaches the has-session flow.
|
|
92
|
+
*
|
|
93
|
+
* @group Session
|
|
94
|
+
*/
|
|
95
|
+
export type SessionFailureResponse = {
|
|
96
|
+
/** Failure payload returned by Session Service. Usually an object, but tests also pass strings. */
|
|
97
|
+
error: string | number | boolean | object;
|
|
98
|
+
response?: {
|
|
99
|
+
/** Response status code. Example: 404. */
|
|
100
|
+
code: number;
|
|
101
|
+
/** Response type. Example: 'NOT_FOUND'. */
|
|
102
|
+
type: string;
|
|
103
|
+
/** Human-readable response description. Example: 'No session found'. */
|
|
104
|
+
description: string;
|
|
105
|
+
/** Cookie base domain used as the `SP_ID` cookie domain fallback. Example: 'spid.no'. */
|
|
106
|
+
baseDomain?: string;
|
|
107
|
+
/** Time span in milliseconds. Example: 30 * 60 * 1000 (for 30 minutes) */
|
|
108
|
+
expiresIn?: number;
|
|
109
|
+
result?: boolean;
|
|
110
|
+
/** Server time in seconds since the Unix Epoch. Example: 1506287788 */
|
|
111
|
+
serverTime?: number;
|
|
112
|
+
[key: string]: string | number | boolean | object | null | undefined;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Object response that does not match one of the SDK-known has-session shapes.
|
|
117
|
+
*
|
|
118
|
+
* This intentionally includes `{}`. Historically `hasSession()` could resolve an empty object or
|
|
119
|
+
* other object payloads that callers inspected themselves, so the SDK preserves those responses.
|
|
120
|
+
*
|
|
121
|
+
* @group Session
|
|
122
|
+
*/
|
|
123
|
+
export type UnrecognizedSessionResponse = object;
|
|
124
|
+
/**
|
|
125
|
+
* Redirect URL returned by `hasSession()` when Session Service needs a full-page session refresh.
|
|
126
|
+
*
|
|
127
|
+
* @group Session
|
|
128
|
+
*/
|
|
129
|
+
export type RedirectSessionResponse = string;
|
|
130
|
+
export type SessionObjectResponse = SessionSuccessResponse | SessionFailureResponse | UnrecognizedSessionResponse;
|
|
131
|
+
export type RedirectSessionPayload = {
|
|
132
|
+
redirectURL: string;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Internal shape returned by `normalizeSessionResponse()` before `hasSession()` post-processing.
|
|
136
|
+
*
|
|
137
|
+
* It includes regular Session Service object responses and redirect responses used for full-page
|
|
138
|
+
* session refresh.
|
|
139
|
+
*/
|
|
140
|
+
export type NormalizedSessionPayload = SessionObjectResponse | RedirectSessionPayload;
|
|
141
|
+
/**
|
|
142
|
+
* Public response union returned from {@link Account#hasSession}.
|
|
143
|
+
*
|
|
144
|
+
* @group Session
|
|
145
|
+
*/
|
|
146
|
+
export type SessionResponse = SessionSuccessResponse | SessionFailureResponse | UnrecognizedSessionResponse | RedirectSessionResponse;
|
package/dist/url.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple utility function that allows looking up URLs from a dictionary
|
|
3
|
+
* @param url - A url like http://example.com, or a key used for lookup
|
|
4
|
+
* @param urlMap - A map of URLs like `{ DEV: 'http://dev.example.com' }`
|
|
5
|
+
* @returns The resolved URL: the mapped value when `url` is a known key, otherwise `url` itself
|
|
6
|
+
* @throws {SDKError} - If the url is not a string or is an empty string
|
|
7
|
+
*/
|
|
8
|
+
export declare function urlMapper(url: string, urlMap: Record<string, string>): string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A utility function that throws an SDKError if an assertion fails. It's mostly useful for
|
|
3
|
+
* validating function inputs.
|
|
4
|
+
*
|
|
5
|
+
* Narrows `condition` for the rest of the enclosing scope (TypeScript assertion signature).
|
|
6
|
+
* @param condition - The condition that we're asserting
|
|
7
|
+
* @param message - The error message
|
|
8
|
+
* @throws {SDKError} - If the condition is falsy it throws the appropriate error
|
|
9
|
+
*/
|
|
10
|
+
export declare function assert(condition: boolean, message?: string): asserts condition;
|
|
11
|
+
/**
|
|
12
|
+
* Checks if a value is a string or not
|
|
13
|
+
* @param value - The value to check
|
|
14
|
+
*/
|
|
15
|
+
export declare function isStr(value: unknown): value is string;
|
|
16
|
+
/**
|
|
17
|
+
* Checks if a value is a non-empty string
|
|
18
|
+
* @param value - The value to check
|
|
19
|
+
*/
|
|
20
|
+
export declare function isNonEmptyString(value: unknown): value is string;
|
|
21
|
+
/**
|
|
22
|
+
* checks if a given value is an object (but not null)
|
|
23
|
+
* @param value - The value to check
|
|
24
|
+
*/
|
|
25
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Checks if a given value is an object with at least one own key
|
|
28
|
+
* @param value - The value to check
|
|
29
|
+
*/
|
|
30
|
+
export declare function isNonEmptyObj(value: unknown): value is Record<string, unknown>;
|
|
31
|
+
/**
|
|
32
|
+
* Checks if a given string is a valid URL
|
|
33
|
+
* @param value - The string to be tested
|
|
34
|
+
* @param mandatoryFields - A list of mandatory fields that should exist in the parsed
|
|
35
|
+
* `URL` object
|
|
36
|
+
*/
|
|
37
|
+
export declare function isUrl(value: string, ...mandatoryFields: (keyof URL)[]): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Checks if a given value is a function
|
|
40
|
+
* @param value - The value to check
|
|
41
|
+
*/
|
|
42
|
+
export declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if a string matches any of the strings in a set of possibilities
|
|
45
|
+
* @param value
|
|
46
|
+
* @param possibilities - An array of strings that'll be used to check the string
|
|
47
|
+
* inclusion. Note that for performance reasons, we don't validate this parameter.
|
|
48
|
+
* @param caseSensitive - Should the check be case sensitive
|
|
49
|
+
*/
|
|
50
|
+
export declare function isStrIn(value: string, possibilities: string[], caseSensitive?: boolean): boolean;
|
package/package.json
CHANGED
|
@@ -1,66 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schibsted/account-sdk-browser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-alpha.10",
|
|
4
4
|
"description": "Schibsted account SDK for browsers",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"default": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
7
13
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"clean": "rimraf
|
|
10
|
-
"docs": "
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
14
|
+
"clean": "rimraf .cache coverage docs dist && npm run clean:legacy",
|
|
15
|
+
"clean:legacy": "rimraf es5",
|
|
16
|
+
"docs:build": "typedoc",
|
|
17
|
+
"docs:watch": "typedoc --watch",
|
|
18
|
+
"docs:open": "node scripts/open-docs.mjs",
|
|
19
|
+
"lint": "biome check",
|
|
20
|
+
"lint:fix": "biome check --fix",
|
|
21
|
+
"build": "vite build",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"pretest": "npm run build",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"precover": "npm run build",
|
|
26
|
+
"cover": "vitest run --coverage",
|
|
27
|
+
"prepublishOnly": "npm run build"
|
|
15
28
|
},
|
|
16
29
|
"author": "",
|
|
17
30
|
"license": "MIT",
|
|
18
|
-
"dependencies": {
|
|
19
|
-
"tiny-emitter": "^2.1.0"
|
|
20
|
-
},
|
|
21
31
|
"devDependencies": {
|
|
22
|
-
"@
|
|
23
|
-
"@
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"regenerator-runtime": "^0.13.7",
|
|
34
|
-
"webpack": "^4.44.1",
|
|
35
|
-
"webpack-cli": "^3.3.12",
|
|
36
|
-
"whatwg-url": "^8.0.0"
|
|
32
|
+
"@biomejs/biome": "2.4.15",
|
|
33
|
+
"@vitest/coverage-v8": "^4.1.7",
|
|
34
|
+
"jsdom": "^29.1.1",
|
|
35
|
+
"open": "^11.0.0",
|
|
36
|
+
"rimraf": "^6.1.3",
|
|
37
|
+
"typedoc": "^0.28.19",
|
|
38
|
+
"typedoc-plugin-mdn-links": "^5.1.1",
|
|
39
|
+
"typescript": "^6.0.3",
|
|
40
|
+
"unplugin-dts": "^1.0.1",
|
|
41
|
+
"vite": "^8.0.14",
|
|
42
|
+
"vitest": "^4.1.7"
|
|
37
43
|
},
|
|
38
44
|
"repository": {
|
|
39
45
|
"type": "git",
|
|
40
46
|
"url": "git://schibsted.ghe.com/user-identity/account-sdk-browser.git"
|
|
41
|
-
}
|
|
42
|
-
"babel": {
|
|
43
|
-
"presets": [
|
|
44
|
-
[
|
|
45
|
-
"@babel/preset-env",
|
|
46
|
-
{
|
|
47
|
-
"useBuiltIns": "usage",
|
|
48
|
-
"corejs": 3,
|
|
49
|
-
"targets": {
|
|
50
|
-
"browsers": [
|
|
51
|
-
"> 1%",
|
|
52
|
-
"last 10 chrome major versions",
|
|
53
|
-
"last 10 firefox major versions",
|
|
54
|
-
"last 10 opera major versions",
|
|
55
|
-
"last 2 safari major versions",
|
|
56
|
-
"last 2 ios major versions",
|
|
57
|
-
"last 2 ie major versions",
|
|
58
|
-
"last 5 edge major versions"
|
|
59
|
-
]
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
]
|
|
63
|
-
]
|
|
64
|
-
},
|
|
65
|
-
"typings": "index.d.ts"
|
|
47
|
+
}
|
|
66
48
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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 { Account } from './account.js';
|
|
6
|
+
import { resolveBrowserWindow } from './resolve-browser-window.js';
|
|
7
|
+
import type { AccountConfig } from './types/account.js';
|
|
8
|
+
|
|
9
|
+
const ACCOUNT_READY_EVENT = 'schAccount:ready';
|
|
10
|
+
const GET_ACCOUNT_SDK_TIMEOUT_MS = 4000;
|
|
11
|
+
|
|
12
|
+
let pendingAccountPromise: Promise<Account> | undefined;
|
|
13
|
+
|
|
14
|
+
const createUnavailableError = (): Error =>
|
|
15
|
+
new Error('Schibsted Account SDK is not available in this browser context');
|
|
16
|
+
|
|
17
|
+
const createTimeoutError = (): Error =>
|
|
18
|
+
new Error(`Schibsted Account SDK was not initialized within ${GET_ACCOUNT_SDK_TIMEOUT_MS} ms`);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates and registers the single Account SDK instance for the current browser window.
|
|
22
|
+
*
|
|
23
|
+
* The created instance is stored on `window.schAccount` and the SDK emits
|
|
24
|
+
* `schAccount:ready` with the instance in `event.detail.instance`.
|
|
25
|
+
*
|
|
26
|
+
* @param config - Client, redirect, environment, and Session Service configuration.
|
|
27
|
+
* @throws {SDKError} If the SDK cannot run in the current browser context, if an Account instance
|
|
28
|
+
* has already been initialized, or if configuration validation fails.
|
|
29
|
+
* @group Core API
|
|
30
|
+
*/
|
|
31
|
+
export function createAccountSdk(config: AccountConfig): Account {
|
|
32
|
+
return new Account(config);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolves the Account SDK instance registered by `createAccountSdk()`.
|
|
37
|
+
*
|
|
38
|
+
* If the instance already exists, the promise resolves immediately. Otherwise it waits for
|
|
39
|
+
* `schAccount:ready` and rejects if initialization does not happen within the SDK timeout.
|
|
40
|
+
* @group Core API
|
|
41
|
+
*/
|
|
42
|
+
export function getAccountSdk(): Promise<Account> {
|
|
43
|
+
const browserWindow = resolveBrowserWindow();
|
|
44
|
+
|
|
45
|
+
if (!browserWindow) {
|
|
46
|
+
const error = createUnavailableError();
|
|
47
|
+
console.error(error.message);
|
|
48
|
+
return Promise.reject(error);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (browserWindow.schAccount) {
|
|
52
|
+
return Promise.resolve(browserWindow.schAccount);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (pendingAccountPromise) {
|
|
56
|
+
return pendingAccountPromise;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
pendingAccountPromise = new Promise<Account>((resolve, reject) => {
|
|
60
|
+
let timeoutId: number | undefined;
|
|
61
|
+
|
|
62
|
+
const cleanup = () => {
|
|
63
|
+
browserWindow.removeEventListener(ACCOUNT_READY_EVENT, resolveRegisteredAccount);
|
|
64
|
+
if (timeoutId !== undefined) {
|
|
65
|
+
browserWindow.clearTimeout(timeoutId);
|
|
66
|
+
}
|
|
67
|
+
pendingAccountPromise = undefined;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const resolveRegisteredAccount = () => {
|
|
71
|
+
const account = browserWindow.schAccount;
|
|
72
|
+
if (!account) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
cleanup();
|
|
77
|
+
resolve(account);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
browserWindow.addEventListener(ACCOUNT_READY_EVENT, resolveRegisteredAccount);
|
|
81
|
+
timeoutId = browserWindow.setTimeout(() => {
|
|
82
|
+
const error = createTimeoutError();
|
|
83
|
+
console.error(error.message);
|
|
84
|
+
cleanup();
|
|
85
|
+
reject(error);
|
|
86
|
+
}, GET_ACCOUNT_SDK_TIMEOUT_MS);
|
|
87
|
+
|
|
88
|
+
resolveRegisteredAccount();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return pendingAccountPromise;
|
|
92
|
+
}
|