@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.
@@ -138,7 +138,7 @@ function getGoogApiClientValue(_isUsingGen) {
138
138
  }
139
139
  return str;
140
140
  }
141
- function dcFetch(url, body, _a, accessToken, _isUsingGen) {
141
+ function dcFetch(url, body, _a, appId, accessToken, appCheckToken, _isUsingGen) {
142
142
  var _this = this;
143
143
  var signal = _a.signal;
144
144
  if (!connectFetch) {
@@ -151,6 +151,12 @@ function dcFetch(url, body, _a, accessToken, _isUsingGen) {
151
151
  if (accessToken) {
152
152
  headers['X-Firebase-Auth-Token'] = accessToken;
153
153
  }
154
+ if (appId) {
155
+ headers['x-firebase-gmpid'] = appId;
156
+ }
157
+ if (appCheckToken) {
158
+ headers['X-Firebase-AppCheck'] = appCheckToken;
159
+ }
154
160
  var bodyStr = JSON.stringify(body);
155
161
  logDebug("Making request out to ".concat(url, " with body: ").concat(bodyStr));
156
162
  return connectFetch(url, {
@@ -208,7 +214,64 @@ function getMessage(obj) {
208
214
  }
209
215
 
210
216
  var name = "@firebase/data-connect";
211
- var version = "0.0.3-dataconnect-preview.d986d4bf2";
217
+ var version = "0.1.0";
218
+
219
+ /**
220
+ * @license
221
+ * Copyright 2024 Google LLC
222
+ *
223
+ * Licensed under the Apache License, Version 2.0 (the "License");
224
+ * you may not use this file except in compliance with the License.
225
+ * You may obtain a copy of the License at
226
+ *
227
+ * http://www.apache.org/licenses/LICENSE-2.0
228
+ *
229
+ * Unless required by applicable law or agreed to in writing, software
230
+ * distributed under the License is distributed on an "AS IS" BASIS,
231
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
232
+ * See the License for the specific language governing permissions and
233
+ * limitations under the License.
234
+ */
235
+ /**
236
+ * @internal
237
+ * Abstraction around AppCheck's token fetching capabilities.
238
+ */
239
+ var AppCheckTokenProvider = /** @class */ (function () {
240
+ function AppCheckTokenProvider(appName_, appCheckProvider) {
241
+ var _this = this;
242
+ this.appName_ = appName_;
243
+ this.appCheckProvider = appCheckProvider;
244
+ this.appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
245
+ if (!this.appCheck) {
246
+ void (appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.get().then(function (appCheck) { return (_this.appCheck = appCheck); }).catch());
247
+ }
248
+ }
249
+ AppCheckTokenProvider.prototype.getToken = function (forceRefresh) {
250
+ var _this = this;
251
+ if (!this.appCheck) {
252
+ return new Promise(function (resolve, reject) {
253
+ // Support delayed initialization of FirebaseAppCheck. This allows our
254
+ // customers to initialize the RTDB SDK before initializing Firebase
255
+ // AppCheck and ensures that all requests are authenticated if a token
256
+ // becomes available before the timoeout below expires.
257
+ setTimeout(function () {
258
+ if (_this.appCheck) {
259
+ _this.getToken(forceRefresh).then(resolve, reject);
260
+ }
261
+ else {
262
+ resolve(null);
263
+ }
264
+ }, 0);
265
+ });
266
+ }
267
+ return this.appCheck.getToken(forceRefresh);
268
+ };
269
+ AppCheckTokenProvider.prototype.addTokenChangeListener = function (listener) {
270
+ var _a;
271
+ void ((_a = this.appCheckProvider) === null || _a === void 0 ? void 0 : _a.get().then(function (appCheck) { return appCheck.addTokenListener(listener); }));
272
+ };
273
+ return AppCheckTokenProvider;
274
+ }());
212
275
 
213
276
  /**
214
277
  * @license
@@ -226,6 +289,7 @@ var version = "0.0.3-dataconnect-preview.d986d4bf2";
226
289
  * See the License for the specific language governing permissions and
227
290
  * limitations under the License.
228
291
  */
292
+ // @internal
229
293
  var FirebaseAuthProvider = /** @class */ (function () {
230
294
  function FirebaseAuthProvider(_appName, _options, _authProvider) {
231
295
  var _this = this;
@@ -514,7 +578,7 @@ function urlBuilder(projectConfig, transportOptions) {
514
578
  logError('Port type is of an invalid type');
515
579
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Incorrect type for port passed in!');
516
580
  }
517
- return "".concat(baseUrl, "/v1alpha/projects/").concat(project, "/locations/").concat(location, "/services/").concat(service, "/connectors/").concat(connector);
581
+ return "".concat(baseUrl, "/v1beta/projects/").concat(project, "/locations/").concat(location, "/services/").concat(service, "/connectors/").concat(connector);
518
582
  }
519
583
  function addToken(url, apiKey) {
520
584
  if (!apiKey) {
@@ -542,12 +606,14 @@ function addToken(url, apiKey) {
542
606
  * limitations under the License.
543
607
  */
544
608
  var RESTTransport = /** @class */ (function () {
545
- function RESTTransport(options, apiKey, authProvider, transportOptions, _isUsingGen) {
609
+ function RESTTransport(options, apiKey, appId, authProvider, appCheckProvider, transportOptions, _isUsingGen) {
546
610
  if (_isUsingGen === void 0) { _isUsingGen = false; }
547
611
  var _this = this;
548
- var _a;
612
+ var _a, _b;
549
613
  this.apiKey = apiKey;
614
+ this.appId = appId;
550
615
  this.authProvider = authProvider;
616
+ this.appCheckProvider = appCheckProvider;
551
617
  this._isUsingGen = _isUsingGen;
552
618
  this._host = '';
553
619
  this._location = 'l';
@@ -555,7 +621,7 @@ var RESTTransport = /** @class */ (function () {
555
621
  this._secure = true;
556
622
  this._project = 'p';
557
623
  this._accessToken = null;
558
- this._authInitialized = false;
624
+ this._appCheckToken = null;
559
625
  this._lastToken = null;
560
626
  // TODO(mtewani): Update U to include shape of body defined in line 13.
561
627
  this.invokeQuery = function (queryName, body) {
@@ -567,7 +633,7 @@ var RESTTransport = /** @class */ (function () {
567
633
  operationName: queryName,
568
634
  variables: body
569
635
  }, // TODO(mtewani): This is a patch, fix this.
570
- abortController, _this._accessToken, _this._isUsingGen);
636
+ abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
571
637
  });
572
638
  return {
573
639
  then: withAuth.then.bind(withAuth),
@@ -581,7 +647,7 @@ var RESTTransport = /** @class */ (function () {
581
647
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
582
648
  operationName: mutationName,
583
649
  variables: body
584
- }, abortController, _this._accessToken, _this._isUsingGen);
650
+ }, abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
585
651
  });
586
652
  return {
587
653
  then: taskResult.then.bind(taskResult),
@@ -615,6 +681,11 @@ var RESTTransport = /** @class */ (function () {
615
681
  logDebug("New Token Available: ".concat(token));
616
682
  _this._accessToken = token;
617
683
  });
684
+ (_b = this.appCheckProvider) === null || _b === void 0 ? void 0 : _b.addTokenChangeListener(function (result) {
685
+ var token = result.token;
686
+ logDebug("New App Check Token Available: ".concat(token));
687
+ _this._appCheckToken = token;
688
+ });
618
689
  }
619
690
  Object.defineProperty(RESTTransport.prototype, "endpointUrl", {
620
691
  get: function () {
@@ -641,28 +712,42 @@ var RESTTransport = /** @class */ (function () {
641
712
  this._accessToken = newToken;
642
713
  };
643
714
  RESTTransport.prototype.getWithAuth = function (forceToken) {
644
- var _this = this;
715
+ var _a;
645
716
  if (forceToken === void 0) { forceToken = false; }
646
- var starterPromise = new Promise(function (resolve) {
647
- return resolve(_this._accessToken);
717
+ return tslib.__awaiter(this, void 0, void 0, function () {
718
+ var starterPromise, _b;
719
+ var _this = this;
720
+ return tslib.__generator(this, function (_c) {
721
+ switch (_c.label) {
722
+ case 0:
723
+ starterPromise = new Promise(function (resolve) {
724
+ return resolve(_this._accessToken);
725
+ });
726
+ if (!this.appCheckProvider) return [3 /*break*/, 2];
727
+ _b = this;
728
+ return [4 /*yield*/, this.appCheckProvider.getToken()];
729
+ case 1:
730
+ _b._appCheckToken = (_a = (_c.sent())) === null || _a === void 0 ? void 0 : _a.token;
731
+ _c.label = 2;
732
+ case 2:
733
+ if (this.authProvider) {
734
+ starterPromise = this.authProvider
735
+ .getToken(/*forceToken=*/ forceToken)
736
+ .then(function (data) {
737
+ if (!data) {
738
+ return null;
739
+ }
740
+ _this._accessToken = data.accessToken;
741
+ return _this._accessToken;
742
+ });
743
+ }
744
+ else {
745
+ starterPromise = new Promise(function (resolve) { return resolve(''); });
746
+ }
747
+ return [2 /*return*/, starterPromise];
748
+ }
749
+ });
648
750
  });
649
- if (!this._authInitialized) {
650
- if (this.authProvider) {
651
- starterPromise = this.authProvider
652
- .getToken(/*forceToken=*/ forceToken)
653
- .then(function (data) {
654
- if (!data) {
655
- return null;
656
- }
657
- _this._accessToken = data.accessToken;
658
- return _this._accessToken;
659
- });
660
- }
661
- else {
662
- starterPromise = new Promise(function (resolve) { return resolve(''); });
663
- }
664
- }
665
- return starterPromise;
666
751
  };
667
752
  RESTTransport.prototype._setLastToken = function (lastToken) {
668
753
  this._lastToken = lastToken;
@@ -793,14 +878,16 @@ function parseOptions(fullHost) {
793
878
  * Class representing Firebase Data Connect
794
879
  */
795
880
  var DataConnect = /** @class */ (function () {
881
+ // @internal
796
882
  function DataConnect(app,
797
883
  // TODO(mtewani): Replace with _dataConnectOptions in the future
798
- dataConnectOptions, _authProvider) {
884
+ dataConnectOptions, _authProvider, _appCheckProvider) {
799
885
  this.app = app;
800
886
  this.dataConnectOptions = dataConnectOptions;
801
887
  this._authProvider = _authProvider;
888
+ this._appCheckProvider = _appCheckProvider;
802
889
  this.isEmulator = false;
803
- this.initialized = false;
890
+ this._initialized = false;
804
891
  this._isUsingGeneratedSdk = false;
805
892
  if (typeof process !== 'undefined' && process.env) {
806
893
  var host = process.env[FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR];
@@ -811,9 +898,7 @@ var DataConnect = /** @class */ (function () {
811
898
  }
812
899
  }
813
900
  }
814
- /*
815
- @internal
816
- */
901
+ // @internal
817
902
  DataConnect.prototype._useGeneratedSdk = function () {
818
903
  if (!this._isUsingGeneratedSdk) {
819
904
  this._isUsingGeneratedSdk = true;
@@ -823,13 +908,15 @@ var DataConnect = /** @class */ (function () {
823
908
  app._removeServiceInstance(this.app, 'data-connect', JSON.stringify(this.getSettings()));
824
909
  return Promise.resolve();
825
910
  };
911
+ // @internal
826
912
  DataConnect.prototype.getSettings = function () {
827
913
  var copy = JSON.parse(JSON.stringify(this.dataConnectOptions));
828
914
  delete copy.projectId;
829
915
  return copy;
830
916
  };
917
+ // @internal
831
918
  DataConnect.prototype.setInitialized = function () {
832
- if (this.initialized) {
919
+ if (this._initialized) {
833
920
  return;
834
921
  }
835
922
  if (this._transportClass === undefined) {
@@ -839,16 +926,20 @@ var DataConnect = /** @class */ (function () {
839
926
  if (this._authProvider) {
840
927
  this._authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this._authProvider);
841
928
  }
842
- this.initialized = true;
843
- this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider, undefined, this._isUsingGeneratedSdk);
929
+ if (this._appCheckProvider) {
930
+ this._appCheckTokenProvider = new AppCheckTokenProvider(this.app.name, this._appCheckProvider);
931
+ }
932
+ this._initialized = true;
933
+ this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this.app.options.appId, this._authTokenProvider, this._appCheckTokenProvider, undefined, this._isUsingGeneratedSdk);
844
934
  if (this._transportOptions) {
845
935
  this._transport.useEmulator(this._transportOptions.host, this._transportOptions.port, this._transportOptions.sslEnabled);
846
936
  }
847
937
  this._queryManager = new QueryManager(this._transport);
848
938
  this._mutationManager = new MutationManager(this._transport);
849
939
  };
940
+ // @internal
850
941
  DataConnect.prototype.enableEmulator = function (transportOptions) {
851
- if (this.initialized) {
942
+ if (this._initialized) {
852
943
  logError('enableEmulator called after initialization');
853
944
  throw new DataConnectError(Code.ALREADY_INITIALIZED, 'DataConnect instance already initialized!');
854
945
  }
@@ -935,6 +1026,7 @@ function registerDataConnect(variant) {
935
1026
  var settings = _a.instanceIdentifier, options = _a.options;
936
1027
  var app = container.getProvider('app').getImmediate();
937
1028
  var authProvider = container.getProvider('auth-internal');
1029
+ var appCheckProvider = container.getProvider('app-check-internal');
938
1030
  var newOpts = options;
939
1031
  if (settings) {
940
1032
  newOpts = JSON.parse(settings);
@@ -942,7 +1034,7 @@ function registerDataConnect(variant) {
942
1034
  if (!app.options.projectId) {
943
1035
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Project ID must be provided. Did you pass in a proper projectId to initializeApp?');
944
1036
  }
945
- return new DataConnect(app, tslib.__assign(tslib.__assign({}, newOpts), { projectId: app.options.projectId }), authProvider);
1037
+ return new DataConnect(app, tslib.__assign(tslib.__assign({}, newOpts), { projectId: app.options.projectId }), authProvider, appCheckProvider);
946
1038
  }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
947
1039
  app.registerVersion(name, version, variant);
948
1040
  // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
@@ -1119,8 +1211,6 @@ initializeFetch(fetch);
1119
1211
  registerDataConnect('node');
1120
1212
 
1121
1213
  exports.DataConnect = DataConnect;
1122
- exports.FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR = FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR;
1123
- exports.FirebaseAuthProvider = FirebaseAuthProvider;
1124
1214
  exports.MUTATION_STR = MUTATION_STR;
1125
1215
  exports.MutationManager = MutationManager;
1126
1216
  exports.QUERY_STR = QUERY_STR;