@firebase/data-connect 0.0.1-dataconnect-preview.81ee5169c → 0.0.2-dataconnect-preview.388b61c7e

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 (38) hide show
  1. package/dist/index.cjs.js +181 -141
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.esm2017.js +180 -140
  4. package/dist/index.esm2017.js.map +1 -1
  5. package/dist/index.esm5.js +183 -143
  6. package/dist/index.esm5.js.map +1 -1
  7. package/dist/index.node.cjs.js +132 -72
  8. package/dist/index.node.cjs.js.map +1 -1
  9. package/dist/internal.d.ts +156 -44
  10. package/dist/node-esm/index.node.esm.js +132 -72
  11. package/dist/node-esm/index.node.esm.js.map +1 -1
  12. package/dist/node-esm/src/api/DataConnect.d.ts +39 -12
  13. package/dist/node-esm/src/api/Mutation.d.ts +33 -8
  14. package/dist/node-esm/src/api/Reference.d.ts +9 -3
  15. package/dist/node-esm/src/api/query.d.ts +52 -2
  16. package/dist/node-esm/src/api.browser.d.ts +12 -7
  17. package/dist/node-esm/src/core/FirebaseAuthProvider.d.ts +5 -5
  18. package/dist/node-esm/src/core/QueryManager.d.ts +7 -7
  19. package/dist/node-esm/src/network/transport/rest.d.ts +13 -25
  20. package/dist/node-esm/src/util/url.d.ts +2 -2
  21. package/dist/private.d.ts +151 -47
  22. package/dist/public.d.ts +135 -37
  23. package/dist/src/api/DataConnect.d.ts +39 -12
  24. package/dist/src/api/Mutation.d.ts +33 -8
  25. package/dist/src/api/Reference.d.ts +9 -3
  26. package/dist/src/api/query.d.ts +52 -2
  27. package/dist/src/api.browser.d.ts +12 -7
  28. package/dist/src/core/FirebaseAuthProvider.d.ts +5 -5
  29. package/dist/src/core/QueryManager.d.ts +7 -7
  30. package/dist/src/network/transport/rest.d.ts +13 -25
  31. package/dist/src/util/url.d.ts +2 -2
  32. package/package.json +7 -7
  33. package/dist/node-esm/test/emulatorSeeder.d.ts +0 -22
  34. package/dist/node-esm/test/queries.test.d.ts +0 -17
  35. package/dist/node-esm/test/util.d.ts +0 -26
  36. package/dist/test/emulatorSeeder.d.ts +0 -22
  37. package/dist/test/queries.test.d.ts +0 -17
  38. package/dist/test/util.d.ts +0 -26
@@ -139,6 +139,8 @@ function dcFetch(url, body, { signal }, accessToken) {
139
139
  method: 'POST',
140
140
  headers,
141
141
  signal
142
+ }).catch(err => {
143
+ throw new DataConnectError(Code.OTHER, "Failed to fetch: " + JSON.stringify(err));
142
144
  })
143
145
  .then(async (response) => {
144
146
  let jsonResponse = null;
@@ -165,7 +167,7 @@ function dcFetch(url, body, { signal }, accessToken) {
165
167
  }
166
168
 
167
169
  const name = "@firebase/data-connect";
168
- const version = "0.0.1-dataconnect-preview.81ee5169c";
170
+ const version = "0.0.2-dataconnect-preview.388b61c7e";
169
171
 
170
172
  /**
171
173
  * @license
@@ -184,20 +186,20 @@ const version = "0.0.1-dataconnect-preview.81ee5169c";
184
186
  * limitations under the License.
185
187
  */
186
188
  class FirebaseAuthProvider {
187
- constructor(appName, options, authProvider_) {
188
- this.appName = appName;
189
- this.options = options;
190
- this.authProvider_ = authProvider_;
191
- this.auth_ = authProvider_.getImmediate({ optional: true });
192
- if (!this.auth_) {
193
- authProvider_.onInit(auth => (this.auth_ = auth));
189
+ constructor(_appName, _options, _authProvider) {
190
+ this._appName = _appName;
191
+ this._options = _options;
192
+ this._authProvider = _authProvider;
193
+ this._auth = _authProvider.getImmediate({ optional: true });
194
+ if (!this._auth) {
195
+ _authProvider.onInit(auth => (this._auth = auth));
194
196
  }
195
197
  }
196
198
  getToken(forceRefresh) {
197
- if (!this.auth_) {
199
+ if (!this._auth) {
198
200
  return new Promise((resolve, reject) => {
199
201
  setTimeout(() => {
200
- if (this.auth_) {
202
+ if (this._auth) {
201
203
  this.getToken(forceRefresh).then(resolve, reject);
202
204
  }
203
205
  else {
@@ -206,7 +208,7 @@ class FirebaseAuthProvider {
206
208
  }, 0);
207
209
  });
208
210
  }
209
- return this.auth_.getToken(forceRefresh).catch(error => {
211
+ return this._auth.getToken(forceRefresh).catch(error => {
210
212
  if (error && error.code === 'auth/token-not-initialized') {
211
213
  logDebug('Got auth/token-not-initialized error. Treating as null token.');
212
214
  return null;
@@ -220,10 +222,10 @@ class FirebaseAuthProvider {
220
222
  }
221
223
  addTokenChangeListener(listener) {
222
224
  var _a;
223
- (_a = this.auth_) === null || _a === void 0 ? void 0 : _a.addAuthTokenListener(listener);
225
+ (_a = this._auth) === null || _a === void 0 ? void 0 : _a.addAuthTokenListener(listener);
224
226
  }
225
227
  removeTokenChangeListener(listener) {
226
- this.authProvider_
228
+ this._authProvider
227
229
  .get()
228
230
  .then(auth => auth.removeAuthTokenListener(listener));
229
231
  }
@@ -245,8 +247,8 @@ class FirebaseAuthProvider {
245
247
  * See the License for the specific language governing permissions and
246
248
  * limitations under the License.
247
249
  */
248
- const QueryStr = 'query';
249
- const MutationStr = 'mutation';
250
+ const QUERY_STR = 'query';
251
+ const MUTATION_STR = 'mutation';
250
252
  const SOURCE_SERVER = 'SERVER';
251
253
  const SOURCE_CACHE = 'CACHE';
252
254
 
@@ -333,7 +335,7 @@ class QueryManager {
333
335
  const ref = {
334
336
  name: queryName,
335
337
  variables,
336
- refType: QueryStr
338
+ refType: QUERY_STR
337
339
  };
338
340
  const key = encoderImpl(ref);
339
341
  const newTrackedQuery = {
@@ -350,7 +352,7 @@ class QueryManager {
350
352
  const key = encoderImpl({
351
353
  name: queryRef.name,
352
354
  variables: queryRef.variables,
353
- refType: QueryStr
355
+ refType: QUERY_STR
354
356
  });
355
357
  const trackedQuery = this._queries.get(key);
356
358
  const subscription = {
@@ -399,7 +401,7 @@ class QueryManager {
399
401
  const key = encoderImpl({
400
402
  name: queryRef.name,
401
403
  variables: queryRef.variables,
402
- refType: QueryStr
404
+ refType: QUERY_STR
403
405
  });
404
406
  const trackedQuery = this._queries.get(key);
405
407
  const result = this.transport.invokeQuery(queryRef.name, queryRef.variables);
@@ -497,24 +499,24 @@ class RESTTransport {
497
499
  var _a;
498
500
  this.apiKey = apiKey;
499
501
  this.authProvider = authProvider;
500
- this.host = '';
501
- this.location = 'l';
502
- this.connectorName = '';
503
- this.secure = true;
504
- this.project = 'p';
505
- this.accessToken = null;
506
- this.authInitialized_ = false;
502
+ this._host = '';
503
+ this._location = 'l';
504
+ this._connectorName = '';
505
+ this._secure = true;
506
+ this._project = 'p';
507
+ this._accessToken = null;
508
+ this._authInitialized = false;
507
509
  // TODO(mtewani): Update U to include shape of body defined in line 13.
508
510
  this.invokeQuery = (queryName, body) => {
509
511
  const abortController = new AbortController();
510
512
  // TODO(mtewani): Update to proper value
511
513
  const withAuth = this.getWithAuth().then(() => {
512
514
  return dcFetch(addToken(`${this.endpointUrl}:executeQuery`, this.apiKey), {
513
- name: `projects/${this.project}/locations/${this.location}/services/${this.serviceName}/connectors/${this.connectorName}`,
515
+ name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,
514
516
  operationName: queryName,
515
517
  variables: body
516
518
  }, // TODO(mtewani): This is a patch, fix this.
517
- abortController, this.accessToken);
519
+ abortController, this._accessToken);
518
520
  });
519
521
  return {
520
522
  then: withAuth.then.bind(withAuth)
@@ -524,10 +526,10 @@ class RESTTransport {
524
526
  const abortController = new AbortController();
525
527
  const taskResult = this.getWithAuth().then(() => {
526
528
  return dcFetch(addToken(`${this.endpointUrl}:executeMutation`, this.apiKey), {
527
- name: `projects/${this.project}/locations/${this.location}/services/${this.serviceName}/connectors/${this.connectorName}`,
529
+ name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,
528
530
  operationName: mutationName,
529
531
  variables: body
530
- }, abortController, this.accessToken);
532
+ }, abortController, this._accessToken);
531
533
  });
532
534
  return {
533
535
  then: taskResult.then.bind(taskResult),
@@ -538,53 +540,53 @@ class RESTTransport {
538
540
  };
539
541
  if (transportOptions) {
540
542
  if (typeof transportOptions.port === 'number') {
541
- this.port = transportOptions.port;
543
+ this._port = transportOptions.port;
542
544
  }
543
545
  if (typeof transportOptions.sslEnabled !== 'undefined') {
544
- this.secure = transportOptions.sslEnabled;
546
+ this._secure = transportOptions.sslEnabled;
545
547
  }
546
- this.host = transportOptions.host;
548
+ this._host = transportOptions.host;
547
549
  }
548
550
  const { location, projectId: project, connector, service } = options;
549
551
  if (location) {
550
- this.location = location;
552
+ this._location = location;
551
553
  }
552
554
  if (project) {
553
- this.project = project;
555
+ this._project = project;
554
556
  }
555
- this.serviceName = service;
557
+ this._serviceName = service;
556
558
  if (!connector) {
557
559
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Connector Name required!');
558
560
  }
559
- this.connectorName = connector;
561
+ this._connectorName = connector;
560
562
  (_a = this.authProvider) === null || _a === void 0 ? void 0 : _a.addTokenChangeListener(token => {
561
563
  logDebug(`New Token Available: ${token}`);
562
- this.accessToken = token;
564
+ this._accessToken = token;
563
565
  });
564
566
  }
565
567
  get endpointUrl() {
566
568
  return urlBuilder({
567
- connector: this.connectorName,
568
- location: this.location,
569
- projectId: this.project,
570
- service: this.serviceName
571
- }, { host: this.host, sslEnabled: this.secure, port: this.port });
569
+ connector: this._connectorName,
570
+ location: this._location,
571
+ projectId: this._project,
572
+ service: this._serviceName
573
+ }, { host: this._host, sslEnabled: this._secure, port: this._port });
572
574
  }
573
575
  useEmulator(host, port, isSecure) {
574
- this.host = host;
576
+ this._host = host;
575
577
  if (typeof port === 'number') {
576
- this.port = port;
578
+ this._port = port;
577
579
  }
578
580
  if (typeof isSecure !== 'undefined') {
579
- this.secure = isSecure;
581
+ this._secure = isSecure;
580
582
  }
581
583
  }
582
584
  onTokenChanged(newToken) {
583
- this.accessToken = newToken;
585
+ this._accessToken = newToken;
584
586
  }
585
587
  getWithAuth() {
586
- let starterPromise = new Promise(resolve => resolve(this.accessToken));
587
- if (!this.authInitialized_) {
588
+ let starterPromise = new Promise(resolve => resolve(this._accessToken));
589
+ if (!this._authInitialized) {
588
590
  if (this.authProvider) {
589
591
  starterPromise = this.authProvider
590
592
  .getToken(/*forceToken=*/ false)
@@ -592,8 +594,8 @@ class RESTTransport {
592
594
  if (!data) {
593
595
  return null;
594
596
  }
595
- this.accessToken = data.accessToken;
596
- return this.accessToken;
597
+ this._accessToken = data.accessToken;
598
+ return this._accessToken;
597
599
  });
598
600
  }
599
601
  else {
@@ -620,23 +622,33 @@ class RESTTransport {
620
622
  * See the License for the specific language governing permissions and
621
623
  * limitations under the License.
622
624
  */
623
- function mutationRef(dcInstance, queryName, variables) {
625
+ /**
626
+ *
627
+ * @param dcInstance Data Connect instance
628
+ * @param mutationName name of mutation
629
+ * @param variables variables to send with mutation
630
+ * @returns `MutationRef`
631
+ */
632
+ function mutationRef(dcInstance, mutationName, variables) {
624
633
  dcInstance.setInitialized();
625
634
  const ref = {
626
635
  dataConnect: dcInstance,
627
- name: queryName,
628
- refType: MutationStr,
636
+ name: mutationName,
637
+ refType: MUTATION_STR,
629
638
  variables: variables
630
639
  };
631
640
  return ref;
632
641
  }
642
+ /**
643
+ * @internal
644
+ */
633
645
  class MutationManager {
634
- constructor(transport) {
635
- this.transport = transport;
646
+ constructor(_transport) {
647
+ this._transport = _transport;
636
648
  this._inflight = [];
637
649
  }
638
650
  executeMutation(mutationRef) {
639
- const result = this.transport.invokeMutation(mutationRef.name, mutationRef.variables);
651
+ const result = this._transport.invokeMutation(mutationRef.name, mutationRef.variables);
640
652
  const withRefPromise = result.then(res => {
641
653
  const obj = Object.assign(Object.assign({}, res), { source: SOURCE_SERVER, ref: mutationRef, fetchTime: Date.now().toLocaleString() });
642
654
  return obj;
@@ -647,6 +659,11 @@ class MutationManager {
647
659
  return withRefPromise;
648
660
  }
649
661
  }
662
+ /**
663
+ * Execute Mutation
664
+ * @param mutationRef mutation to execute
665
+ * @returns `MutationRef`
666
+ */
650
667
  function executeMutation(mutationRef) {
651
668
  return mutationRef.dataConnect._mutationManager.executeMutation(mutationRef);
652
669
  }
@@ -681,11 +698,16 @@ function parseOptions(fullHost) {
681
698
  const port = Number(portAsString);
682
699
  return { host, port, sslEnabled: isSecure };
683
700
  }
701
+ /**
702
+ * Class representing Firebase Data Connect
703
+ */
684
704
  class DataConnect {
685
- constructor(app, dataConnectOptions, authProvider) {
705
+ constructor(app,
706
+ // TODO(mtewani): Replace with _dataConnectOptions in the future
707
+ dataConnectOptions, _authProvider) {
686
708
  this.app = app;
687
709
  this.dataConnectOptions = dataConnectOptions;
688
- this.authProvider = authProvider;
710
+ this._authProvider = _authProvider;
689
711
  this.isEmulator = false;
690
712
  this.initialized = false;
691
713
  if (typeof process !== 'undefined' && process.env) {
@@ -693,7 +715,7 @@ class DataConnect {
693
715
  if (host) {
694
716
  logDebug('Found custom host. Using emulator');
695
717
  this.isEmulator = true;
696
- this.transportOptions = parseOptions(host);
718
+ this._transportOptions = parseOptions(host);
697
719
  }
698
720
  }
699
721
  }
@@ -710,30 +732,37 @@ class DataConnect {
710
732
  if (this.initialized) {
711
733
  return;
712
734
  }
713
- if (this.transportClass === undefined) {
735
+ if (this._transportClass === undefined) {
714
736
  logDebug('transportClass not provided. Defaulting to RESTTransport.');
715
- this.transportClass = RESTTransport;
737
+ this._transportClass = RESTTransport;
716
738
  }
717
- if (this.authProvider) {
718
- this.authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this.authProvider);
739
+ if (this._authProvider) {
740
+ this._authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this._authProvider);
719
741
  }
720
742
  this.initialized = true;
721
- this._transport = new this.transportClass(this.dataConnectOptions, this.app.options.apiKey, this.authTokenProvider);
722
- if (this.transportOptions) {
723
- this._transport.useEmulator(this.transportOptions.host, this.transportOptions.port, this.transportOptions.sslEnabled);
743
+ this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider);
744
+ if (this._transportOptions) {
745
+ this._transport.useEmulator(this._transportOptions.host, this._transportOptions.port, this._transportOptions.sslEnabled);
724
746
  }
725
747
  this._queryManager = new QueryManager(this._transport);
726
748
  this._mutationManager = new MutationManager(this._transport);
727
749
  }
728
750
  enableEmulator(transportOptions) {
729
751
  if (this.initialized) {
730
- logError('enableEmulator called without initializing');
752
+ logError('enableEmulator called after initialization');
731
753
  throw new DataConnectError(Code.ALREADY_INITIALIZED, 'DataConnect instance already initialized!');
732
754
  }
733
- this.transportOptions = transportOptions;
755
+ this._transportOptions = transportOptions;
734
756
  this.isEmulator = true;
735
757
  }
736
758
  }
759
+ /**
760
+ * Connect to the DataConnect Emulator
761
+ * @param dc Data Connect instance
762
+ * @param host host of emulator server
763
+ * @param port port of emulator server
764
+ * @param sslEnabled use https
765
+ */
737
766
  function connectDataConnectEmulator(dc, host, port, sslEnabled = false) {
738
767
  dc.enableEmulator({ host, port, sslEnabled });
739
768
  }
@@ -772,8 +801,13 @@ function getDataConnect(appOrOptions, optionalOptions) {
772
801
  options: dcOptions
773
802
  });
774
803
  }
804
+ /**
805
+ * Delete DataConnect instance
806
+ * @param dataConnect DataConnect instance
807
+ * @returns
808
+ */
775
809
  function terminate(dataConnect) {
776
- dataConnect._delete();
810
+ return dataConnect._delete();
777
811
  // TODO(mtewani): Stop pending tasks
778
812
  }
779
813
 
@@ -825,19 +859,37 @@ function registerDataConnect(variant) {
825
859
  * See the License for the specific language governing permissions and
826
860
  * limitations under the License.
827
861
  */
862
+ /**
863
+ * Execute Query
864
+ * @param queryRef query to execute.
865
+ * @returns `QueryPromise`
866
+ */
828
867
  function executeQuery(queryRef) {
829
868
  return queryRef.dataConnect._queryManager.executeQuery(queryRef);
830
869
  }
870
+ /**
871
+ * Execute Query
872
+ * @param dcInstance Data Connect instance to use.
873
+ * @param queryName Query to execute
874
+ * @param variables Variables to execute with
875
+ * @param initialCache initial cache to use for client hydration
876
+ * @returns `QueryRef`
877
+ */
831
878
  function queryRef(dcInstance, queryName, variables, initialCache) {
832
879
  dcInstance.setInitialized();
833
880
  dcInstance._queryManager.track(queryName, variables, initialCache);
834
881
  return {
835
882
  dataConnect: dcInstance,
836
- refType: QueryStr,
883
+ refType: QUERY_STR,
837
884
  name: queryName,
838
885
  variables: variables
839
886
  };
840
887
  }
888
+ /**
889
+ * Converts serialized ref to query ref
890
+ * @param serializedRef ref to convert to `QueryRef`
891
+ * @returns `QueryRef`
892
+ */
841
893
  function toQueryRef(serializedRef) {
842
894
  const { refInfo: { name, variables, connectorConfig } } = serializedRef;
843
895
  return queryRef(getDataConnect(connectorConfig), name, variables);
@@ -859,11 +911,19 @@ function toQueryRef(serializedRef) {
859
911
  * See the License for the specific language governing permissions and
860
912
  * limitations under the License.
861
913
  */
914
+ /**
915
+ * Subscribe to a `QueryRef`
916
+ * @param queryRefOrSerializedResult query ref or serialized result.
917
+ * @param observerOrOnNext observer object or next function.
918
+ * @param onError Callback to call when error gets thrown.
919
+ * @param onComplete Called when subscription completes.
920
+ * @returns `SubscriptionOptions`
921
+ */
862
922
  function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComplete) {
863
923
  let ref;
864
924
  let initialCache;
865
925
  if ('refInfo' in queryRefOrSerializedResult) {
866
- let serializedRef = queryRefOrSerializedResult;
926
+ const serializedRef = queryRefOrSerializedResult;
867
927
  const { data, source, fetchTime } = serializedRef;
868
928
  initialCache = {
869
929
  data,
@@ -909,5 +969,5 @@ function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComp
909
969
  initializeFetch(fetch);
910
970
  registerDataConnect('node');
911
971
 
912
- export { DataConnect, FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR, FirebaseAuthProvider, MutationManager, MutationStr, QueryStr, SOURCE_CACHE, SOURCE_SERVER, connectDataConnectEmulator, executeMutation, executeQuery, getDataConnect, mutationRef, parseOptions, queryRef, setLogLevel, subscribe, terminate, toQueryRef };
972
+ 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 };
913
973
  //# sourceMappingURL=index.node.esm.js.map