@firebase/data-connect 0.0.2-dataconnect-preview.388b61c7e → 0.0.3-dataconnect-preview.d986d4bf2

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 (34) hide show
  1. package/dist/index.cjs.js +146 -27
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.esm2017.js +145 -28
  4. package/dist/index.esm2017.js.map +1 -1
  5. package/dist/index.esm5.js +146 -23
  6. package/dist/index.esm5.js.map +1 -1
  7. package/dist/index.node.cjs.js +147 -22
  8. package/dist/index.node.cjs.js.map +1 -1
  9. package/dist/internal.d.ts +32 -5
  10. package/dist/node-esm/index.node.esm.js +145 -28
  11. package/dist/node-esm/index.node.esm.js.map +1 -1
  12. package/dist/node-esm/src/api/DataConnect.d.ts +9 -0
  13. package/dist/node-esm/src/api/index.d.ts +1 -0
  14. package/dist/node-esm/src/api/query.d.ts +1 -1
  15. package/dist/node-esm/src/core/FirebaseAuthProvider.d.ts +1 -1
  16. package/dist/node-esm/src/core/QueryManager.d.ts +1 -1
  17. package/dist/node-esm/src/core/error.d.ts +2 -1
  18. package/dist/node-esm/src/network/fetch.d.ts +1 -1
  19. package/dist/node-esm/src/network/transport/index.d.ts +1 -1
  20. package/dist/node-esm/src/network/transport/rest.d.ts +20 -9
  21. package/dist/node-esm/src/util/validateArgs.d.ts +33 -0
  22. package/dist/private.d.ts +16 -5
  23. package/dist/public.d.ts +5 -3
  24. package/dist/src/api/DataConnect.d.ts +9 -0
  25. package/dist/src/api/index.d.ts +1 -0
  26. package/dist/src/api/query.d.ts +1 -1
  27. package/dist/src/core/FirebaseAuthProvider.d.ts +1 -1
  28. package/dist/src/core/QueryManager.d.ts +1 -1
  29. package/dist/src/core/error.d.ts +2 -1
  30. package/dist/src/network/fetch.d.ts +1 -1
  31. package/dist/src/network/transport/index.d.ts +1 -1
  32. package/dist/src/network/transport/rest.d.ts +20 -9
  33. package/dist/src/util/validateArgs.d.ts +33 -0
  34. package/package.json +10 -6
@@ -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.388b61c7e";
8
+ var version = "0.0.3-dataconnect-preview.d986d4bf2";
9
9
 
10
10
  /**
11
11
  * @license
@@ -55,7 +55,8 @@ var Code = {
55
55
  NOT_INITIALIZED: 'not-initialized',
56
56
  NOT_SUPPORTED: 'not-supported',
57
57
  INVALID_ARGUMENT: 'invalid-argument',
58
- PARTIAL_ERROR: 'partial-error'
58
+ PARTIAL_ERROR: 'partial-error',
59
+ UNAUTHORIZED: 'unauthorized'
59
60
  };
60
61
  /** An error returned by a DataConnect operation. */
61
62
  var DataConnectError = /** @class */ (function (_super) {
@@ -169,7 +170,8 @@ var FirebaseAuthProvider = /** @class */ (function () {
169
170
  FirebaseAuthProvider.prototype.removeTokenChangeListener = function (listener) {
170
171
  this._authProvider
171
172
  .get()
172
- .then(function (auth) { return auth.removeAuthTokenListener(listener); });
173
+ .then(function (auth) { return auth.removeAuthTokenListener(listener); })
174
+ .catch(function (err) { return logError(err); });
173
175
  };
174
176
  return FirebaseAuthProvider;
175
177
  }());
@@ -440,14 +442,22 @@ function addToken(url, apiKey) {
440
442
  * limitations under the License.
441
443
  */
442
444
  var connectFetch = globalThis.fetch;
443
- function dcFetch(url, body, _a, accessToken) {
445
+ function getGoogApiClientValue(_isUsingGen) {
446
+ var str = 'gl-js/ fire/' + SDK_VERSION;
447
+ if (_isUsingGen) {
448
+ str += ' web/gen';
449
+ }
450
+ return str;
451
+ }
452
+ function dcFetch(url, body, _a, accessToken, _isUsingGen) {
444
453
  var _this = this;
445
454
  var signal = _a.signal;
446
455
  if (!connectFetch) {
447
456
  throw new DataConnectError(Code.OTHER, 'No Fetch Implementation detected!');
448
457
  }
449
458
  var headers = {
450
- 'Content-Type': 'application/json'
459
+ 'Content-Type': 'application/json',
460
+ 'X-Goog-Api-Client': getGoogApiClientValue(_isUsingGen)
451
461
  };
452
462
  if (accessToken) {
453
463
  headers['X-Firebase-Auth-Token'] = accessToken;
@@ -459,11 +469,12 @@ function dcFetch(url, body, _a, accessToken) {
459
469
  method: 'POST',
460
470
  headers: headers,
461
471
  signal: signal
462
- }).catch(function (err) {
463
- throw new DataConnectError(Code.OTHER, "Failed to fetch: " + JSON.stringify(err));
472
+ })
473
+ .catch(function (err) {
474
+ throw new DataConnectError(Code.OTHER, 'Failed to fetch: ' + JSON.stringify(err));
464
475
  })
465
476
  .then(function (response) { return __awaiter(_this, void 0, void 0, function () {
466
- var jsonResponse, e_1;
477
+ var jsonResponse, e_1, message;
467
478
  return __generator(this, function (_a) {
468
479
  switch (_a.label) {
469
480
  case 0:
@@ -479,9 +490,13 @@ function dcFetch(url, body, _a, accessToken) {
479
490
  e_1 = _a.sent();
480
491
  throw new DataConnectError(Code.OTHER, JSON.stringify(e_1));
481
492
  case 4:
493
+ message = getMessage(jsonResponse);
482
494
  if (response.status >= 400) {
483
495
  logError('Error while performing request: ' + JSON.stringify(jsonResponse));
484
- throw new DataConnectError(Code.OTHER, JSON.stringify(jsonResponse));
496
+ if (response.status === 401) {
497
+ throw new DataConnectError(Code.UNAUTHORIZED, message);
498
+ }
499
+ throw new DataConnectError(Code.OTHER, message);
485
500
  }
486
501
  return [2 /*return*/, jsonResponse];
487
502
  }
@@ -495,6 +510,12 @@ function dcFetch(url, body, _a, accessToken) {
495
510
  }
496
511
  return res;
497
512
  });
513
+ }
514
+ function getMessage(obj) {
515
+ if ('message' in obj) {
516
+ return obj.message;
517
+ }
518
+ return JSON.stringify(obj);
498
519
  }
499
520
 
500
521
  /**
@@ -514,11 +535,13 @@ function dcFetch(url, body, _a, accessToken) {
514
535
  * limitations under the License.
515
536
  */
516
537
  var RESTTransport = /** @class */ (function () {
517
- function RESTTransport(options, apiKey, authProvider, transportOptions) {
538
+ function RESTTransport(options, apiKey, authProvider, transportOptions, _isUsingGen) {
539
+ if (_isUsingGen === void 0) { _isUsingGen = false; }
518
540
  var _this = this;
519
541
  var _a;
520
542
  this.apiKey = apiKey;
521
543
  this.authProvider = authProvider;
544
+ this._isUsingGen = _isUsingGen;
522
545
  this._host = '';
523
546
  this._location = 'l';
524
547
  this._connectorName = '';
@@ -526,30 +549,32 @@ var RESTTransport = /** @class */ (function () {
526
549
  this._project = 'p';
527
550
  this._accessToken = null;
528
551
  this._authInitialized = false;
552
+ this._lastToken = null;
529
553
  // TODO(mtewani): Update U to include shape of body defined in line 13.
530
554
  this.invokeQuery = function (queryName, body) {
531
555
  var abortController = new AbortController();
532
556
  // TODO(mtewani): Update to proper value
533
- var withAuth = _this.getWithAuth().then(function () {
557
+ var withAuth = _this.withRetry(function () {
534
558
  return dcFetch(addToken("".concat(_this.endpointUrl, ":executeQuery"), _this.apiKey), {
535
559
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
536
560
  operationName: queryName,
537
561
  variables: body
538
562
  }, // TODO(mtewani): This is a patch, fix this.
539
- abortController, _this._accessToken);
563
+ abortController, _this._accessToken, _this._isUsingGen);
540
564
  });
541
565
  return {
542
- then: withAuth.then.bind(withAuth)
566
+ then: withAuth.then.bind(withAuth),
567
+ catch: withAuth.catch.bind(withAuth)
543
568
  };
544
569
  };
545
570
  this.invokeMutation = function (mutationName, body) {
546
571
  var abortController = new AbortController();
547
- var taskResult = _this.getWithAuth().then(function () {
572
+ var taskResult = _this.withRetry(function () {
548
573
  return dcFetch(addToken("".concat(_this.endpointUrl, ":executeMutation"), _this.apiKey), {
549
574
  name: "projects/".concat(_this._project, "/locations/").concat(_this._location, "/services/").concat(_this._serviceName, "/connectors/").concat(_this._connectorName),
550
575
  operationName: mutationName,
551
576
  variables: body
552
- }, abortController, _this._accessToken);
577
+ }, abortController, _this._accessToken, _this._isUsingGen);
553
578
  });
554
579
  return {
555
580
  then: taskResult.then.bind(taskResult),
@@ -608,15 +633,16 @@ var RESTTransport = /** @class */ (function () {
608
633
  RESTTransport.prototype.onTokenChanged = function (newToken) {
609
634
  this._accessToken = newToken;
610
635
  };
611
- RESTTransport.prototype.getWithAuth = function () {
636
+ RESTTransport.prototype.getWithAuth = function (forceToken) {
612
637
  var _this = this;
638
+ if (forceToken === void 0) { forceToken = false; }
613
639
  var starterPromise = new Promise(function (resolve) {
614
640
  return resolve(_this._accessToken);
615
641
  });
616
642
  if (!this._authInitialized) {
617
643
  if (this.authProvider) {
618
644
  starterPromise = this.authProvider
619
- .getToken(/*forceToken=*/ false)
645
+ .getToken(/*forceToken=*/ forceToken)
620
646
  .then(function (data) {
621
647
  if (!data) {
622
648
  return null;
@@ -631,6 +657,32 @@ var RESTTransport = /** @class */ (function () {
631
657
  }
632
658
  return starterPromise;
633
659
  };
660
+ RESTTransport.prototype._setLastToken = function (lastToken) {
661
+ this._lastToken = lastToken;
662
+ };
663
+ RESTTransport.prototype.withRetry = function (promiseFactory, retry) {
664
+ var _this = this;
665
+ if (retry === void 0) { retry = false; }
666
+ var isNewToken = false;
667
+ return this.getWithAuth(retry)
668
+ .then(function (res) {
669
+ isNewToken = _this._lastToken !== res;
670
+ _this._lastToken = res;
671
+ return res;
672
+ })
673
+ .then(promiseFactory)
674
+ .catch(function (err) {
675
+ // Only retry if the result is unauthorized and the last token isn't the same as the new one.
676
+ if ('code' in err &&
677
+ err.code === Code.UNAUTHORIZED &&
678
+ !retry &&
679
+ isNewToken) {
680
+ logDebug('Retrying due to unauthorized');
681
+ return _this.withRetry(promiseFactory, true);
682
+ }
683
+ throw err;
684
+ });
685
+ };
634
686
  return RESTTransport;
635
687
  }());
636
688
 
@@ -742,6 +794,7 @@ var DataConnect = /** @class */ (function () {
742
794
  this._authProvider = _authProvider;
743
795
  this.isEmulator = false;
744
796
  this.initialized = false;
797
+ this._isUsingGeneratedSdk = false;
745
798
  if (typeof process !== 'undefined' && process.env) {
746
799
  var host = process.env[FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR];
747
800
  if (host) {
@@ -751,6 +804,14 @@ var DataConnect = /** @class */ (function () {
751
804
  }
752
805
  }
753
806
  }
807
+ /*
808
+ @internal
809
+ */
810
+ DataConnect.prototype._useGeneratedSdk = function () {
811
+ if (!this._isUsingGeneratedSdk) {
812
+ this._isUsingGeneratedSdk = true;
813
+ }
814
+ };
754
815
  DataConnect.prototype._delete = function () {
755
816
  _removeServiceInstance(this.app, 'data-connect', JSON.stringify(this.getSettings()));
756
817
  return Promise.resolve();
@@ -772,7 +833,7 @@ var DataConnect = /** @class */ (function () {
772
833
  this._authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this._authProvider);
773
834
  }
774
835
  this.initialized = true;
775
- this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider);
836
+ this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider, undefined, this._isUsingGeneratedSdk);
776
837
  if (this._transportOptions) {
777
838
  this._transport.useEmulator(this._transportOptions.host, this._transportOptions.port, this._transportOptions.sslEnabled);
778
839
  }
@@ -811,7 +872,7 @@ function getDataConnect(appOrOptions, optionalOptions) {
811
872
  dcOptions = optionalOptions;
812
873
  app = appOrOptions;
813
874
  }
814
- if (!app) {
875
+ if (!app || Object.keys(app).length === 0) {
815
876
  app = getApp();
816
877
  }
817
878
  var provider = _getProvider(app, 'data-connect');
@@ -825,9 +886,7 @@ function getDataConnect(appOrOptions, optionalOptions) {
825
886
  return dcInstance;
826
887
  }
827
888
  }
828
- if (!dcOptions) {
829
- throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
830
- }
889
+ validateDCOptions(dcOptions);
831
890
  logDebug('Creating new DataConnect instance');
832
891
  // Initialize with options.
833
892
  return provider.initialize({
@@ -835,6 +894,24 @@ function getDataConnect(appOrOptions, optionalOptions) {
835
894
  options: dcOptions
836
895
  });
837
896
  }
897
+ /**
898
+ *
899
+ * @param dcOptions
900
+ * @returns {void}
901
+ * @internal
902
+ */
903
+ function validateDCOptions(dcOptions) {
904
+ var fields = ['connector', 'location', 'service'];
905
+ if (!dcOptions) {
906
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
907
+ }
908
+ fields.forEach(function (field) {
909
+ if (dcOptions[field] === null || dcOptions[field] === undefined) {
910
+ throw new DataConnectError(Code.INVALID_ARGUMENT, "".concat(field, " Required"));
911
+ }
912
+ });
913
+ return true;
914
+ }
838
915
  /**
839
916
  * Delete DataConnect instance
840
917
  * @param dataConnect DataConnect instance
@@ -855,6 +932,9 @@ function registerDataConnect(variant) {
855
932
  if (settings) {
856
933
  newOpts = JSON.parse(settings);
857
934
  }
935
+ if (!app.options.projectId) {
936
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'Project ID must be provided. Did you pass in a proper projectId to initializeApp?');
937
+ }
858
938
  return new DataConnect(app, __assign(__assign({}, newOpts), { projectId: app.options.projectId }), authProvider);
859
939
  }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
860
940
  registerVersion(name, version, variant);
@@ -914,6 +994,49 @@ function toQueryRef(serializedRef) {
914
994
  return queryRef(getDataConnect(connectorConfig), name, variables);
915
995
  }
916
996
 
997
+ /**
998
+ * @license
999
+ * Copyright 2024 Google LLC
1000
+ *
1001
+ * Licensed under the Apache License, Version 2.0 (the "License");
1002
+ * you may not use this file except in compliance with the License.
1003
+ * You may obtain a copy of the License at
1004
+ *
1005
+ * http://www.apache.org/licenses/LICENSE-2.0
1006
+ *
1007
+ * Unless required by applicable law or agreed to in writing, software
1008
+ * distributed under the License is distributed on an "AS IS" BASIS,
1009
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
+ * See the License for the specific language governing permissions and
1011
+ * limitations under the License.
1012
+ */
1013
+ /**
1014
+ * The generated SDK will allow the user to pass in either the variable or the data connect instance with the variable,
1015
+ * and this function validates the variables and returns back the DataConnect instance and variables based on the arguments passed in.
1016
+ * @param connectorConfig
1017
+ * @param dcOrVars
1018
+ * @param vars
1019
+ * @param validateVars
1020
+ * @returns {DataConnect} and {Variables} instance
1021
+ * @internal
1022
+ */
1023
+ function validateArgs(connectorConfig, dcOrVars, vars, validateVars) {
1024
+ var dcInstance;
1025
+ var realVars;
1026
+ if (dcOrVars && 'enableEmulator' in dcOrVars) {
1027
+ dcInstance = dcOrVars;
1028
+ realVars = vars;
1029
+ }
1030
+ else {
1031
+ dcInstance = getDataConnect(connectorConfig);
1032
+ realVars = dcOrVars;
1033
+ }
1034
+ if (!dcInstance || (!realVars && validateVars)) {
1035
+ throw new DataConnectError(Code.INVALID_ARGUMENT, 'Variables required.');
1036
+ }
1037
+ return { dc: dcInstance, vars: realVars };
1038
+ }
1039
+
917
1040
  /**
918
1041
  * @license
919
1042
  * Copyright 2024 Google LLC
@@ -976,5 +1099,5 @@ function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComp
976
1099
  */
977
1100
  registerDataConnect();
978
1101
 
979
- 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 };
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 };
980
1103
  //# sourceMappingURL=index.esm5.js.map