@saasquatch/component-environment 1.0.0-1 → 1.0.0-2
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/CHANGELOG.md +13 -0
- package/README.md +1 -0
- package/dist/index.d.ts +226 -5
- package/dist/index.js +430 -19
- package/dist/index.mjs +387 -0
- package/package.json +6 -3
- package/src/fetchLocale.ts +10 -4
- package/src/listeners.ts +17 -15
- package/dist/LocaleContext.d.ts +0 -7
- package/dist/LocaleContext.js +0 -43
- package/dist/ProgramContext.d.ts +0 -13
- package/dist/ProgramContext.js +0 -29
- package/dist/UserIdentityContext.d.ts +0 -10
- package/dist/UserIdentityContext.js +0 -124
- package/dist/contexts/LocaleContext.d.ts +0 -18
- package/dist/contexts/LocaleContext.js +0 -47
- package/dist/contexts/ProgramContext.d.ts +0 -18
- package/dist/contexts/ProgramContext.js +0 -48
- package/dist/contexts/UserIdentityContext.d.ts +0 -26
- package/dist/contexts/UserIdentityContext.js +0 -156
- package/dist/debug.d.ts +0 -1
- package/dist/debug.js +0 -10
- package/dist/environment.d.ts +0 -15
- package/dist/environment.js +0 -107
- package/dist/fetchLocale.d.ts +0 -1
- package/dist/fetchLocale.js +0 -41
- package/dist/listeners.d.ts +0 -1
- package/dist/listeners.js +0 -51
- package/dist/types.d.ts +0 -147
- package/dist/types.js +0 -7
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import decode from "jwt-decode";
|
|
2
|
-
import { ContextProvider } from "dom-context";
|
|
3
|
-
import { equal } from "@wry/equality";
|
|
4
|
-
import { getEnvironmentSDK } from "./environment";
|
|
5
|
-
import { setLocale } from "./LocaleContext";
|
|
6
|
-
import { USER_CONTEXT_NAME, } from "./types";
|
|
7
|
-
export function lazilyStartUserContext() {
|
|
8
|
-
let globalProvider = window.squatchUserIdentity;
|
|
9
|
-
if (!globalProvider) {
|
|
10
|
-
// Lazily creates a global provider
|
|
11
|
-
globalProvider = new ContextProvider({
|
|
12
|
-
element: document.documentElement,
|
|
13
|
-
initialState: _getInitialValue(),
|
|
14
|
-
contextName: USER_CONTEXT_NAME,
|
|
15
|
-
}).start();
|
|
16
|
-
window.squatchUserIdentity = globalProvider;
|
|
17
|
-
}
|
|
18
|
-
return globalProvider;
|
|
19
|
-
}
|
|
20
|
-
function isDecodedSquatchJWT(decoded) {
|
|
21
|
-
return decoded.user && decoded.user.id && decoded.user.accountId;
|
|
22
|
-
}
|
|
23
|
-
function isDecodedWidgetAPIJWT(decoded) {
|
|
24
|
-
return decoded.sub && /.*:.*@.*:users/.test(decoded.sub);
|
|
25
|
-
}
|
|
26
|
-
export function userIdentityFromJwt(jwt) {
|
|
27
|
-
if (!jwt)
|
|
28
|
-
return undefined;
|
|
29
|
-
try {
|
|
30
|
-
const decoded = decode(jwt);
|
|
31
|
-
const exp = decoded.exp;
|
|
32
|
-
let userId = undefined;
|
|
33
|
-
let accountId = undefined;
|
|
34
|
-
if (isDecodedWidgetAPIJWT(decoded)) {
|
|
35
|
-
// Pull the accountId and userId from the subject and Base64-decode them
|
|
36
|
-
// NOTE: This is to support classic theme engine widget token generation
|
|
37
|
-
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
|
|
38
|
-
if (matches?.[1])
|
|
39
|
-
accountId = atob(matches[1]);
|
|
40
|
-
if (matches?.[2])
|
|
41
|
-
userId = atob(matches[2]);
|
|
42
|
-
}
|
|
43
|
-
else if (isDecodedSquatchJWT(decoded)) {
|
|
44
|
-
accountId = decoded.user.accountId;
|
|
45
|
-
userId = decoded.user.id;
|
|
46
|
-
}
|
|
47
|
-
if (!userId || !accountId) {
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
// Check if the JWT has expired
|
|
51
|
-
if (exp && Date.now() >= exp * 1000) {
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
return {
|
|
55
|
-
id: userId,
|
|
56
|
-
accountId: accountId,
|
|
57
|
-
jwt,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
// Invalid JWT
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function _getInitialValue() {
|
|
66
|
-
const sdk = getEnvironmentSDK();
|
|
67
|
-
switch (sdk.type) {
|
|
68
|
-
case "SquatchAndroid":
|
|
69
|
-
case "SquatchJS2":
|
|
70
|
-
return {
|
|
71
|
-
id: sdk.widgetIdent.userId,
|
|
72
|
-
accountId: sdk.widgetIdent.accountId,
|
|
73
|
-
jwt: sdk.widgetIdent.token,
|
|
74
|
-
};
|
|
75
|
-
case "SquatchPortal":
|
|
76
|
-
// Portals can have the jwt provided as a URL parameter, so look for that first
|
|
77
|
-
const searchParams = new URLSearchParams(document.location.search);
|
|
78
|
-
if (searchParams.has("jwt")) {
|
|
79
|
-
return userIdentityFromJwt(searchParams.get("jwt"));
|
|
80
|
-
}
|
|
81
|
-
// Look for user identity in local storage
|
|
82
|
-
const stored = localStorage.getItem(USER_CONTEXT_NAME);
|
|
83
|
-
if (!stored)
|
|
84
|
-
return undefined;
|
|
85
|
-
try {
|
|
86
|
-
const potentialUserIdent = JSON.parse(stored);
|
|
87
|
-
const identity = userIdentityFromJwt(potentialUserIdent.jwt);
|
|
88
|
-
if (identity) {
|
|
89
|
-
return {
|
|
90
|
-
...potentialUserIdent,
|
|
91
|
-
...identity,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
return undefined;
|
|
95
|
-
}
|
|
96
|
-
catch (e) {
|
|
97
|
-
// Not valid JSON
|
|
98
|
-
return undefined;
|
|
99
|
-
}
|
|
100
|
-
case "SquatchAdmin":
|
|
101
|
-
case "None":
|
|
102
|
-
// Not logged in for admin portal / none default case
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Overide the globally defined user context, and persists the user identity in local storage
|
|
108
|
-
*
|
|
109
|
-
* @param identity the new identity of the user, or undefined if logged out
|
|
110
|
-
*/
|
|
111
|
-
export function setUserIdentity(identity) {
|
|
112
|
-
const globalProvider = lazilyStartUserContext();
|
|
113
|
-
if (!equal(globalProvider.context, identity)) {
|
|
114
|
-
setLocale(undefined);
|
|
115
|
-
globalProvider.context = identity;
|
|
116
|
-
}
|
|
117
|
-
// Portals store identity in local storage
|
|
118
|
-
if (identity && getEnvironmentSDK().type === "SquatchPortal") {
|
|
119
|
-
localStorage.setItem(USER_CONTEXT_NAME, JSON.stringify(identity));
|
|
120
|
-
}
|
|
121
|
-
else if (!identity) {
|
|
122
|
-
localStorage.removeItem(USER_CONTEXT_NAME);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { ContextProvider } from "dom-context";
|
|
2
|
-
/**
|
|
3
|
-
* Lazily start the locale context provider. If it already exists, the existing provider is
|
|
4
|
-
* returned. This function is safe to call multiple times.
|
|
5
|
-
*
|
|
6
|
-
* @returns The global locale context provider
|
|
7
|
-
*/
|
|
8
|
-
export declare function lazilyStartLocaleContext(): ContextProvider<string | undefined>;
|
|
9
|
-
/**
|
|
10
|
-
* Overide the globally defined Locale context
|
|
11
|
-
*
|
|
12
|
-
* @param locale the new locale used by the user
|
|
13
|
-
*/
|
|
14
|
-
export declare function setLocale(locale?: string): void;
|
|
15
|
-
/**
|
|
16
|
-
* Get the current value of the locale context
|
|
17
|
-
*/
|
|
18
|
-
export declare function getLocale(): string | undefined;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLocale = exports.setLocale = exports.lazilyStartLocaleContext = void 0;
|
|
4
|
-
const dom_context_1 = require("dom-context");
|
|
5
|
-
const types_1 = require("../types");
|
|
6
|
-
const debug_1 = require("../debug");
|
|
7
|
-
const debug = (...args) => (0, debug_1.debug)(types_1.LOCALE_CONTEXT_NAME, ...args);
|
|
8
|
-
/**
|
|
9
|
-
* Lazily start the locale context provider. If it already exists, the existing provider is
|
|
10
|
-
* returned. This function is safe to call multiple times.
|
|
11
|
-
*
|
|
12
|
-
* @returns The global locale context provider
|
|
13
|
-
*/
|
|
14
|
-
function lazilyStartLocaleContext() {
|
|
15
|
-
let globalProvider = window.squatchLocale;
|
|
16
|
-
if (!globalProvider) {
|
|
17
|
-
debug("Creating locale context provider");
|
|
18
|
-
globalProvider = new dom_context_1.ContextProvider({
|
|
19
|
-
element: document.documentElement,
|
|
20
|
-
initialState: window.widgetIdent?.locale || navigator.language.replace("-", "_"),
|
|
21
|
-
contextName: types_1.LOCALE_CONTEXT_NAME,
|
|
22
|
-
}).start();
|
|
23
|
-
window.squatchLocale = globalProvider;
|
|
24
|
-
}
|
|
25
|
-
return globalProvider;
|
|
26
|
-
}
|
|
27
|
-
exports.lazilyStartLocaleContext = lazilyStartLocaleContext;
|
|
28
|
-
/**
|
|
29
|
-
* Overide the globally defined Locale context
|
|
30
|
-
*
|
|
31
|
-
* @param locale the new locale used by the user
|
|
32
|
-
*/
|
|
33
|
-
function setLocale(locale) {
|
|
34
|
-
const globalProvider = lazilyStartLocaleContext();
|
|
35
|
-
if (globalProvider.context !== locale) {
|
|
36
|
-
debug(`Setting locale context value [${locale}]`);
|
|
37
|
-
globalProvider.context = locale;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.setLocale = setLocale;
|
|
41
|
-
/**
|
|
42
|
-
* Get the current value of the locale context
|
|
43
|
-
*/
|
|
44
|
-
function getLocale() {
|
|
45
|
-
return window.squatchLocale?.context;
|
|
46
|
-
}
|
|
47
|
-
exports.getLocale = getLocale;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { ContextProvider } from "dom-context";
|
|
2
|
-
/**
|
|
3
|
-
* Lazily start the program context provider. If it already exists, the existing provider is
|
|
4
|
-
* returned. This function is safe to call multiple times.
|
|
5
|
-
*
|
|
6
|
-
* @returns The global program context provider
|
|
7
|
-
*/
|
|
8
|
-
export declare function lazilyStartProgramContext(): ContextProvider<string | undefined>;
|
|
9
|
-
/**
|
|
10
|
-
* Overide the globally defined Program ID context
|
|
11
|
-
*
|
|
12
|
-
* @param programId the new programID used by the user, or undefined if logged out
|
|
13
|
-
*/
|
|
14
|
-
export declare function setProgramId(programId: string | undefined): void;
|
|
15
|
-
/**
|
|
16
|
-
* Get the current value of the program context
|
|
17
|
-
*/
|
|
18
|
-
export declare function getProgramId(): string | undefined;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getProgramId = exports.setProgramId = exports.lazilyStartProgramContext = void 0;
|
|
4
|
-
const dom_context_1 = require("dom-context");
|
|
5
|
-
const types_1 = require("../types");
|
|
6
|
-
const debug_1 = require("../debug");
|
|
7
|
-
const debug = (...args) => (0, debug_1.debug)(types_1.PROGRAM_CONTEXT_NAME, ...args);
|
|
8
|
-
/**
|
|
9
|
-
* Lazily start the program context provider. If it already exists, the existing provider is
|
|
10
|
-
* returned. This function is safe to call multiple times.
|
|
11
|
-
*
|
|
12
|
-
* @returns The global program context provider
|
|
13
|
-
*/
|
|
14
|
-
function lazilyStartProgramContext() {
|
|
15
|
-
let globalProvider = window.squatchProgramId;
|
|
16
|
-
if (!globalProvider) {
|
|
17
|
-
debug("Creating program context provider");
|
|
18
|
-
// Lazily creates a global provider
|
|
19
|
-
globalProvider = new dom_context_1.ContextProvider({
|
|
20
|
-
element: document.documentElement,
|
|
21
|
-
initialState: window.widgetIdent?.programId || undefined,
|
|
22
|
-
contextName: types_1.PROGRAM_CONTEXT_NAME,
|
|
23
|
-
}).start();
|
|
24
|
-
window.squatchProgramId = globalProvider;
|
|
25
|
-
}
|
|
26
|
-
return globalProvider;
|
|
27
|
-
}
|
|
28
|
-
exports.lazilyStartProgramContext = lazilyStartProgramContext;
|
|
29
|
-
/**
|
|
30
|
-
* Overide the globally defined Program ID context
|
|
31
|
-
*
|
|
32
|
-
* @param programId the new programID used by the user, or undefined if logged out
|
|
33
|
-
*/
|
|
34
|
-
function setProgramId(programId) {
|
|
35
|
-
const globalProvider = lazilyStartProgramContext();
|
|
36
|
-
if (globalProvider.context !== programId) {
|
|
37
|
-
debug(`Setting program context value [${programId}]`);
|
|
38
|
-
globalProvider.context = programId;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
exports.setProgramId = setProgramId;
|
|
42
|
-
/**
|
|
43
|
-
* Get the current value of the program context
|
|
44
|
-
*/
|
|
45
|
-
function getProgramId() {
|
|
46
|
-
return window.squatchLocale?.context;
|
|
47
|
-
}
|
|
48
|
-
exports.getProgramId = getProgramId;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { ContextProvider } from "dom-context";
|
|
2
|
-
import { UserIdentity } from "../types";
|
|
3
|
-
/**
|
|
4
|
-
* Lazily start the user context provider. If it already exists, the existing provider is
|
|
5
|
-
* returned. This function is safe to call multiple times.
|
|
6
|
-
*
|
|
7
|
-
* @returns The global user context provider
|
|
8
|
-
*/
|
|
9
|
-
export declare function lazilyStartUserContext(): ContextProvider<UserIdentity | undefined>;
|
|
10
|
-
/**
|
|
11
|
-
* Extract a user identity from a JWT
|
|
12
|
-
*
|
|
13
|
-
* @param jwt The JWT to extract a user identity from
|
|
14
|
-
* @returns The user identity or undefined if the JWT is not valid
|
|
15
|
-
*/
|
|
16
|
-
export declare function userIdentityFromJwt(jwt?: string): UserIdentity | undefined;
|
|
17
|
-
/**
|
|
18
|
-
* Overide the globally defined user context, and persists the user identity in local storage
|
|
19
|
-
*
|
|
20
|
-
* @param identity the new identity of the user, or undefined if logged out
|
|
21
|
-
*/
|
|
22
|
-
export declare function setUserIdentity(identity?: UserIdentity): void;
|
|
23
|
-
/**
|
|
24
|
-
* Get the current value of the user context
|
|
25
|
-
*/
|
|
26
|
-
export declare function getUserIdentity(): UserIdentity | undefined;
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getUserIdentity = exports.setUserIdentity = exports.userIdentityFromJwt = exports.lazilyStartUserContext = void 0;
|
|
7
|
-
const jwt_decode_1 = __importDefault(require("jwt-decode"));
|
|
8
|
-
const dom_context_1 = require("dom-context");
|
|
9
|
-
const equality_1 = require("@wry/equality");
|
|
10
|
-
const environment_1 = require("../environment");
|
|
11
|
-
const types_1 = require("../types");
|
|
12
|
-
const listeners_1 = require("../listeners");
|
|
13
|
-
const debug_1 = require("../debug");
|
|
14
|
-
const debug = (...args) => (0, debug_1.debug)(types_1.USER_CONTEXT_NAME, ...args);
|
|
15
|
-
/**
|
|
16
|
-
* Lazily start the user context provider. If it already exists, the existing provider is
|
|
17
|
-
* returned. This function is safe to call multiple times.
|
|
18
|
-
*
|
|
19
|
-
* @returns The global user context provider
|
|
20
|
-
*/
|
|
21
|
-
function lazilyStartUserContext() {
|
|
22
|
-
let globalProvider = window.squatchUserIdentity;
|
|
23
|
-
if (!globalProvider) {
|
|
24
|
-
debug("Creating user context provider");
|
|
25
|
-
// Lazily creates a global provider
|
|
26
|
-
globalProvider = new dom_context_1.ContextProvider({
|
|
27
|
-
element: document.documentElement,
|
|
28
|
-
initialState: _getInitialValue(),
|
|
29
|
-
contextName: types_1.USER_CONTEXT_NAME,
|
|
30
|
-
}).start();
|
|
31
|
-
window.squatchUserIdentity = globalProvider;
|
|
32
|
-
(0, listeners_1.startUserContextListenerForLocale)();
|
|
33
|
-
}
|
|
34
|
-
return globalProvider;
|
|
35
|
-
}
|
|
36
|
-
exports.lazilyStartUserContext = lazilyStartUserContext;
|
|
37
|
-
function isDecodedSquatchJWT(decoded) {
|
|
38
|
-
return decoded.user && decoded.user.id && decoded.user.accountId;
|
|
39
|
-
}
|
|
40
|
-
function isDecodedWidgetAPIJWT(decoded) {
|
|
41
|
-
return decoded.sub && /.*:.*@.*:users/.test(decoded.sub);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Extract a user identity from a JWT
|
|
45
|
-
*
|
|
46
|
-
* @param jwt The JWT to extract a user identity from
|
|
47
|
-
* @returns The user identity or undefined if the JWT is not valid
|
|
48
|
-
*/
|
|
49
|
-
function userIdentityFromJwt(jwt) {
|
|
50
|
-
if (!jwt)
|
|
51
|
-
return undefined;
|
|
52
|
-
try {
|
|
53
|
-
const decoded = (0, jwt_decode_1.default)(jwt);
|
|
54
|
-
const exp = decoded.exp;
|
|
55
|
-
let userId = undefined;
|
|
56
|
-
let accountId = undefined;
|
|
57
|
-
if (isDecodedWidgetAPIJWT(decoded)) {
|
|
58
|
-
// Pull the accountId and userId from the subject and Base64-decode them
|
|
59
|
-
// NOTE: This is to support classic theme engine widget token generation
|
|
60
|
-
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
|
|
61
|
-
if (matches?.[1])
|
|
62
|
-
accountId = atob(matches[1]);
|
|
63
|
-
if (matches?.[2])
|
|
64
|
-
userId = atob(matches[2]);
|
|
65
|
-
}
|
|
66
|
-
else if (isDecodedSquatchJWT(decoded)) {
|
|
67
|
-
accountId = decoded.user.accountId;
|
|
68
|
-
userId = decoded.user.id;
|
|
69
|
-
}
|
|
70
|
-
if (!userId || !accountId) {
|
|
71
|
-
return undefined;
|
|
72
|
-
}
|
|
73
|
-
// Check if the JWT has expired
|
|
74
|
-
if (exp && Date.now() >= exp * 1000) {
|
|
75
|
-
return undefined;
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
id: userId,
|
|
79
|
-
accountId: accountId,
|
|
80
|
-
jwt,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
// Invalid JWT
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.userIdentityFromJwt = userIdentityFromJwt;
|
|
89
|
-
function _getInitialValue() {
|
|
90
|
-
const sdk = (0, environment_1.getEnvironmentSDK)();
|
|
91
|
-
switch (sdk.type) {
|
|
92
|
-
case "SquatchAndroid":
|
|
93
|
-
case "SquatchJS2":
|
|
94
|
-
return {
|
|
95
|
-
id: sdk.widgetIdent.userId,
|
|
96
|
-
accountId: sdk.widgetIdent.accountId,
|
|
97
|
-
jwt: sdk.widgetIdent.token,
|
|
98
|
-
};
|
|
99
|
-
case "SquatchPortal":
|
|
100
|
-
// Portals can have the jwt provided as a URL parameter, so look for that first
|
|
101
|
-
const searchParams = new URLSearchParams(document.location.search);
|
|
102
|
-
if (searchParams.has("jwt")) {
|
|
103
|
-
return userIdentityFromJwt(searchParams.get("jwt"));
|
|
104
|
-
}
|
|
105
|
-
// Look for user identity in local storage
|
|
106
|
-
const stored = localStorage.getItem(types_1.USER_CONTEXT_NAME);
|
|
107
|
-
if (!stored)
|
|
108
|
-
return undefined;
|
|
109
|
-
try {
|
|
110
|
-
const potentialUserIdent = JSON.parse(stored);
|
|
111
|
-
const identity = userIdentityFromJwt(potentialUserIdent.jwt);
|
|
112
|
-
if (identity) {
|
|
113
|
-
return {
|
|
114
|
-
...potentialUserIdent,
|
|
115
|
-
...identity,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
return undefined;
|
|
119
|
-
}
|
|
120
|
-
catch (e) {
|
|
121
|
-
// Not valid JSON
|
|
122
|
-
return undefined;
|
|
123
|
-
}
|
|
124
|
-
case "SquatchAdmin":
|
|
125
|
-
case "None":
|
|
126
|
-
// Not logged in for admin portal / none default case
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Overide the globally defined user context, and persists the user identity in local storage
|
|
132
|
-
*
|
|
133
|
-
* @param identity the new identity of the user, or undefined if logged out
|
|
134
|
-
*/
|
|
135
|
-
function setUserIdentity(identity) {
|
|
136
|
-
const globalProvider = lazilyStartUserContext();
|
|
137
|
-
if (!(0, equality_1.equal)(globalProvider.context, identity)) {
|
|
138
|
-
debug(`Setting user context value [${JSON.stringify(identity)}]`);
|
|
139
|
-
globalProvider.context = identity;
|
|
140
|
-
}
|
|
141
|
-
// Portals store identity in local storage
|
|
142
|
-
if (identity && (0, environment_1.getEnvironmentSDK)().type === "SquatchPortal") {
|
|
143
|
-
localStorage.setItem(types_1.USER_CONTEXT_NAME, JSON.stringify(identity));
|
|
144
|
-
}
|
|
145
|
-
else if (!identity) {
|
|
146
|
-
localStorage.removeItem(types_1.USER_CONTEXT_NAME);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
exports.setUserIdentity = setUserIdentity;
|
|
150
|
-
/**
|
|
151
|
-
* Get the current value of the user context
|
|
152
|
-
*/
|
|
153
|
-
function getUserIdentity() {
|
|
154
|
-
return window.squatchUserIdentity?.context;
|
|
155
|
-
}
|
|
156
|
-
exports.getUserIdentity = getUserIdentity;
|
package/dist/debug.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function debug(ns: string, ...args: any[]): void;
|
package/dist/debug.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.debug = void 0;
|
|
4
|
-
const debugEnabled = localStorage.getItem("debug");
|
|
5
|
-
function debug(ns, ...args) {
|
|
6
|
-
if (debugEnabled) {
|
|
7
|
-
console.debug(`sq:environment:${ns}`, ...args);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.debug = debug;
|
package/dist/environment.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Environment, EnvironmentSDK, EngagementMedium } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* Get the type of environment that this widget is being rendered in
|
|
4
|
-
*
|
|
5
|
-
* Should never return null.
|
|
6
|
-
*/
|
|
7
|
-
export declare function getEnvironment(): Environment;
|
|
8
|
-
/**
|
|
9
|
-
* Get the SDK for interacting with the host environment
|
|
10
|
-
*/
|
|
11
|
-
export declare function getEnvironmentSDK(): EnvironmentSDK;
|
|
12
|
-
export declare function isDemo(): boolean;
|
|
13
|
-
export declare function getTenantAlias(): string;
|
|
14
|
-
export declare function getAppDomain(): string;
|
|
15
|
-
export declare function getEngagementMedium(): EngagementMedium;
|
package/dist/environment.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getEngagementMedium = exports.getAppDomain = exports.getTenantAlias = exports.isDemo = exports.getEnvironmentSDK = exports.getEnvironment = void 0;
|
|
4
|
-
const types_1 = require("./types");
|
|
5
|
-
/**
|
|
6
|
-
* Get the type of environment that this widget is being rendered in
|
|
7
|
-
*
|
|
8
|
-
* Should never return null.
|
|
9
|
-
*/
|
|
10
|
-
function getEnvironment() {
|
|
11
|
-
return getEnvironmentSDK().type;
|
|
12
|
-
}
|
|
13
|
-
exports.getEnvironment = getEnvironment;
|
|
14
|
-
/**
|
|
15
|
-
* Get the SDK for interacting with the host environment
|
|
16
|
-
*/
|
|
17
|
-
function getEnvironmentSDK() {
|
|
18
|
-
//@ts-ignore
|
|
19
|
-
if (window["SquatchAndroid"]) {
|
|
20
|
-
return {
|
|
21
|
-
type: "SquatchAndroid",
|
|
22
|
-
//@ts-ignore
|
|
23
|
-
android: window["SquatchAndroid"],
|
|
24
|
-
//@ts-ignore
|
|
25
|
-
widgetIdent: window["widgetIdent"],
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
//@ts-ignore
|
|
29
|
-
if (window["SquatchPortal"]) {
|
|
30
|
-
return {
|
|
31
|
-
type: "SquatchPortal",
|
|
32
|
-
//@ts-ignore
|
|
33
|
-
env: window["SquatchPortal"],
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
//@ts-ignore
|
|
37
|
-
if (window["SquatchAdmin"]) {
|
|
38
|
-
return {
|
|
39
|
-
type: "SquatchAdmin",
|
|
40
|
-
//@ts-ignore
|
|
41
|
-
adminSDK: window["SquatchAdmin"],
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
// Vanilla components mutates `widgetIdent` for portal, causing boilerplate to render as SquatchJS2
|
|
45
|
-
if (window["widgetIdent"] && window["widgetIdent"]?.env !== "demo") {
|
|
46
|
-
return {
|
|
47
|
-
type: "SquatchJS2",
|
|
48
|
-
//@ts-ignore
|
|
49
|
-
api: window.frameElement?.["squatchJsApi"],
|
|
50
|
-
//@ts-ignore
|
|
51
|
-
widgetIdent: window["widgetIdent"],
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
return {
|
|
55
|
-
type: "None",
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
exports.getEnvironmentSDK = getEnvironmentSDK;
|
|
59
|
-
function isDemo() {
|
|
60
|
-
const sdk = getEnvironmentSDK();
|
|
61
|
-
return sdk.type === "None" || sdk.type === "SquatchAdmin";
|
|
62
|
-
}
|
|
63
|
-
exports.isDemo = isDemo;
|
|
64
|
-
// Fake tenant alias in demo mode
|
|
65
|
-
const FAKE_TENANT = "demo";
|
|
66
|
-
function getTenantAlias() {
|
|
67
|
-
const sdk = getEnvironmentSDK();
|
|
68
|
-
switch (sdk.type) {
|
|
69
|
-
case "SquatchAndroid":
|
|
70
|
-
case "SquatchJS2":
|
|
71
|
-
return sdk.widgetIdent.tenantAlias;
|
|
72
|
-
case "SquatchAdmin":
|
|
73
|
-
case "None":
|
|
74
|
-
return FAKE_TENANT;
|
|
75
|
-
case "SquatchPortal":
|
|
76
|
-
return sdk.env.tenantAlias;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
exports.getTenantAlias = getTenantAlias;
|
|
80
|
-
const DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
|
|
81
|
-
function getAppDomain() {
|
|
82
|
-
const sdk = getEnvironmentSDK();
|
|
83
|
-
switch (sdk.type) {
|
|
84
|
-
case "SquatchAndroid":
|
|
85
|
-
case "SquatchJS2":
|
|
86
|
-
return sdk.widgetIdent.appDomain;
|
|
87
|
-
case "SquatchPortal":
|
|
88
|
-
return sdk.env?.appDomain || DEFAULT_DOMAIN;
|
|
89
|
-
case "SquatchAdmin":
|
|
90
|
-
case "None":
|
|
91
|
-
return DEFAULT_DOMAIN;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
exports.getAppDomain = getAppDomain;
|
|
95
|
-
function getEngagementMedium() {
|
|
96
|
-
const sdk = getEnvironmentSDK();
|
|
97
|
-
switch (sdk.type) {
|
|
98
|
-
case "SquatchJS2":
|
|
99
|
-
return sdk.widgetIdent.engagementMedium || types_1.DEFAULT_MEDIUM;
|
|
100
|
-
case "SquatchAndroid":
|
|
101
|
-
case "SquatchPortal":
|
|
102
|
-
case "SquatchAdmin":
|
|
103
|
-
case "None":
|
|
104
|
-
return types_1.DEFAULT_MEDIUM;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
exports.getEngagementMedium = getEngagementMedium;
|
package/dist/fetchLocale.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function fetchLocale(): Promise<string | undefined>;
|
package/dist/fetchLocale.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchLocale = void 0;
|
|
4
|
-
const environment_1 = require("./environment");
|
|
5
|
-
const types_1 = require("./types");
|
|
6
|
-
const debug_1 = require("./debug");
|
|
7
|
-
const debug = (...args) => (0, debug_1.debug)(types_1.LOCALE_CONTEXT_NAME, ...args);
|
|
8
|
-
const GET_LOCALE = `
|
|
9
|
-
query {
|
|
10
|
-
viewer {
|
|
11
|
-
... on User {
|
|
12
|
-
locale
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
`;
|
|
17
|
-
async function fetchLocale() {
|
|
18
|
-
debug("Fetching locale from GraphQL for current user");
|
|
19
|
-
try {
|
|
20
|
-
const result = await fetch(`${(0, environment_1.getAppDomain)()}/api/v1/${(0, environment_1.getTenantAlias)()}/graphql`, {
|
|
21
|
-
method: "POST",
|
|
22
|
-
headers: { "Content-Type": "application/json" },
|
|
23
|
-
body: JSON.stringify({
|
|
24
|
-
query: GET_LOCALE,
|
|
25
|
-
}),
|
|
26
|
-
});
|
|
27
|
-
if (!result.ok) {
|
|
28
|
-
throw new Error("Failed to fetch locale");
|
|
29
|
-
}
|
|
30
|
-
const json = await result.json();
|
|
31
|
-
if (json.errors) {
|
|
32
|
-
throw new Error(JSON.stringify(json.errors, null, 2));
|
|
33
|
-
}
|
|
34
|
-
return json.viewer.locale || undefined;
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
debug(`Failed to fetch locale for current user`, e.message);
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
exports.fetchLocale = fetchLocale;
|
package/dist/listeners.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function startUserContextListenerForLocale(): void;
|
package/dist/listeners.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.startUserContextListenerForLocale = void 0;
|
|
7
|
-
const equality_1 = __importDefault(require("@wry/equality"));
|
|
8
|
-
const dom_context_1 = require("dom-context");
|
|
9
|
-
const types_1 = require("./types");
|
|
10
|
-
const LocaleContext_1 = require("./contexts/LocaleContext");
|
|
11
|
-
const UserIdentityContext_1 = require("./contexts/UserIdentityContext");
|
|
12
|
-
const fetchLocale_1 = require("./fetchLocale");
|
|
13
|
-
const debug_1 = require("./debug");
|
|
14
|
-
const debug = (...args) => (0, debug_1.debug)(types_1.LOCALE_CONTEXT_NAME, ...args);
|
|
15
|
-
const userContextListenerDiv = (() => {
|
|
16
|
-
const id = "__environment_context_listener";
|
|
17
|
-
let div = document.getElementById(id);
|
|
18
|
-
if (!div) {
|
|
19
|
-
div = document.createElement("div");
|
|
20
|
-
div.id = id;
|
|
21
|
-
document.documentElement.appendChild(div);
|
|
22
|
-
}
|
|
23
|
-
return div;
|
|
24
|
-
})();
|
|
25
|
-
// Listens to user changes and refetches the locale from GraphQL
|
|
26
|
-
const userContextListenerForLocale = new dom_context_1.ContextListener({
|
|
27
|
-
contextName: types_1.USER_CONTEXT_NAME,
|
|
28
|
-
element: userContextListenerDiv,
|
|
29
|
-
onChange: async (next) => {
|
|
30
|
-
if (next) {
|
|
31
|
-
const userProvider = (0, UserIdentityContext_1.lazilyStartUserContext)();
|
|
32
|
-
if (!(0, equality_1.default)(userProvider.context, next)) {
|
|
33
|
-
debug("User context changed, refetching locale");
|
|
34
|
-
const locale = await (0, fetchLocale_1.fetchLocale)();
|
|
35
|
-
const localeProvider = (0, LocaleContext_1.lazilyStartLocaleContext)();
|
|
36
|
-
if (localeProvider.context !== locale) {
|
|
37
|
-
debug(`New value fetched from GraphQL [${locale}]`);
|
|
38
|
-
localeProvider.context =
|
|
39
|
-
locale ||
|
|
40
|
-
window.widgetIdent?.locale ||
|
|
41
|
-
navigator.language.replace("-", "_");
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
function startUserContextListenerForLocale() {
|
|
48
|
-
debug("Starting user context listener for locale updates");
|
|
49
|
-
userContextListenerForLocale.start();
|
|
50
|
-
}
|
|
51
|
-
exports.startUserContextListenerForLocale = startUserContextListenerForLocale;
|