@saasquatch/component-environment 1.0.0-4 → 1.0.0-5

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 CHANGED
@@ -162,6 +162,72 @@ declare function getTenantAlias(): string;
162
162
  declare function getAppDomain(): string;
163
163
  declare function getEngagementMedium(): EngagementMedium;
164
164
 
165
+ interface PartialPath {
166
+ pathname: string;
167
+ search?: string;
168
+ hash?: string;
169
+ }
170
+ interface HistoryEntry extends PartialPath {
171
+ state?: object;
172
+ }
173
+ declare type To = string | PartialPath;
174
+ interface ListenableHistory {
175
+ listen(fn: () => void): () => void;
176
+ push(to: To, state?: object): void;
177
+ replace(to: To, state?: object): void;
178
+ back(): void;
179
+ forward(): void;
180
+ go(delta?: number): void;
181
+ location: HistoryEntry;
182
+ }
183
+ declare global {
184
+ interface Window {
185
+ squatchHistory: ListenableHistory;
186
+ }
187
+ }
188
+ declare class BrowserHistory implements ListenableHistory {
189
+ private listeners;
190
+ constructor();
191
+ listen(fn: () => void): () => void;
192
+ private notify;
193
+ get location(): {
194
+ pathname: string;
195
+ search: string;
196
+ hash: string;
197
+ state: any;
198
+ };
199
+ back(): void;
200
+ forward(): void;
201
+ go(delta: number): void;
202
+ push(to: To, state?: object): void;
203
+ replace(to: To, state?: object): void;
204
+ }
205
+ declare class MemoryHistory implements ListenableHistory {
206
+ private history;
207
+ private index;
208
+ private listeners;
209
+ listen(fn: () => void): () => void;
210
+ private current;
211
+ get state(): object | undefined;
212
+ notify(): void;
213
+ get location(): HistoryEntry;
214
+ back(): void;
215
+ forward(): void;
216
+ go(delta: number): void;
217
+ push(to: To, state?: object): void;
218
+ replace(to: To, state?: object): void;
219
+ }
220
+ declare class LazyHistory implements ListenableHistory {
221
+ get location(): HistoryEntry;
222
+ listen(fn: () => void): () => void;
223
+ back(): void;
224
+ forward(): void;
225
+ go(delta: number): void;
226
+ push(to: To, state?: object): void;
227
+ replace(to: To, state?: object): void;
228
+ }
229
+ declare const _default: LazyHistory;
230
+
165
231
  /**
166
232
  * Lazily start the user context provider. If it already exists, the existing provider is
167
233
  * returned. This function is safe to call multiple times.
@@ -223,4 +289,4 @@ declare function setProgramId(programId: string | undefined): void;
223
289
  */
224
290
  declare function getProgramId(): string | undefined;
225
291
 
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 };
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 };
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
7
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
8
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
9
  var __getProtoOf = Object.getPrototypeOf;
@@ -19,6 +21,7 @@ var __spreadValues = (a, b) => {
19
21
  }
20
22
  return a;
21
23
  };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
25
  var __export = (target, all) => {
23
26
  for (var name in all)
24
27
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -37,8 +40,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
37
40
  // src/index.ts
38
41
  var src_exports = {};
39
42
  __export(src_exports, {
43
+ BrowserHistory: () => BrowserHistory,
40
44
  DEFAULT_MEDIUM: () => DEFAULT_MEDIUM,
41
45
  LOCALE_CONTEXT_NAME: () => LOCALE_CONTEXT_NAME,
46
+ LazyHistory: () => LazyHistory,
47
+ MemoryHistory: () => MemoryHistory,
42
48
  PROGRAM_CONTEXT_NAME: () => PROGRAM_CONTEXT_NAME,
43
49
  USER_CONTEXT_NAME: () => USER_CONTEXT_NAME,
44
50
  getAppDomain: () => getAppDomain,
@@ -49,6 +55,7 @@ __export(src_exports, {
49
55
  getProgramId: () => getProgramId,
50
56
  getTenantAlias: () => getTenantAlias,
51
57
  getUserIdentity: () => getUserIdentity,
58
+ history: () => history_default,
52
59
  isDemo: () => isDemo,
53
60
  lazilyStartLocaleContext: () => lazilyStartLocaleContext,
54
61
  lazilyStartProgramContext: () => lazilyStartProgramContext,
@@ -148,6 +155,199 @@ function getEngagementMedium() {
148
155
  }
149
156
  }
150
157
 
158
+ // src/debug.ts
159
+ var debugEnabled = (() => {
160
+ var _a;
161
+ try {
162
+ return (_a = window == null ? void 0 : window.localStorage) == null ? void 0 : _a.getItem("debug");
163
+ } catch (e) {
164
+ return false;
165
+ }
166
+ })();
167
+ function debug(ns, ...args) {
168
+ if (debugEnabled) {
169
+ console.debug(`sq:environment:${ns}`, ...args);
170
+ }
171
+ }
172
+
173
+ // src/history.ts
174
+ var debug2 = (...args) => debug("history", ...args);
175
+ function normalizeTo(to) {
176
+ if (typeof to === "string")
177
+ return to;
178
+ const url = new URL(to.pathname, window.origin);
179
+ if (to.search)
180
+ url.search = to.search;
181
+ if (to.hash)
182
+ url.hash = to.hash;
183
+ return url;
184
+ }
185
+ var BrowserHistory = class {
186
+ constructor() {
187
+ this.listeners = /* @__PURE__ */ new Set();
188
+ window.addEventListener("popstate", this.notify.bind(this));
189
+ window.addEventListener("pushState", this.notify.bind(this));
190
+ window.addEventListener("replaceState", this.notify.bind(this));
191
+ }
192
+ listen(fn) {
193
+ this.listeners.add(fn);
194
+ return () => {
195
+ this.listeners.delete(fn);
196
+ };
197
+ }
198
+ notify() {
199
+ debug2("notifying listeners of route change", this.location);
200
+ if (this.listeners) {
201
+ this.listeners.forEach((listener) => listener());
202
+ }
203
+ }
204
+ get location() {
205
+ return {
206
+ pathname: window.location.pathname,
207
+ search: window.location.search,
208
+ hash: window.location.hash,
209
+ state: window.history.state
210
+ };
211
+ }
212
+ back() {
213
+ this.go(-1);
214
+ }
215
+ forward() {
216
+ this.go(1);
217
+ }
218
+ go(delta) {
219
+ window.history.go(delta);
220
+ }
221
+ push(to, state) {
222
+ const url = normalizeTo(to);
223
+ window.history.pushState(state, "", url);
224
+ }
225
+ replace(to, state) {
226
+ const url = normalizeTo(to);
227
+ window.history.replaceState(state, "", url);
228
+ }
229
+ };
230
+ var MemoryHistory = class {
231
+ constructor() {
232
+ this.history = [{ pathname: "/", search: "", hash: "" }];
233
+ this.index = 0;
234
+ this.listeners = /* @__PURE__ */ new Set();
235
+ }
236
+ listen(fn) {
237
+ this.listeners.add(fn);
238
+ return () => {
239
+ this.listeners.delete(fn);
240
+ };
241
+ }
242
+ current() {
243
+ return this.history[this.index];
244
+ }
245
+ get state() {
246
+ return this.current().state;
247
+ }
248
+ notify() {
249
+ debug2("notifying listeners of route change", this.location);
250
+ this.listeners.forEach((listener) => listener());
251
+ }
252
+ get location() {
253
+ return this.history[this.index];
254
+ }
255
+ back() {
256
+ this.go(-1);
257
+ }
258
+ forward() {
259
+ this.go(1);
260
+ }
261
+ go(delta) {
262
+ this.index += delta;
263
+ if (this.index < 0)
264
+ this.index = 0;
265
+ if (this.index >= this.history.length)
266
+ this.index = this.history.length - 1;
267
+ this.notify();
268
+ }
269
+ push(to, state) {
270
+ let entry;
271
+ if (typeof to === "string") {
272
+ entry = {
273
+ pathname: to,
274
+ search: this.history[this.index].search,
275
+ hash: this.history[this.index].hash,
276
+ state
277
+ };
278
+ } else {
279
+ entry = __spreadProps(__spreadValues({}, to), {
280
+ state
281
+ });
282
+ }
283
+ this.index++;
284
+ this.history.splice(this.index, this.history.length, entry);
285
+ this.notify();
286
+ }
287
+ replace(to, state) {
288
+ if (typeof to === "string") {
289
+ this.history[this.index] = {
290
+ pathname: to,
291
+ search: this.history[this.index].search,
292
+ hash: this.history[this.index].hash,
293
+ state
294
+ };
295
+ } else {
296
+ this.history[this.index] = __spreadProps(__spreadValues({}, to), {
297
+ state
298
+ });
299
+ }
300
+ this.notify();
301
+ }
302
+ };
303
+ function createHistory() {
304
+ if (getEnvironment() === "SquatchPortal") {
305
+ let wrapHistoryFunction2 = function(functionName) {
306
+ const orig = window.history[functionName];
307
+ return function(...args) {
308
+ const returnValue = orig.apply(window.history, args);
309
+ const event = new Event(functionName);
310
+ window.dispatchEvent(event);
311
+ return returnValue;
312
+ };
313
+ };
314
+ var wrapHistoryFunction = wrapHistoryFunction2;
315
+ window.history.pushState = wrapHistoryFunction2("pushState");
316
+ window.history.replaceState = wrapHistoryFunction2("replaceState");
317
+ return new BrowserHistory();
318
+ } else {
319
+ return new MemoryHistory();
320
+ }
321
+ }
322
+ function lazyHistory() {
323
+ window.squatchHistory = window.squatchHistory || createHistory();
324
+ return window.squatchHistory;
325
+ }
326
+ var LazyHistory = class {
327
+ get location() {
328
+ return lazyHistory().location;
329
+ }
330
+ listen(fn) {
331
+ return lazyHistory().listen(fn);
332
+ }
333
+ back() {
334
+ lazyHistory().back();
335
+ }
336
+ forward() {
337
+ lazyHistory().forward();
338
+ }
339
+ go(delta) {
340
+ lazyHistory().go(delta);
341
+ }
342
+ push(to, state) {
343
+ lazyHistory().push(to, state);
344
+ }
345
+ replace(to, state) {
346
+ lazyHistory().replace(to, state);
347
+ }
348
+ };
349
+ var history_default = new LazyHistory();
350
+
151
351
  // src/contexts/UserIdentityContext.ts
152
352
  var import_jwt_decode = __toESM(require("jwt-decode"));
153
353
  var import_dom_context3 = require("dom-context");
@@ -158,22 +358,12 @@ var import_dom_context2 = require("dom-context");
158
358
 
159
359
  // src/contexts/LocaleContext.ts
160
360
  var import_dom_context = require("dom-context");
161
-
162
- // src/debug.ts
163
- var debugEnabled = localStorage.getItem("debug");
164
- function debug(ns, ...args) {
165
- if (debugEnabled) {
166
- console.debug(`sq:environment:${ns}`, ...args);
167
- }
168
- }
169
-
170
- // src/contexts/LocaleContext.ts
171
- var debug2 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
361
+ var debug3 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
172
362
  function lazilyStartLocaleContext() {
173
363
  var _a;
174
364
  let globalProvider = window.squatchLocale;
175
365
  if (!globalProvider) {
176
- debug2("Creating locale context provider");
366
+ debug3("Creating locale context provider");
177
367
  globalProvider = new import_dom_context.ContextProvider({
178
368
  element: document.documentElement,
179
369
  initialState: ((_a = window.widgetIdent) == null ? void 0 : _a.locale) || navigator.language.replace("-", "_"),
@@ -186,7 +376,7 @@ function lazilyStartLocaleContext() {
186
376
  function setLocale(locale) {
187
377
  const globalProvider = lazilyStartLocaleContext();
188
378
  if (globalProvider.context !== locale) {
189
- debug2(`Setting locale context value [${locale}]`);
379
+ debug3(`Setting locale context value [${locale}]`);
190
380
  globalProvider.context = locale;
191
381
  }
192
382
  }
@@ -196,7 +386,7 @@ function getLocale() {
196
386
  }
197
387
 
198
388
  // src/fetchLocale.ts
199
- var debug3 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
389
+ var debug4 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
200
390
  var GET_LOCALE = `
201
391
  query {
202
392
  viewer {
@@ -208,7 +398,7 @@ var GET_LOCALE = `
208
398
  `;
209
399
  async function fetchLocale() {
210
400
  var _a;
211
- debug3("Fetching locale from GraphQL for current user");
401
+ debug4("Fetching locale from GraphQL for current user");
212
402
  try {
213
403
  const result = await fetch(`${getAppDomain()}/api/v1/${getTenantAlias()}/graphql`, {
214
404
  method: "POST",
@@ -229,13 +419,13 @@ async function fetchLocale() {
229
419
  }
230
420
  return json.data.viewer.locale || void 0;
231
421
  } catch (e) {
232
- debug3(`Failed to fetch locale for current user`, e.message);
422
+ debug4(`Failed to fetch locale for current user`, e.message);
233
423
  return void 0;
234
424
  }
235
425
  }
236
426
 
237
427
  // src/listeners.ts
238
- var debug4 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
428
+ var debug5 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
239
429
  var userContextListenerDiv = (() => {
240
430
  const id = "__environment_context_listener";
241
431
  let div = document.getElementById(id);
@@ -255,30 +445,30 @@ var userContextListenerForLocale = new import_dom_context2.ContextListener({
255
445
  const defaultLocale = ((_a = window.widgetIdent) == null ? void 0 : _a.locale) || navigator.language.replace("-", "_");
256
446
  let newLocale;
257
447
  if (next) {
258
- debug4("User context changed, refetching locale");
448
+ debug5("User context changed, refetching locale");
259
449
  const locale = await fetchLocale();
260
450
  if (localeProvider.context !== locale) {
261
- debug4(`New value fetched from GraphQL [${locale}]`);
451
+ debug5(`New value fetched from GraphQL [${locale}]`);
262
452
  newLocale = locale || defaultLocale;
263
453
  }
264
454
  } else {
265
455
  newLocale = defaultLocale;
266
456
  }
267
- debug4(`Setting locale context to [${newLocale}]`);
457
+ debug5(`Setting locale context to [${newLocale}]`);
268
458
  localeProvider.context = newLocale;
269
459
  }
270
460
  });
271
461
  function startUserContextListenerForLocale() {
272
- debug4("Starting user context listener for locale updates");
462
+ debug5("Starting user context listener for locale updates");
273
463
  userContextListenerForLocale.start();
274
464
  }
275
465
 
276
466
  // src/contexts/UserIdentityContext.ts
277
- var debug5 = (...args) => debug(USER_CONTEXT_NAME, ...args);
467
+ var debug6 = (...args) => debug(USER_CONTEXT_NAME, ...args);
278
468
  function lazilyStartUserContext() {
279
469
  let globalProvider = window.squatchUserIdentity;
280
470
  if (!globalProvider) {
281
- debug5("Creating user context provider");
471
+ debug6("Creating user context provider");
282
472
  globalProvider = new import_dom_context3.ContextProvider({
283
473
  element: document.documentElement,
284
474
  initialState: _getInitialValue(),
@@ -364,7 +554,7 @@ function _getInitialValue() {
364
554
  function setUserIdentity(identity) {
365
555
  const globalProvider = lazilyStartUserContext();
366
556
  if (!(0, import_equality.equal)(globalProvider.context, identity)) {
367
- debug5(`Setting user context value [${JSON.stringify(identity)}]`);
557
+ debug6(`Setting user context value [${JSON.stringify(identity)}]`);
368
558
  globalProvider.context = identity;
369
559
  }
370
560
  if (identity && getEnvironmentSDK().type === "SquatchPortal") {
@@ -380,12 +570,12 @@ function getUserIdentity() {
380
570
 
381
571
  // src/contexts/ProgramContext.ts
382
572
  var import_dom_context4 = require("dom-context");
383
- var debug6 = (...args) => debug(PROGRAM_CONTEXT_NAME, ...args);
573
+ var debug7 = (...args) => debug(PROGRAM_CONTEXT_NAME, ...args);
384
574
  function lazilyStartProgramContext() {
385
575
  var _a;
386
576
  let globalProvider = window.squatchProgramId;
387
577
  if (!globalProvider) {
388
- debug6("Creating program context provider");
578
+ debug7("Creating program context provider");
389
579
  globalProvider = new import_dom_context4.ContextProvider({
390
580
  element: document.documentElement,
391
581
  initialState: ((_a = window.widgetIdent) == null ? void 0 : _a.programId) || void 0,
@@ -398,7 +588,7 @@ function lazilyStartProgramContext() {
398
588
  function setProgramId(programId) {
399
589
  const globalProvider = lazilyStartProgramContext();
400
590
  if (globalProvider.context !== programId) {
401
- debug6(`Setting program context value [${programId}]`);
591
+ debug7(`Setting program context value [${programId}]`);
402
592
  globalProvider.context = programId;
403
593
  }
404
594
  }
@@ -408,8 +598,11 @@ function getProgramId() {
408
598
  }
409
599
  // Annotate the CommonJS export names for ESM import in node:
410
600
  0 && (module.exports = {
601
+ BrowserHistory,
411
602
  DEFAULT_MEDIUM,
412
603
  LOCALE_CONTEXT_NAME,
604
+ LazyHistory,
605
+ MemoryHistory,
413
606
  PROGRAM_CONTEXT_NAME,
414
607
  USER_CONTEXT_NAME,
415
608
  getAppDomain,
@@ -420,6 +613,7 @@ function getProgramId() {
420
613
  getProgramId,
421
614
  getTenantAlias,
422
615
  getUserIdentity,
616
+ history,
423
617
  isDemo,
424
618
  lazilyStartLocaleContext,
425
619
  lazilyStartProgramContext,
package/dist/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
3
5
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -15,6 +17,7 @@ var __spreadValues = (a, b) => {
15
17
  }
16
18
  return a;
17
19
  };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
18
21
 
19
22
  // src/types.ts
20
23
  var USER_CONTEXT_NAME = "sq:user-identity";
@@ -104,6 +107,199 @@ function getEngagementMedium() {
104
107
  }
105
108
  }
106
109
 
110
+ // src/debug.ts
111
+ var debugEnabled = (() => {
112
+ var _a;
113
+ try {
114
+ return (_a = window == null ? void 0 : window.localStorage) == null ? void 0 : _a.getItem("debug");
115
+ } catch (e) {
116
+ return false;
117
+ }
118
+ })();
119
+ function debug(ns, ...args) {
120
+ if (debugEnabled) {
121
+ console.debug(`sq:environment:${ns}`, ...args);
122
+ }
123
+ }
124
+
125
+ // src/history.ts
126
+ var debug2 = (...args) => debug("history", ...args);
127
+ function normalizeTo(to) {
128
+ if (typeof to === "string")
129
+ return to;
130
+ const url = new URL(to.pathname, window.origin);
131
+ if (to.search)
132
+ url.search = to.search;
133
+ if (to.hash)
134
+ url.hash = to.hash;
135
+ return url;
136
+ }
137
+ var BrowserHistory = class {
138
+ constructor() {
139
+ this.listeners = /* @__PURE__ */ new Set();
140
+ window.addEventListener("popstate", this.notify.bind(this));
141
+ window.addEventListener("pushState", this.notify.bind(this));
142
+ window.addEventListener("replaceState", this.notify.bind(this));
143
+ }
144
+ listen(fn) {
145
+ this.listeners.add(fn);
146
+ return () => {
147
+ this.listeners.delete(fn);
148
+ };
149
+ }
150
+ notify() {
151
+ debug2("notifying listeners of route change", this.location);
152
+ if (this.listeners) {
153
+ this.listeners.forEach((listener) => listener());
154
+ }
155
+ }
156
+ get location() {
157
+ return {
158
+ pathname: window.location.pathname,
159
+ search: window.location.search,
160
+ hash: window.location.hash,
161
+ state: window.history.state
162
+ };
163
+ }
164
+ back() {
165
+ this.go(-1);
166
+ }
167
+ forward() {
168
+ this.go(1);
169
+ }
170
+ go(delta) {
171
+ window.history.go(delta);
172
+ }
173
+ push(to, state) {
174
+ const url = normalizeTo(to);
175
+ window.history.pushState(state, "", url);
176
+ }
177
+ replace(to, state) {
178
+ const url = normalizeTo(to);
179
+ window.history.replaceState(state, "", url);
180
+ }
181
+ };
182
+ var MemoryHistory = class {
183
+ constructor() {
184
+ this.history = [{ pathname: "/", search: "", hash: "" }];
185
+ this.index = 0;
186
+ this.listeners = /* @__PURE__ */ new Set();
187
+ }
188
+ listen(fn) {
189
+ this.listeners.add(fn);
190
+ return () => {
191
+ this.listeners.delete(fn);
192
+ };
193
+ }
194
+ current() {
195
+ return this.history[this.index];
196
+ }
197
+ get state() {
198
+ return this.current().state;
199
+ }
200
+ notify() {
201
+ debug2("notifying listeners of route change", this.location);
202
+ this.listeners.forEach((listener) => listener());
203
+ }
204
+ get location() {
205
+ return this.history[this.index];
206
+ }
207
+ back() {
208
+ this.go(-1);
209
+ }
210
+ forward() {
211
+ this.go(1);
212
+ }
213
+ go(delta) {
214
+ this.index += delta;
215
+ if (this.index < 0)
216
+ this.index = 0;
217
+ if (this.index >= this.history.length)
218
+ this.index = this.history.length - 1;
219
+ this.notify();
220
+ }
221
+ push(to, state) {
222
+ let entry;
223
+ if (typeof to === "string") {
224
+ entry = {
225
+ pathname: to,
226
+ search: this.history[this.index].search,
227
+ hash: this.history[this.index].hash,
228
+ state
229
+ };
230
+ } else {
231
+ entry = __spreadProps(__spreadValues({}, to), {
232
+ state
233
+ });
234
+ }
235
+ this.index++;
236
+ this.history.splice(this.index, this.history.length, entry);
237
+ this.notify();
238
+ }
239
+ replace(to, state) {
240
+ if (typeof to === "string") {
241
+ this.history[this.index] = {
242
+ pathname: to,
243
+ search: this.history[this.index].search,
244
+ hash: this.history[this.index].hash,
245
+ state
246
+ };
247
+ } else {
248
+ this.history[this.index] = __spreadProps(__spreadValues({}, to), {
249
+ state
250
+ });
251
+ }
252
+ this.notify();
253
+ }
254
+ };
255
+ function createHistory() {
256
+ if (getEnvironment() === "SquatchPortal") {
257
+ let wrapHistoryFunction2 = function(functionName) {
258
+ const orig = window.history[functionName];
259
+ return function(...args) {
260
+ const returnValue = orig.apply(window.history, args);
261
+ const event = new Event(functionName);
262
+ window.dispatchEvent(event);
263
+ return returnValue;
264
+ };
265
+ };
266
+ var wrapHistoryFunction = wrapHistoryFunction2;
267
+ window.history.pushState = wrapHistoryFunction2("pushState");
268
+ window.history.replaceState = wrapHistoryFunction2("replaceState");
269
+ return new BrowserHistory();
270
+ } else {
271
+ return new MemoryHistory();
272
+ }
273
+ }
274
+ function lazyHistory() {
275
+ window.squatchHistory = window.squatchHistory || createHistory();
276
+ return window.squatchHistory;
277
+ }
278
+ var LazyHistory = class {
279
+ get location() {
280
+ return lazyHistory().location;
281
+ }
282
+ listen(fn) {
283
+ return lazyHistory().listen(fn);
284
+ }
285
+ back() {
286
+ lazyHistory().back();
287
+ }
288
+ forward() {
289
+ lazyHistory().forward();
290
+ }
291
+ go(delta) {
292
+ lazyHistory().go(delta);
293
+ }
294
+ push(to, state) {
295
+ lazyHistory().push(to, state);
296
+ }
297
+ replace(to, state) {
298
+ lazyHistory().replace(to, state);
299
+ }
300
+ };
301
+ var history_default = new LazyHistory();
302
+
107
303
  // src/contexts/UserIdentityContext.ts
108
304
  import decode from "jwt-decode";
109
305
  import { ContextProvider as ContextProvider2 } from "dom-context";
@@ -114,22 +310,12 @@ import { ContextListener } from "dom-context";
114
310
 
115
311
  // src/contexts/LocaleContext.ts
116
312
  import { ContextProvider } from "dom-context";
117
-
118
- // src/debug.ts
119
- var debugEnabled = localStorage.getItem("debug");
120
- function debug(ns, ...args) {
121
- if (debugEnabled) {
122
- console.debug(`sq:environment:${ns}`, ...args);
123
- }
124
- }
125
-
126
- // src/contexts/LocaleContext.ts
127
- var debug2 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
313
+ var debug3 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
128
314
  function lazilyStartLocaleContext() {
129
315
  var _a;
130
316
  let globalProvider = window.squatchLocale;
131
317
  if (!globalProvider) {
132
- debug2("Creating locale context provider");
318
+ debug3("Creating locale context provider");
133
319
  globalProvider = new ContextProvider({
134
320
  element: document.documentElement,
135
321
  initialState: ((_a = window.widgetIdent) == null ? void 0 : _a.locale) || navigator.language.replace("-", "_"),
@@ -142,7 +328,7 @@ function lazilyStartLocaleContext() {
142
328
  function setLocale(locale) {
143
329
  const globalProvider = lazilyStartLocaleContext();
144
330
  if (globalProvider.context !== locale) {
145
- debug2(`Setting locale context value [${locale}]`);
331
+ debug3(`Setting locale context value [${locale}]`);
146
332
  globalProvider.context = locale;
147
333
  }
148
334
  }
@@ -152,7 +338,7 @@ function getLocale() {
152
338
  }
153
339
 
154
340
  // src/fetchLocale.ts
155
- var debug3 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
341
+ var debug4 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
156
342
  var GET_LOCALE = `
157
343
  query {
158
344
  viewer {
@@ -164,7 +350,7 @@ var GET_LOCALE = `
164
350
  `;
165
351
  async function fetchLocale() {
166
352
  var _a;
167
- debug3("Fetching locale from GraphQL for current user");
353
+ debug4("Fetching locale from GraphQL for current user");
168
354
  try {
169
355
  const result = await fetch(`${getAppDomain()}/api/v1/${getTenantAlias()}/graphql`, {
170
356
  method: "POST",
@@ -185,13 +371,13 @@ async function fetchLocale() {
185
371
  }
186
372
  return json.data.viewer.locale || void 0;
187
373
  } catch (e) {
188
- debug3(`Failed to fetch locale for current user`, e.message);
374
+ debug4(`Failed to fetch locale for current user`, e.message);
189
375
  return void 0;
190
376
  }
191
377
  }
192
378
 
193
379
  // src/listeners.ts
194
- var debug4 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
380
+ var debug5 = (...args) => debug(LOCALE_CONTEXT_NAME, ...args);
195
381
  var userContextListenerDiv = (() => {
196
382
  const id = "__environment_context_listener";
197
383
  let div = document.getElementById(id);
@@ -211,30 +397,30 @@ var userContextListenerForLocale = new ContextListener({
211
397
  const defaultLocale = ((_a = window.widgetIdent) == null ? void 0 : _a.locale) || navigator.language.replace("-", "_");
212
398
  let newLocale;
213
399
  if (next) {
214
- debug4("User context changed, refetching locale");
400
+ debug5("User context changed, refetching locale");
215
401
  const locale = await fetchLocale();
216
402
  if (localeProvider.context !== locale) {
217
- debug4(`New value fetched from GraphQL [${locale}]`);
403
+ debug5(`New value fetched from GraphQL [${locale}]`);
218
404
  newLocale = locale || defaultLocale;
219
405
  }
220
406
  } else {
221
407
  newLocale = defaultLocale;
222
408
  }
223
- debug4(`Setting locale context to [${newLocale}]`);
409
+ debug5(`Setting locale context to [${newLocale}]`);
224
410
  localeProvider.context = newLocale;
225
411
  }
226
412
  });
227
413
  function startUserContextListenerForLocale() {
228
- debug4("Starting user context listener for locale updates");
414
+ debug5("Starting user context listener for locale updates");
229
415
  userContextListenerForLocale.start();
230
416
  }
231
417
 
232
418
  // src/contexts/UserIdentityContext.ts
233
- var debug5 = (...args) => debug(USER_CONTEXT_NAME, ...args);
419
+ var debug6 = (...args) => debug(USER_CONTEXT_NAME, ...args);
234
420
  function lazilyStartUserContext() {
235
421
  let globalProvider = window.squatchUserIdentity;
236
422
  if (!globalProvider) {
237
- debug5("Creating user context provider");
423
+ debug6("Creating user context provider");
238
424
  globalProvider = new ContextProvider2({
239
425
  element: document.documentElement,
240
426
  initialState: _getInitialValue(),
@@ -320,7 +506,7 @@ function _getInitialValue() {
320
506
  function setUserIdentity(identity) {
321
507
  const globalProvider = lazilyStartUserContext();
322
508
  if (!equal(globalProvider.context, identity)) {
323
- debug5(`Setting user context value [${JSON.stringify(identity)}]`);
509
+ debug6(`Setting user context value [${JSON.stringify(identity)}]`);
324
510
  globalProvider.context = identity;
325
511
  }
326
512
  if (identity && getEnvironmentSDK().type === "SquatchPortal") {
@@ -336,12 +522,12 @@ function getUserIdentity() {
336
522
 
337
523
  // src/contexts/ProgramContext.ts
338
524
  import { ContextProvider as ContextProvider3 } from "dom-context";
339
- var debug6 = (...args) => debug(PROGRAM_CONTEXT_NAME, ...args);
525
+ var debug7 = (...args) => debug(PROGRAM_CONTEXT_NAME, ...args);
340
526
  function lazilyStartProgramContext() {
341
527
  var _a;
342
528
  let globalProvider = window.squatchProgramId;
343
529
  if (!globalProvider) {
344
- debug6("Creating program context provider");
530
+ debug7("Creating program context provider");
345
531
  globalProvider = new ContextProvider3({
346
532
  element: document.documentElement,
347
533
  initialState: ((_a = window.widgetIdent) == null ? void 0 : _a.programId) || void 0,
@@ -354,7 +540,7 @@ function lazilyStartProgramContext() {
354
540
  function setProgramId(programId) {
355
541
  const globalProvider = lazilyStartProgramContext();
356
542
  if (globalProvider.context !== programId) {
357
- debug6(`Setting program context value [${programId}]`);
543
+ debug7(`Setting program context value [${programId}]`);
358
544
  globalProvider.context = programId;
359
545
  }
360
546
  }
@@ -363,8 +549,11 @@ function getProgramId() {
363
549
  return (_a = window.squatchLocale) == null ? void 0 : _a.context;
364
550
  }
365
551
  export {
552
+ BrowserHistory,
366
553
  DEFAULT_MEDIUM,
367
554
  LOCALE_CONTEXT_NAME,
555
+ LazyHistory,
556
+ MemoryHistory,
368
557
  PROGRAM_CONTEXT_NAME,
369
558
  USER_CONTEXT_NAME,
370
559
  getAppDomain,
@@ -375,6 +564,7 @@ export {
375
564
  getProgramId,
376
565
  getTenantAlias,
377
566
  getUserIdentity,
567
+ history_default as history,
378
568
  isDemo,
379
569
  lazilyStartLocaleContext,
380
570
  lazilyStartProgramContext,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saasquatch/component-environment",
3
- "version": "1.0.0-4",
3
+ "version": "1.0.0-5",
4
4
  "description": "Environment and contexts for SaaSquatch components",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",