@saasquatch/component-environment 1.0.0-0 → 1.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2022-06-16
9
+
10
+ Initial version.
11
+
12
+ [unreleased]: https://github.com/saasquatch/program-tools/compare/component-environment@1.0.0...HEAD
13
+ [1.0.0]: https://github.com/saasquatch/program-tools/releases/tag/%40saasquatch%2Fcomponent-environment%401.0.0
package/README.md ADDED
@@ -0,0 +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)`.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,226 @@
1
- export * from "./types";
2
- export * from "./environment";
3
- export * from "./contexts/UserIdentityContext";
4
- export * from "./contexts/LocaleContext";
5
- export * from "./contexts/ProgramContext";
1
+ import { ContextProvider } from 'dom-context';
2
+
3
+ declare global {
4
+ interface Window {
5
+ SquatchPortal?: PortalEnv;
6
+ widgetIdent?: WidgetIdent;
7
+ squatchUserIdentity?: ContextProvider<UserIdentity | undefined>;
8
+ squatchLocale?: ContextProvider<string | undefined>;
9
+ squatchProgramId?: ContextProvider<string | undefined>;
10
+ }
11
+ }
12
+ declare type UserContextName = "sq:user-identity";
13
+ declare type LocaleContextName = "sq:locale";
14
+ declare type ProgramContextName = "sq:program-id";
15
+ declare const USER_CONTEXT_NAME: UserContextName;
16
+ declare const LOCALE_CONTEXT_NAME: LocaleContextName;
17
+ declare const PROGRAM_CONTEXT_NAME: ProgramContextName;
18
+ /**
19
+ * The value stored in the UserContext
20
+ */
21
+ declare type UserIdentity = {
22
+ id: string;
23
+ accountId: string;
24
+ jwt?: string;
25
+ managedIdentity?: {
26
+ email: string;
27
+ emailVerified: boolean;
28
+ sessionData?: {
29
+ [key: string]: any;
30
+ };
31
+ };
32
+ };
33
+ declare type UserId = {
34
+ id: string;
35
+ accountId: string;
36
+ };
37
+ interface DecodedSquatchJWT {
38
+ exp?: number;
39
+ user: {
40
+ accountId: string;
41
+ id: string;
42
+ };
43
+ }
44
+ interface DecodedWidgetAPIJWT {
45
+ exp?: number;
46
+ sub: string;
47
+ }
48
+ declare type EngagementMedium = "EMBED" | "POPUP";
49
+ declare const DEFAULT_MEDIUM: EngagementMedium;
50
+ /**
51
+ * Provided by the SaaSquatch GraphQL backend when a widget is rendered.
52
+ *
53
+ * Source: https://github.com/saasquatch/saasquatch/blob/805e51284f818f8656b6458bcee6181f378819d3/packages/saasquatch-core/app/saasquatch/controllers/api/widget/WidgetApi.java
54
+ *
55
+ */
56
+ interface WidgetIdent {
57
+ tenantAlias: string;
58
+ appDomain: string;
59
+ token: string;
60
+ userId: string;
61
+ accountId: string;
62
+ locale?: string;
63
+ engagementMedium?: "POPUP" | "EMBED";
64
+ programId?: string;
65
+ env?: string;
66
+ }
67
+ /**
68
+ * Portal env doesn't include User Id
69
+ */
70
+ declare type PortalEnv = Pick<WidgetIdent, "tenantAlias" | "appDomain" | "programId">;
71
+ /**
72
+ * An interface for interacting with the SaaSquatch Admin Portal.
73
+ *
74
+ * Used for rendering widgets in a preview/demo mode.
75
+ */
76
+ interface SquatchAdmin {
77
+ /**
78
+ * Provides a way of providing user feedback when a widget is rendered in the SaaSquatch admin portal
79
+ *
80
+ * @param text
81
+ */
82
+ showMessage(text: string): void;
83
+ }
84
+ /**
85
+ * Type for the Javascript environment added by https://github.com/saasquatch/squatch-android
86
+ *
87
+ * Should exist as `window.SquatchAndroid`
88
+ */
89
+ interface SquatchAndroid {
90
+ /**
91
+ *
92
+ * @param shareLink
93
+ * @param messageLink fallback URL to redirect to if the app is not installed
94
+ */
95
+ shareOnFacebook(shareLink: string, messageLink: string): void;
96
+ /**
97
+ * Shows a native Android toast
98
+ *
99
+ * @param text
100
+ */
101
+ showToast(text: string): void;
102
+ }
103
+ /**
104
+ * An interface provided by Squatch.js V2 for widgets.
105
+ *
106
+ * See: https://github.com/saasquatch/squatch-js/blob/8f2b218c9d55567e0cc12d27d635a5fb545e6842/src/widgets/Widget.ts#L47
107
+ *
108
+ */
109
+ interface SquatchJS2 {
110
+ /**
111
+ * Opens the current popup widget (if loaded as a popup)
112
+ */
113
+ open?: () => void;
114
+ /**
115
+ * Closes the current popup widget (if loaded as a popup)
116
+ */
117
+ close?: () => void;
118
+ /**
119
+ * DEPRECATED used to update user details from inside the widget.
120
+ *
121
+ * Should no longer be used. Replace with natively using the GraphQL API and re-rendering locally. Will be removed in a future version of Squatch.js
122
+ *
123
+ * @deprecated
124
+ */
125
+ reload(userDetails: {
126
+ email: string;
127
+ firstName: string;
128
+ lastName: string;
129
+ }, jwt: string): void;
130
+ }
131
+ declare type Environment = EnvironmentSDK["type"];
132
+ declare type EnvironmentSDK = {
133
+ type: "SquatchJS2";
134
+ api: SquatchJS2;
135
+ widgetIdent: WidgetIdent;
136
+ } | {
137
+ type: "SquatchAndroid";
138
+ android: SquatchAndroid;
139
+ widgetIdent: WidgetIdent;
140
+ } | {
141
+ type: "SquatchPortal";
142
+ env: PortalEnv;
143
+ } | {
144
+ type: "SquatchAdmin";
145
+ adminSDK: SquatchAdmin;
146
+ } | {
147
+ type: "None";
148
+ };
149
+
150
+ /**
151
+ * Get the type of environment that this widget is being rendered in
152
+ *
153
+ * Should never return null.
154
+ */
155
+ declare function getEnvironment(): Environment;
156
+ /**
157
+ * Get the SDK for interacting with the host environment
158
+ */
159
+ declare function getEnvironmentSDK(): EnvironmentSDK;
160
+ declare function isDemo(): boolean;
161
+ declare function getTenantAlias(): string;
162
+ declare function getAppDomain(): string;
163
+ declare function getEngagementMedium(): EngagementMedium;
164
+
165
+ /**
166
+ * Lazily start the user context provider. If it already exists, the existing provider is
167
+ * returned. This function is safe to call multiple times.
168
+ *
169
+ * @returns The global user context provider
170
+ */
171
+ declare function lazilyStartUserContext(): ContextProvider<UserIdentity | undefined>;
172
+ /**
173
+ * Extract a user identity from a JWT
174
+ *
175
+ * @param jwt The JWT to extract a user identity from
176
+ * @returns The user identity or undefined if the JWT is not valid
177
+ */
178
+ declare function userIdentityFromJwt(jwt?: string): UserIdentity | undefined;
179
+ /**
180
+ * Overide the globally defined user context, and persists the user identity in local storage
181
+ *
182
+ * @param identity the new identity of the user, or undefined if logged out
183
+ */
184
+ declare function setUserIdentity(identity?: UserIdentity): void;
185
+ /**
186
+ * Get the current value of the user context
187
+ */
188
+ declare function getUserIdentity(): UserIdentity | undefined;
189
+
190
+ /**
191
+ * Lazily start the locale context provider. If it already exists, the existing provider is
192
+ * returned. This function is safe to call multiple times.
193
+ *
194
+ * @returns The global locale context provider
195
+ */
196
+ declare function lazilyStartLocaleContext(): ContextProvider<string | undefined>;
197
+ /**
198
+ * Overide the globally defined Locale context
199
+ *
200
+ * @param locale the new locale used by the user
201
+ */
202
+ declare function setLocale(locale?: string): void;
203
+ /**
204
+ * Get the current value of the locale context
205
+ */
206
+ declare function getLocale(): string | undefined;
207
+
208
+ /**
209
+ * Lazily start the program context provider. If it already exists, the existing provider is
210
+ * returned. This function is safe to call multiple times.
211
+ *
212
+ * @returns The global program context provider
213
+ */
214
+ declare function lazilyStartProgramContext(): ContextProvider<string | undefined>;
215
+ /**
216
+ * Overide the globally defined Program ID context
217
+ *
218
+ * @param programId the new programID used by the user, or undefined if logged out
219
+ */
220
+ declare function setProgramId(programId: string | undefined): void;
221
+ /**
222
+ * Get the current value of the program context
223
+ */
224
+ declare function getProgramId(): string | undefined;
225
+
226
+ export { DEFAULT_MEDIUM, DecodedSquatchJWT, DecodedWidgetAPIJWT, EngagementMedium, Environment, EnvironmentSDK, LOCALE_CONTEXT_NAME, LocaleContextName, PROGRAM_CONTEXT_NAME, PortalEnv, ProgramContextName, SquatchAdmin, SquatchAndroid, SquatchJS2, USER_CONTEXT_NAME, UserContextName, UserId, UserIdentity, WidgetIdent, getAppDomain, getEngagementMedium, getEnvironment, getEnvironmentSDK, getLocale, getProgramId, getTenantAlias, getUserIdentity, isDemo, lazilyStartLocaleContext, lazilyStartProgramContext, lazilyStartUserContext, setLocale, setProgramId, setUserIdentity, userIdentityFromJwt };