@saasquatch/component-environment 1.0.0-5 → 1.0.1-1
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/dist/index.d.ts +2 -1
- package/dist/index.js +14 -6
- package/dist/index.mjs +12 -5
- package/package.json +7 -2
package/dist/index.d.ts
CHANGED
|
@@ -253,6 +253,7 @@ declare function setUserIdentity(identity?: UserIdentity): void;
|
|
|
253
253
|
*/
|
|
254
254
|
declare function getUserIdentity(): UserIdentity | undefined;
|
|
255
255
|
|
|
256
|
+
declare function validateLocale(locale?: string): string | undefined;
|
|
256
257
|
/**
|
|
257
258
|
* Lazily start the locale context provider. If it already exists, the existing provider is
|
|
258
259
|
* returned. This function is safe to call multiple times.
|
|
@@ -289,4 +290,4 @@ declare function setProgramId(programId: string | undefined): void;
|
|
|
289
290
|
*/
|
|
290
291
|
declare function getProgramId(): string | undefined;
|
|
291
292
|
|
|
292
|
-
export { BrowserHistory, DEFAULT_MEDIUM, DecodedSquatchJWT, DecodedWidgetAPIJWT, EngagementMedium, Environment, EnvironmentSDK, HistoryEntry, LOCALE_CONTEXT_NAME, LazyHistory, LocaleContextName, MemoryHistory, PROGRAM_CONTEXT_NAME, PortalEnv, ProgramContextName, SquatchAdmin, SquatchAndroid, SquatchJS2, USER_CONTEXT_NAME, UserContextName, UserId, UserIdentity, WidgetIdent, getAppDomain, getEngagementMedium, getEnvironment, getEnvironmentSDK, getLocale, getProgramId, getTenantAlias, getUserIdentity, _default as history, isDemo, lazilyStartLocaleContext, lazilyStartProgramContext, lazilyStartUserContext, setLocale, setProgramId, setUserIdentity, userIdentityFromJwt };
|
|
293
|
+
export { BrowserHistory, DEFAULT_MEDIUM, DecodedSquatchJWT, DecodedWidgetAPIJWT, EngagementMedium, Environment, EnvironmentSDK, HistoryEntry, LOCALE_CONTEXT_NAME, LazyHistory, LocaleContextName, MemoryHistory, PROGRAM_CONTEXT_NAME, PortalEnv, ProgramContextName, SquatchAdmin, SquatchAndroid, SquatchJS2, USER_CONTEXT_NAME, UserContextName, UserId, UserIdentity, WidgetIdent, getAppDomain, getEngagementMedium, getEnvironment, getEnvironmentSDK, getLocale, getProgramId, getTenantAlias, getUserIdentity, _default as history, isDemo, lazilyStartLocaleContext, lazilyStartProgramContext, lazilyStartUserContext, setLocale, setProgramId, setUserIdentity, userIdentityFromJwt, validateLocale };
|
package/dist/index.js
CHANGED
|
@@ -63,7 +63,8 @@ __export(src_exports, {
|
|
|
63
63
|
setLocale: () => setLocale,
|
|
64
64
|
setProgramId: () => setProgramId,
|
|
65
65
|
setUserIdentity: () => setUserIdentity,
|
|
66
|
-
userIdentityFromJwt: () => userIdentityFromJwt
|
|
66
|
+
userIdentityFromJwt: () => userIdentityFromJwt,
|
|
67
|
+
validateLocale: () => validateLocale
|
|
67
68
|
});
|
|
68
69
|
module.exports = __toCommonJS(src_exports);
|
|
69
70
|
|
|
@@ -288,8 +289,8 @@ var MemoryHistory = class {
|
|
|
288
289
|
if (typeof to === "string") {
|
|
289
290
|
this.history[this.index] = {
|
|
290
291
|
pathname: to,
|
|
291
|
-
search:
|
|
292
|
-
hash:
|
|
292
|
+
search: "",
|
|
293
|
+
hash: "",
|
|
293
294
|
state
|
|
294
295
|
};
|
|
295
296
|
} else {
|
|
@@ -359,14 +360,20 @@ var import_dom_context2 = require("dom-context");
|
|
|
359
360
|
// src/contexts/LocaleContext.ts
|
|
360
361
|
var import_dom_context = require("dom-context");
|
|
361
362
|
var debug3 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
|
|
363
|
+
function validateLocale(locale) {
|
|
364
|
+
if (locale && /^[a-z]{2}_(?:[A-Z]{2}|[0-9]{3})$/.test(locale)) {
|
|
365
|
+
return locale;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
362
368
|
function lazilyStartLocaleContext() {
|
|
363
369
|
var _a;
|
|
364
370
|
let globalProvider = window.squatchLocale;
|
|
365
371
|
if (!globalProvider) {
|
|
366
372
|
debug3("Creating locale context provider");
|
|
373
|
+
const locale = ((_a = window.widgetIdent) == null ? void 0 : _a.locale) ?? validateLocale(navigator.language.replace("-", "_"));
|
|
367
374
|
globalProvider = new import_dom_context.ContextProvider({
|
|
368
375
|
element: document.documentElement,
|
|
369
|
-
initialState:
|
|
376
|
+
initialState: locale,
|
|
370
377
|
contextName: LOCALE_CONTEXT_NAME
|
|
371
378
|
}).start();
|
|
372
379
|
window.squatchLocale = globalProvider;
|
|
@@ -594,7 +601,7 @@ function setProgramId(programId) {
|
|
|
594
601
|
}
|
|
595
602
|
function getProgramId() {
|
|
596
603
|
var _a;
|
|
597
|
-
return (_a = window.
|
|
604
|
+
return (_a = window.squatchProgramId) == null ? void 0 : _a.context;
|
|
598
605
|
}
|
|
599
606
|
// Annotate the CommonJS export names for ESM import in node:
|
|
600
607
|
0 && (module.exports = {
|
|
@@ -621,5 +628,6 @@ function getProgramId() {
|
|
|
621
628
|
setLocale,
|
|
622
629
|
setProgramId,
|
|
623
630
|
setUserIdentity,
|
|
624
|
-
userIdentityFromJwt
|
|
631
|
+
userIdentityFromJwt,
|
|
632
|
+
validateLocale
|
|
625
633
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -240,8 +240,8 @@ var MemoryHistory = class {
|
|
|
240
240
|
if (typeof to === "string") {
|
|
241
241
|
this.history[this.index] = {
|
|
242
242
|
pathname: to,
|
|
243
|
-
search:
|
|
244
|
-
hash:
|
|
243
|
+
search: "",
|
|
244
|
+
hash: "",
|
|
245
245
|
state
|
|
246
246
|
};
|
|
247
247
|
} else {
|
|
@@ -311,14 +311,20 @@ import { ContextListener } from "dom-context";
|
|
|
311
311
|
// src/contexts/LocaleContext.ts
|
|
312
312
|
import { ContextProvider } from "dom-context";
|
|
313
313
|
var debug3 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
|
|
314
|
+
function validateLocale(locale) {
|
|
315
|
+
if (locale && /^[a-z]{2}_(?:[A-Z]{2}|[0-9]{3})$/.test(locale)) {
|
|
316
|
+
return locale;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
314
319
|
function lazilyStartLocaleContext() {
|
|
315
320
|
var _a;
|
|
316
321
|
let globalProvider = window.squatchLocale;
|
|
317
322
|
if (!globalProvider) {
|
|
318
323
|
debug3("Creating locale context provider");
|
|
324
|
+
const locale = ((_a = window.widgetIdent) == null ? void 0 : _a.locale) ?? validateLocale(navigator.language.replace("-", "_"));
|
|
319
325
|
globalProvider = new ContextProvider({
|
|
320
326
|
element: document.documentElement,
|
|
321
|
-
initialState:
|
|
327
|
+
initialState: locale,
|
|
322
328
|
contextName: LOCALE_CONTEXT_NAME
|
|
323
329
|
}).start();
|
|
324
330
|
window.squatchLocale = globalProvider;
|
|
@@ -546,7 +552,7 @@ function setProgramId(programId) {
|
|
|
546
552
|
}
|
|
547
553
|
function getProgramId() {
|
|
548
554
|
var _a;
|
|
549
|
-
return (_a = window.
|
|
555
|
+
return (_a = window.squatchProgramId) == null ? void 0 : _a.context;
|
|
550
556
|
}
|
|
551
557
|
export {
|
|
552
558
|
BrowserHistory,
|
|
@@ -572,5 +578,6 @@ export {
|
|
|
572
578
|
setLocale,
|
|
573
579
|
setProgramId,
|
|
574
580
|
setUserIdentity,
|
|
575
|
-
userIdentityFromJwt
|
|
581
|
+
userIdentityFromJwt,
|
|
582
|
+
validateLocale
|
|
576
583
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saasquatch/component-environment",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-1",
|
|
4
4
|
"description": "Environment and contexts for SaaSquatch components",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
12
|
-
"test": "
|
|
12
|
+
"test": "jest"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=16.15"
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
"author": "Johan Venter <johan@saasquatch.com>",
|
|
23
23
|
"license": "ISC",
|
|
24
24
|
"devDependencies": {
|
|
25
|
+
"@types/jest": "^28.1.4",
|
|
26
|
+
"jest": "^28.1.2",
|
|
27
|
+
"jest-environment-jsdom": "^28.1.2",
|
|
28
|
+
"ts-jest": "^28.0.5",
|
|
29
|
+
"ts-node": "^10.8.2",
|
|
25
30
|
"tsup": "^6.1.2",
|
|
26
31
|
"typescript": "^4.7.3"
|
|
27
32
|
},
|