@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
@@ -30,7 +30,8 @@ var Code = {
30
30
  NOT_INITIALIZED: 'not-initialized',
31
31
  NOT_SUPPORTED: 'not-supported',
32
32
  INVALID_ARGUMENT: 'invalid-argument',
33
- PARTIAL_ERROR: 'partial-error'
33
+ PARTIAL_ERROR: 'partial-error',
34
+ UNAUTHORIZED: 'unauthorized'
34
35
  };
35
36
  /** An error returned by a DataConnect operation. */
36
37
  var DataConnectError = /** @class */ (function (_super) {
@@ -130,18 +131,32 @@ var connectFetch = globalThis.fetch;
130
131
  function initializeFetch(fetchImpl) {
131
132
  connectFetch = fetchImpl;
132
133
  }
133
- function dcFetch(url, body, _a, accessToken) {
134
+ function getGoogApiClientValue(_isUsingGen) {
135
+ var str = 'gl-js/ fire/' + SDK_VERSION;
136
+ if (_isUsingGen) {
137
+ str += ' web/gen';
138
+ }
139
+ return str;
140
+ }
141
+ function dcFetch(url, body, _a, appId, accessToken, appCheckToken, _isUsingGen) {
134
142
  var _this = this;
135
143
  var signal = _a.signal;
136
144
  if (!connectFetch) {
137
145
  throw new DataConnectError(Code.OTHER, 'No Fetch Implementation detected!');
138
146
  }
139
147
  var headers = {
140
- 'Content-Type': 'application/json'
148
+ 'Content-Type': 'application/json',
149
+ 'X-Goog-Api-Client': getGoogApiClientValue(_isUsingGen)
141
150
  };
142
151
  if (accessToken) {
143
152
  headers['X-Firebase-Auth-Token'] = accessToken;
144
153
  }
154
+ if (appId) {
155
+ headers['x-firebase-gmpid'] = appId;
156
+ }
157
+ if (appCheckToken) {
158
+ headers['X-Firebase-AppCheck'] = appCheckToken;
159
+ }
145
160
  var bodyStr = JSON.stringify(body);
146
161
  logDebug("Making request out to ".concat(url, " with body: ").concat(bodyStr));
147
162
  return connectFetch(url, {
@@ -149,11 +164,12 @@ function dcFetch(url, body, _a, accessToken) {
149
164
  method: 'POST',
150
165
  headers: headers,
151
166
  signal: signal
152
- }).catch(function (err) {
153
- throw new DataConnectError(Code.OTHER, "Failed to fetch: " + JSON.stringify(err));
167
+ })
168
+ .catch(function (err) {
169
+ throw new DataConnectError(Code.OTHER, 'Failed to fetch: ' + JSON.stringify(err));
154
170
  })
155
171
  .then(function (response) { return tslib.__awaiter(_this, void 0, void 0, function () {
156
- var jsonResponse, e_1;
172
+ var jsonResponse, e_1, message;
157
173
  return tslib.__generator(this, function (_a) {
158
174
  switch (_a.label) {
159
175
  case 0:
@@ -169,9 +185,13 @@ function dcFetch(url, body, _a, accessToken) {
169
185
  e_1 = _a.sent();
170
186
  throw new DataConnectError(Code.OTHER, JSON.stringify(e_1));
171
187
  case 4:
188
+ message = getMessage(jsonResponse);
172
189
  if (response.status >= 400) {
173
190
  logError('Error while performing request: ' + JSON.stringify(jsonResponse));
174
- throw new DataConnectError(Code.OTHER, JSON.stringify(jsonResponse));
191
+ if (response.status === 401) {
192
+ throw new DataConnectError(Code.UNAUTHORIZED, message);
193
+ }
194
+ throw new DataConnectError(Code.OTHER, message);
175
195
  }
176
196
  return [2 /*return*/, jsonResponse];
177
197
  }
@@ -185,10 +205,16 @@ function dcFetch(url, body, _a, accessToken) {
185
205
  }
186
206
  return res;
187
207
  });
208
+ }
209
+ function getMessage(obj) {
210
+ if ('message' in obj) {
211
+ return obj.message;
212
+ }
213
+ return JSON.stringify(obj);
188
214
  }
189
215
 
190
216
  var name = "@firebase/data-connect";
191
- var version = "0.0.2-dataconnect-preview.877f8b7d0";
217
+ var version = "0.0.3-canary.beaa4dffb";
192
218
 
193
219
  /**
194
220
  * @license
@@ -206,6 +232,64 @@ var version = "0.0.2-dataconnect-preview.877f8b7d0";
206
232
  * See the License for the specific language governing permissions and
207
233
  * limitations under the License.
208
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
+ }());
275
+
276
+ /**
277
+ * @license
278
+ * Copyright 2024 Google LLC
279
+ *
280
+ * Licensed under the Apache License, Version 2.0 (the "License");
281
+ * you may not use this file except in compliance with the License.
282
+ * You may obtain a copy of the License at
283
+ *
284
+ * http://www.apache.org/licenses/LICENSE-2.0
285
+ *
286
+ * Unless required by applicable law or agreed to in writing, software
287
+ * distributed under the License is distributed on an "AS IS" BASIS,
288
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
289
+ * See the License for the specific language governing permissions and
290
+ * limitations under the License.
291
+ */
292
+ // @internal
209
293
  var FirebaseAuthProvider = /** @class */ (function () {
210
294
  function FirebaseAuthProvider(_appName, _options, _authProvider) {
211
295
  var _this = this;
@@ -250,7 +334,8 @@ var FirebaseAuthProvider = /** @class */ (function () {
250
334
  FirebaseAuthProvider.prototype.removeTokenChangeListener = function (listener) {
251
335
  this._authProvider
252
336
  .get()
253
- .then(function (auth) { return auth.removeAuthTokenListener(listener); });
337
+ .then(function (auth) { return auth.removeAuthTokenListener(listener); })
338
+ .catch(function (err) { return logError(err); });
254
339
  };
255
340
  return FirebaseAuthProvider;
256
341
  }());
@@ -493,7 +578,7 @@ function urlBuilder(projectConfig, transportOptions) {
493
578
  logError('Port type is of an invalid type');
494
579
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Incorrect type for port passed in!');
495
580
  }
496
- 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);
497
582
  }
498
583
  function addToken(url, apiKey) {
499
584
  if (!apiKey) {
@@ -521,42 +606,48 @@ function addToken(url, apiKey) {
521
606
  * limitations under the License.
522
607
  */
523
608
  var RESTTransport = /** @class */ (function () {
524
- function RESTTransport(options, apiKey, authProvider, transportOptions) {
609
+ function RESTTransport(options, apiKey, appId, authProvider, appCheckProvider, transportOptions, _isUsingGen) {
610
+ if (_isUsingGen === void 0) { _isUsingGen = false; }
525
611
  var _this = this;
526
- var _a;
612
+ var _a, _b;
527
613
  this.apiKey = apiKey;
614
+ this.appId = appId;
528
615
  this.authProvider = authProvider;
616
+ this.appCheckProvider = appCheckProvider;
617
+ this._isUsingGen = _isUsingGen;
529
618
  this._host = '';
530
619
  this._location = 'l';
531
620
  this._connectorName = '';
532
621
  this._secure = true;
533
622
  this._project = 'p';
534
623
  this._accessToken = null;
535
- this._authInitialized = false;
624
+ this._appCheckToken = null;
625
+ this._lastToken = null;
536
626
  // TODO(mtewani): Update U to include shape of body defined in line 13.
537
627
  this.invokeQuery = function (queryName, body) {
538
628
  var abortController = new AbortController();
539
629
  // TODO(mtewani): Update to proper value
540
- var withAuth = _this.getWithAuth().then(function () {
630
+ var withAuth = _this.withRetry(function () {
541
631
  return dcFetch(addToken("".concat(_this.endpointUrl, ":executeQuery"), _this.apiKey), {
542
632
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
543
633
  operationName: queryName,
544
634
  variables: body
545
635
  }, // TODO(mtewani): This is a patch, fix this.
546
- abortController, _this._accessToken);
636
+ abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
547
637
  });
548
638
  return {
549
- then: withAuth.then.bind(withAuth)
639
+ then: withAuth.then.bind(withAuth),
640
+ catch: withAuth.catch.bind(withAuth)
550
641
  };
551
642
  };
552
643
  this.invokeMutation = function (mutationName, body) {
553
644
  var abortController = new AbortController();
554
- var taskResult = _this.getWithAuth().then(function () {
645
+ var taskResult = _this.withRetry(function () {
555
646
  return dcFetch(addToken("".concat(_this.endpointUrl, ":executeMutation"), _this.apiKey), {
556
647
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
557
648
  operationName: mutationName,
558
649
  variables: body
559
- }, abortController, _this._accessToken);
650
+ }, abortController, _this.appId, _this._accessToken, _this._appCheckToken, _this._isUsingGen);
560
651
  });
561
652
  return {
562
653
  then: taskResult.then.bind(taskResult),
@@ -590,6 +681,11 @@ var RESTTransport = /** @class */ (function () {
590
681
  logDebug("New Token Available: ".concat(token));
591
682
  _this._accessToken = token;
592
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
+ });
593
689
  }
594
690
  Object.defineProperty(RESTTransport.prototype, "endpointUrl", {
595
691
  get: function () {
@@ -615,28 +711,69 @@ var RESTTransport = /** @class */ (function () {
615
711
  RESTTransport.prototype.onTokenChanged = function (newToken) {
616
712
  this._accessToken = newToken;
617
713
  };
618
- RESTTransport.prototype.getWithAuth = function () {
619
- var _this = this;
620
- var starterPromise = new Promise(function (resolve) {
621
- return resolve(_this._accessToken);
714
+ RESTTransport.prototype.getWithAuth = function (forceToken) {
715
+ var _a;
716
+ if (forceToken === void 0) { forceToken = false; }
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
+ });
622
750
  });
623
- if (!this._authInitialized) {
624
- if (this.authProvider) {
625
- starterPromise = this.authProvider
626
- .getToken(/*forceToken=*/ false)
627
- .then(function (data) {
628
- if (!data) {
629
- return null;
630
- }
631
- _this._accessToken = data.accessToken;
632
- return _this._accessToken;
633
- });
634
- }
635
- else {
636
- starterPromise = new Promise(function (resolve) { return resolve(''); });
751
+ };
752
+ RESTTransport.prototype._setLastToken = function (lastToken) {
753
+ this._lastToken = lastToken;
754
+ };
755
+ RESTTransport.prototype.withRetry = function (promiseFactory, retry) {
756
+ var _this = this;
757
+ if (retry === void 0) { retry = false; }
758
+ var isNewToken = false;
759
+ return this.getWithAuth(retry)
760
+ .then(function (res) {
761
+ isNewToken = _this._lastToken !== res;
762
+ _this._lastToken = res;
763
+ return res;
764
+ })
765
+ .then(promiseFactory)
766
+ .catch(function (err) {
767
+ // Only retry if the result is unauthorized and the last token isn't the same as the new one.
768
+ if ('code' in err &&
769
+ err.code === Code.UNAUTHORIZED &&
770
+ !retry &&
771
+ isNewToken) {
772
+ logDebug('Retrying due to unauthorized');
773
+ return _this.withRetry(promiseFactory, true);
637
774
  }
638
- }
639
- return starterPromise;
775
+ throw err;
776
+ });
640
777
  };
641
778
  return RESTTransport;
642
779
  }());
@@ -657,16 +794,26 @@ var RESTTransport = /** @class */ (function () {
657
794
  * See the License for the specific language governing permissions and
658
795
  * limitations under the License.
659
796
  */
660
- function mutationRef(dcInstance, queryName, variables) {
797
+ /**
798
+ *
799
+ * @param dcInstance Data Connect instance
800
+ * @param mutationName name of mutation
801
+ * @param variables variables to send with mutation
802
+ * @returns `MutationRef`
803
+ */
804
+ function mutationRef(dcInstance, mutationName, variables) {
661
805
  dcInstance.setInitialized();
662
806
  var ref = {
663
807
  dataConnect: dcInstance,
664
- name: queryName,
808
+ name: mutationName,
665
809
  refType: MUTATION_STR,
666
810
  variables: variables
667
811
  };
668
812
  return ref;
669
813
  }
814
+ /**
815
+ * @internal
816
+ */
670
817
  var MutationManager = /** @class */ (function () {
671
818
  function MutationManager(_transport) {
672
819
  this._transport = _transport;
@@ -688,6 +835,11 @@ var MutationManager = /** @class */ (function () {
688
835
  };
689
836
  return MutationManager;
690
837
  }());
838
+ /**
839
+ * Execute Mutation
840
+ * @param mutationRef mutation to execute
841
+ * @returns `MutationRef`
842
+ */
691
843
  function executeMutation(mutationRef) {
692
844
  return mutationRef.dataConnect._mutationManager.executeMutation(mutationRef);
693
845
  }
@@ -722,15 +874,21 @@ function parseOptions(fullHost) {
722
874
  var port = Number(portAsString);
723
875
  return { host: host, port: port, sslEnabled: isSecure };
724
876
  }
877
+ /**
878
+ * Class representing Firebase Data Connect
879
+ */
725
880
  var DataConnect = /** @class */ (function () {
881
+ // @internal
726
882
  function DataConnect(app,
727
883
  // TODO(mtewani): Replace with _dataConnectOptions in the future
728
- dataConnectOptions, _authProvider) {
884
+ dataConnectOptions, _authProvider, _appCheckProvider) {
729
885
  this.app = app;
730
886
  this.dataConnectOptions = dataConnectOptions;
731
887
  this._authProvider = _authProvider;
888
+ this._appCheckProvider = _appCheckProvider;
732
889
  this.isEmulator = false;
733
- this.initialized = false;
890
+ this._initialized = false;
891
+ this._isUsingGeneratedSdk = false;
734
892
  if (typeof process !== 'undefined' && process.env) {
735
893
  var host = process.env[FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR];
736
894
  if (host) {
@@ -740,17 +898,25 @@ var DataConnect = /** @class */ (function () {
740
898
  }
741
899
  }
742
900
  }
901
+ // @internal
902
+ DataConnect.prototype._useGeneratedSdk = function () {
903
+ if (!this._isUsingGeneratedSdk) {
904
+ this._isUsingGeneratedSdk = true;
905
+ }
906
+ };
743
907
  DataConnect.prototype._delete = function () {
744
908
  app._removeServiceInstance(this.app, 'data-connect', JSON.stringify(this.getSettings()));
745
909
  return Promise.resolve();
746
910
  };
911
+ // @internal
747
912
  DataConnect.prototype.getSettings = function () {
748
913
  var copy = JSON.parse(JSON.stringify(this.dataConnectOptions));
749
914
  delete copy.projectId;
750
915
  return copy;
751
916
  };
917
+ // @internal
752
918
  DataConnect.prototype.setInitialized = function () {
753
- if (this.initialized) {
919
+ if (this._initialized) {
754
920
  return;
755
921
  }
756
922
  if (this._transportClass === undefined) {
@@ -760,16 +926,20 @@ var DataConnect = /** @class */ (function () {
760
926
  if (this._authProvider) {
761
927
  this._authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this._authProvider);
762
928
  }
763
- this.initialized = true;
764
- this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider);
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);
765
934
  if (this._transportOptions) {
766
935
  this._transport.useEmulator(this._transportOptions.host, this._transportOptions.port, this._transportOptions.sslEnabled);
767
936
  }
768
937
  this._queryManager = new QueryManager(this._transport);
769
938
  this._mutationManager = new MutationManager(this._transport);
770
939
  };
940
+ // @internal
771
941
  DataConnect.prototype.enableEmulator = function (transportOptions) {
772
- if (this.initialized) {
942
+ if (this._initialized) {
773
943
  logError('enableEmulator called after initialization');
774
944
  throw new DataConnectError(Code.ALREADY_INITIALIZED, 'DataConnect instance already initialized!');
775
945
  }
@@ -778,6 +948,13 @@ var DataConnect = /** @class */ (function () {
778
948
  };
779
949
  return DataConnect;
780
950
  }());
951
+ /**
952
+ * Connect to the DataConnect Emulator
953
+ * @param dc Data Connect instance
954
+ * @param host host of emulator server
955
+ * @param port port of emulator server
956
+ * @param sslEnabled use https
957
+ */
781
958
  function connectDataConnectEmulator(dc, host, port, sslEnabled) {
782
959
  if (sslEnabled === void 0) { sslEnabled = false; }
783
960
  dc.enableEmulator({ host: host, port: port, sslEnabled: sslEnabled });
@@ -793,7 +970,7 @@ function getDataConnect(appOrOptions, optionalOptions) {
793
970
  dcOptions = optionalOptions;
794
971
  app$1 = appOrOptions;
795
972
  }
796
- if (!app$1) {
973
+ if (!app$1 || Object.keys(app$1).length === 0) {
797
974
  app$1 = app.getApp();
798
975
  }
799
976
  var provider = app._getProvider(app$1, 'data-connect');
@@ -807,9 +984,7 @@ function getDataConnect(appOrOptions, optionalOptions) {
807
984
  return dcInstance;
808
985
  }
809
986
  }
810
- if (!dcOptions) {
811
- throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
812
- }
987
+ validateDCOptions(dcOptions);
813
988
  logDebug('Creating new DataConnect instance');
814
989
  // Initialize with options.
815
990
  return provider.initialize({
@@ -817,6 +992,29 @@ function getDataConnect(appOrOptions, optionalOptions) {
817
992
  options: dcOptions
818
993
  });
819
994
  }
995
+ /**
996
+ *
997
+ * @param dcOptions
998
+ * @returns {void}
999
+ * @internal
1000
+ */
1001
+ function validateDCOptions(dcOptions) {
1002
+ var fields = ['connector', 'location', 'service'];
1003
+ if (!dcOptions) {
1004
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
1005
+ }
1006
+ fields.forEach(function (field) {
1007
+ if (dcOptions[field] === null || dcOptions[field] === undefined) {
1008
+ throw new DataConnectError(Code.INVALID_ARGUMENT, "".concat(field, " Required"));
1009
+ }
1010
+ });
1011
+ return true;
1012
+ }
1013
+ /**
1014
+ * Delete DataConnect instance
1015
+ * @param dataConnect DataConnect instance
1016
+ * @returns
1017
+ */
820
1018
  function terminate(dataConnect) {
821
1019
  return dataConnect._delete();
822
1020
  // TODO(mtewani): Stop pending tasks
@@ -828,11 +1026,15 @@ function registerDataConnect(variant) {
828
1026
  var settings = _a.instanceIdentifier, options = _a.options;
829
1027
  var app = container.getProvider('app').getImmediate();
830
1028
  var authProvider = container.getProvider('auth-internal');
1029
+ var appCheckProvider = container.getProvider('app-check-internal');
831
1030
  var newOpts = options;
832
1031
  if (settings) {
833
1032
  newOpts = JSON.parse(settings);
834
1033
  }
835
- return new DataConnect(app, tslib.__assign(tslib.__assign({}, newOpts), { projectId: app.options.projectId }), authProvider);
1034
+ if (!app.options.projectId) {
1035
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'Project ID must be provided. Did you pass in a proper projectId to initializeApp?');
1036
+ }
1037
+ return new DataConnect(app, tslib.__assign(tslib.__assign({}, newOpts), { projectId: app.options.projectId }), authProvider, appCheckProvider);
836
1038
  }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
837
1039
  app.registerVersion(name, version, variant);
838
1040
  // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
@@ -855,9 +1057,22 @@ function registerDataConnect(variant) {
855
1057
  * See the License for the specific language governing permissions and
856
1058
  * limitations under the License.
857
1059
  */
1060
+ /**
1061
+ * Execute Query
1062
+ * @param queryRef query to execute.
1063
+ * @returns `QueryPromise`
1064
+ */
858
1065
  function executeQuery(queryRef) {
859
1066
  return queryRef.dataConnect._queryManager.executeQuery(queryRef);
860
1067
  }
1068
+ /**
1069
+ * Execute Query
1070
+ * @param dcInstance Data Connect instance to use.
1071
+ * @param queryName Query to execute
1072
+ * @param variables Variables to execute with
1073
+ * @param initialCache initial cache to use for client hydration
1074
+ * @returns `QueryRef`
1075
+ */
861
1076
  function queryRef(dcInstance, queryName, variables, initialCache) {
862
1077
  dcInstance.setInitialized();
863
1078
  dcInstance._queryManager.track(queryName, variables, initialCache);
@@ -868,6 +1083,11 @@ function queryRef(dcInstance, queryName, variables, initialCache) {
868
1083
  variables: variables
869
1084
  };
870
1085
  }
1086
+ /**
1087
+ * Converts serialized ref to query ref
1088
+ * @param serializedRef ref to convert to `QueryRef`
1089
+ * @returns `QueryRef`
1090
+ */
871
1091
  function toQueryRef(serializedRef) {
872
1092
  var _a = serializedRef.refInfo, name = _a.name, variables = _a.variables, connectorConfig = _a.connectorConfig;
873
1093
  return queryRef(getDataConnect(connectorConfig), name, variables);
@@ -889,6 +1109,57 @@ function toQueryRef(serializedRef) {
889
1109
  * See the License for the specific language governing permissions and
890
1110
  * limitations under the License.
891
1111
  */
1112
+ /**
1113
+ * The generated SDK will allow the user to pass in either the variable or the data connect instance with the variable,
1114
+ * and this function validates the variables and returns back the DataConnect instance and variables based on the arguments passed in.
1115
+ * @param connectorConfig
1116
+ * @param dcOrVars
1117
+ * @param vars
1118
+ * @param validateVars
1119
+ * @returns {DataConnect} and {Variables} instance
1120
+ * @internal
1121
+ */
1122
+ function validateArgs(connectorConfig, dcOrVars, vars, validateVars) {
1123
+ var dcInstance;
1124
+ var realVars;
1125
+ if (dcOrVars && 'enableEmulator' in dcOrVars) {
1126
+ dcInstance = dcOrVars;
1127
+ realVars = vars;
1128
+ }
1129
+ else {
1130
+ dcInstance = getDataConnect(connectorConfig);
1131
+ realVars = dcOrVars;
1132
+ }
1133
+ if (!dcInstance || (!realVars && validateVars)) {
1134
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'Variables required.');
1135
+ }
1136
+ return { dc: dcInstance, vars: realVars };
1137
+ }
1138
+
1139
+ /**
1140
+ * @license
1141
+ * Copyright 2024 Google LLC
1142
+ *
1143
+ * Licensed under the Apache License, Version 2.0 (the "License");
1144
+ * you may not use this file except in compliance with the License.
1145
+ * You may obtain a copy of the License at
1146
+ *
1147
+ * http://www.apache.org/licenses/LICENSE-2.0
1148
+ *
1149
+ * Unless required by applicable law or agreed to in writing, software
1150
+ * distributed under the License is distributed on an "AS IS" BASIS,
1151
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1152
+ * See the License for the specific language governing permissions and
1153
+ * limitations under the License.
1154
+ */
1155
+ /**
1156
+ * Subscribe to a `QueryRef`
1157
+ * @param queryRefOrSerializedResult query ref or serialized result.
1158
+ * @param observerOrOnNext observer object or next function.
1159
+ * @param onError Callback to call when error gets thrown.
1160
+ * @param onComplete Called when subscription completes.
1161
+ * @returns `SubscriptionOptions`
1162
+ */
892
1163
  function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComplete) {
893
1164
  var ref;
894
1165
  var initialCache;
@@ -940,8 +1211,6 @@ initializeFetch(fetch);
940
1211
  registerDataConnect('node');
941
1212
 
942
1213
  exports.DataConnect = DataConnect;
943
- exports.FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR = FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR;
944
- exports.FirebaseAuthProvider = FirebaseAuthProvider;
945
1214
  exports.MUTATION_STR = MUTATION_STR;
946
1215
  exports.MutationManager = MutationManager;
947
1216
  exports.QUERY_STR = QUERY_STR;
@@ -958,4 +1227,6 @@ exports.setLogLevel = setLogLevel;
958
1227
  exports.subscribe = subscribe;
959
1228
  exports.terminate = terminate;
960
1229
  exports.toQueryRef = toQueryRef;
1230
+ exports.validateArgs = validateArgs;
1231
+ exports.validateDCOptions = validateDCOptions;
961
1232
  //# sourceMappingURL=index.node.cjs.js.map