@saasquatch/component-environment 1.0.2 → 1.0.3

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 CHANGED
@@ -1,21 +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.
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,6 +137,9 @@ declare type EnvironmentSDK = {
137
137
  type: "SquatchAndroid";
138
138
  android: SquatchAndroid;
139
139
  widgetIdent: WidgetIdent;
140
+ } | {
141
+ type: "SquatchIOS";
142
+ widgetIdent: WidgetIdent;
140
143
  } | {
141
144
  type: "SquatchPortal";
142
145
  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;
82
+ var _a, _b, _c;
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 (window["widgetIdent"] && ((_a = window["widgetIdent"]) == null ? void 0 : _a.env) !== "demo") {
102
+ if (((_a = window.frameElement) == null ? void 0 : _a["squatchJsApi"]) && ((_b = window["widgetIdent"]) == null ? void 0 : _b.env) !== "demo") {
103
103
  return {
104
104
  type: "SquatchJS2",
105
- api: (_b = window.frameElement) == null ? void 0 : _b["squatchJsApi"],
105
+ api: (_c = window.frameElement) == null ? void 0 : _c["squatchJsApi"],
106
106
  widgetIdent: window["widgetIdent"]
107
107
  };
108
108
  }
@@ -116,11 +116,13 @@ function isDemo() {
116
116
  }
117
117
  var FAKE_TENANT = "demo";
118
118
  function getTenantAlias() {
119
+ var _a;
119
120
  const sdk = getEnvironmentSDK();
120
121
  switch (sdk.type) {
121
122
  case "SquatchAndroid":
123
+ case "SquatchIOS":
122
124
  case "SquatchJS2":
123
- return sdk.widgetIdent.tenantAlias;
125
+ return (_a = sdk.widgetIdent) == null ? void 0 : _a.tenantAlias;
124
126
  case "SquatchAdmin":
125
127
  case "None":
126
128
  return FAKE_TENANT;
@@ -130,25 +132,28 @@ function getTenantAlias() {
130
132
  }
131
133
  var DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
132
134
  function getAppDomain() {
133
- var _a;
135
+ var _a, _b;
134
136
  const sdk = getEnvironmentSDK();
135
137
  switch (sdk.type) {
136
138
  case "SquatchAndroid":
139
+ case "SquatchIOS":
137
140
  case "SquatchJS2":
138
- return sdk.widgetIdent.appDomain;
141
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
139
142
  case "SquatchPortal":
140
- return ((_a = sdk.env) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
143
+ return ((_b = sdk.env) == null ? void 0 : _b.appDomain) || DEFAULT_DOMAIN;
141
144
  case "SquatchAdmin":
142
145
  case "None":
143
146
  return DEFAULT_DOMAIN;
144
147
  }
145
148
  }
146
149
  function getEngagementMedium() {
150
+ var _a;
147
151
  const sdk = getEnvironmentSDK();
148
152
  switch (sdk.type) {
149
153
  case "SquatchJS2":
150
- return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
154
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.engagementMedium) || DEFAULT_MEDIUM;
151
155
  case "SquatchAndroid":
156
+ case "SquatchIOS":
152
157
  case "SquatchPortal":
153
158
  case "SquatchAdmin":
154
159
  case "None":
@@ -511,9 +516,11 @@ function userIdentityFromJwt(jwt) {
511
516
  userId = decoded.user.id;
512
517
  }
513
518
  if (!userId || !accountId) {
519
+ debug6("No user information");
514
520
  return void 0;
515
521
  }
516
522
  if (exp && Date.now() >= exp * 1e3) {
523
+ debug6("JWT has expired");
517
524
  return void 0;
518
525
  }
519
526
  return {
@@ -522,14 +529,21 @@ function userIdentityFromJwt(jwt) {
522
529
  jwt
523
530
  };
524
531
  } catch (e) {
532
+ debug6("Invalid JWT");
525
533
  return void 0;
526
534
  }
527
535
  }
528
536
  function _getInitialValue() {
529
537
  const sdk = getEnvironmentSDK();
530
538
  switch (sdk.type) {
539
+ case "SquatchIOS":
531
540
  case "SquatchAndroid":
532
541
  case "SquatchJS2":
542
+ if (!sdk.widgetIdent)
543
+ return void 0;
544
+ const { userId, accountId, token } = sdk.widgetIdent;
545
+ if (!userId || !accountId || !token)
546
+ return void 0;
533
547
  return {
534
548
  id: sdk.widgetIdent.userId,
535
549
  accountId: sdk.widgetIdent.accountId,
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;
33
+ var _a, _b, _c;
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 (window["widgetIdent"] && ((_a = window["widgetIdent"]) == null ? void 0 : _a.env) !== "demo") {
53
+ if (((_a = window.frameElement) == null ? void 0 : _a["squatchJsApi"]) && ((_b = window["widgetIdent"]) == null ? void 0 : _b.env) !== "demo") {
54
54
  return {
55
55
  type: "SquatchJS2",
56
- api: (_b = window.frameElement) == null ? void 0 : _b["squatchJsApi"],
56
+ api: (_c = window.frameElement) == null ? void 0 : _c["squatchJsApi"],
57
57
  widgetIdent: window["widgetIdent"]
58
58
  };
59
59
  }
@@ -67,11 +67,13 @@ function isDemo() {
67
67
  }
68
68
  var FAKE_TENANT = "demo";
69
69
  function getTenantAlias() {
70
+ var _a;
70
71
  const sdk = getEnvironmentSDK();
71
72
  switch (sdk.type) {
72
73
  case "SquatchAndroid":
74
+ case "SquatchIOS":
73
75
  case "SquatchJS2":
74
- return sdk.widgetIdent.tenantAlias;
76
+ return (_a = sdk.widgetIdent) == null ? void 0 : _a.tenantAlias;
75
77
  case "SquatchAdmin":
76
78
  case "None":
77
79
  return FAKE_TENANT;
@@ -81,25 +83,28 @@ function getTenantAlias() {
81
83
  }
82
84
  var DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
83
85
  function getAppDomain() {
84
- var _a;
86
+ var _a, _b;
85
87
  const sdk = getEnvironmentSDK();
86
88
  switch (sdk.type) {
87
89
  case "SquatchAndroid":
90
+ case "SquatchIOS":
88
91
  case "SquatchJS2":
89
- return sdk.widgetIdent.appDomain;
92
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
90
93
  case "SquatchPortal":
91
- return ((_a = sdk.env) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
94
+ return ((_b = sdk.env) == null ? void 0 : _b.appDomain) || DEFAULT_DOMAIN;
92
95
  case "SquatchAdmin":
93
96
  case "None":
94
97
  return DEFAULT_DOMAIN;
95
98
  }
96
99
  }
97
100
  function getEngagementMedium() {
101
+ var _a;
98
102
  const sdk = getEnvironmentSDK();
99
103
  switch (sdk.type) {
100
104
  case "SquatchJS2":
101
- return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
105
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.engagementMedium) || DEFAULT_MEDIUM;
102
106
  case "SquatchAndroid":
107
+ case "SquatchIOS":
103
108
  case "SquatchPortal":
104
109
  case "SquatchAdmin":
105
110
  case "None":
@@ -462,9 +467,11 @@ function userIdentityFromJwt(jwt) {
462
467
  userId = decoded.user.id;
463
468
  }
464
469
  if (!userId || !accountId) {
470
+ debug6("No user information");
465
471
  return void 0;
466
472
  }
467
473
  if (exp && Date.now() >= exp * 1e3) {
474
+ debug6("JWT has expired");
468
475
  return void 0;
469
476
  }
470
477
  return {
@@ -473,14 +480,21 @@ function userIdentityFromJwt(jwt) {
473
480
  jwt
474
481
  };
475
482
  } catch (e) {
483
+ debug6("Invalid JWT");
476
484
  return void 0;
477
485
  }
478
486
  }
479
487
  function _getInitialValue() {
480
488
  const sdk = getEnvironmentSDK();
481
489
  switch (sdk.type) {
490
+ case "SquatchIOS":
482
491
  case "SquatchAndroid":
483
492
  case "SquatchJS2":
493
+ if (!sdk.widgetIdent)
494
+ return void 0;
495
+ const { userId, accountId, token } = sdk.widgetIdent;
496
+ if (!userId || !accountId || !token)
497
+ return void 0;
484
498
  return {
485
499
  id: sdk.widgetIdent.userId,
486
500
  accountId: sdk.widgetIdent.accountId,
package/package.json CHANGED
@@ -1,38 +1,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
- }
1
+ {
2
+ "name": "@saasquatch/component-environment",
3
+ "version": "1.0.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": "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
+ }