@saasquatch/component-environment 1.0.2 → 1.0.4-0

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",
@@ -87,6 +87,12 @@ function getEnvironmentSDK() {
87
87
  widgetIdent: window["widgetIdent"]
88
88
  };
89
89
  }
90
+ if (window["SquatchIOS"]) {
91
+ return {
92
+ type: "SquatchIOS",
93
+ widgetIdent: window["widgetIdent"]
94
+ };
95
+ }
90
96
  if (window["SquatchPortal"]) {
91
97
  return {
92
98
  type: "SquatchPortal",
@@ -99,10 +105,10 @@ function getEnvironmentSDK() {
99
105
  adminSDK: window["SquatchAdmin"]
100
106
  };
101
107
  }
102
- if (window["widgetIdent"] && ((_a = window["widgetIdent"]) == null ? void 0 : _a.env) !== "demo") {
108
+ if (((_a = window.frameElement) == null ? void 0 : _a["squatchJsApi"]) && ((_b = window["widgetIdent"]) == null ? void 0 : _b.env) !== "demo") {
103
109
  return {
104
110
  type: "SquatchJS2",
105
- api: (_b = window.frameElement) == null ? void 0 : _b["squatchJsApi"],
111
+ api: (_c = window.frameElement) == null ? void 0 : _c["squatchJsApi"],
106
112
  widgetIdent: window["widgetIdent"]
107
113
  };
108
114
  }
@@ -116,11 +122,13 @@ function isDemo() {
116
122
  }
117
123
  var FAKE_TENANT = "demo";
118
124
  function getTenantAlias() {
125
+ var _a;
119
126
  const sdk = getEnvironmentSDK();
120
127
  switch (sdk.type) {
121
128
  case "SquatchAndroid":
129
+ case "SquatchIOS":
122
130
  case "SquatchJS2":
123
- return sdk.widgetIdent.tenantAlias;
131
+ return (_a = sdk.widgetIdent) == null ? void 0 : _a.tenantAlias;
124
132
  case "SquatchAdmin":
125
133
  case "None":
126
134
  return FAKE_TENANT;
@@ -130,25 +138,28 @@ function getTenantAlias() {
130
138
  }
131
139
  var DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
132
140
  function getAppDomain() {
133
- var _a;
141
+ var _a, _b;
134
142
  const sdk = getEnvironmentSDK();
135
143
  switch (sdk.type) {
136
144
  case "SquatchAndroid":
145
+ case "SquatchIOS":
137
146
  case "SquatchJS2":
138
- return sdk.widgetIdent.appDomain;
147
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
139
148
  case "SquatchPortal":
140
- return ((_a = sdk.env) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
149
+ return ((_b = sdk.env) == null ? void 0 : _b.appDomain) || DEFAULT_DOMAIN;
141
150
  case "SquatchAdmin":
142
151
  case "None":
143
152
  return DEFAULT_DOMAIN;
144
153
  }
145
154
  }
146
155
  function getEngagementMedium() {
156
+ var _a;
147
157
  const sdk = getEnvironmentSDK();
148
158
  switch (sdk.type) {
149
159
  case "SquatchJS2":
150
- return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
160
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.engagementMedium) || DEFAULT_MEDIUM;
151
161
  case "SquatchAndroid":
162
+ case "SquatchIOS":
152
163
  case "SquatchPortal":
153
164
  case "SquatchAdmin":
154
165
  case "None":
@@ -511,9 +522,11 @@ function userIdentityFromJwt(jwt) {
511
522
  userId = decoded.user.id;
512
523
  }
513
524
  if (!userId || !accountId) {
525
+ debug6("No user information");
514
526
  return void 0;
515
527
  }
516
528
  if (exp && Date.now() >= exp * 1e3) {
529
+ debug6("JWT has expired");
517
530
  return void 0;
518
531
  }
519
532
  return {
@@ -522,14 +535,21 @@ function userIdentityFromJwt(jwt) {
522
535
  jwt
523
536
  };
524
537
  } catch (e) {
538
+ debug6("Invalid JWT");
525
539
  return void 0;
526
540
  }
527
541
  }
528
542
  function _getInitialValue() {
529
543
  const sdk = getEnvironmentSDK();
530
544
  switch (sdk.type) {
545
+ case "SquatchIOS":
531
546
  case "SquatchAndroid":
532
547
  case "SquatchJS2":
548
+ if (!sdk.widgetIdent)
549
+ return void 0;
550
+ const { userId, accountId, token } = sdk.widgetIdent;
551
+ if (!userId || !accountId || !token)
552
+ return void 0;
533
553
  return {
534
554
  id: sdk.widgetIdent.userId,
535
555
  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",
@@ -38,6 +38,12 @@ function getEnvironmentSDK() {
38
38
  widgetIdent: window["widgetIdent"]
39
39
  };
40
40
  }
41
+ if (window["SquatchIOS"]) {
42
+ return {
43
+ type: "SquatchIOS",
44
+ widgetIdent: window["widgetIdent"]
45
+ };
46
+ }
41
47
  if (window["SquatchPortal"]) {
42
48
  return {
43
49
  type: "SquatchPortal",
@@ -50,10 +56,10 @@ function getEnvironmentSDK() {
50
56
  adminSDK: window["SquatchAdmin"]
51
57
  };
52
58
  }
53
- if (window["widgetIdent"] && ((_a = window["widgetIdent"]) == null ? void 0 : _a.env) !== "demo") {
59
+ if (((_a = window.frameElement) == null ? void 0 : _a["squatchJsApi"]) && ((_b = window["widgetIdent"]) == null ? void 0 : _b.env) !== "demo") {
54
60
  return {
55
61
  type: "SquatchJS2",
56
- api: (_b = window.frameElement) == null ? void 0 : _b["squatchJsApi"],
62
+ api: (_c = window.frameElement) == null ? void 0 : _c["squatchJsApi"],
57
63
  widgetIdent: window["widgetIdent"]
58
64
  };
59
65
  }
@@ -67,11 +73,13 @@ function isDemo() {
67
73
  }
68
74
  var FAKE_TENANT = "demo";
69
75
  function getTenantAlias() {
76
+ var _a;
70
77
  const sdk = getEnvironmentSDK();
71
78
  switch (sdk.type) {
72
79
  case "SquatchAndroid":
80
+ case "SquatchIOS":
73
81
  case "SquatchJS2":
74
- return sdk.widgetIdent.tenantAlias;
82
+ return (_a = sdk.widgetIdent) == null ? void 0 : _a.tenantAlias;
75
83
  case "SquatchAdmin":
76
84
  case "None":
77
85
  return FAKE_TENANT;
@@ -81,25 +89,28 @@ function getTenantAlias() {
81
89
  }
82
90
  var DEFAULT_DOMAIN = "https://app.referralsaasquatch.com";
83
91
  function getAppDomain() {
84
- var _a;
92
+ var _a, _b;
85
93
  const sdk = getEnvironmentSDK();
86
94
  switch (sdk.type) {
87
95
  case "SquatchAndroid":
96
+ case "SquatchIOS":
88
97
  case "SquatchJS2":
89
- return sdk.widgetIdent.appDomain;
98
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
90
99
  case "SquatchPortal":
91
- return ((_a = sdk.env) == null ? void 0 : _a.appDomain) || DEFAULT_DOMAIN;
100
+ return ((_b = sdk.env) == null ? void 0 : _b.appDomain) || DEFAULT_DOMAIN;
92
101
  case "SquatchAdmin":
93
102
  case "None":
94
103
  return DEFAULT_DOMAIN;
95
104
  }
96
105
  }
97
106
  function getEngagementMedium() {
107
+ var _a;
98
108
  const sdk = getEnvironmentSDK();
99
109
  switch (sdk.type) {
100
110
  case "SquatchJS2":
101
- return sdk.widgetIdent.engagementMedium || DEFAULT_MEDIUM;
111
+ return ((_a = sdk.widgetIdent) == null ? void 0 : _a.engagementMedium) || DEFAULT_MEDIUM;
102
112
  case "SquatchAndroid":
113
+ case "SquatchIOS":
103
114
  case "SquatchPortal":
104
115
  case "SquatchAdmin":
105
116
  case "None":
@@ -462,9 +473,11 @@ function userIdentityFromJwt(jwt) {
462
473
  userId = decoded.user.id;
463
474
  }
464
475
  if (!userId || !accountId) {
476
+ debug6("No user information");
465
477
  return void 0;
466
478
  }
467
479
  if (exp && Date.now() >= exp * 1e3) {
480
+ debug6("JWT has expired");
468
481
  return void 0;
469
482
  }
470
483
  return {
@@ -473,14 +486,21 @@ function userIdentityFromJwt(jwt) {
473
486
  jwt
474
487
  };
475
488
  } catch (e) {
489
+ debug6("Invalid JWT");
476
490
  return void 0;
477
491
  }
478
492
  }
479
493
  function _getInitialValue() {
480
494
  const sdk = getEnvironmentSDK();
481
495
  switch (sdk.type) {
496
+ case "SquatchIOS":
482
497
  case "SquatchAndroid":
483
498
  case "SquatchJS2":
499
+ if (!sdk.widgetIdent)
500
+ return void 0;
501
+ const { userId, accountId, token } = sdk.widgetIdent;
502
+ if (!userId || !accountId || !token)
503
+ return void 0;
484
504
  return {
485
505
  id: sdk.widgetIdent.userId,
486
506
  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.4-0",
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
+ }