@firebase/data-connect 0.0.2-dataconnect-preview.877f8b7d0 → 0.0.3-canary.beaa4dffb

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.
Files changed (42) hide show
  1. package/dist/index.cjs.js +304 -54
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.esm2017.js +303 -53
  4. package/dist/index.esm2017.js.map +1 -1
  5. package/dist/index.esm5.js +323 -52
  6. package/dist/index.esm5.js.map +1 -1
  7. package/dist/index.node.cjs.js +324 -53
  8. package/dist/index.node.cjs.js.map +1 -1
  9. package/dist/internal.d.ts +186 -41
  10. package/dist/node-esm/index.node.esm.js +303 -53
  11. package/dist/node-esm/index.node.esm.js.map +1 -1
  12. package/dist/node-esm/src/api/DataConnect.d.ts +47 -3
  13. package/dist/node-esm/src/api/Mutation.d.ts +26 -1
  14. package/dist/node-esm/src/api/Reference.d.ts +6 -0
  15. package/dist/node-esm/src/api/index.d.ts +1 -0
  16. package/dist/node-esm/src/api/query.d.ts +51 -1
  17. package/dist/node-esm/src/api.browser.d.ts +12 -7
  18. package/dist/node-esm/src/core/AppCheckTokenProvider.d.ts +30 -0
  19. package/dist/node-esm/src/core/FirebaseAuthProvider.d.ts +1 -1
  20. package/dist/node-esm/src/core/QueryManager.d.ts +1 -1
  21. package/dist/node-esm/src/core/error.d.ts +2 -1
  22. package/dist/node-esm/src/network/fetch.d.ts +1 -1
  23. package/dist/node-esm/src/network/transport/index.d.ts +8 -10
  24. package/dist/node-esm/src/network/transport/rest.d.ts +24 -10
  25. package/dist/node-esm/src/util/validateArgs.d.ts +33 -0
  26. package/dist/private.d.ts +152 -59
  27. package/dist/public.d.ts +133 -50
  28. package/dist/src/api/DataConnect.d.ts +47 -3
  29. package/dist/src/api/Mutation.d.ts +26 -1
  30. package/dist/src/api/Reference.d.ts +6 -0
  31. package/dist/src/api/index.d.ts +1 -0
  32. package/dist/src/api/query.d.ts +51 -1
  33. package/dist/src/api.browser.d.ts +12 -7
  34. package/dist/src/core/AppCheckTokenProvider.d.ts +30 -0
  35. package/dist/src/core/FirebaseAuthProvider.d.ts +1 -1
  36. package/dist/src/core/QueryManager.d.ts +1 -1
  37. package/dist/src/core/error.d.ts +2 -1
  38. package/dist/src/network/fetch.d.ts +1 -1
  39. package/dist/src/network/transport/index.d.ts +8 -10
  40. package/dist/src/network/transport/rest.d.ts +24 -10
  41. package/dist/src/util/validateArgs.d.ts +33 -0
  42. package/package.json +11 -7
@@ -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.2-dataconnect-preview.877f8b7d0";
8
+ var version = "0.0.3-canary.beaa4dffb";
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
@@ -55,7 +112,8 @@ var Code = {
55
112
  NOT_INITIALIZED: 'not-initialized',
56
113
  NOT_SUPPORTED: 'not-supported',
57
114
  INVALID_ARGUMENT: 'invalid-argument',
58
- PARTIAL_ERROR: 'partial-error'
115
+ PARTIAL_ERROR: 'partial-error',
116
+ UNAUTHORIZED: 'unauthorized'
59
117
  };
60
118
  /** An error returned by a DataConnect operation. */
61
119
  var DataConnectError = /** @class */ (function (_super) {
@@ -125,6 +183,7 @@ function logError(msg) {
125
183
  * See the License for the specific language governing permissions and
126
184
  * limitations under the License.
127
185
  */
186
+ // @internal
128
187
  var FirebaseAuthProvider = /** @class */ (function () {
129
188
  function FirebaseAuthProvider(_appName, _options, _authProvider) {
130
189
  var _this = this;
@@ -169,7 +228,8 @@ var FirebaseAuthProvider = /** @class */ (function () {
169
228
  FirebaseAuthProvider.prototype.removeTokenChangeListener = function (listener) {
170
229
  this._authProvider
171
230
  .get()
172
- .then(function (auth) { return auth.removeAuthTokenListener(listener); });
231
+ .then(function (auth) { return auth.removeAuthTokenListener(listener); })
232
+ .catch(function (err) { return logError(err); });
173
233
  };
174
234
  return FirebaseAuthProvider;
175
235
  }());
@@ -412,7 +472,7 @@ function urlBuilder(projectConfig, transportOptions) {
412
472
  logError('Port type is of an invalid type');
413
473
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Incorrect type for port passed in!');
414
474
  }
415
- 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);
416
476
  }
417
477
  function addToken(url, apiKey) {
418
478
  if (!apiKey) {
@@ -440,18 +500,32 @@ function addToken(url, apiKey) {
440
500
  * limitations under the License.
441
501
  */
442
502
  var connectFetch = globalThis.fetch;
443
- function dcFetch(url, body, _a, accessToken) {
503
+ function getGoogApiClientValue(_isUsingGen) {
504
+ var str = 'gl-js/ fire/' + SDK_VERSION;
505
+ if (_isUsingGen) {
506
+ str += ' web/gen';
507
+ }
508
+ return str;
509
+ }
510
+ function dcFetch(url, body, _a, appId, accessToken, appCheckToken, _isUsingGen) {
444
511
  var _this = this;
445
512
  var signal = _a.signal;
446
513
  if (!connectFetch) {
447
514
  throw new DataConnectError(Code.OTHER, 'No Fetch Implementation detected!');
448
515
  }
449
516
  var headers = {
450
- 'Content-Type': 'application/json'
517
+ 'Content-Type': 'application/json',
518
+ 'X-Goog-Api-Client': getGoogApiClientValue(_isUsingGen)
451
519
  };
452
520
  if (accessToken) {
453
521
  headers['X-Firebase-Auth-Token'] = accessToken;
454
522
  }
523
+ if (appId) {
524
+ headers['x-firebase-gmpid'] = appId;
525
+ }
526
+ if (appCheckToken) {
527
+ headers['X-Firebase-AppCheck'] = appCheckToken;
528
+ }
455
529
  var bodyStr = JSON.stringify(body);
456
530
  logDebug("Making request out to ".concat(url, " with body: ").concat(bodyStr));
457
531
  return connectFetch(url, {
@@ -459,11 +533,12 @@ function dcFetch(url, body, _a, accessToken) {
459
533
  method: 'POST',
460
534
  headers: headers,
461
535
  signal: signal
462
- }).catch(function (err) {
463
- throw new DataConnectError(Code.OTHER, "Failed to fetch: " + JSON.stringify(err));
536
+ })
537
+ .catch(function (err) {
538
+ throw new DataConnectError(Code.OTHER, 'Failed to fetch: ' + JSON.stringify(err));
464
539
  })
465
540
  .then(function (response) { return __awaiter(_this, void 0, void 0, function () {
466
- var jsonResponse, e_1;
541
+ var jsonResponse, e_1, message;
467
542
  return __generator(this, function (_a) {
468
543
  switch (_a.label) {
469
544
  case 0:
@@ -479,9 +554,13 @@ function dcFetch(url, body, _a, accessToken) {
479
554
  e_1 = _a.sent();
480
555
  throw new DataConnectError(Code.OTHER, JSON.stringify(e_1));
481
556
  case 4:
557
+ message = getMessage(jsonResponse);
482
558
  if (response.status >= 400) {
483
559
  logError('Error while performing request: ' + JSON.stringify(jsonResponse));
484
- throw new DataConnectError(Code.OTHER, JSON.stringify(jsonResponse));
560
+ if (response.status === 401) {
561
+ throw new DataConnectError(Code.UNAUTHORIZED, message);
562
+ }
563
+ throw new DataConnectError(Code.OTHER, message);
485
564
  }
486
565
  return [2 /*return*/, jsonResponse];
487
566
  }
@@ -495,6 +574,12 @@ function dcFetch(url, body, _a, accessToken) {
495
574
  }
496
575
  return res;
497
576
  });
577
+ }
578
+ function getMessage(obj) {
579
+ if ('message' in obj) {
580
+ return obj.message;
581
+ }
582
+ return JSON.stringify(obj);
498
583
  }
499
584
 
500
585
  /**
@@ -514,42 +599,48 @@ function dcFetch(url, body, _a, accessToken) {
514
599
  * limitations under the License.
515
600
  */
516
601
  var RESTTransport = /** @class */ (function () {
517
- function RESTTransport(options, apiKey, authProvider, transportOptions) {
602
+ function RESTTransport(options, apiKey, appId, authProvider, appCheckProvider, transportOptions, _isUsingGen) {
603
+ if (_isUsingGen === void 0) { _isUsingGen = false; }
518
604
  var _this = this;
519
- var _a;
605
+ var _a, _b;
520
606
  this.apiKey = apiKey;
607
+ this.appId = appId;
521
608
  this.authProvider = authProvider;
609
+ this.appCheckProvider = appCheckProvider;
610
+ this._isUsingGen = _isUsingGen;
522
611
  this._host = '';
523
612
  this._location = 'l';
524
613
  this._connectorName = '';
525
614
  this._secure = true;
526
615
  this._project = 'p';
527
616
  this._accessToken = null;
528
- this._authInitialized = false;
617
+ this._appCheckToken = null;
618
+ this._lastToken = null;
529
619
  // TODO(mtewani): Update U to include shape of body defined in line 13.
530
620
  this.invokeQuery = function (queryName, body) {
531
621
  var abortController = new AbortController();
532
622
  // TODO(mtewani): Update to proper value
533
- var withAuth = _this.getWithAuth().then(function () {
623
+ var withAuth = _this.withRetry(function () {
534
624
  return dcFetch(addToken("".concat(_this.endpointUrl, ":executeQuery"), _this.apiKey), {
535
625
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
536
626
  operationName: queryName,
537
627
  variables: body
538
628
  }, // TODO(mtewani): This is a patch, fix this.
539
- abortController, _this._accessToken);
629
+ abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
540
630
  });
541
631
  return {
542
- then: withAuth.then.bind(withAuth)
632
+ then: withAuth.then.bind(withAuth),
633
+ catch: withAuth.catch.bind(withAuth)
543
634
  };
544
635
  };
545
636
  this.invokeMutation = function (mutationName, body) {
546
637
  var abortController = new AbortController();
547
- var taskResult = _this.getWithAuth().then(function () {
638
+ var taskResult = _this.withRetry(function () {
548
639
  return dcFetch(addToken("".concat(_this.endpointUrl, ":executeMutation"), _this.apiKey), {
549
640
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
550
641
  operationName: mutationName,
551
642
  variables: body
552
- }, abortController, _this._accessToken);
643
+ }, abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
553
644
  });
554
645
  return {
555
646
  then: taskResult.then.bind(taskResult),
@@ -583,6 +674,11 @@ var RESTTransport = /** @class */ (function () {
583
674
  logDebug("New Token Available: ".concat(token));
584
675
  _this._accessToken = token;
585
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
+ });
586
682
  }
587
683
  Object.defineProperty(RESTTransport.prototype, "endpointUrl", {
588
684
  get: function () {
@@ -608,28 +704,69 @@ var RESTTransport = /** @class */ (function () {
608
704
  RESTTransport.prototype.onTokenChanged = function (newToken) {
609
705
  this._accessToken = newToken;
610
706
  };
611
- RESTTransport.prototype.getWithAuth = function () {
612
- var _this = this;
613
- var starterPromise = new Promise(function (resolve) {
614
- return resolve(_this._accessToken);
707
+ RESTTransport.prototype.getWithAuth = function (forceToken) {
708
+ var _a;
709
+ if (forceToken === void 0) { forceToken = false; }
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
+ });
615
743
  });
616
- if (!this._authInitialized) {
617
- if (this.authProvider) {
618
- starterPromise = this.authProvider
619
- .getToken(/*forceToken=*/ false)
620
- .then(function (data) {
621
- if (!data) {
622
- return null;
623
- }
624
- _this._accessToken = data.accessToken;
625
- return _this._accessToken;
626
- });
627
- }
628
- else {
629
- starterPromise = new Promise(function (resolve) { return resolve(''); });
744
+ };
745
+ RESTTransport.prototype._setLastToken = function (lastToken) {
746
+ this._lastToken = lastToken;
747
+ };
748
+ RESTTransport.prototype.withRetry = function (promiseFactory, retry) {
749
+ var _this = this;
750
+ if (retry === void 0) { retry = false; }
751
+ var isNewToken = false;
752
+ return this.getWithAuth(retry)
753
+ .then(function (res) {
754
+ isNewToken = _this._lastToken !== res;
755
+ _this._lastToken = res;
756
+ return res;
757
+ })
758
+ .then(promiseFactory)
759
+ .catch(function (err) {
760
+ // Only retry if the result is unauthorized and the last token isn't the same as the new one.
761
+ if ('code' in err &&
762
+ err.code === Code.UNAUTHORIZED &&
763
+ !retry &&
764
+ isNewToken) {
765
+ logDebug('Retrying due to unauthorized');
766
+ return _this.withRetry(promiseFactory, true);
630
767
  }
631
- }
632
- return starterPromise;
768
+ throw err;
769
+ });
633
770
  };
634
771
  return RESTTransport;
635
772
  }());
@@ -650,16 +787,26 @@ var RESTTransport = /** @class */ (function () {
650
787
  * See the License for the specific language governing permissions and
651
788
  * limitations under the License.
652
789
  */
653
- function mutationRef(dcInstance, queryName, variables) {
790
+ /**
791
+ *
792
+ * @param dcInstance Data Connect instance
793
+ * @param mutationName name of mutation
794
+ * @param variables variables to send with mutation
795
+ * @returns `MutationRef`
796
+ */
797
+ function mutationRef(dcInstance, mutationName, variables) {
654
798
  dcInstance.setInitialized();
655
799
  var ref = {
656
800
  dataConnect: dcInstance,
657
- name: queryName,
801
+ name: mutationName,
658
802
  refType: MUTATION_STR,
659
803
  variables: variables
660
804
  };
661
805
  return ref;
662
806
  }
807
+ /**
808
+ * @internal
809
+ */
663
810
  var MutationManager = /** @class */ (function () {
664
811
  function MutationManager(_transport) {
665
812
  this._transport = _transport;
@@ -681,6 +828,11 @@ var MutationManager = /** @class */ (function () {
681
828
  };
682
829
  return MutationManager;
683
830
  }());
831
+ /**
832
+ * Execute Mutation
833
+ * @param mutationRef mutation to execute
834
+ * @returns `MutationRef`
835
+ */
684
836
  function executeMutation(mutationRef) {
685
837
  return mutationRef.dataConnect._mutationManager.executeMutation(mutationRef);
686
838
  }
@@ -715,15 +867,21 @@ function parseOptions(fullHost) {
715
867
  var port = Number(portAsString);
716
868
  return { host: host, port: port, sslEnabled: isSecure };
717
869
  }
870
+ /**
871
+ * Class representing Firebase Data Connect
872
+ */
718
873
  var DataConnect = /** @class */ (function () {
874
+ // @internal
719
875
  function DataConnect(app,
720
876
  // TODO(mtewani): Replace with _dataConnectOptions in the future
721
- dataConnectOptions, _authProvider) {
877
+ dataConnectOptions, _authProvider, _appCheckProvider) {
722
878
  this.app = app;
723
879
  this.dataConnectOptions = dataConnectOptions;
724
880
  this._authProvider = _authProvider;
881
+ this._appCheckProvider = _appCheckProvider;
725
882
  this.isEmulator = false;
726
- this.initialized = false;
883
+ this._initialized = false;
884
+ this._isUsingGeneratedSdk = false;
727
885
  if (typeof process !== 'undefined' && process.env) {
728
886
  var host = process.env[FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR];
729
887
  if (host) {
@@ -733,17 +891,25 @@ var DataConnect = /** @class */ (function () {
733
891
  }
734
892
  }
735
893
  }
894
+ // @internal
895
+ DataConnect.prototype._useGeneratedSdk = function () {
896
+ if (!this._isUsingGeneratedSdk) {
897
+ this._isUsingGeneratedSdk = true;
898
+ }
899
+ };
736
900
  DataConnect.prototype._delete = function () {
737
901
  _removeServiceInstance(this.app, 'data-connect', JSON.stringify(this.getSettings()));
738
902
  return Promise.resolve();
739
903
  };
904
+ // @internal
740
905
  DataConnect.prototype.getSettings = function () {
741
906
  var copy = JSON.parse(JSON.stringify(this.dataConnectOptions));
742
907
  delete copy.projectId;
743
908
  return copy;
744
909
  };
910
+ // @internal
745
911
  DataConnect.prototype.setInitialized = function () {
746
- if (this.initialized) {
912
+ if (this._initialized) {
747
913
  return;
748
914
  }
749
915
  if (this._transportClass === undefined) {
@@ -753,16 +919,20 @@ var DataConnect = /** @class */ (function () {
753
919
  if (this._authProvider) {
754
920
  this._authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this._authProvider);
755
921
  }
756
- this.initialized = true;
757
- this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider);
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);
758
927
  if (this._transportOptions) {
759
928
  this._transport.useEmulator(this._transportOptions.host, this._transportOptions.port, this._transportOptions.sslEnabled);
760
929
  }
761
930
  this._queryManager = new QueryManager(this._transport);
762
931
  this._mutationManager = new MutationManager(this._transport);
763
932
  };
933
+ // @internal
764
934
  DataConnect.prototype.enableEmulator = function (transportOptions) {
765
- if (this.initialized) {
935
+ if (this._initialized) {
766
936
  logError('enableEmulator called after initialization');
767
937
  throw new DataConnectError(Code.ALREADY_INITIALIZED, 'DataConnect instance already initialized!');
768
938
  }
@@ -771,6 +941,13 @@ var DataConnect = /** @class */ (function () {
771
941
  };
772
942
  return DataConnect;
773
943
  }());
944
+ /**
945
+ * Connect to the DataConnect Emulator
946
+ * @param dc Data Connect instance
947
+ * @param host host of emulator server
948
+ * @param port port of emulator server
949
+ * @param sslEnabled use https
950
+ */
774
951
  function connectDataConnectEmulator(dc, host, port, sslEnabled) {
775
952
  if (sslEnabled === void 0) { sslEnabled = false; }
776
953
  dc.enableEmulator({ host: host, port: port, sslEnabled: sslEnabled });
@@ -786,7 +963,7 @@ function getDataConnect(appOrOptions, optionalOptions) {
786
963
  dcOptions = optionalOptions;
787
964
  app = appOrOptions;
788
965
  }
789
- if (!app) {
966
+ if (!app || Object.keys(app).length === 0) {
790
967
  app = getApp();
791
968
  }
792
969
  var provider = _getProvider(app, 'data-connect');
@@ -800,9 +977,7 @@ function getDataConnect(appOrOptions, optionalOptions) {
800
977
  return dcInstance;
801
978
  }
802
979
  }
803
- if (!dcOptions) {
804
- throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
805
- }
980
+ validateDCOptions(dcOptions);
806
981
  logDebug('Creating new DataConnect instance');
807
982
  // Initialize with options.
808
983
  return provider.initialize({
@@ -810,6 +985,29 @@ function getDataConnect(appOrOptions, optionalOptions) {
810
985
  options: dcOptions
811
986
  });
812
987
  }
988
+ /**
989
+ *
990
+ * @param dcOptions
991
+ * @returns {void}
992
+ * @internal
993
+ */
994
+ function validateDCOptions(dcOptions) {
995
+ var fields = ['connector', 'location', 'service'];
996
+ if (!dcOptions) {
997
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
998
+ }
999
+ fields.forEach(function (field) {
1000
+ if (dcOptions[field] === null || dcOptions[field] === undefined) {
1001
+ throw new DataConnectError(Code.INVALID_ARGUMENT, "".concat(field, " Required"));
1002
+ }
1003
+ });
1004
+ return true;
1005
+ }
1006
+ /**
1007
+ * Delete DataConnect instance
1008
+ * @param dataConnect DataConnect instance
1009
+ * @returns
1010
+ */
813
1011
  function terminate(dataConnect) {
814
1012
  return dataConnect._delete();
815
1013
  // TODO(mtewani): Stop pending tasks
@@ -821,11 +1019,15 @@ function registerDataConnect(variant) {
821
1019
  var settings = _a.instanceIdentifier, options = _a.options;
822
1020
  var app = container.getProvider('app').getImmediate();
823
1021
  var authProvider = container.getProvider('auth-internal');
1022
+ var appCheckProvider = container.getProvider('app-check-internal');
824
1023
  var newOpts = options;
825
1024
  if (settings) {
826
1025
  newOpts = JSON.parse(settings);
827
1026
  }
828
- return new DataConnect(app, __assign(__assign({}, newOpts), { projectId: app.options.projectId }), authProvider);
1027
+ if (!app.options.projectId) {
1028
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'Project ID must be provided. Did you pass in a proper projectId to initializeApp?');
1029
+ }
1030
+ return new DataConnect(app, __assign(__assign({}, newOpts), { projectId: app.options.projectId }), authProvider, appCheckProvider);
829
1031
  }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
830
1032
  registerVersion(name, version, variant);
831
1033
  // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
@@ -848,9 +1050,22 @@ function registerDataConnect(variant) {
848
1050
  * See the License for the specific language governing permissions and
849
1051
  * limitations under the License.
850
1052
  */
1053
+ /**
1054
+ * Execute Query
1055
+ * @param queryRef query to execute.
1056
+ * @returns `QueryPromise`
1057
+ */
851
1058
  function executeQuery(queryRef) {
852
1059
  return queryRef.dataConnect._queryManager.executeQuery(queryRef);
853
1060
  }
1061
+ /**
1062
+ * Execute Query
1063
+ * @param dcInstance Data Connect instance to use.
1064
+ * @param queryName Query to execute
1065
+ * @param variables Variables to execute with
1066
+ * @param initialCache initial cache to use for client hydration
1067
+ * @returns `QueryRef`
1068
+ */
854
1069
  function queryRef(dcInstance, queryName, variables, initialCache) {
855
1070
  dcInstance.setInitialized();
856
1071
  dcInstance._queryManager.track(queryName, variables, initialCache);
@@ -861,6 +1076,11 @@ function queryRef(dcInstance, queryName, variables, initialCache) {
861
1076
  variables: variables
862
1077
  };
863
1078
  }
1079
+ /**
1080
+ * Converts serialized ref to query ref
1081
+ * @param serializedRef ref to convert to `QueryRef`
1082
+ * @returns `QueryRef`
1083
+ */
864
1084
  function toQueryRef(serializedRef) {
865
1085
  var _a = serializedRef.refInfo, name = _a.name, variables = _a.variables, connectorConfig = _a.connectorConfig;
866
1086
  return queryRef(getDataConnect(connectorConfig), name, variables);
@@ -882,6 +1102,57 @@ function toQueryRef(serializedRef) {
882
1102
  * See the License for the specific language governing permissions and
883
1103
  * limitations under the License.
884
1104
  */
1105
+ /**
1106
+ * The generated SDK will allow the user to pass in either the variable or the data connect instance with the variable,
1107
+ * and this function validates the variables and returns back the DataConnect instance and variables based on the arguments passed in.
1108
+ * @param connectorConfig
1109
+ * @param dcOrVars
1110
+ * @param vars
1111
+ * @param validateVars
1112
+ * @returns {DataConnect} and {Variables} instance
1113
+ * @internal
1114
+ */
1115
+ function validateArgs(connectorConfig, dcOrVars, vars, validateVars) {
1116
+ var dcInstance;
1117
+ var realVars;
1118
+ if (dcOrVars && 'enableEmulator' in dcOrVars) {
1119
+ dcInstance = dcOrVars;
1120
+ realVars = vars;
1121
+ }
1122
+ else {
1123
+ dcInstance = getDataConnect(connectorConfig);
1124
+ realVars = dcOrVars;
1125
+ }
1126
+ if (!dcInstance || (!realVars && validateVars)) {
1127
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'Variables required.');
1128
+ }
1129
+ return { dc: dcInstance, vars: realVars };
1130
+ }
1131
+
1132
+ /**
1133
+ * @license
1134
+ * Copyright 2024 Google LLC
1135
+ *
1136
+ * Licensed under the Apache License, Version 2.0 (the "License");
1137
+ * you may not use this file except in compliance with the License.
1138
+ * You may obtain a copy of the License at
1139
+ *
1140
+ * http://www.apache.org/licenses/LICENSE-2.0
1141
+ *
1142
+ * Unless required by applicable law or agreed to in writing, software
1143
+ * distributed under the License is distributed on an "AS IS" BASIS,
1144
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1145
+ * See the License for the specific language governing permissions and
1146
+ * limitations under the License.
1147
+ */
1148
+ /**
1149
+ * Subscribe to a `QueryRef`
1150
+ * @param queryRefOrSerializedResult query ref or serialized result.
1151
+ * @param observerOrOnNext observer object or next function.
1152
+ * @param onError Callback to call when error gets thrown.
1153
+ * @param onComplete Called when subscription completes.
1154
+ * @returns `SubscriptionOptions`
1155
+ */
885
1156
  function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComplete) {
886
1157
  var ref;
887
1158
  var initialCache;
@@ -920,5 +1191,5 @@ function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComp
920
1191
  */
921
1192
  registerDataConnect();
922
1193
 
923
- 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 };
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 };
924
1195
  //# sourceMappingURL=index.esm5.js.map