@schematichq/schematic-react 1.0.0 → 1.0.2

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.
@@ -624,6 +624,7 @@ function contextString(context) {
624
624
  }
625
625
  var anonymousIdKey = "schematicId";
626
626
  var Schematic = class {
627
+ additionalHeaders = {};
627
628
  apiKey;
628
629
  apiUrl = "https://api.schematichq.com";
629
630
  conn = null;
@@ -643,6 +644,9 @@ var Schematic = class {
643
644
  this.eventQueue = [];
644
645
  this.useWebSocket = options?.useWebSocket ?? false;
645
646
  this.flagListener = options?.flagListener;
647
+ if (options?.additionalHeaders) {
648
+ this.additionalHeaders = options.additionalHeaders;
649
+ }
646
650
  if (options?.storage) {
647
651
  this.storage = options.storage;
648
652
  } else if (typeof localStorage !== "undefined") {
@@ -676,8 +680,9 @@ var Schematic = class {
676
680
  return fetch(requestUrl, {
677
681
  method: "POST",
678
682
  headers: {
679
- "X-Schematic-Api-Key": this.apiKey,
680
- "Content-Type": "application/json;charset=UTF-8"
683
+ ...this.additionalHeaders ?? {},
684
+ "Content-Type": "application/json;charset=UTF-8",
685
+ "X-Schematic-Api-Key": this.apiKey
681
686
  },
682
687
  body: JSON.stringify(context)
683
688
  }).then((response) => {
@@ -700,6 +705,7 @@ var Schematic = class {
700
705
  return fetch(requestUrl, {
701
706
  method: "POST",
702
707
  headers: {
708
+ ...this.additionalHeaders ?? {},
703
709
  "Content-Type": "application/json;charset=UTF-8",
704
710
  "X-Schematic-Api-Key": this.apiKey
705
711
  },
@@ -805,6 +811,7 @@ var Schematic = class {
805
811
  await fetch(captureUrl, {
806
812
  method: "POST",
807
813
  headers: {
814
+ ...this.additionalHeaders ?? {},
808
815
  "Content-Type": "application/json;charset=UTF-8"
809
816
  },
810
817
  body: payload
@@ -854,8 +861,7 @@ var Schematic = class {
854
861
  wsSendMessage = (socket, context) => {
855
862
  return new Promise((resolve, reject) => {
856
863
  if (contextString(context) == contextString(this.context)) {
857
- resolve();
858
- return;
864
+ return resolve(this.setIsPending(false));
859
865
  }
860
866
  this.context = context;
861
867
  const sendMessage = () => {
@@ -868,7 +874,7 @@ var Schematic = class {
868
874
  (message.flags ?? []).forEach(
869
875
  (flag) => {
870
876
  this.values[contextString(context)][flag.flag] = flag.value;
871
- this.notifyFlagValueListeners(flag.flag);
877
+ this.notifyFlagValueListeners(flag.flag, flag.value);
872
878
  }
873
879
  );
874
880
  if (this.flagListener) {
@@ -912,7 +918,9 @@ var Schematic = class {
912
918
  };
913
919
  setIsPending = (isPending) => {
914
920
  this.isPending = isPending;
915
- this.isPendingListeners.forEach((listener) => listener());
921
+ this.isPendingListeners.forEach(
922
+ (listener) => notifyListener(listener, isPending)
923
+ );
916
924
  };
917
925
  // flagValues state
918
926
  getFlagValue = (flagKey) => {
@@ -932,11 +940,18 @@ var Schematic = class {
932
940
  this.flagValueListeners[flagKey].delete(listener);
933
941
  };
934
942
  };
935
- notifyFlagValueListeners = (flagKey) => {
943
+ notifyFlagValueListeners = (flagKey, value) => {
936
944
  const listeners = this.flagValueListeners?.[flagKey] ?? [];
937
- listeners.forEach((listener) => listener());
945
+ listeners.forEach((listener) => notifyListener(listener, value));
938
946
  };
939
947
  };
948
+ var notifyListener = (listener, value) => {
949
+ if (listener.length > 0) {
950
+ listener(value);
951
+ } else {
952
+ listener();
953
+ }
954
+ };
940
955
 
941
956
  // src/context/schematic.tsx
942
957
  var import_react = __toESM(require("react"));
@@ -582,6 +582,7 @@ function contextString(context) {
582
582
  }
583
583
  var anonymousIdKey = "schematicId";
584
584
  var Schematic = class {
585
+ additionalHeaders = {};
585
586
  apiKey;
586
587
  apiUrl = "https://api.schematichq.com";
587
588
  conn = null;
@@ -601,6 +602,9 @@ var Schematic = class {
601
602
  this.eventQueue = [];
602
603
  this.useWebSocket = options?.useWebSocket ?? false;
603
604
  this.flagListener = options?.flagListener;
605
+ if (options?.additionalHeaders) {
606
+ this.additionalHeaders = options.additionalHeaders;
607
+ }
604
608
  if (options?.storage) {
605
609
  this.storage = options.storage;
606
610
  } else if (typeof localStorage !== "undefined") {
@@ -634,8 +638,9 @@ var Schematic = class {
634
638
  return fetch(requestUrl, {
635
639
  method: "POST",
636
640
  headers: {
637
- "X-Schematic-Api-Key": this.apiKey,
638
- "Content-Type": "application/json;charset=UTF-8"
641
+ ...this.additionalHeaders ?? {},
642
+ "Content-Type": "application/json;charset=UTF-8",
643
+ "X-Schematic-Api-Key": this.apiKey
639
644
  },
640
645
  body: JSON.stringify(context)
641
646
  }).then((response) => {
@@ -658,6 +663,7 @@ var Schematic = class {
658
663
  return fetch(requestUrl, {
659
664
  method: "POST",
660
665
  headers: {
666
+ ...this.additionalHeaders ?? {},
661
667
  "Content-Type": "application/json;charset=UTF-8",
662
668
  "X-Schematic-Api-Key": this.apiKey
663
669
  },
@@ -763,6 +769,7 @@ var Schematic = class {
763
769
  await fetch(captureUrl, {
764
770
  method: "POST",
765
771
  headers: {
772
+ ...this.additionalHeaders ?? {},
766
773
  "Content-Type": "application/json;charset=UTF-8"
767
774
  },
768
775
  body: payload
@@ -812,8 +819,7 @@ var Schematic = class {
812
819
  wsSendMessage = (socket, context) => {
813
820
  return new Promise((resolve, reject) => {
814
821
  if (contextString(context) == contextString(this.context)) {
815
- resolve();
816
- return;
822
+ return resolve(this.setIsPending(false));
817
823
  }
818
824
  this.context = context;
819
825
  const sendMessage = () => {
@@ -826,7 +832,7 @@ var Schematic = class {
826
832
  (message.flags ?? []).forEach(
827
833
  (flag) => {
828
834
  this.values[contextString(context)][flag.flag] = flag.value;
829
- this.notifyFlagValueListeners(flag.flag);
835
+ this.notifyFlagValueListeners(flag.flag, flag.value);
830
836
  }
831
837
  );
832
838
  if (this.flagListener) {
@@ -870,7 +876,9 @@ var Schematic = class {
870
876
  };
871
877
  setIsPending = (isPending) => {
872
878
  this.isPending = isPending;
873
- this.isPendingListeners.forEach((listener) => listener());
879
+ this.isPendingListeners.forEach(
880
+ (listener) => notifyListener(listener, isPending)
881
+ );
874
882
  };
875
883
  // flagValues state
876
884
  getFlagValue = (flagKey) => {
@@ -890,11 +898,18 @@ var Schematic = class {
890
898
  this.flagValueListeners[flagKey].delete(listener);
891
899
  };
892
900
  };
893
- notifyFlagValueListeners = (flagKey) => {
901
+ notifyFlagValueListeners = (flagKey, value) => {
894
902
  const listeners = this.flagValueListeners?.[flagKey] ?? [];
895
- listeners.forEach((listener) => listener());
903
+ listeners.forEach((listener) => notifyListener(listener, value));
896
904
  };
897
905
  };
906
+ var notifyListener = (listener, value) => {
907
+ if (listener.length > 0) {
908
+ listener(value);
909
+ } else {
910
+ listener();
911
+ }
912
+ };
898
913
 
899
914
  // src/context/schematic.tsx
900
915
  import React, { createContext, useEffect, useMemo } from "react";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schematichq/schematic-react",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "dist/schematic-react.cjs.js",
5
5
  "module": "dist/schematic-react.esm.js",
6
6
  "types": "dist/schematic-react.d.ts",
@@ -17,23 +17,21 @@
17
17
  },
18
18
  "scripts": {
19
19
  "dev": "yarn tsc --watch",
20
- "build": "yarn tsc && yarn openapi && yarn format && yarn lint && yarn clean && yarn build:cjs && yarn build:esm && yarn build:types",
20
+ "build": "yarn tsc && yarn format && yarn lint && yarn clean && yarn build:cjs && yarn build:esm && yarn build:types",
21
21
  "build:cjs": "npx esbuild src/index.ts --bundle --external:react --format=cjs --outfile=dist/schematic-react.cjs.js",
22
22
  "build:esm": "npx esbuild src/index.ts --bundle --external:react --format=esm --outfile=dist/schematic-react.esm.js",
23
23
  "build:types": "npx tsc && npx api-extractor run",
24
24
  "clean": "rm -rf dist",
25
25
  "format": "prettier --write \"src/**/*.{ts,tsx}\"",
26
26
  "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --fix",
27
- "openapi": "rm -rf src/api/ && npx openapi-generator-cli generate -c openapi-config.yaml && prettier --write \"src/api/**/*.{ts,tsx}\"",
28
27
  "test": "jest --config jest.config.js",
29
28
  "tsc": "npx tsc"
30
29
  },
31
30
  "dependencies": {
32
- "@schematichq/schematic-js": "^1.0.0"
31
+ "@schematichq/schematic-js": "^1.0.2"
33
32
  },
34
33
  "devDependencies": {
35
34
  "@microsoft/api-extractor": "^7.47.9",
36
- "@openapitools/openapi-generator-cli": "^2.13.9",
37
35
  "@types/jest": "^29.5.13",
38
36
  "@types/react": "^18.3.9",
39
37
  "@typescript-eslint/eslint-plugin": "^8.7.0",