@saasquatch/component-environment 1.0.2-3 → 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 ReferralSaaSquatch.com, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,46 +1,46 @@
1
- Provides the environment for running SaaSquatch web components.
2
-
3
- SaaSquatch web components can run in a number of different environments, including:
4
-
5
- - in a widget via `squatch-js`
6
- - in a microsite (portal)
7
- - in a mobile SDK (i.e. `squatch-android`)
8
- - in the admin portal
9
-
10
- In each environment, a different set of context information about the tenant, user and program are provided, and the goal of this package is to normalize the differences in environments to provide a common API.
11
-
12
- The environment is provided in a set of contexts through `dom-context`, which provides vanilla global context providers. They can be accessed through a raw `ContextListener` or via `useDomContext` in `dom-context-hooks`.
13
-
14
- ## General environment
15
-
16
- ### `getEnvironment()`
17
-
18
- Get the environment type. The current possible values are: `SquatchJS2`, `SquatchAndroid`, `SquatchPortal`, `SquatchAdmin` or `None`.
19
-
20
- ### `isDemo()`
21
-
22
- Returns whether components should run in demo/preview mode.
23
-
24
- ### `getTenantAlias()`
25
-
26
- Get the current tenant alias.
27
-
28
- ### `getAppDomain()`
29
-
30
- Get the SaaSquatch app domain.
31
-
32
- ### `getEngagementMedium()`
33
-
34
- Get the current engagement medium. This is particularly important in widgets rendered by `squatch-js` for informing metadata about share link clicks.
35
-
36
- ## User identity
37
-
38
- The user identity context name is exported in a constant `USER_CONTEXT_NAME`. The current value can be retrieved with `getUserIdentity()` and set with `setUserIdentity(identity)`.
39
-
40
- ## Locale
41
-
42
- The locale context name is exported in a constnat `LOCALE_CONTEXT_NAME`. The current value can be retrieved with `getLocale()` and set with `setLocal(locale)`.
43
-
44
- ## Program ID
45
-
46
- The program ID context name is exported in a constant `PROGRAM_CONTEXT_NAME`. The current value can be retrieved with `getProgramId()` and set with `setProgramId(programId)`.
1
+ Provides the environment for running SaaSquatch web components.
2
+
3
+ SaaSquatch web components can run in a number of different environments, including:
4
+
5
+ - in a widget via `squatch-js`
6
+ - in a microsite (portal)
7
+ - in a mobile SDK (i.e. `squatch-android`)
8
+ - in the admin portal
9
+
10
+ In each environment, a different set of context information about the tenant, user and program are provided, and the goal of this package is to normalize the differences in environments to provide a common API.
11
+
12
+ The environment is provided in a set of contexts through `dom-context`, which provides vanilla global context providers. They can be accessed through a raw `ContextListener` or via `useDomContext` in `dom-context-hooks`.
13
+
14
+ ## General environment
15
+
16
+ ### `getEnvironment()`
17
+
18
+ Get the environment type. The current possible values are: `SquatchJS2`, `SquatchAndroid`, `SquatchPortal`, `SquatchAdmin` or `None`.
19
+
20
+ ### `isDemo()`
21
+
22
+ Returns whether components should run in demo/preview mode.
23
+
24
+ ### `getTenantAlias()`
25
+
26
+ Get the current tenant alias.
27
+
28
+ ### `getAppDomain()`
29
+
30
+ Get the SaaSquatch app domain.
31
+
32
+ ### `getEngagementMedium()`
33
+
34
+ Get the current engagement medium. This is particularly important in widgets rendered by `squatch-js` for informing metadata about share link clicks.
35
+
36
+ ## User identity
37
+
38
+ The user identity context name is exported in a constant `USER_CONTEXT_NAME`. The current value can be retrieved with `getUserIdentity()` and set with `setUserIdentity(identity)`.
39
+
40
+ ## Locale
41
+
42
+ The locale context name is exported in a constnat `LOCALE_CONTEXT_NAME`. The current value can be retrieved with `getLocale()` and set with `setLocal(locale)`.
43
+
44
+ ## Program ID
45
+
46
+ The program ID context name is exported in a constant `PROGRAM_CONTEXT_NAME`. The current value can be retrieved with `getProgramId()` and set with `setProgramId(programId)`.
package/dist/index.d.ts CHANGED
@@ -137,9 +137,6 @@ declare type EnvironmentSDK = {
137
137
  type: "SquatchAndroid";
138
138
  android: SquatchAndroid;
139
139
  widgetIdent: WidgetIdent;
140
- } | {
141
- type: "SquatchIOS";
142
- widgetIdent: WidgetIdent;
143
140
  } | {
144
141
  type: "SquatchPortal";
145
142
  env: PortalEnv;
package/dist/index.js CHANGED
@@ -79,7 +79,7 @@ function getEnvironment() {
79
79
  return getEnvironmentSDK().type;
80
80
  }
81
81
  function getEnvironmentSDK() {
82
- var _a, _b, _c;
82
+ var _a, _b;
83
83
  if (window["SquatchAndroid"]) {
84
84
  return {
85
85
  type: "SquatchAndroid",
@@ -99,10 +99,10 @@ function getEnvironmentSDK() {
99
99
  adminSDK: window["SquatchAdmin"]
100
100
  };
101
101
  }
102
- if (((_a = window.frameElement) == null ? void 0 : _a["squatchJsApi"]) && ((_b = window["widgetIdent"]) == null ? void 0 : _b.env) !== "demo") {
102
+ if (window["widgetIdent"] && ((_a = window["widgetIdent"]) == null ? void 0 : _a.env) !== "demo") {
103
103
  return {
104
104
  type: "SquatchJS2",
105
- api: (_c = window.frameElement) == null ? void 0 : _c["squatchJsApi"],
105
+ api: (_b = window.frameElement) == null ? void 0 : _b["squatchJsApi"],
106
106
  widgetIdent: window["widgetIdent"]
107
107
  };
108
108
  }
@@ -116,13 +116,11 @@ function isDemo() {
116
116
  }
117
117
  var FAKE_TENANT = "demo";
118
118
  function getTenantAlias() {
119
- var _a;
120
119
  const sdk = getEnvironmentSDK();
121
120
  switch (sdk.type) {
122
121
  case "SquatchAndroid":
123
- case "SquatchIOS":
124
122
  case "SquatchJS2":
125
- return (_a = sdk.widgetIdent) == null ? void 0 : _a.tenantAlias;
123
+ return sdk.widgetIdent.tenantAlias;
126
124
  case "SquatchAdmin":
127
125
  case "None":
128
126
  return FAKE_TENANT;
@@ -132,28 +130,25 @@ function getTenantAlias() {
132
130
  }
133
131
  var DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
134
132
  function getAppDomain() {
135
- var _a, _b;
133
+ var _a;
136
134
  const sdk = getEnvironmentSDK();
137
135
  switch (sdk.type) {
138
136
  case "SquatchAndroid":
139
- case "SquatchIOS":
140
137
  case "SquatchJS2":
141
- return ((_a = sdk.widgetIdent) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
138
+ return sdk.widgetIdent.appDomain;
142
139
  case "SquatchPortal":
143
- return ((_b = sdk.env) == null ? void 0 : _b.appDomain) || DEFAULT_DOMAIN;
140
+ return ((_a = sdk.env) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
144
141
  case "SquatchAdmin":
145
142
  case "None":
146
143
  return DEFAULT_DOMAIN;
147
144
  }
148
145
  }
149
146
  function getEngagementMedium() {
150
- var _a;
151
147
  const sdk = getEnvironmentSDK();
152
148
  switch (sdk.type) {
153
149
  case "SquatchJS2":
154
- return ((_a = sdk.widgetIdent) == null ? void 0 : _a.engagementMedium) || DEFAULT_MEDIUM;
150
+ return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
155
151
  case "SquatchAndroid":
156
- case "SquatchIOS":
157
152
  case "SquatchPortal":
158
153
  case "SquatchAdmin":
159
154
  case "None":
@@ -533,14 +528,8 @@ function userIdentityFromJwt(jwt) {
533
528
  function _getInitialValue() {
534
529
  const sdk = getEnvironmentSDK();
535
530
  switch (sdk.type) {
536
- case "SquatchIOS":
537
531
  case "SquatchAndroid":
538
532
  case "SquatchJS2":
539
- if (!sdk.widgetIdent)
540
- return void 0;
541
- const { userId, accountId, token } = sdk.widgetIdent;
542
- if (!userId || !accountId || !token)
543
- return void 0;
544
533
  return {
545
534
  id: sdk.widgetIdent.userId,
546
535
  accountId: sdk.widgetIdent.accountId,
@@ -571,16 +560,13 @@ function _getInitialValue() {
571
560
  }
572
561
  function setUserIdentity(identity) {
573
562
  const globalProvider = lazilyStartUserContext();
574
- const sdk = getEnvironmentSDK();
575
- const widgetIdent = sdk.type === "SquatchJS2" ? sdk.widgetIdent : void 0;
576
- console.log({ sdk, widgetIdent });
577
563
  if (!(0, import_equality.equal)(globalProvider.context, identity)) {
578
564
  debug6(`Setting user context value [${JSON.stringify(identity)}]`);
579
565
  globalProvider.context = identity;
580
566
  }
581
567
  if (identity && getEnvironmentSDK().type === "SquatchPortal") {
582
568
  localStorage.setItem(USER_CONTEXT_NAME, JSON.stringify(identity));
583
- } else if (!identity && getEnvironmentSDK().type === "SquatchPortal") {
569
+ } else if (!identity) {
584
570
  localStorage.removeItem(USER_CONTEXT_NAME);
585
571
  }
586
572
  }
package/dist/index.mjs CHANGED
@@ -30,7 +30,7 @@ function getEnvironment() {
30
30
  return getEnvironmentSDK().type;
31
31
  }
32
32
  function getEnvironmentSDK() {
33
- var _a, _b, _c;
33
+ var _a, _b;
34
34
  if (window["SquatchAndroid"]) {
35
35
  return {
36
36
  type: "SquatchAndroid",
@@ -50,10 +50,10 @@ function getEnvironmentSDK() {
50
50
  adminSDK: window["SquatchAdmin"]
51
51
  };
52
52
  }
53
- if (((_a = window.frameElement) == null ? void 0 : _a["squatchJsApi"]) && ((_b = window["widgetIdent"]) == null ? void 0 : _b.env) !== "demo") {
53
+ if (window["widgetIdent"] && ((_a = window["widgetIdent"]) == null ? void 0 : _a.env) !== "demo") {
54
54
  return {
55
55
  type: "SquatchJS2",
56
- api: (_c = window.frameElement) == null ? void 0 : _c["squatchJsApi"],
56
+ api: (_b = window.frameElement) == null ? void 0 : _b["squatchJsApi"],
57
57
  widgetIdent: window["widgetIdent"]
58
58
  };
59
59
  }
@@ -67,13 +67,11 @@ function isDemo() {
67
67
  }
68
68
  var FAKE_TENANT = "demo";
69
69
  function getTenantAlias() {
70
- var _a;
71
70
  const sdk = getEnvironmentSDK();
72
71
  switch (sdk.type) {
73
72
  case "SquatchAndroid":
74
- case "SquatchIOS":
75
73
  case "SquatchJS2":
76
- return (_a = sdk.widgetIdent) == null ? void 0 : _a.tenantAlias;
74
+ return sdk.widgetIdent.tenantAlias;
77
75
  case "SquatchAdmin":
78
76
  case "None":
79
77
  return FAKE_TENANT;
@@ -83,28 +81,25 @@ function getTenantAlias() {
83
81
  }
84
82
  var DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
85
83
  function getAppDomain() {
86
- var _a, _b;
84
+ var _a;
87
85
  const sdk = getEnvironmentSDK();
88
86
  switch (sdk.type) {
89
87
  case "SquatchAndroid":
90
- case "SquatchIOS":
91
88
  case "SquatchJS2":
92
- return ((_a = sdk.widgetIdent) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
89
+ return sdk.widgetIdent.appDomain;
93
90
  case "SquatchPortal":
94
- return ((_b = sdk.env) == null ? void 0 : _b.appDomain) || DEFAULT_DOMAIN;
91
+ return ((_a = sdk.env) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
95
92
  case "SquatchAdmin":
96
93
  case "None":
97
94
  return DEFAULT_DOMAIN;
98
95
  }
99
96
  }
100
97
  function getEngagementMedium() {
101
- var _a;
102
98
  const sdk = getEnvironmentSDK();
103
99
  switch (sdk.type) {
104
100
  case "SquatchJS2":
105
- return ((_a = sdk.widgetIdent) == null ? void 0 : _a.engagementMedium) || DEFAULT_MEDIUM;
101
+ return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
106
102
  case "SquatchAndroid":
107
- case "SquatchIOS":
108
103
  case "SquatchPortal":
109
104
  case "SquatchAdmin":
110
105
  case "None":
@@ -484,14 +479,8 @@ function userIdentityFromJwt(jwt) {
484
479
  function _getInitialValue() {
485
480
  const sdk = getEnvironmentSDK();
486
481
  switch (sdk.type) {
487
- case "SquatchIOS":
488
482
  case "SquatchAndroid":
489
483
  case "SquatchJS2":
490
- if (!sdk.widgetIdent)
491
- return void 0;
492
- const { userId, accountId, token } = sdk.widgetIdent;
493
- if (!userId || !accountId || !token)
494
- return void 0;
495
484
  return {
496
485
  id: sdk.widgetIdent.userId,
497
486
  accountId: sdk.widgetIdent.accountId,
@@ -522,16 +511,13 @@ function _getInitialValue() {
522
511
  }
523
512
  function setUserIdentity(identity) {
524
513
  const globalProvider = lazilyStartUserContext();
525
- const sdk = getEnvironmentSDK();
526
- const widgetIdent = sdk.type === "SquatchJS2" ? sdk.widgetIdent : void 0;
527
- console.log({ sdk, widgetIdent });
528
514
  if (!equal(globalProvider.context, identity)) {
529
515
  debug6(`Setting user context value [${JSON.stringify(identity)}]`);
530
516
  globalProvider.context = identity;
531
517
  }
532
518
  if (identity && getEnvironmentSDK().type === "SquatchPortal") {
533
519
  localStorage.setItem(USER_CONTEXT_NAME, JSON.stringify(identity));
534
- } else if (!identity && getEnvironmentSDK().type === "SquatchPortal") {
520
+ } else if (!identity) {
535
521
  localStorage.removeItem(USER_CONTEXT_NAME);
536
522
  }
537
523
  }
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
- {
2
- "name": "@saasquatch/component-environment",
3
- "version": "1.0.2-3",
4
- "description": "Environment and contexts for SaaSquatch components",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "files": [
8
- "dist/"
9
- ],
10
- "scripts": {
11
- "build": "tsup src/index.ts --format esm,cjs --dts",
12
- "test": "jest"
13
- },
14
- "engines": {
15
- "node": ">=16.15"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "https://github.com/saasquatch/program-tools",
20
- "directory": "packages/component-environment"
21
- },
22
- "author": "Johan Venter <johan@saasquatch.com>",
23
- "license": "ISC",
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",
30
- "tsup": "^6.1.2",
31
- "typescript": "^4.7.3"
32
- },
33
- "dependencies": {
34
- "@wry/equality": "^0.5.2",
35
- "dom-context": "^1.2.0",
36
- "jwt-decode": "^3.1.2"
37
- }
38
- }
1
+ {
2
+ "name": "@saasquatch/component-environment",
3
+ "version": "1.0.2",
4
+ "description": "Environment and contexts for SaaSquatch components",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "files": [
8
+ "dist/"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsup src/index.ts --format esm,cjs --dts",
12
+ "test": "jest"
13
+ },
14
+ "engines": {
15
+ "node": ">=16.15"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/saasquatch/program-tools",
20
+ "directory": "packages/component-environment"
21
+ },
22
+ "author": "ReferralSaaSquatch.com, Inc.",
23
+ "license": "MIT",
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",
30
+ "tsup": "^6.1.2",
31
+ "typescript": "^4.7.3"
32
+ },
33
+ "dependencies": {
34
+ "@wry/equality": "^0.5.2",
35
+ "dom-context": "^1.2.0",
36
+ "jwt-decode": "^3.1.2"
37
+ }
38
+ }