@firebase/data-connect 0.0.3-dataconnect-preview.d986d4bf2 → 0.1.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.
@@ -5,7 +5,7 @@ import { FirebaseError } from '@firebase/util';
5
5
  import { Logger } from '@firebase/logger';
6
6
 
7
7
  var name = "@firebase/data-connect";
8
- var version = "0.0.3-dataconnect-preview.d986d4bf2";
8
+ var version = "0.1.0";
9
9
 
10
10
  /**
11
11
  * @license
@@ -33,6 +33,63 @@ function setSDKVersion(version) {
33
33
  SDK_VERSION = version;
34
34
  }
35
35
 
36
+ /**
37
+ * @license
38
+ * Copyright 2024 Google LLC
39
+ *
40
+ * Licensed under the Apache License, Version 2.0 (the "License");
41
+ * you may not use this file except in compliance with the License.
42
+ * You may obtain a copy of the License at
43
+ *
44
+ * http://www.apache.org/licenses/LICENSE-2.0
45
+ *
46
+ * Unless required by applicable law or agreed to in writing, software
47
+ * distributed under the License is distributed on an "AS IS" BASIS,
48
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49
+ * See the License for the specific language governing permissions and
50
+ * limitations under the License.
51
+ */
52
+ /**
53
+ * @internal
54
+ * Abstraction around AppCheck's token fetching capabilities.
55
+ */
56
+ var AppCheckTokenProvider = /** @class */ (function () {
57
+ function AppCheckTokenProvider(appName_, appCheckProvider) {
58
+ var _this = this;
59
+ this.appName_ = appName_;
60
+ this.appCheckProvider = appCheckProvider;
61
+ this.appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
62
+ if (!this.appCheck) {
63
+ void (appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.get().then(function (appCheck) { return (_this.appCheck = appCheck); }).catch());
64
+ }
65
+ }
66
+ AppCheckTokenProvider.prototype.getToken = function (forceRefresh) {
67
+ var _this = this;
68
+ if (!this.appCheck) {
69
+ return new Promise(function (resolve, reject) {
70
+ // Support delayed initialization of FirebaseAppCheck. This allows our
71
+ // customers to initialize the RTDB SDK before initializing Firebase
72
+ // AppCheck and ensures that all requests are authenticated if a token
73
+ // becomes available before the timoeout below expires.
74
+ setTimeout(function () {
75
+ if (_this.appCheck) {
76
+ _this.getToken(forceRefresh).then(resolve, reject);
77
+ }
78
+ else {
79
+ resolve(null);
80
+ }
81
+ }, 0);
82
+ });
83
+ }
84
+ return this.appCheck.getToken(forceRefresh);
85
+ };
86
+ AppCheckTokenProvider.prototype.addTokenChangeListener = function (listener) {
87
+ var _a;
88
+ void ((_a = this.appCheckProvider) === null || _a === void 0 ? void 0 : _a.get().then(function (appCheck) { return appCheck.addTokenListener(listener); }));
89
+ };
90
+ return AppCheckTokenProvider;
91
+ }());
92
+
36
93
  /**
37
94
  * @license
38
95
  * Copyright 2024 Google LLC
@@ -126,6 +183,7 @@ function logError(msg) {
126
183
  * See the License for the specific language governing permissions and
127
184
  * limitations under the License.
128
185
  */
186
+ // @internal
129
187
  var FirebaseAuthProvider = /** @class */ (function () {
130
188
  function FirebaseAuthProvider(_appName, _options, _authProvider) {
131
189
  var _this = this;
@@ -414,7 +472,7 @@ function urlBuilder(projectConfig, transportOptions) {
414
472
  logError('Port type is of an invalid type');
415
473
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Incorrect type for port passed in!');
416
474
  }
417
- return "".concat(baseUrl, "/v1alpha/projects/").concat(project, "/locations/").concat(location, "/services/").concat(service, "/connectors/").concat(connector);
475
+ return "".concat(baseUrl, "/v1beta/projects/").concat(project, "/locations/").concat(location, "/services/").concat(service, "/connectors/").concat(connector);
418
476
  }
419
477
  function addToken(url, apiKey) {
420
478
  if (!apiKey) {
@@ -449,7 +507,7 @@ function getGoogApiClientValue(_isUsingGen) {
449
507
  }
450
508
  return str;
451
509
  }
452
- function dcFetch(url, body, _a, accessToken, _isUsingGen) {
510
+ function dcFetch(url, body, _a, appId, accessToken, appCheckToken, _isUsingGen) {
453
511
  var _this = this;
454
512
  var signal = _a.signal;
455
513
  if (!connectFetch) {
@@ -462,6 +520,12 @@ function dcFetch(url, body, _a, accessToken, _isUsingGen) {
462
520
  if (accessToken) {
463
521
  headers['X-Firebase-Auth-Token'] = accessToken;
464
522
  }
523
+ if (appId) {
524
+ headers['x-firebase-gmpid'] = appId;
525
+ }
526
+ if (appCheckToken) {
527
+ headers['X-Firebase-AppCheck'] = appCheckToken;
528
+ }
465
529
  var bodyStr = JSON.stringify(body);
466
530
  logDebug("Making request out to ".concat(url, " with body: ").concat(bodyStr));
467
531
  return connectFetch(url, {
@@ -535,12 +599,14 @@ function getMessage(obj) {
535
599
  * limitations under the License.
536
600
  */
537
601
  var RESTTransport = /** @class */ (function () {
538
- function RESTTransport(options, apiKey, authProvider, transportOptions, _isUsingGen) {
602
+ function RESTTransport(options, apiKey, appId, authProvider, appCheckProvider, transportOptions, _isUsingGen) {
539
603
  if (_isUsingGen === void 0) { _isUsingGen = false; }
540
604
  var _this = this;
541
- var _a;
605
+ var _a, _b;
542
606
  this.apiKey = apiKey;
607
+ this.appId = appId;
543
608
  this.authProvider = authProvider;
609
+ this.appCheckProvider = appCheckProvider;
544
610
  this._isUsingGen = _isUsingGen;
545
611
  this._host = '';
546
612
  this._location = 'l';
@@ -548,7 +614,7 @@ var RESTTransport = /** @class */ (function () {
548
614
  this._secure = true;
549
615
  this._project = 'p';
550
616
  this._accessToken = null;
551
- this._authInitialized = false;
617
+ this._appCheckToken = null;
552
618
  this._lastToken = null;
553
619
  // TODO(mtewani): Update U to include shape of body defined in line 13.
554
620
  this.invokeQuery = function (queryName, body) {
@@ -560,7 +626,7 @@ var RESTTransport = /** @class */ (function () {
560
626
  operationName: queryName,
561
627
  variables: body
562
628
  }, // TODO(mtewani): This is a patch, fix this.
563
- abortController, _this._accessToken, _this._isUsingGen);
629
+ abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
564
630
  });
565
631
  return {
566
632
  then: withAuth.then.bind(withAuth),
@@ -574,7 +640,7 @@ var RESTTransport = /** @class */ (function () {
574
640
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
575
641
  operationName: mutationName,
576
642
  variables: body
577
- }, abortController, _this._accessToken, _this._isUsingGen);
643
+ }, abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
578
644
  });
579
645
  return {
580
646
  then: taskResult.then.bind(taskResult),
@@ -608,6 +674,11 @@ var RESTTransport = /** @class */ (function () {
608
674
  logDebug("New Token Available: ".concat(token));
609
675
  _this._accessToken = token;
610
676
  });
677
+ (_b = this.appCheckProvider) === null || _b === void 0 ? void 0 : _b.addTokenChangeListener(function (result) {
678
+ var token = result.token;
679
+ logDebug("New App Check Token Available: ".concat(token));
680
+ _this._appCheckToken = token;
681
+ });
611
682
  }
612
683
  Object.defineProperty(RESTTransport.prototype, "endpointUrl", {
613
684
  get: function () {
@@ -634,28 +705,42 @@ var RESTTransport = /** @class */ (function () {
634
705
  this._accessToken = newToken;
635
706
  };
636
707
  RESTTransport.prototype.getWithAuth = function (forceToken) {
637
- var _this = this;
708
+ var _a;
638
709
  if (forceToken === void 0) { forceToken = false; }
639
- var starterPromise = new Promise(function (resolve) {
640
- return resolve(_this._accessToken);
710
+ return __awaiter(this, void 0, void 0, function () {
711
+ var starterPromise, _b;
712
+ var _this = this;
713
+ return __generator(this, function (_c) {
714
+ switch (_c.label) {
715
+ case 0:
716
+ starterPromise = new Promise(function (resolve) {
717
+ return resolve(_this._accessToken);
718
+ });
719
+ if (!this.appCheckProvider) return [3 /*break*/, 2];
720
+ _b = this;
721
+ return [4 /*yield*/, this.appCheckProvider.getToken()];
722
+ case 1:
723
+ _b._appCheckToken = (_a = (_c.sent())) === null || _a === void 0 ? void 0 : _a.token;
724
+ _c.label = 2;
725
+ case 2:
726
+ if (this.authProvider) {
727
+ starterPromise = this.authProvider
728
+ .getToken(/*forceToken=*/ forceToken)
729
+ .then(function (data) {
730
+ if (!data) {
731
+ return null;
732
+ }
733
+ _this._accessToken = data.accessToken;
734
+ return _this._accessToken;
735
+ });
736
+ }
737
+ else {
738
+ starterPromise = new Promise(function (resolve) { return resolve(''); });
739
+ }
740
+ return [2 /*return*/, starterPromise];
741
+ }
742
+ });
641
743
  });
642
- if (!this._authInitialized) {
643
- if (this.authProvider) {
644
- starterPromise = this.authProvider
645
- .getToken(/*forceToken=*/ forceToken)
646
- .then(function (data) {
647
- if (!data) {
648
- return null;
649
- }
650
- _this._accessToken = data.accessToken;
651
- return _this._accessToken;
652
- });
653
- }
654
- else {
655
- starterPromise = new Promise(function (resolve) { return resolve(''); });
656
- }
657
- }
658
- return starterPromise;
659
744
  };
660
745
  RESTTransport.prototype._setLastToken = function (lastToken) {
661
746
  this._lastToken = lastToken;
@@ -786,14 +871,16 @@ function parseOptions(fullHost) {
786
871
  * Class representing Firebase Data Connect
787
872
  */
788
873
  var DataConnect = /** @class */ (function () {
874
+ // @internal
789
875
  function DataConnect(app,
790
876
  // TODO(mtewani): Replace with _dataConnectOptions in the future
791
- dataConnectOptions, _authProvider) {
877
+ dataConnectOptions, _authProvider, _appCheckProvider) {
792
878
  this.app = app;
793
879
  this.dataConnectOptions = dataConnectOptions;
794
880
  this._authProvider = _authProvider;
881
+ this._appCheckProvider = _appCheckProvider;
795
882
  this.isEmulator = false;
796
- this.initialized = false;
883
+ this._initialized = false;
797
884
  this._isUsingGeneratedSdk = false;
798
885
  if (typeof process !== 'undefined' && process.env) {
799
886
  var host = process.env[FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR];
@@ -804,9 +891,7 @@ var DataConnect = /** @class */ (function () {
804
891
  }
805
892
  }
806
893
  }
807
- /*
808
- @internal
809
- */
894
+ // @internal
810
895
  DataConnect.prototype._useGeneratedSdk = function () {
811
896
  if (!this._isUsingGeneratedSdk) {
812
897
  this._isUsingGeneratedSdk = true;
@@ -816,13 +901,15 @@ var DataConnect = /** @class */ (function () {
816
901
  _removeServiceInstance(this.app, 'data-connect', JSON.stringify(this.getSettings()));
817
902
  return Promise.resolve();
818
903
  };
904
+ // @internal
819
905
  DataConnect.prototype.getSettings = function () {
820
906
  var copy = JSON.parse(JSON.stringify(this.dataConnectOptions));
821
907
  delete copy.projectId;
822
908
  return copy;
823
909
  };
910
+ // @internal
824
911
  DataConnect.prototype.setInitialized = function () {
825
- if (this.initialized) {
912
+ if (this._initialized) {
826
913
  return;
827
914
  }
828
915
  if (this._transportClass === undefined) {
@@ -832,16 +919,20 @@ var DataConnect = /** @class */ (function () {
832
919
  if (this._authProvider) {
833
920
  this._authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this._authProvider);
834
921
  }
835
- this.initialized = true;
836
- this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider, undefined, this._isUsingGeneratedSdk);
922
+ if (this._appCheckProvider) {
923
+ this._appCheckTokenProvider = new AppCheckTokenProvider(this.app.name, this._appCheckProvider);
924
+ }
925
+ this._initialized = true;
926
+ this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this.app.options.appId, this._authTokenProvider, this._appCheckTokenProvider, undefined, this._isUsingGeneratedSdk);
837
927
  if (this._transportOptions) {
838
928
  this._transport.useEmulator(this._transportOptions.host, this._transportOptions.port, this._transportOptions.sslEnabled);
839
929
  }
840
930
  this._queryManager = new QueryManager(this._transport);
841
931
  this._mutationManager = new MutationManager(this._transport);
842
932
  };
933
+ // @internal
843
934
  DataConnect.prototype.enableEmulator = function (transportOptions) {
844
- if (this.initialized) {
935
+ if (this._initialized) {
845
936
  logError('enableEmulator called after initialization');
846
937
  throw new DataConnectError(Code.ALREADY_INITIALIZED, 'DataConnect instance already initialized!');
847
938
  }
@@ -928,6 +1019,7 @@ function registerDataConnect(variant) {
928
1019
  var settings = _a.instanceIdentifier, options = _a.options;
929
1020
  var app = container.getProvider('app').getImmediate();
930
1021
  var authProvider = container.getProvider('auth-internal');
1022
+ var appCheckProvider = container.getProvider('app-check-internal');
931
1023
  var newOpts = options;
932
1024
  if (settings) {
933
1025
  newOpts = JSON.parse(settings);
@@ -935,7 +1027,7 @@ function registerDataConnect(variant) {
935
1027
  if (!app.options.projectId) {
936
1028
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Project ID must be provided. Did you pass in a proper projectId to initializeApp?');
937
1029
  }
938
- return new DataConnect(app, __assign(__assign({}, newOpts), { projectId: app.options.projectId }), authProvider);
1030
+ return new DataConnect(app, __assign(__assign({}, newOpts), { projectId: app.options.projectId }), authProvider, appCheckProvider);
939
1031
  }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
940
1032
  registerVersion(name, version, variant);
941
1033
  // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
@@ -1099,5 +1191,5 @@ function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComp
1099
1191
  */
1100
1192
  registerDataConnect();
1101
1193
 
1102
- export { DataConnect, FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR, FirebaseAuthProvider, MUTATION_STR, MutationManager, QUERY_STR, SOURCE_CACHE, SOURCE_SERVER, connectDataConnectEmulator, executeMutation, executeQuery, getDataConnect, mutationRef, parseOptions, queryRef, setLogLevel, subscribe, terminate, toQueryRef, validateArgs, validateDCOptions };
1194
+ export { DataConnect, MUTATION_STR, MutationManager, QUERY_STR, SOURCE_CACHE, SOURCE_SERVER, connectDataConnectEmulator, executeMutation, executeQuery, getDataConnect, mutationRef, parseOptions, queryRef, setLogLevel, subscribe, terminate, toQueryRef, validateArgs, validateDCOptions };
1103
1195
  //# sourceMappingURL=index.esm5.js.map