@saasquatch/component-environment 1.0.0-1 → 1.0.0-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/index.d.ts +226 -5
- package/dist/index.js +429 -19
- package/dist/index.mjs +386 -0
- package/package.json +9 -3
- 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
- package/src/contexts/LocaleContext.ts +0 -50
- package/src/contexts/ProgramContext.ts +0 -50
- package/src/contexts/UserIdentityContext.ts +0 -164
- package/src/debug.ts +0 -7
- package/src/environment.ts +0 -114
- package/src/fetchLocale.ts +0 -52
- package/src/index.ts +0 -5
- package/src/listeners.ts +0 -51
- package/src/types.ts +0 -173
- package/tsconfig.json +0 -15
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __spreadValues = (a, b) => {
|
|
8
|
+
for (var prop in b || (b = {}))
|
|
9
|
+
if (__hasOwnProp.call(b, prop))
|
|
10
|
+
__defNormalProp(a, prop, b[prop]);
|
|
11
|
+
if (__getOwnPropSymbols)
|
|
12
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
13
|
+
if (__propIsEnum.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
}
|
|
16
|
+
return a;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/types.ts
|
|
20
|
+
var USER_CONTEXT_NAME = "sq:user-identity";
|
|
21
|
+
var LOCALE_CONTEXT_NAME = "sq:locale";
|
|
22
|
+
var PROGRAM_CONTEXT_NAME = "sq:program-id";
|
|
23
|
+
var DEFAULT_MEDIUM = "EMBED";
|
|
24
|
+
|
|
25
|
+
// src/environment.ts
|
|
26
|
+
function getEnvironment() {
|
|
27
|
+
return getEnvironmentSDK().type;
|
|
28
|
+
}
|
|
29
|
+
function getEnvironmentSDK() {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
if (window["SquatchAndroid"]) {
|
|
32
|
+
return {
|
|
33
|
+
type: "SquatchAndroid",
|
|
34
|
+
android: window["SquatchAndroid"],
|
|
35
|
+
widgetIdent: window["widgetIdent"]
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (window["SquatchPortal"]) {
|
|
39
|
+
return {
|
|
40
|
+
type: "SquatchPortal",
|
|
41
|
+
env: window["SquatchPortal"]
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (window["SquatchAdmin"]) {
|
|
45
|
+
return {
|
|
46
|
+
type: "SquatchAdmin",
|
|
47
|
+
adminSDK: window["SquatchAdmin"]
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (window["widgetIdent"] && ((_a = window["widgetIdent"]) == null ? void 0 : _a.env) !== "demo") {
|
|
51
|
+
return {
|
|
52
|
+
type: "SquatchJS2",
|
|
53
|
+
api: (_b = window.frameElement) == null ? void 0 : _b["squatchJsApi"],
|
|
54
|
+
widgetIdent: window["widgetIdent"]
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
type: "None"
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function isDemo() {
|
|
62
|
+
const sdk = getEnvironmentSDK();
|
|
63
|
+
return sdk.type === "None" || sdk.type === "SquatchAdmin";
|
|
64
|
+
}
|
|
65
|
+
var 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
|
+
var DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
|
|
80
|
+
function getAppDomain() {
|
|
81
|
+
var _a;
|
|
82
|
+
const sdk = getEnvironmentSDK();
|
|
83
|
+
switch (sdk.type) {
|
|
84
|
+
case "SquatchAndroid":
|
|
85
|
+
case "SquatchJS2":
|
|
86
|
+
return sdk.widgetIdent.appDomain;
|
|
87
|
+
case "SquatchPortal":
|
|
88
|
+
return ((_a = sdk.env) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
|
|
89
|
+
case "SquatchAdmin":
|
|
90
|
+
case "None":
|
|
91
|
+
return DEFAULT_DOMAIN;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function getEngagementMedium() {
|
|
95
|
+
const sdk = getEnvironmentSDK();
|
|
96
|
+
switch (sdk.type) {
|
|
97
|
+
case "SquatchJS2":
|
|
98
|
+
return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
|
|
99
|
+
case "SquatchAndroid":
|
|
100
|
+
case "SquatchPortal":
|
|
101
|
+
case "SquatchAdmin":
|
|
102
|
+
case "None":
|
|
103
|
+
return DEFAULT_MEDIUM;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/contexts/UserIdentityContext.ts
|
|
108
|
+
import decode from "jwt-decode";
|
|
109
|
+
import { ContextProvider as ContextProvider2 } from "dom-context";
|
|
110
|
+
import { equal } from "@wry/equality";
|
|
111
|
+
|
|
112
|
+
// src/listeners.ts
|
|
113
|
+
import { ContextListener } from "dom-context";
|
|
114
|
+
|
|
115
|
+
// src/contexts/LocaleContext.ts
|
|
116
|
+
import { ContextProvider } from "dom-context";
|
|
117
|
+
|
|
118
|
+
// src/debug.ts
|
|
119
|
+
var debugEnabled = localStorage.getItem("debug");
|
|
120
|
+
function debug(ns, ...args) {
|
|
121
|
+
if (debugEnabled) {
|
|
122
|
+
console.debug(`sq:environment:${ns}`, ...args);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/contexts/LocaleContext.ts
|
|
127
|
+
var debug2 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
|
|
128
|
+
function lazilyStartLocaleContext() {
|
|
129
|
+
var _a;
|
|
130
|
+
let globalProvider = window.squatchLocale;
|
|
131
|
+
if (!globalProvider) {
|
|
132
|
+
debug2("Creating locale context provider");
|
|
133
|
+
globalProvider = new ContextProvider({
|
|
134
|
+
element: document.documentElement,
|
|
135
|
+
initialState: ((_a = window.widgetIdent) == null ? void 0 : _a.locale) || navigator.language.replace("-", "_"),
|
|
136
|
+
contextName: LOCALE_CONTEXT_NAME
|
|
137
|
+
}).start();
|
|
138
|
+
window.squatchLocale = globalProvider;
|
|
139
|
+
}
|
|
140
|
+
return globalProvider;
|
|
141
|
+
}
|
|
142
|
+
function setLocale(locale) {
|
|
143
|
+
const globalProvider = lazilyStartLocaleContext();
|
|
144
|
+
if (globalProvider.context !== locale) {
|
|
145
|
+
debug2(`Setting locale context value [${locale}]`);
|
|
146
|
+
globalProvider.context = locale;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function getLocale() {
|
|
150
|
+
var _a;
|
|
151
|
+
return (_a = window.squatchLocale) == null ? void 0 : _a.context;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/fetchLocale.ts
|
|
155
|
+
var debug3 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
|
|
156
|
+
var GET_LOCALE = `
|
|
157
|
+
query {
|
|
158
|
+
viewer {
|
|
159
|
+
... on User {
|
|
160
|
+
locale
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
`;
|
|
165
|
+
async function fetchLocale() {
|
|
166
|
+
var _a;
|
|
167
|
+
debug3("Fetching locale from GraphQL for current user");
|
|
168
|
+
try {
|
|
169
|
+
const result = await fetch(`${getAppDomain()}/api/v1/${getTenantAlias()}/graphql`, {
|
|
170
|
+
method: "POST",
|
|
171
|
+
headers: {
|
|
172
|
+
"Content-Type": "application/json",
|
|
173
|
+
Authorization: `Bearer ${(_a = getUserIdentity()) == null ? void 0 : _a.jwt}`
|
|
174
|
+
},
|
|
175
|
+
body: JSON.stringify({
|
|
176
|
+
query: GET_LOCALE
|
|
177
|
+
})
|
|
178
|
+
});
|
|
179
|
+
if (!result.ok) {
|
|
180
|
+
throw new Error("Failed to fetch locale");
|
|
181
|
+
}
|
|
182
|
+
const json = await result.json();
|
|
183
|
+
if (json.errors) {
|
|
184
|
+
throw new Error(JSON.stringify(json.errors, null, 2));
|
|
185
|
+
}
|
|
186
|
+
return json.data.viewer.locale || void 0;
|
|
187
|
+
} catch (e) {
|
|
188
|
+
debug3(`Failed to fetch locale for current user`, e.message);
|
|
189
|
+
return void 0;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// src/listeners.ts
|
|
194
|
+
var debug4 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
|
|
195
|
+
var userContextListenerDiv = (() => {
|
|
196
|
+
const id = "__environment_context_listener";
|
|
197
|
+
let div = document.getElementById(id);
|
|
198
|
+
if (!div) {
|
|
199
|
+
div = document.createElement("div");
|
|
200
|
+
div.id = id;
|
|
201
|
+
document.documentElement.appendChild(div);
|
|
202
|
+
}
|
|
203
|
+
return div;
|
|
204
|
+
})();
|
|
205
|
+
var userContextListenerForLocale = new ContextListener({
|
|
206
|
+
contextName: USER_CONTEXT_NAME,
|
|
207
|
+
element: userContextListenerDiv,
|
|
208
|
+
onChange: async (next) => {
|
|
209
|
+
var _a;
|
|
210
|
+
const localeProvider = lazilyStartLocaleContext();
|
|
211
|
+
const defaultLocale = ((_a = window.widgetIdent) == null ? void 0 : _a.locale) || navigator.language.replace("-", "_");
|
|
212
|
+
let newLocale;
|
|
213
|
+
if (next) {
|
|
214
|
+
debug4("User context changed, refetching locale");
|
|
215
|
+
const locale = await fetchLocale();
|
|
216
|
+
if (localeProvider.context !== locale) {
|
|
217
|
+
debug4(`New value fetched from GraphQL [${locale}]`);
|
|
218
|
+
newLocale = locale || defaultLocale;
|
|
219
|
+
}
|
|
220
|
+
} else {
|
|
221
|
+
newLocale = defaultLocale;
|
|
222
|
+
}
|
|
223
|
+
debug4(`Setting locale context to [${newLocale}]`);
|
|
224
|
+
localeProvider.context = newLocale;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
function startUserContextListenerForLocale() {
|
|
228
|
+
debug4("Starting user context listener for locale updates");
|
|
229
|
+
userContextListenerForLocale.start();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// src/contexts/UserIdentityContext.ts
|
|
233
|
+
var debug5 = (...args) => debug(USER_CONTEXT_NAME, ...args);
|
|
234
|
+
function lazilyStartUserContext() {
|
|
235
|
+
let globalProvider = window.squatchUserIdentity;
|
|
236
|
+
if (!globalProvider) {
|
|
237
|
+
debug5("Creating user context provider");
|
|
238
|
+
globalProvider = new ContextProvider2({
|
|
239
|
+
element: document.documentElement,
|
|
240
|
+
initialState: _getInitialValue(),
|
|
241
|
+
contextName: USER_CONTEXT_NAME
|
|
242
|
+
}).start();
|
|
243
|
+
window.squatchUserIdentity = globalProvider;
|
|
244
|
+
startUserContextListenerForLocale();
|
|
245
|
+
}
|
|
246
|
+
return globalProvider;
|
|
247
|
+
}
|
|
248
|
+
function isDecodedSquatchJWT(decoded) {
|
|
249
|
+
return decoded.user && decoded.user.id && decoded.user.accountId;
|
|
250
|
+
}
|
|
251
|
+
function isDecodedWidgetAPIJWT(decoded) {
|
|
252
|
+
return decoded.sub && /.*:.*@.*:users/.test(decoded.sub);
|
|
253
|
+
}
|
|
254
|
+
function userIdentityFromJwt(jwt) {
|
|
255
|
+
if (!jwt)
|
|
256
|
+
return void 0;
|
|
257
|
+
try {
|
|
258
|
+
const decoded = decode(jwt);
|
|
259
|
+
const exp = decoded.exp;
|
|
260
|
+
let userId = void 0;
|
|
261
|
+
let accountId = void 0;
|
|
262
|
+
if (isDecodedWidgetAPIJWT(decoded)) {
|
|
263
|
+
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
|
|
264
|
+
if (matches == null ? void 0 : matches[1])
|
|
265
|
+
accountId = atob(matches[1]);
|
|
266
|
+
if (matches == null ? void 0 : matches[2])
|
|
267
|
+
userId = atob(matches[2]);
|
|
268
|
+
} else if (isDecodedSquatchJWT(decoded)) {
|
|
269
|
+
accountId = decoded.user.accountId;
|
|
270
|
+
userId = decoded.user.id;
|
|
271
|
+
}
|
|
272
|
+
if (!userId || !accountId) {
|
|
273
|
+
return void 0;
|
|
274
|
+
}
|
|
275
|
+
if (exp && Date.now() >= exp * 1e3) {
|
|
276
|
+
return void 0;
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
id: userId,
|
|
280
|
+
accountId,
|
|
281
|
+
jwt
|
|
282
|
+
};
|
|
283
|
+
} catch (e) {
|
|
284
|
+
return void 0;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function _getInitialValue() {
|
|
288
|
+
const sdk = getEnvironmentSDK();
|
|
289
|
+
switch (sdk.type) {
|
|
290
|
+
case "SquatchAndroid":
|
|
291
|
+
case "SquatchJS2":
|
|
292
|
+
return {
|
|
293
|
+
id: sdk.widgetIdent.userId,
|
|
294
|
+
accountId: sdk.widgetIdent.accountId,
|
|
295
|
+
jwt: sdk.widgetIdent.token
|
|
296
|
+
};
|
|
297
|
+
case "SquatchPortal":
|
|
298
|
+
const searchParams = new URLSearchParams(document.location.search);
|
|
299
|
+
if (searchParams.has("jwt")) {
|
|
300
|
+
return userIdentityFromJwt(searchParams.get("jwt"));
|
|
301
|
+
}
|
|
302
|
+
const stored = localStorage.getItem(USER_CONTEXT_NAME);
|
|
303
|
+
if (!stored)
|
|
304
|
+
return void 0;
|
|
305
|
+
try {
|
|
306
|
+
const potentialUserIdent = JSON.parse(stored);
|
|
307
|
+
const identity = userIdentityFromJwt(potentialUserIdent.jwt);
|
|
308
|
+
if (identity) {
|
|
309
|
+
return __spreadValues(__spreadValues({}, potentialUserIdent), identity);
|
|
310
|
+
}
|
|
311
|
+
return void 0;
|
|
312
|
+
} catch (e) {
|
|
313
|
+
return void 0;
|
|
314
|
+
}
|
|
315
|
+
case "SquatchAdmin":
|
|
316
|
+
case "None":
|
|
317
|
+
return void 0;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function setUserIdentity(identity) {
|
|
321
|
+
const globalProvider = lazilyStartUserContext();
|
|
322
|
+
if (!equal(globalProvider.context, identity)) {
|
|
323
|
+
debug5(`Setting user context value [${JSON.stringify(identity)}]`);
|
|
324
|
+
globalProvider.context = identity;
|
|
325
|
+
}
|
|
326
|
+
if (identity && getEnvironmentSDK().type === "SquatchPortal") {
|
|
327
|
+
localStorage.setItem(USER_CONTEXT_NAME, JSON.stringify(identity));
|
|
328
|
+
} else if (!identity) {
|
|
329
|
+
localStorage.removeItem(USER_CONTEXT_NAME);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function getUserIdentity() {
|
|
333
|
+
var _a;
|
|
334
|
+
return (_a = window.squatchUserIdentity) == null ? void 0 : _a.context;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/contexts/ProgramContext.ts
|
|
338
|
+
import { ContextProvider as ContextProvider3 } from "dom-context";
|
|
339
|
+
var debug6 = (...args) => debug(PROGRAM_CONTEXT_NAME, ...args);
|
|
340
|
+
function lazilyStartProgramContext() {
|
|
341
|
+
var _a;
|
|
342
|
+
let globalProvider = window.squatchProgramId;
|
|
343
|
+
if (!globalProvider) {
|
|
344
|
+
debug6("Creating program context provider");
|
|
345
|
+
globalProvider = new ContextProvider3({
|
|
346
|
+
element: document.documentElement,
|
|
347
|
+
initialState: ((_a = window.widgetIdent) == null ? void 0 : _a.programId) || void 0,
|
|
348
|
+
contextName: PROGRAM_CONTEXT_NAME
|
|
349
|
+
}).start();
|
|
350
|
+
window.squatchProgramId = globalProvider;
|
|
351
|
+
}
|
|
352
|
+
return globalProvider;
|
|
353
|
+
}
|
|
354
|
+
function setProgramId(programId) {
|
|
355
|
+
const globalProvider = lazilyStartProgramContext();
|
|
356
|
+
if (globalProvider.context !== programId) {
|
|
357
|
+
debug6(`Setting program context value [${programId}]`);
|
|
358
|
+
globalProvider.context = programId;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function getProgramId() {
|
|
362
|
+
var _a;
|
|
363
|
+
return (_a = window.squatchLocale) == null ? void 0 : _a.context;
|
|
364
|
+
}
|
|
365
|
+
export {
|
|
366
|
+
DEFAULT_MEDIUM,
|
|
367
|
+
LOCALE_CONTEXT_NAME,
|
|
368
|
+
PROGRAM_CONTEXT_NAME,
|
|
369
|
+
USER_CONTEXT_NAME,
|
|
370
|
+
getAppDomain,
|
|
371
|
+
getEngagementMedium,
|
|
372
|
+
getEnvironment,
|
|
373
|
+
getEnvironmentSDK,
|
|
374
|
+
getLocale,
|
|
375
|
+
getProgramId,
|
|
376
|
+
getTenantAlias,
|
|
377
|
+
getUserIdentity,
|
|
378
|
+
isDemo,
|
|
379
|
+
lazilyStartLocaleContext,
|
|
380
|
+
lazilyStartProgramContext,
|
|
381
|
+
lazilyStartUserContext,
|
|
382
|
+
setLocale,
|
|
383
|
+
setProgramId,
|
|
384
|
+
setUserIdentity,
|
|
385
|
+
userIdentityFromJwt
|
|
386
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saasquatch/component-environment",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-4",
|
|
4
4
|
"description": "Environment and contexts for SaaSquatch components",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/"
|
|
9
|
+
],
|
|
6
10
|
"scripts": {
|
|
7
|
-
"build": "
|
|
11
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
8
12
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
13
|
},
|
|
10
14
|
"engines": {
|
|
@@ -12,11 +16,13 @@
|
|
|
12
16
|
},
|
|
13
17
|
"repository": {
|
|
14
18
|
"type": "git",
|
|
15
|
-
"url": "
|
|
19
|
+
"url": "https://github.com/saasquatch/program-tools",
|
|
20
|
+
"directory": "packages/component-environment"
|
|
16
21
|
},
|
|
17
22
|
"author": "Johan Venter <johan@saasquatch.com>",
|
|
18
23
|
"license": "ISC",
|
|
19
24
|
"devDependencies": {
|
|
25
|
+
"tsup": "^6.1.2",
|
|
20
26
|
"typescript": "^4.7.3"
|
|
21
27
|
},
|
|
22
28
|
"dependencies": {
|
package/dist/LocaleContext.d.ts
DELETED
package/dist/LocaleContext.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// const CONTEXT_NAME = "sq:locale";
|
|
2
|
-
// const GET_LOCALE = gql`
|
|
3
|
-
// query {
|
|
4
|
-
// viewer {
|
|
5
|
-
// ... on User {
|
|
6
|
-
// locale
|
|
7
|
-
// }
|
|
8
|
-
// }
|
|
9
|
-
// }
|
|
10
|
-
// `;
|
|
11
|
-
export function lazilyStartLocaleContext() {
|
|
12
|
-
// const globalProvider = window.squatchLocale;
|
|
13
|
-
// const user = useUserIdentity();
|
|
14
|
-
// useEffect(() => {
|
|
15
|
-
// // Clear locale if user is undefined
|
|
16
|
-
// if (!user && globalProvider) {
|
|
17
|
-
// return (globalProvider.context = undefined);
|
|
18
|
-
// }
|
|
19
|
-
// fetch({});
|
|
20
|
-
// }, [user]);
|
|
21
|
-
// const [fetch, { data }] = useLazyQuery(GET_LOCALE);
|
|
22
|
-
// const locale = data?.viewer?.locale;
|
|
23
|
-
// if (!globalProvider) {
|
|
24
|
-
// // Lazily creates a global provider
|
|
25
|
-
// window.squatchLocale = new ContextProvider<string>({
|
|
26
|
-
// element: document.documentElement,
|
|
27
|
-
// initialState: locale || window.widgetIdent?.locale || undefined,
|
|
28
|
-
// contextName: CONTEXT_NAME,
|
|
29
|
-
// }).start();
|
|
30
|
-
// } else if (locale !== globalProvider.context) {
|
|
31
|
-
// globalProvider.context = locale;
|
|
32
|
-
// }
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Overide the globally defined Locale context
|
|
36
|
-
*
|
|
37
|
-
* @param locale the new locale used by the user
|
|
38
|
-
*/
|
|
39
|
-
export function setLocale(locale) {
|
|
40
|
-
const globalProvider = window.squatchLocale;
|
|
41
|
-
if (globalProvider)
|
|
42
|
-
globalProvider.context = locale;
|
|
43
|
-
}
|
package/dist/ProgramContext.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { ContextProvider } from "dom-context";
|
|
2
|
-
/**
|
|
3
|
-
* Lazily start the program context provider
|
|
4
|
-
*
|
|
5
|
-
* @returns The global program context provider
|
|
6
|
-
*/
|
|
7
|
-
export declare function lazilyStartProgramContext(): ContextProvider<string | undefined>;
|
|
8
|
-
/**
|
|
9
|
-
* Overide the globally defined Program ID context
|
|
10
|
-
*
|
|
11
|
-
* @param programId the new programID used by the user, or undefined if logged out
|
|
12
|
-
*/
|
|
13
|
-
export declare function setProgramId(programId: string | undefined): void;
|
package/dist/ProgramContext.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { ContextProvider } from "dom-context";
|
|
2
|
-
import { PROGRAM_CONTEXT_NAME } from "./types";
|
|
3
|
-
/**
|
|
4
|
-
* Lazily start the program context provider
|
|
5
|
-
*
|
|
6
|
-
* @returns The global program context provider
|
|
7
|
-
*/
|
|
8
|
-
export function lazilyStartProgramContext() {
|
|
9
|
-
let globalProvider = window.squatchProgramId;
|
|
10
|
-
if (!globalProvider) {
|
|
11
|
-
// Lazily creates a global provider
|
|
12
|
-
globalProvider = new ContextProvider({
|
|
13
|
-
element: document.documentElement,
|
|
14
|
-
initialState: window.widgetIdent?.programId || undefined,
|
|
15
|
-
contextName: PROGRAM_CONTEXT_NAME,
|
|
16
|
-
}).start();
|
|
17
|
-
window.squatchProgramId = globalProvider;
|
|
18
|
-
}
|
|
19
|
-
return globalProvider;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Overide the globally defined Program ID context
|
|
23
|
-
*
|
|
24
|
-
* @param programId the new programID used by the user, or undefined if logged out
|
|
25
|
-
*/
|
|
26
|
-
export function setProgramId(programId) {
|
|
27
|
-
const globalProvider = lazilyStartProgramContext();
|
|
28
|
-
globalProvider.context = programId;
|
|
29
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ContextProvider } from "dom-context";
|
|
2
|
-
import { UserIdentity } from "./types";
|
|
3
|
-
export declare function lazilyStartUserContext(): ContextProvider<UserIdentity | undefined>;
|
|
4
|
-
export declare function userIdentityFromJwt(jwt?: string): UserIdentity | undefined;
|
|
5
|
-
/**
|
|
6
|
-
* Overide the globally defined user context, and persists the user identity in local storage
|
|
7
|
-
*
|
|
8
|
-
* @param identity the new identity of the user, or undefined if logged out
|
|
9
|
-
*/
|
|
10
|
-
export declare function setUserIdentity(identity?: UserIdentity): void;
|
|
@@ -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;
|