@imtbl/sdk 1.5.1 → 1.5.3-alpha

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.
@@ -1,4 +1,6 @@
1
1
  import globalAxios from 'axios';
2
+ import { memorise } from 'lru-memorise';
3
+ import { getGlobalisedValue as getGlobalisedValue$1 } from 'global-const';
2
4
 
3
5
  /* tslint:disable */
4
6
  /* eslint-disable */
@@ -4461,12 +4463,6 @@ var Detail;
4461
4463
  Detail["DOMAIN"] = "domain";
4462
4464
  Detail["SDK_VERSION"] = "sdkVersion";
4463
4465
  })(Detail || (Detail = {}));
4464
- var Store;
4465
- (function (Store) {
4466
- Store["EVENTS"] = "events";
4467
- Store["RUNTIME"] = "runtime";
4468
- })(Store || (Store = {}));
4469
- const POLLING_FREQUENCY = 5000;
4470
4466
 
4471
4467
  const IMTBL_API = 'https://api.x.immutable.com';
4472
4468
  async function post(path, data) {
@@ -4526,6 +4522,11 @@ const setItem = (key, payload) => {
4526
4522
  return false;
4527
4523
  };
4528
4524
 
4525
+ var Store;
4526
+ (function (Store) {
4527
+ Store["EVENTS"] = "events";
4528
+ Store["RUNTIME"] = "runtime";
4529
+ })(Store || (Store = {}));
4529
4530
  // In memory storage for events and other data
4530
4531
  let EVENT_STORE;
4531
4532
  let RUNTIME_DETAILS;
@@ -4553,6 +4554,10 @@ const getDetail = (key) => {
4553
4554
  const getAllDetails = () => RUNTIME_DETAILS;
4554
4555
  // Events
4555
4556
  const getEvents = () => EVENT_STORE;
4557
+ const addEvent = (event) => {
4558
+ EVENT_STORE.push(event);
4559
+ setItem(Store.EVENTS, EVENT_STORE);
4560
+ };
4556
4561
  const removeSentEvents = (numberOfEvents) => {
4557
4562
  EVENT_STORE = EVENT_STORE.slice(numberOfEvents);
4558
4563
  setItem(Store.EVENTS, EVENT_STORE);
@@ -4571,9 +4576,7 @@ const flattenProperties = (properties) => {
4571
4576
  };
4572
4577
 
4573
4578
  // WARNING: DO NOT CHANGE THE STRING BELOW. IT GETS REPLACED AT BUILD TIME.
4574
- const SDK_VERSION = '1.5.1';
4575
- let initialised = false;
4576
- const isInitialised = () => initialised;
4579
+ const SDK_VERSION = '1.5.3-alpha';
4577
4580
  const getReferrer = () => {
4578
4581
  if (isNode()) {
4579
4582
  return '';
@@ -4610,21 +4613,28 @@ const getRuntimeDetails = () => {
4610
4613
  screen: `${window.screen.width}x${window.screen.height}`,
4611
4614
  };
4612
4615
  };
4616
+ let initialised = false;
4617
+ const isInitialised = () => initialised;
4613
4618
  const initialise = async () => {
4614
- const runtimeDetails = flattenProperties(getRuntimeDetails());
4615
- const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
4616
- const body = {
4617
- version: 1,
4618
- data: {
4619
- runtimeDetails,
4620
- runtimeId: existingRuntimeId,
4621
- },
4622
- };
4623
- const response = await post('/v1/sdk/initialise', body);
4624
- // Get runtimeId and store it
4625
- const { runtimeId } = response;
4626
- storeDetail(Detail.RUNTIME_ID, runtimeId);
4627
4619
  initialised = true;
4620
+ try {
4621
+ const runtimeDetails = flattenProperties(getRuntimeDetails());
4622
+ const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
4623
+ const body = {
4624
+ version: 1,
4625
+ data: {
4626
+ runtimeDetails,
4627
+ runtimeId: existingRuntimeId,
4628
+ },
4629
+ };
4630
+ const response = await post('/v1/sdk/initialise', body);
4631
+ // Get runtimeId and store it
4632
+ const { runtimeId } = response;
4633
+ storeDetail(Detail.RUNTIME_ID, runtimeId);
4634
+ }
4635
+ catch (error) {
4636
+ initialised = false;
4637
+ }
4628
4638
  };
4629
4639
 
4630
4640
  function errorBoundary(fn) {
@@ -4655,6 +4665,32 @@ function isTestEnvironment() {
4655
4665
  // Just use process.env.CI for now.
4656
4666
  return process.env.JEST_WORKER_ID !== undefined;
4657
4667
  }
4668
+
4669
+ const GLOBALISE_KEY = 'imtbl__metrics';
4670
+ const MEMORISE_TIMEFRAME = 1000;
4671
+ const MEMORISE_MAX = 1000;
4672
+ const getGlobalisedValue = (key, value) => getGlobalisedValue$1(GLOBALISE_KEY, key, value);
4673
+ const getGlobalisedCachedFunction = (key, fn) => {
4674
+ // Some applications (esp backend, or frontends using the split bundles) can sometimes
4675
+ // initialise the same request multiple times. This will prevent multiple of the
4676
+ // same event,value from being reported in a 1 second period.
4677
+ const memorisedFn = memorise(fn, {
4678
+ lruOptions: { ttl: MEMORISE_TIMEFRAME, max: MEMORISE_MAX },
4679
+ });
4680
+ return getGlobalisedValue$1(GLOBALISE_KEY, key, memorisedFn);
4681
+ };
4682
+
4683
+ const POLLING_FREQUENCY = 5000;
4684
+ // Store the event in the event store
4685
+ const trackFn = (moduleName, eventName, properties) => {
4686
+ const event = {
4687
+ event: `${moduleName}.${eventName}`,
4688
+ time: new Date().toISOString(),
4689
+ ...(properties && { properties: flattenProperties(properties) }),
4690
+ };
4691
+ addEvent(event);
4692
+ };
4693
+ errorBoundary(getGlobalisedCachedFunction('track', trackFn));
4658
4694
  // Sending events to the server
4659
4695
  const flushFn = async () => {
4660
4696
  // Don't flush if not initialised
@@ -4690,11 +4726,32 @@ const flushPoll = async () => {
4690
4726
  await flush();
4691
4727
  setTimeout(flushPoll, POLLING_FREQUENCY);
4692
4728
  };
4729
+ let flushingStarted = false;
4730
+ const startFlushing = () => {
4731
+ if (flushingStarted) {
4732
+ return;
4733
+ }
4734
+ flushingStarted = true;
4735
+ flushPoll();
4736
+ };
4693
4737
  // This will get initialised when module is imported.
4694
4738
  if (!isTestEnvironment()) {
4695
- errorBoundary(flushPoll)();
4739
+ errorBoundary(getGlobalisedValue('startFlushing', startFlushing))();
4696
4740
  }
4697
4741
 
4742
+ const setEnvironmentFn = (env) => {
4743
+ storeDetail(Detail.ENVIRONMENT, env);
4744
+ };
4745
+ errorBoundary(getGlobalisedValue('setEnvironment', setEnvironmentFn));
4746
+ const setPassportClientIdFn = (passportClientId) => {
4747
+ storeDetail(Detail.PASSPORT_CLIENT_ID, passportClientId);
4748
+ };
4749
+ errorBoundary(getGlobalisedValue('setPassportClientId', setPassportClientIdFn));
4750
+ const setPublishableApiKeyFn = (publishableApiKey) => {
4751
+ storeDetail(Detail.PUBLISHABLE_API_KEY, publishableApiKey);
4752
+ };
4753
+ errorBoundary(getGlobalisedValue('setPublishableApiKey', setPublishableApiKeyFn));
4754
+
4698
4755
  var Environment;
4699
4756
  (function (Environment) {
4700
4757
  Environment["PRODUCTION"] = "production";
@@ -4720,7 +4777,7 @@ class APIError extends Error {
4720
4777
 
4721
4778
  /* eslint-disable implicit-arrow-linebreak */
4722
4779
  const defaultHeaders = {
4723
- sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-1.5.1',
4780
+ sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-1.5.3-alpha',
4724
4781
  };
4725
4782
  /**
4726
4783
  * createAPIConfiguration to create a custom Configuration