@firebase/data-connect 0.0.1-dataconnect-preview.81ee5169c → 0.0.2-dataconnect-preview.877f8b7d0

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 (36) hide show
  1. package/dist/index.cjs.js +123 -139
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.esm2017.js +122 -138
  4. package/dist/index.esm2017.js.map +1 -1
  5. package/dist/index.esm5.js +125 -141
  6. package/dist/index.esm5.js.map +1 -1
  7. package/dist/index.node.cjs.js +74 -70
  8. package/dist/index.node.cjs.js.map +1 -1
  9. package/dist/internal.d.ts +30 -37
  10. package/dist/node-esm/index.node.esm.js +74 -70
  11. package/dist/node-esm/index.node.esm.js.map +1 -1
  12. package/dist/node-esm/src/api/DataConnect.d.ts +6 -12
  13. package/dist/node-esm/src/api/Mutation.d.ts +8 -8
  14. package/dist/node-esm/src/api/Reference.d.ts +3 -3
  15. package/dist/node-esm/src/api/query.d.ts +2 -2
  16. package/dist/node-esm/src/core/FirebaseAuthProvider.d.ts +5 -5
  17. package/dist/node-esm/src/core/QueryManager.d.ts +7 -7
  18. package/dist/node-esm/src/network/transport/rest.d.ts +13 -25
  19. package/dist/node-esm/src/util/url.d.ts +2 -2
  20. package/dist/private.d.ts +30 -37
  21. package/dist/public.d.ts +13 -28
  22. package/dist/src/api/DataConnect.d.ts +6 -12
  23. package/dist/src/api/Mutation.d.ts +8 -8
  24. package/dist/src/api/Reference.d.ts +3 -3
  25. package/dist/src/api/query.d.ts +2 -2
  26. package/dist/src/core/FirebaseAuthProvider.d.ts +5 -5
  27. package/dist/src/core/QueryManager.d.ts +7 -7
  28. package/dist/src/network/transport/rest.d.ts +13 -25
  29. package/dist/src/util/url.d.ts +2 -2
  30. package/package.json +7 -7
  31. package/dist/node-esm/test/emulatorSeeder.d.ts +0 -22
  32. package/dist/node-esm/test/queries.test.d.ts +0 -17
  33. package/dist/node-esm/test/util.d.ts +0 -26
  34. package/dist/test/emulatorSeeder.d.ts +0 -22
  35. package/dist/test/queries.test.d.ts +0 -17
  36. package/dist/test/util.d.ts +0 -26
@@ -1,10 +1,10 @@
1
1
  import { _removeServiceInstance, getApp, _getProvider, _registerComponent, registerVersion, SDK_VERSION as SDK_VERSION$1 } from '@firebase/app';
2
2
  import { Component } from '@firebase/component';
3
- import { Logger } from '@firebase/logger';
4
3
  import { FirebaseError } from '@firebase/util';
4
+ import { Logger } from '@firebase/logger';
5
5
 
6
6
  const name = "@firebase/data-connect";
7
- const version = "0.0.1-dataconnect-preview.81ee5169c";
7
+ const version = "0.0.2-dataconnect-preview.877f8b7d0";
8
8
 
9
9
  /**
10
10
  * @license
@@ -32,6 +32,52 @@ function setSDKVersion(version) {
32
32
  SDK_VERSION = version;
33
33
  }
34
34
 
35
+ /**
36
+ * @license
37
+ * Copyright 2024 Google LLC
38
+ *
39
+ * Licensed under the Apache License, Version 2.0 (the "License");
40
+ * you may not use this file except in compliance with the License.
41
+ * You may obtain a copy of the License at
42
+ *
43
+ * http://www.apache.org/licenses/LICENSE-2.0
44
+ *
45
+ * Unless required by applicable law or agreed to in writing, software
46
+ * distributed under the License is distributed on an "AS IS" BASIS,
47
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48
+ * See the License for the specific language governing permissions and
49
+ * limitations under the License.
50
+ */
51
+ const Code = {
52
+ OTHER: 'other',
53
+ ALREADY_INITIALIZED: 'already-initialized',
54
+ NOT_INITIALIZED: 'not-initialized',
55
+ NOT_SUPPORTED: 'not-supported',
56
+ INVALID_ARGUMENT: 'invalid-argument',
57
+ PARTIAL_ERROR: 'partial-error'
58
+ };
59
+ /** An error returned by a DataConnect operation. */
60
+ class DataConnectError extends FirebaseError {
61
+ /** @hideconstructor */
62
+ constructor(
63
+ /**
64
+ * The backend error code associated with this error.
65
+ */
66
+ code,
67
+ /**
68
+ * A custom error description.
69
+ */
70
+ message) {
71
+ super(code, message);
72
+ this.code = code;
73
+ this.message = message;
74
+ // HACK: We write a toString property directly because Error is not a real
75
+ // class and so inheritance does not work correctly. We could alternatively
76
+ // do the same "back-door inheritance" trick that FirebaseError does.
77
+ this.toString = () => `${this.name}: [code=${this.code}]: ${this.message}`;
78
+ }
79
+ }
80
+
35
81
  /**
36
82
  * @license
37
83
  * Copyright 2024 Google LLC
@@ -76,20 +122,20 @@ function logError(msg) {
76
122
  * limitations under the License.
77
123
  */
78
124
  class FirebaseAuthProvider {
79
- constructor(appName, options, authProvider_) {
80
- this.appName = appName;
81
- this.options = options;
82
- this.authProvider_ = authProvider_;
83
- this.auth_ = authProvider_.getImmediate({ optional: true });
84
- if (!this.auth_) {
85
- authProvider_.onInit(auth => (this.auth_ = auth));
125
+ constructor(_appName, _options, _authProvider) {
126
+ this._appName = _appName;
127
+ this._options = _options;
128
+ this._authProvider = _authProvider;
129
+ this._auth = _authProvider.getImmediate({ optional: true });
130
+ if (!this._auth) {
131
+ _authProvider.onInit(auth => (this._auth = auth));
86
132
  }
87
133
  }
88
134
  getToken(forceRefresh) {
89
- if (!this.auth_) {
135
+ if (!this._auth) {
90
136
  return new Promise((resolve, reject) => {
91
137
  setTimeout(() => {
92
- if (this.auth_) {
138
+ if (this._auth) {
93
139
  this.getToken(forceRefresh).then(resolve, reject);
94
140
  }
95
141
  else {
@@ -98,7 +144,7 @@ class FirebaseAuthProvider {
98
144
  }, 0);
99
145
  });
100
146
  }
101
- return this.auth_.getToken(forceRefresh).catch(error => {
147
+ return this._auth.getToken(forceRefresh).catch(error => {
102
148
  if (error && error.code === 'auth/token-not-initialized') {
103
149
  logDebug('Got auth/token-not-initialized error. Treating as null token.');
104
150
  return null;
@@ -112,10 +158,10 @@ class FirebaseAuthProvider {
112
158
  }
113
159
  addTokenChangeListener(listener) {
114
160
  var _a;
115
- (_a = this.auth_) === null || _a === void 0 ? void 0 : _a.addAuthTokenListener(listener);
161
+ (_a = this._auth) === null || _a === void 0 ? void 0 : _a.addAuthTokenListener(listener);
116
162
  }
117
163
  removeTokenChangeListener(listener) {
118
- this.authProvider_
164
+ this._authProvider
119
165
  .get()
120
166
  .then(auth => auth.removeAuthTokenListener(listener));
121
167
  }
@@ -137,8 +183,8 @@ class FirebaseAuthProvider {
137
183
  * See the License for the specific language governing permissions and
138
184
  * limitations under the License.
139
185
  */
140
- const QueryStr = 'query';
141
- const MutationStr = 'mutation';
186
+ const QUERY_STR = 'query';
187
+ const MUTATION_STR = 'mutation';
142
188
  const SOURCE_SERVER = 'SERVER';
143
189
  const SOURCE_CACHE = 'CACHE';
144
190
 
@@ -225,7 +271,7 @@ class QueryManager {
225
271
  const ref = {
226
272
  name: queryName,
227
273
  variables,
228
- refType: QueryStr
274
+ refType: QUERY_STR
229
275
  };
230
276
  const key = encoderImpl(ref);
231
277
  const newTrackedQuery = {
@@ -242,7 +288,7 @@ class QueryManager {
242
288
  const key = encoderImpl({
243
289
  name: queryRef.name,
244
290
  variables: queryRef.variables,
245
- refType: QueryStr
291
+ refType: QUERY_STR
246
292
  });
247
293
  const trackedQuery = this._queries.get(key);
248
294
  const subscription = {
@@ -291,7 +337,7 @@ class QueryManager {
291
337
  const key = encoderImpl({
292
338
  name: queryRef.name,
293
339
  variables: queryRef.variables,
294
- refType: QueryStr
340
+ refType: QUERY_STR
295
341
  });
296
342
  const trackedQuery = this._queries.get(key);
297
343
  const result = this.transport.invokeQuery(queryRef.name, queryRef.variables);
@@ -328,52 +374,6 @@ function compareDates(str1, str2) {
328
374
  return date1.getTime() < date2.getTime();
329
375
  }
330
376
 
331
- /**
332
- * @license
333
- * Copyright 2024 Google LLC
334
- *
335
- * Licensed under the Apache License, Version 2.0 (the "License");
336
- * you may not use this file except in compliance with the License.
337
- * You may obtain a copy of the License at
338
- *
339
- * http://www.apache.org/licenses/LICENSE-2.0
340
- *
341
- * Unless required by applicable law or agreed to in writing, software
342
- * distributed under the License is distributed on an "AS IS" BASIS,
343
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
344
- * See the License for the specific language governing permissions and
345
- * limitations under the License.
346
- */
347
- const Code = {
348
- OTHER: 'other',
349
- ALREADY_INITIALIZED: 'already-initialized',
350
- NOT_INITIALIZED: 'not-initialized',
351
- NOT_SUPPORTED: 'not-supported',
352
- INVALID_ARGUMENT: 'invalid-argument',
353
- PARTIAL_ERROR: 'partial-error'
354
- };
355
- /** An error returned by a DataConnect operation. */
356
- class DataConnectError extends FirebaseError {
357
- /** @hideconstructor */
358
- constructor(
359
- /**
360
- * The backend error code associated with this error.
361
- */
362
- code,
363
- /**
364
- * A custom error description.
365
- */
366
- message) {
367
- super(code, message);
368
- this.code = code;
369
- this.message = message;
370
- // HACK: We write a toString property directly because Error is not a real
371
- // class and so inheritance does not work correctly. We could alternatively
372
- // do the same "back-door inheritance" trick that FirebaseError does.
373
- this.toString = () => `${this.name}: [code=${this.code}]: ${this.message}`;
374
- }
375
- }
376
-
377
377
  /**
378
378
  * @license
379
379
  * Copyright 2024 Google LLC
@@ -448,6 +448,8 @@ function dcFetch(url, body, { signal }, accessToken) {
448
448
  method: 'POST',
449
449
  headers,
450
450
  signal
451
+ }).catch(err => {
452
+ throw new DataConnectError(Code.OTHER, "Failed to fetch: " + JSON.stringify(err));
451
453
  })
452
454
  .then(async (response) => {
453
455
  let jsonResponse = null;
@@ -494,24 +496,24 @@ class RESTTransport {
494
496
  var _a;
495
497
  this.apiKey = apiKey;
496
498
  this.authProvider = authProvider;
497
- this.host = '';
498
- this.location = 'l';
499
- this.connectorName = '';
500
- this.secure = true;
501
- this.project = 'p';
502
- this.accessToken = null;
503
- this.authInitialized_ = false;
499
+ this._host = '';
500
+ this._location = 'l';
501
+ this._connectorName = '';
502
+ this._secure = true;
503
+ this._project = 'p';
504
+ this._accessToken = null;
505
+ this._authInitialized = false;
504
506
  // TODO(mtewani): Update U to include shape of body defined in line 13.
505
507
  this.invokeQuery = (queryName, body) => {
506
508
  const abortController = new AbortController();
507
509
  // TODO(mtewani): Update to proper value
508
510
  const withAuth = this.getWithAuth().then(() => {
509
511
  return dcFetch(addToken(`${this.endpointUrl}:executeQuery`, this.apiKey), {
510
- name: `projects/${this.project}/locations/${this.location}/services/${this.serviceName}/connectors/${this.connectorName}`,
512
+ name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,
511
513
  operationName: queryName,
512
514
  variables: body
513
515
  }, // TODO(mtewani): This is a patch, fix this.
514
- abortController, this.accessToken);
516
+ abortController, this._accessToken);
515
517
  });
516
518
  return {
517
519
  then: withAuth.then.bind(withAuth)
@@ -521,10 +523,10 @@ class RESTTransport {
521
523
  const abortController = new AbortController();
522
524
  const taskResult = this.getWithAuth().then(() => {
523
525
  return dcFetch(addToken(`${this.endpointUrl}:executeMutation`, this.apiKey), {
524
- name: `projects/${this.project}/locations/${this.location}/services/${this.serviceName}/connectors/${this.connectorName}`,
526
+ name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,
525
527
  operationName: mutationName,
526
528
  variables: body
527
- }, abortController, this.accessToken);
529
+ }, abortController, this._accessToken);
528
530
  });
529
531
  return {
530
532
  then: taskResult.then.bind(taskResult),
@@ -535,53 +537,53 @@ class RESTTransport {
535
537
  };
536
538
  if (transportOptions) {
537
539
  if (typeof transportOptions.port === 'number') {
538
- this.port = transportOptions.port;
540
+ this._port = transportOptions.port;
539
541
  }
540
542
  if (typeof transportOptions.sslEnabled !== 'undefined') {
541
- this.secure = transportOptions.sslEnabled;
543
+ this._secure = transportOptions.sslEnabled;
542
544
  }
543
- this.host = transportOptions.host;
545
+ this._host = transportOptions.host;
544
546
  }
545
547
  const { location, projectId: project, connector, service } = options;
546
548
  if (location) {
547
- this.location = location;
549
+ this._location = location;
548
550
  }
549
551
  if (project) {
550
- this.project = project;
552
+ this._project = project;
551
553
  }
552
- this.serviceName = service;
554
+ this._serviceName = service;
553
555
  if (!connector) {
554
556
  throw new DataConnectError(Code.INVALID_ARGUMENT, 'Connector Name required!');
555
557
  }
556
- this.connectorName = connector;
558
+ this._connectorName = connector;
557
559
  (_a = this.authProvider) === null || _a === void 0 ? void 0 : _a.addTokenChangeListener(token => {
558
560
  logDebug(`New Token Available: ${token}`);
559
- this.accessToken = token;
561
+ this._accessToken = token;
560
562
  });
561
563
  }
562
564
  get endpointUrl() {
563
565
  return urlBuilder({
564
- connector: this.connectorName,
565
- location: this.location,
566
- projectId: this.project,
567
- service: this.serviceName
568
- }, { host: this.host, sslEnabled: this.secure, port: this.port });
566
+ connector: this._connectorName,
567
+ location: this._location,
568
+ projectId: this._project,
569
+ service: this._serviceName
570
+ }, { host: this._host, sslEnabled: this._secure, port: this._port });
569
571
  }
570
572
  useEmulator(host, port, isSecure) {
571
- this.host = host;
573
+ this._host = host;
572
574
  if (typeof port === 'number') {
573
- this.port = port;
575
+ this._port = port;
574
576
  }
575
577
  if (typeof isSecure !== 'undefined') {
576
- this.secure = isSecure;
578
+ this._secure = isSecure;
577
579
  }
578
580
  }
579
581
  onTokenChanged(newToken) {
580
- this.accessToken = newToken;
582
+ this._accessToken = newToken;
581
583
  }
582
584
  getWithAuth() {
583
- let starterPromise = new Promise(resolve => resolve(this.accessToken));
584
- if (!this.authInitialized_) {
585
+ let starterPromise = new Promise(resolve => resolve(this._accessToken));
586
+ if (!this._authInitialized) {
585
587
  if (this.authProvider) {
586
588
  starterPromise = this.authProvider
587
589
  .getToken(/*forceToken=*/ false)
@@ -589,8 +591,8 @@ class RESTTransport {
589
591
  if (!data) {
590
592
  return null;
591
593
  }
592
- this.accessToken = data.accessToken;
593
- return this.accessToken;
594
+ this._accessToken = data.accessToken;
595
+ return this._accessToken;
594
596
  });
595
597
  }
596
598
  else {
@@ -622,18 +624,18 @@ function mutationRef(dcInstance, queryName, variables) {
622
624
  const ref = {
623
625
  dataConnect: dcInstance,
624
626
  name: queryName,
625
- refType: MutationStr,
627
+ refType: MUTATION_STR,
626
628
  variables: variables
627
629
  };
628
630
  return ref;
629
631
  }
630
632
  class MutationManager {
631
- constructor(transport) {
632
- this.transport = transport;
633
+ constructor(_transport) {
634
+ this._transport = _transport;
633
635
  this._inflight = [];
634
636
  }
635
637
  executeMutation(mutationRef) {
636
- const result = this.transport.invokeMutation(mutationRef.name, mutationRef.variables);
638
+ const result = this._transport.invokeMutation(mutationRef.name, mutationRef.variables);
637
639
  const withRefPromise = result.then(res => {
638
640
  const obj = Object.assign(Object.assign({}, res), { source: SOURCE_SERVER, ref: mutationRef, fetchTime: Date.now().toLocaleString() });
639
641
  return obj;
@@ -679,10 +681,12 @@ function parseOptions(fullHost) {
679
681
  return { host, port, sslEnabled: isSecure };
680
682
  }
681
683
  class DataConnect {
682
- constructor(app, dataConnectOptions, authProvider) {
684
+ constructor(app,
685
+ // TODO(mtewani): Replace with _dataConnectOptions in the future
686
+ dataConnectOptions, _authProvider) {
683
687
  this.app = app;
684
688
  this.dataConnectOptions = dataConnectOptions;
685
- this.authProvider = authProvider;
689
+ this._authProvider = _authProvider;
686
690
  this.isEmulator = false;
687
691
  this.initialized = false;
688
692
  if (typeof process !== 'undefined' && process.env) {
@@ -690,7 +694,7 @@ class DataConnect {
690
694
  if (host) {
691
695
  logDebug('Found custom host. Using emulator');
692
696
  this.isEmulator = true;
693
- this.transportOptions = parseOptions(host);
697
+ this._transportOptions = parseOptions(host);
694
698
  }
695
699
  }
696
700
  }
@@ -707,27 +711,27 @@ class DataConnect {
707
711
  if (this.initialized) {
708
712
  return;
709
713
  }
710
- if (this.transportClass === undefined) {
714
+ if (this._transportClass === undefined) {
711
715
  logDebug('transportClass not provided. Defaulting to RESTTransport.');
712
- this.transportClass = RESTTransport;
716
+ this._transportClass = RESTTransport;
713
717
  }
714
- if (this.authProvider) {
715
- this.authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this.authProvider);
718
+ if (this._authProvider) {
719
+ this._authTokenProvider = new FirebaseAuthProvider(this.app.name, this.app.options, this._authProvider);
716
720
  }
717
721
  this.initialized = true;
718
- this._transport = new this.transportClass(this.dataConnectOptions, this.app.options.apiKey, this.authTokenProvider);
719
- if (this.transportOptions) {
720
- this._transport.useEmulator(this.transportOptions.host, this.transportOptions.port, this.transportOptions.sslEnabled);
722
+ this._transport = new this._transportClass(this.dataConnectOptions, this.app.options.apiKey, this._authTokenProvider);
723
+ if (this._transportOptions) {
724
+ this._transport.useEmulator(this._transportOptions.host, this._transportOptions.port, this._transportOptions.sslEnabled);
721
725
  }
722
726
  this._queryManager = new QueryManager(this._transport);
723
727
  this._mutationManager = new MutationManager(this._transport);
724
728
  }
725
729
  enableEmulator(transportOptions) {
726
730
  if (this.initialized) {
727
- logError('enableEmulator called without initializing');
731
+ logError('enableEmulator called after initialization');
728
732
  throw new DataConnectError(Code.ALREADY_INITIALIZED, 'DataConnect instance already initialized!');
729
733
  }
730
- this.transportOptions = transportOptions;
734
+ this._transportOptions = transportOptions;
731
735
  this.isEmulator = true;
732
736
  }
733
737
  }
@@ -770,7 +774,7 @@ function getDataConnect(appOrOptions, optionalOptions) {
770
774
  });
771
775
  }
772
776
  function terminate(dataConnect) {
773
- dataConnect._delete();
777
+ return dataConnect._delete();
774
778
  // TODO(mtewani): Stop pending tasks
775
779
  }
776
780
 
@@ -830,7 +834,7 @@ function queryRef(dcInstance, queryName, variables, initialCache) {
830
834
  dcInstance._queryManager.track(queryName, variables, initialCache);
831
835
  return {
832
836
  dataConnect: dcInstance,
833
- refType: QueryStr,
837
+ refType: QUERY_STR,
834
838
  name: queryName,
835
839
  variables: variables
836
840
  };
@@ -860,7 +864,7 @@ function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComp
860
864
  let ref;
861
865
  let initialCache;
862
866
  if ('refInfo' in queryRefOrSerializedResult) {
863
- let serializedRef = queryRefOrSerializedResult;
867
+ const serializedRef = queryRefOrSerializedResult;
864
868
  const { data, source, fetchTime } = serializedRef;
865
869
  initialCache = {
866
870
  data,
@@ -892,27 +896,7 @@ function subscribe(queryRefOrSerializedResult, observerOrOnNext, onError, onComp
892
896
  *
893
897
  * @packageDocumentation
894
898
  */
895
- registerDataConnect();
896
- // import { getDataConnect, queryRef } from './api';
897
- // import { getApp } from '@firebase/app';
898
- // const app = getApp();
899
- // const dc = getDataConnect({ location: '', connector: '', serviceId: '', projectId: '' });
900
- // interface Response {
901
- // name: string;
902
- // }
903
- // const converter: Converter<Response> = {
904
- // fromDataConnect(input: string) {
905
- // return { name: input };
906
- // },
907
- // fromType(input) {
908
- // return input;
909
- // }
910
- // };
911
- // const myRef = queryRef(dc, '', converter);
912
- // // Ref's shouldn't have access to their own cache, right?
913
- // const a = execute(myRef);
914
- // subscribe(myRef, (res) => {
915
- // })
899
+ registerDataConnect();
916
900
 
917
- 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 };
901
+ 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 };
918
902
  //# sourceMappingURL=index.esm2017.js.map