@hyper-fetch/firebase 6.0.0 → 7.0.0

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.
@@ -60,7 +60,7 @@ var RealtimeDBMethods = /* @__PURE__ */ ((RealtimeDBMethods2) => {
60
60
  })(RealtimeDBMethods || {});
61
61
 
62
62
  // src/adapter/adapter.ts
63
- import { getAdapterBindings } from "@hyper-fetch/core";
63
+ import { Adapter } from "@hyper-fetch/core";
64
64
  import { Database as Database3 } from "firebase/database";
65
65
 
66
66
  // src/realtime/utils/result.utils.ts
@@ -243,9 +243,16 @@ var mapRealtimeConstraint = ({ type, values }) => {
243
243
  };
244
244
 
245
245
  // src/realtime/realtime.sockets.ts
246
- import { getSocketAdapterBindings } from "@hyper-fetch/sockets";
246
+ import { SocketAdapter } from "@hyper-fetch/sockets";
247
247
  import { onValue, query, ref, goOffline, goOnline } from "firebase/database";
248
248
 
249
+ // src/firestore/firestore.types.ts
250
+ var FirestoreDocOrQuery = /* @__PURE__ */ ((FirestoreDocOrQuery2) => {
251
+ FirestoreDocOrQuery2["DOC"] = "doc";
252
+ FirestoreDocOrQuery2["QUERY"] = "query";
253
+ return FirestoreDocOrQuery2;
254
+ })(FirestoreDocOrQuery || {});
255
+
249
256
  // src/utils/misc.ts
250
257
  var getStatus = (res) => {
251
258
  return Array.isArray(res) && res.length === 0 || res == null ? "emptyResource" : "success";
@@ -253,98 +260,92 @@ var getStatus = (res) => {
253
260
  var isDocOrQuery = (fullUrl) => {
254
261
  const withoutSurroundingSlashes = fullUrl.replace(/^\/|\/$/g, "");
255
262
  const pathElements = withoutSurroundingSlashes.split("/").length;
256
- return pathElements % 2 === 0 ? "doc" : "query";
263
+ return pathElements % 2 === 0 ? "doc" /* DOC */ : "query" /* QUERY */;
257
264
  };
258
265
 
259
266
  // src/realtime/realtime.sockets.ts
260
267
  var realtimeSockets = (database) => {
261
- return (socket) => {
262
- const {
263
- open,
264
- connecting,
265
- reconnectionAttempts,
266
- listeners,
267
- removeListener,
268
- onConnect,
269
- onReconnect,
270
- onDisconnect,
271
- onListen,
272
- onOpen,
273
- onClose,
274
- onEvent,
275
- onError
276
- } = getSocketAdapterBindings(socket, { open: true });
277
- const connect = () => {
278
- const enabled = onConnect();
279
- if (enabled) {
280
- goOnline(database);
281
- onOpen();
282
- }
283
- };
284
- const disconnect = () => {
285
- goOffline(database);
286
- onDisconnect();
287
- onClose();
288
- };
289
- const reconnect = () => {
290
- onReconnect(disconnect, connect);
291
- };
292
- const listen = (listener, callback) => {
293
- var _a;
294
- const fullUrl = socket.url + listener.endpoint;
295
- const path = ref(database, fullUrl);
296
- const { options } = listener;
297
- const onlyOnce = (options == null ? void 0 : options.onlyOnce) || false;
298
- const params = ((_a = options == null ? void 0 : options.constraints) == null ? void 0 : _a.map((constraint) => mapRealtimeConstraint(constraint))) || [];
299
- const queryConstraints = query(path, ...params);
300
- let unsubscribe = () => {
301
- };
302
- let unmount = () => {
268
+ return new SocketAdapter({
269
+ name: "firebase-realtime",
270
+ defaultConnected: true
271
+ }).setConnector(
272
+ ({ socket, onConnect, onReconnect, onDisconnect, onListen, onConnected, onDisconnected, onEvent, onError }) => {
273
+ const connect = () => __async(void 0, null, function* () {
274
+ const enabled = onConnect();
275
+ if (enabled) {
276
+ goOnline(database);
277
+ onConnected();
278
+ }
279
+ });
280
+ const disconnect = () => __async(void 0, null, function* () {
281
+ goOffline(database);
282
+ onDisconnect();
283
+ onDisconnected();
284
+ });
285
+ const reconnect = () => {
286
+ onReconnect({ disconnect, connect });
303
287
  };
304
- let clearListeners = () => {
288
+ const listen = (listener, callback) => {
289
+ var _a;
290
+ const fullUrl = socket.url + listener.topic;
291
+ const path = ref(database, fullUrl);
292
+ const { options } = listener;
293
+ const onlyOnce = (options == null ? void 0 : options.onlyOnce) || false;
294
+ const params = ((_a = options == null ? void 0 : options.constraints) == null ? void 0 : _a.map((constraint) => mapRealtimeConstraint(constraint))) || [];
295
+ const queryConstraints = query(path, ...params);
296
+ let unsubscribe = () => {
297
+ };
298
+ let unmount = () => {
299
+ };
300
+ let clearListeners = () => {
301
+ };
302
+ unsubscribe = onValue(
303
+ queryConstraints,
304
+ (snapshot) => {
305
+ const response = isDocOrQuery(fullUrl) === "doc" ? snapshot.val() : getOrderedResultRealtime(snapshot);
306
+ const status = getStatus(response);
307
+ const extra = { ref: path, snapshot, status };
308
+ callback({ data: response, extra });
309
+ onEvent({ topic: listener.topic, data: response, extra });
310
+ },
311
+ (error) => {
312
+ onError({ error });
313
+ },
314
+ { onlyOnce }
315
+ );
316
+ unmount = onListen({ listener, callback, onUnmount: unsubscribe });
317
+ clearListeners = () => {
318
+ unsubscribe();
319
+ unmount();
320
+ };
321
+ return clearListeners;
305
322
  };
306
- unsubscribe = onValue(
307
- queryConstraints,
308
- (snapshot) => {
309
- const response = isDocOrQuery(fullUrl) === "doc" ? snapshot.val() : getOrderedResultRealtime(snapshot);
310
- const status = getStatus(response);
311
- const extra = { ref: path, snapshot, status };
312
- callback({ data: response, extra });
313
- onEvent(listener.endpoint, response, extra);
314
- },
315
- (error) => {
316
- onError(error);
317
- },
318
- { onlyOnce }
319
- );
320
- unmount = onListen(listener, callback, unsubscribe);
321
- clearListeners = () => {
322
- unsubscribe();
323
- unmount();
323
+ const emit = () => __async(void 0, null, function* () {
324
+ throw new Error("Cannot emit from Realtime database socket.");
325
+ });
326
+ return {
327
+ connect,
328
+ reconnect,
329
+ disconnect,
330
+ listen,
331
+ emit
324
332
  };
325
- return clearListeners;
326
- };
327
- const emit = () => __async(void 0, null, function* () {
328
- throw new Error("Cannot emit from Realtime database socket.");
329
- });
330
- return {
331
- open,
332
- reconnectionAttempts,
333
- listeners,
334
- connecting,
335
- listen,
336
- removeListener,
337
- emit,
338
- connect,
339
- reconnect,
340
- disconnect
341
- };
342
- };
333
+ }
334
+ );
343
335
  };
344
336
 
345
337
  // src/realtime/realtime.methods.ts
346
338
  import { get, push, query as query2, ref as ref2, remove, set, update } from "firebase/database";
347
- var getRealtimeDbBrowserMethods = (request, database, url, onSuccess, onError, resolve, events) => {
339
+ var getRealtimeDbBrowserMethods = ({
340
+ database,
341
+ url,
342
+ onSuccess,
343
+ onError,
344
+ onResponseStart,
345
+ onRequestStart,
346
+ onRequestEnd,
347
+ onResponseEnd
348
+ }) => {
348
349
  const [fullUrl] = url.split("?");
349
350
  const path = ref2(database, fullUrl);
350
351
  const methods = {
@@ -377,17 +378,24 @@ var getRealtimeDbBrowserMethods = (request, database, url, onSuccess, onError, r
377
378
  };
378
379
  return (methodName, data) => __async(void 0, null, function* () {
379
380
  try {
380
- events.onRequestStart();
381
+ onRequestStart();
381
382
  const { result, status, extra } = yield methods[methodName](data);
382
- events.onRequestEnd();
383
- events.onResponseStart();
384
- onSuccess(result, status, extra, resolve);
385
- events.onResponseEnd();
386
- } catch (e) {
387
- events.onRequestEnd();
388
- events.onResponseStart();
389
- onError(e, "error", {}, resolve);
390
- events.onResponseEnd();
383
+ onRequestEnd();
384
+ onResponseStart();
385
+ onSuccess({ data: result, status, extra });
386
+ onResponseEnd();
387
+ } catch (error) {
388
+ onRequestEnd();
389
+ onResponseStart();
390
+ onError({
391
+ error,
392
+ status: "error",
393
+ // TODO - fix this
394
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
395
+ // @ts-ignore
396
+ extra: {}
397
+ });
398
+ onResponseEnd();
391
399
  }
392
400
  });
393
401
  };
@@ -398,10 +406,14 @@ var getOrderedResultFirestore = (snapshot) => {
398
406
  snapshot.docs.forEach((d) => {
399
407
  result.push(__spreadProps(__spreadValues({}, d.data()), { __key: d.id }));
400
408
  });
401
- return result.length > 0 ? result : null;
409
+ return result;
402
410
  };
403
411
  var getGroupedResultFirestore = (snapshot) => {
404
- const groupedResult = { added: [], modified: [], removed: [] };
412
+ const groupedResult = {
413
+ added: [],
414
+ modified: [],
415
+ removed: []
416
+ };
405
417
  snapshot.docChanges().forEach((change) => {
406
418
  groupedResult[change.type].push(change.doc.data());
407
419
  });
@@ -457,7 +469,16 @@ import {
457
469
  setDoc,
458
470
  updateDoc
459
471
  } from "firebase/firestore";
460
- var getFirestoreBrowserMethods = (request, database, url, onSuccess, onError, resolve, events) => {
472
+ var getFirestoreBrowserMethods = ({
473
+ database,
474
+ url,
475
+ onSuccess,
476
+ onError,
477
+ onResponseStart,
478
+ onRequestStart,
479
+ onRequestEnd,
480
+ onResponseEnd
481
+ }) => {
461
482
  const [cleanUrl] = url.split("?");
462
483
  const methods = {
463
484
  getDoc: () => __async(void 0, null, function* () {
@@ -502,156 +523,146 @@ var getFirestoreBrowserMethods = (request, database, url, onSuccess, onError, re
502
523
  };
503
524
  return (methodName, data) => __async(void 0, null, function* () {
504
525
  try {
505
- events.onRequestStart();
526
+ onRequestStart();
506
527
  const { result, status, extra } = yield methods[methodName](data);
507
- events.onRequestEnd();
508
- events.onResponseStart();
509
- onSuccess(result, status, extra, resolve);
510
- events.onResponseEnd();
511
- } catch (e) {
512
- events.onRequestEnd();
513
- events.onResponseStart();
514
- onError(e, "error", {}, resolve);
515
- events.onResponseEnd();
528
+ onRequestEnd();
529
+ onResponseStart();
530
+ onSuccess({ data: result, status, extra });
531
+ onResponseEnd();
532
+ } catch (error) {
533
+ onRequestEnd();
534
+ onResponseStart();
535
+ onError({ error, status: "error", extra: {} });
536
+ onResponseEnd();
516
537
  }
517
538
  });
518
539
  };
519
540
 
520
541
  // src/firestore/firestore.sockets.ts
521
- import { getSocketAdapterBindings as getSocketAdapterBindings2 } from "@hyper-fetch/sockets";
522
- import { onSnapshot, doc as doc2, query as query4, collection as collection2, disableNetwork, enableNetwork } from "firebase/firestore";
542
+ import { SocketAdapter as SocketAdapter2 } from "@hyper-fetch/sockets";
543
+ import {
544
+ collection as collection2,
545
+ disableNetwork,
546
+ doc as doc2,
547
+ enableNetwork,
548
+ onSnapshot,
549
+ query as query4
550
+ } from "firebase/firestore";
523
551
  var firestoreSockets = (database) => {
524
- return (socket) => {
525
- const {
526
- open,
527
- connecting,
528
- reconnectionAttempts,
529
- listeners,
530
- removeListener,
531
- onConnect,
532
- onReconnect,
533
- onDisconnect,
534
- onListen,
535
- onOpen,
536
- onClose,
537
- onEvent,
538
- onError
539
- } = getSocketAdapterBindings2(socket, { open: true });
540
- const connect = () => {
541
- const enabled = onConnect();
542
- if (enabled) {
543
- enableNetwork(database);
544
- onOpen();
545
- }
546
- };
547
- const disconnect = () => {
548
- disableNetwork(database);
549
- onDisconnect();
550
- onClose();
551
- };
552
- const reconnect = () => {
553
- onReconnect(disconnect, connect);
554
- };
555
- const listen = (listener, callback) => {
556
- const fullUrl = socket.url + listener.endpoint;
557
- const { options } = listener;
558
- let path;
559
- const queryType = isDocOrQuery(fullUrl);
560
- if (queryType === "doc") {
561
- path = doc2(database, fullUrl);
562
- } else {
563
- const constraints = (options == null ? void 0 : options.constraints) || [];
564
- const queryConstraints = constraints.map((constr) => mapConstraint(constr)) || [];
565
- path = query4(collection2(database, fullUrl), ...queryConstraints);
566
- }
567
- let unsubscribe = () => {
568
- };
569
- let unmount = () => {
570
- };
571
- let clearListeners = () => {
552
+ return new SocketAdapter2(
553
+ {
554
+ name: "firebase-firestore",
555
+ defaultConnected: true
556
+ }
557
+ ).setConnector(
558
+ ({ socket, onConnect, onReconnect, onDisconnect, onListen, onConnected, onDisconnected, onEvent, onError }) => {
559
+ const connect = () => __async(void 0, null, function* () {
560
+ const enabled = onConnect();
561
+ if (enabled) {
562
+ enableNetwork(database);
563
+ onConnected();
564
+ }
565
+ });
566
+ const disconnect = () => __async(void 0, null, function* () {
567
+ disableNetwork(database);
568
+ onDisconnect();
569
+ onDisconnected();
570
+ });
571
+ const reconnect = () => {
572
+ onReconnect({ disconnect, connect });
572
573
  };
573
- unsubscribe = onSnapshot(
574
- path,
575
- (snapshot) => {
576
- const getDocData = (s) => s.data() ? __spreadProps(__spreadValues({}, s.data()), { __key: s.id }) : null;
577
- const response = queryType === "doc" ? getDocData(snapshot) : getOrderedResultFirestore(snapshot);
578
- const status = getStatus(response);
579
- const groupedResult = (options == null ? void 0 : options.groupByChangeType) === true ? getGroupedResultFirestore(snapshot) : null;
580
- const extra = { ref: path, snapshot, groupedResult, status };
581
- callback({ data: response, extra });
582
- onEvent(listener.endpoint, response, extra);
583
- },
584
- (error) => {
585
- onError(error);
574
+ const listen = (listener, callback) => {
575
+ const fullUrl = socket.url + listener.topic;
576
+ const { options } = listener;
577
+ let path;
578
+ const queryType = isDocOrQuery(fullUrl);
579
+ if (queryType === "doc" /* DOC */) {
580
+ path = doc2(database, fullUrl);
581
+ } else {
582
+ const constraints = (options == null ? void 0 : options.constraints) || [];
583
+ const queryConstraints = constraints.map((constr) => mapConstraint(constr)) || [];
584
+ path = query4(collection2(database, fullUrl), ...queryConstraints);
586
585
  }
587
- );
588
- unmount = onListen(listener, callback, unsubscribe);
589
- clearListeners = () => {
590
- unsubscribe();
591
- unmount();
586
+ let unsubscribe = () => {
587
+ };
588
+ let unmount = () => {
589
+ };
590
+ let clearListeners = () => {
591
+ };
592
+ unsubscribe = onSnapshot(
593
+ path,
594
+ (snapshot) => {
595
+ const getDocData = (s) => s.data() ? __spreadProps(__spreadValues({}, s.data()), { __key: s.id }) : null;
596
+ const response = queryType === "doc" /* DOC */ ? getDocData(snapshot) : getOrderedResultFirestore(snapshot);
597
+ const status = getStatus(response);
598
+ const groupedResult = (options == null ? void 0 : options.groupByChangeType) === true ? getGroupedResultFirestore(snapshot) : null;
599
+ const extra = { ref: path, snapshot, groupedResult, status };
600
+ callback({ data: response, extra });
601
+ onEvent({ topic: listener.topic, data: response, extra });
602
+ },
603
+ (error) => {
604
+ onError({ error });
605
+ }
606
+ );
607
+ unmount = onListen({ listener, callback, onUnmount: unsubscribe });
608
+ clearListeners = () => {
609
+ unsubscribe();
610
+ unmount();
611
+ };
612
+ return clearListeners;
592
613
  };
593
- return clearListeners;
594
- };
595
- const emit = () => __async(void 0, null, function* () {
596
- throw new Error("Cannot emit from Firestore database socket.");
597
- });
598
- return {
599
- open,
600
- reconnectionAttempts,
601
- listeners,
602
- connecting,
603
- listen,
604
- removeListener,
605
- emit,
606
- connect,
607
- reconnect,
608
- disconnect
609
- };
610
- };
614
+ const emit = () => __async(void 0, null, function* () {
615
+ throw new Error("Cannot emit from Firestore database socket.");
616
+ });
617
+ return {
618
+ connect,
619
+ reconnect,
620
+ disconnect,
621
+ listen,
622
+ emit
623
+ };
624
+ }
625
+ );
611
626
  };
612
627
 
613
628
  // src/adapter/adapter.ts
614
- var firebaseAdapter = (database) => () => {
615
- const adapter = (request, requestId) => __async(void 0, null, function* () {
616
- const { fullUrl, onSuccess, onError, onResponseStart, onResponseEnd, onRequestStart, onRequestEnd } = yield getAdapterBindings(request, requestId, "error", {});
617
- return new Promise((resolve) => {
629
+ var FirebaseAdapter = (database) => {
630
+ return new Adapter({
631
+ name: "firebase",
632
+ defaultMethod: "getDoc",
633
+ defaultExtra: {},
634
+ systemErrorStatus: "error",
635
+ systemErrorExtra: {}
636
+ }).setFetcher(
637
+ (_0) => __async(void 0, [_0], function* ({ request, onSuccess, onError, onResponseStart, onResponseEnd, onRequestStart, onRequestEnd }) {
638
+ const fullUrl = `${request.client.url}${request.endpoint}`;
618
639
  if (database instanceof Database3) {
619
- const {
620
- method = "get" /* get */,
621
- queryParams,
622
- data,
623
- options
624
- } = request;
625
- const availableMethods = getRealtimeDbBrowserMethods(
626
- request,
640
+ const { method = "get" /* get */, queryParams, payload, options } = request;
641
+ const availableMethods = getRealtimeDbBrowserMethods({
627
642
  database,
628
- fullUrl,
643
+ url: fullUrl,
629
644
  onSuccess,
630
645
  onError,
631
- resolve,
632
- {
633
- onResponseStart,
634
- onResponseEnd,
635
- onRequestStart,
636
- onRequestEnd
637
- }
638
- );
646
+ onResponseStart,
647
+ onResponseEnd,
648
+ onRequestStart,
649
+ onRequestEnd
650
+ });
639
651
  if (!Object.values(RealtimeDBMethods).includes(method)) {
640
652
  throw new Error(`Cannot find method ${method} in Realtime database available methods.`);
641
653
  }
642
654
  availableMethods(method, {
643
655
  constraints: (queryParams == null ? void 0 : queryParams.constraints) || [],
644
656
  options,
645
- data
657
+ payload
646
658
  });
647
659
  } else {
648
- const {
649
- method = "getDocs" /* getDocs */,
650
- queryParams,
651
- data,
652
- options
653
- } = request;
654
- const availableMethods = getFirestoreBrowserMethods(request, database, fullUrl, onSuccess, onError, resolve, {
660
+ const { method = "getDocs" /* getDocs */, queryParams, payload, options } = request;
661
+ const availableMethods = getFirestoreBrowserMethods({
662
+ database,
663
+ url: fullUrl,
664
+ onSuccess,
665
+ onError,
655
666
  onResponseStart,
656
667
  onResponseEnd,
657
668
  onRequestStart,
@@ -662,19 +673,18 @@ var firebaseAdapter = (database) => () => {
662
673
  }
663
674
  availableMethods(method, {
664
675
  constraints: (queryParams == null ? void 0 : queryParams.constraints) ? queryParams.constraints : [],
665
- data,
676
+ payload,
666
677
  options
667
678
  });
668
679
  }
669
- });
670
- });
671
- return adapter;
680
+ })
681
+ );
672
682
  };
673
683
 
674
684
  // src/adapter/adapter.sockets.ts
675
685
  import { Database as Database4 } from "firebase/database";
676
686
  import { Firestore as Firestore3 } from "firebase/firestore";
677
- var firebaseSocketsAdapter = (database) => {
687
+ var FirebaseSocketsAdapter = (database) => {
678
688
  if (database instanceof Database4) {
679
689
  return realtimeSockets(database);
680
690
  }
@@ -697,13 +707,14 @@ export {
697
707
  $startAfter,
698
708
  $startAt,
699
709
  $where,
710
+ FirebaseAdapter,
711
+ FirebaseSocketsAdapter,
712
+ FirestoreDocOrQuery,
700
713
  FirestoreMethods,
701
714
  FirestoreQueryConstraints,
702
715
  RealtimeDBMethods,
703
716
  RealtimeQueryConstraints,
704
717
  SharedQueryConstraints,
705
- firebaseAdapter,
706
- firebaseSocketsAdapter,
707
718
  firestoreSockets,
708
719
  getFirestoreBrowserMethods,
709
720
  getGroupedResultFirestore,
@@ -714,4 +725,4 @@ export {
714
725
  mapRealtimeConstraint,
715
726
  realtimeSockets
716
727
  };
717
- //# sourceMappingURL=index.esm.js.map
728
+ //# sourceMappingURL=index.js.map