@hyper-fetch/firebase-admin 7.5.3 → 8.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.
Files changed (115) hide show
  1. package/.eslintrc.cjs +12 -0
  2. package/__tests__/features/firestore/admin/constraints.admin.spec.ts +1 -1
  3. package/__tests__/features/firestore/admin/methods.admin.spec.ts +1 -1
  4. package/__tests__/features/firestore/admin/sockets.base.spec.ts +1 -1
  5. package/__tests__/features/firestore/shared/constraints.shared.tests.ts +3 -3
  6. package/__tests__/features/firestore/shared/methods/add-doc.test.suite.ts +3 -3
  7. package/__tests__/features/firestore/shared/methods/delete-doc.test.suite.ts +2 -2
  8. package/__tests__/features/firestore/shared/methods/get-doc.test.suite.ts +2 -2
  9. package/__tests__/features/firestore/shared/methods/get-docs.test.suite.ts +2 -2
  10. package/__tests__/features/firestore/shared/methods/on-snapshot.test.suite.ts +9 -9
  11. package/__tests__/features/firestore/shared/methods/set-doc.test.suite.ts +2 -2
  12. package/__tests__/features/firestore/shared/methods/update-doc.test.suite.ts +2 -2
  13. package/__tests__/features/firestore/shared/methods.shared.tests.ts +1 -1
  14. package/__tests__/features/realtime/admin/methods.admin.spec.ts +1 -1
  15. package/__tests__/features/realtime/admin/sockets.base.spec.ts +1 -1
  16. package/__tests__/features/realtime/shared/constraints.shared.tests.ts +3 -5
  17. package/__tests__/features/realtime/shared/methods/get.test.suite.ts +3 -3
  18. package/__tests__/features/realtime/shared/methods/on-value.test.suite.ts +9 -9
  19. package/__tests__/features/realtime/shared/methods/push.test.suite.ts +2 -2
  20. package/__tests__/features/realtime/shared/methods/remove.test.suite.ts +2 -2
  21. package/__tests__/features/realtime/shared/methods/set.test.suite.ts +9 -7
  22. package/__tests__/features/realtime/shared/methods/update.test.suite.ts +2 -2
  23. package/__tests__/features/realtime/shared/methods.shared.tests.ts +1 -1
  24. package/__tests__/shared/request-events.shared.ts +10 -10
  25. package/__tests__/tsconfig.json +15 -2
  26. package/__tests__/utils/seed.admin.ts +2 -2
  27. package/__tests__/vitest.setup.ts +9 -0
  28. package/dist/adapter/adapter.d.ts +3 -0
  29. package/dist/adapter/adapter.d.ts.map +1 -0
  30. package/dist/adapter/adapter.sockets.d.ts +3 -0
  31. package/dist/adapter/adapter.sockets.d.ts.map +1 -0
  32. package/dist/adapter/index.d.ts +4 -0
  33. package/dist/adapter/index.d.ts.map +1 -0
  34. package/dist/adapter/types/adapter.base.types.d.ts +11 -0
  35. package/dist/adapter/types/adapter.base.types.d.ts.map +1 -0
  36. package/dist/adapter/types/adapter.firestore.types.d.ts +45 -0
  37. package/dist/adapter/types/adapter.firestore.types.d.ts.map +1 -0
  38. package/dist/adapter/types/adapter.realtime.types.d.ts +39 -0
  39. package/dist/adapter/types/adapter.realtime.types.d.ts.map +1 -0
  40. package/dist/adapter/types/firestore.socket.types.d.ts +17 -0
  41. package/dist/adapter/types/firestore.socket.types.d.ts.map +1 -0
  42. package/dist/adapter/types/index.d.ts +6 -0
  43. package/dist/adapter/types/index.d.ts.map +1 -0
  44. package/dist/adapter/types/realtime.socket.types.d.ts +12 -0
  45. package/dist/adapter/types/realtime.socket.types.d.ts.map +1 -0
  46. package/dist/constraints/constraints.types.d.ts +13 -0
  47. package/dist/constraints/constraints.types.d.ts.map +1 -0
  48. package/dist/constraints/firebase.constraints.d.ts +86 -0
  49. package/dist/constraints/firebase.constraints.d.ts.map +1 -0
  50. package/dist/constraints/index.d.ts +3 -0
  51. package/dist/constraints/index.d.ts.map +1 -0
  52. package/dist/firestore/firestore.methods.d.ts +21 -0
  53. package/dist/firestore/firestore.methods.d.ts.map +1 -0
  54. package/dist/firestore/firestore.sockets.d.ts +4 -0
  55. package/dist/firestore/firestore.sockets.d.ts.map +1 -0
  56. package/dist/firestore/index.d.ts +4 -0
  57. package/dist/firestore/index.d.ts.map +1 -0
  58. package/dist/firestore/utils/constraints.utils.d.ts +5 -0
  59. package/dist/firestore/utils/constraints.utils.d.ts.map +1 -0
  60. package/dist/firestore/utils/index.d.ts +4 -0
  61. package/dist/firestore/utils/index.d.ts.map +1 -0
  62. package/dist/firestore/utils/ref.utils.d.ts +3 -0
  63. package/dist/firestore/utils/ref.utils.d.ts.map +1 -0
  64. package/dist/firestore/utils/result.utils.d.ts +10 -0
  65. package/dist/firestore/utils/result.utils.d.ts.map +1 -0
  66. package/dist/index.d.ts +5 -271
  67. package/dist/index.d.ts.map +1 -0
  68. package/dist/index.mjs +670 -0
  69. package/dist/index.mjs.map +1 -0
  70. package/dist/realtime/index.d.ts +4 -0
  71. package/dist/realtime/index.d.ts.map +1 -0
  72. package/dist/realtime/realtime.methods.d.ts +19 -0
  73. package/dist/realtime/realtime.methods.d.ts.map +1 -0
  74. package/dist/realtime/realtime.sockets.d.ts +4 -0
  75. package/dist/realtime/realtime.sockets.d.ts.map +1 -0
  76. package/dist/realtime/utils/constraints.utils.d.ts +5 -0
  77. package/dist/realtime/utils/constraints.utils.d.ts.map +1 -0
  78. package/dist/realtime/utils/index.d.ts +3 -0
  79. package/dist/realtime/utils/index.d.ts.map +1 -0
  80. package/dist/realtime/utils/result.utils.d.ts +3 -0
  81. package/dist/realtime/utils/result.utils.d.ts.map +1 -0
  82. package/dist/utils/cache.utils.d.ts +11 -0
  83. package/dist/utils/cache.utils.d.ts.map +1 -0
  84. package/dist/utils/index.d.ts +3 -0
  85. package/dist/utils/index.d.ts.map +1 -0
  86. package/dist/utils/misc.d.ts +3 -0
  87. package/dist/utils/misc.d.ts.map +1 -0
  88. package/package.json +8 -11
  89. package/src/adapter/adapter.sockets.ts +1 -1
  90. package/src/adapter/adapter.ts +13 -14
  91. package/src/adapter/types/adapter.base.types.ts +4 -4
  92. package/src/adapter/types/adapter.firestore.types.ts +4 -4
  93. package/src/adapter/types/adapter.realtime.types.ts +4 -4
  94. package/src/adapter/types/firestore.socket.types.ts +3 -3
  95. package/src/adapter/types/realtime.socket.types.ts +3 -3
  96. package/src/constraints/constraints.types.ts +1 -1
  97. package/src/constraints/firebase.constraints.ts +1 -1
  98. package/src/firestore/firestore.methods.ts +4 -4
  99. package/src/firestore/firestore.sockets.ts +3 -9
  100. package/src/firestore/utils/constraints.utils.ts +4 -3
  101. package/src/firestore/utils/ref.utils.ts +1 -1
  102. package/src/firestore/utils/result.utils.ts +1 -1
  103. package/src/realtime/realtime.methods.ts +4 -4
  104. package/src/realtime/realtime.sockets.ts +2 -2
  105. package/src/realtime/utils/constraints.utils.ts +3 -2
  106. package/src/realtime/utils/result.utils.ts +1 -1
  107. package/src/utils/cache.utils.ts +4 -2
  108. package/tsconfig.json +1 -1
  109. package/vite.config.ts +45 -0
  110. package/.eslintrc.json +0 -11
  111. package/__tests__/global.d.ts +0 -1
  112. package/__tests__/jest.setup.ts +0 -8
  113. package/dist/index.js +0 -686
  114. package/dist/index.js.map +0 -7
  115. package/jest.config.ts +0 -9
package/dist/index.mjs ADDED
@@ -0,0 +1,670 @@
1
+ import { CollectionReference, DocumentSnapshot, Firestore } from "firebase-admin/firestore";
2
+ import { SocketAdapter } from "@hyper-fetch/sockets";
3
+ import { Adapter } from "@hyper-fetch/core";
4
+ //#region src/adapter/types/adapter.firestore.types.ts
5
+ var FirestoreMethods = /* @__PURE__ */ function(FirestoreMethods) {
6
+ FirestoreMethods["addDoc"] = "addDoc";
7
+ FirestoreMethods["getDoc"] = "getDoc";
8
+ FirestoreMethods["getDocs"] = "getDocs";
9
+ FirestoreMethods["setDoc"] = "setDoc";
10
+ FirestoreMethods["updateDoc"] = "updateDoc";
11
+ FirestoreMethods["deleteDoc"] = "deleteDoc";
12
+ return FirestoreMethods;
13
+ }({});
14
+ //#endregion
15
+ //#region src/adapter/types/adapter.realtime.types.ts
16
+ var RealtimeDBMethods = /* @__PURE__ */ function(RealtimeDBMethods) {
17
+ RealtimeDBMethods["set"] = "set";
18
+ RealtimeDBMethods["push"] = "push";
19
+ RealtimeDBMethods["update"] = "update";
20
+ RealtimeDBMethods["get"] = "get";
21
+ RealtimeDBMethods["remove"] = "remove";
22
+ return RealtimeDBMethods;
23
+ }({});
24
+ //#endregion
25
+ //#region src/firestore/utils/result.utils.ts
26
+ var getOrderedResultFirestore = (snapshot) => {
27
+ const result = [];
28
+ snapshot.docs.forEach((d) => {
29
+ result.push({
30
+ ...d.data(),
31
+ __key: d.id
32
+ });
33
+ });
34
+ return result;
35
+ };
36
+ var getGroupedResultFirestore = (snapshot) => {
37
+ const groupedResult = {
38
+ added: [],
39
+ modified: [],
40
+ removed: []
41
+ };
42
+ snapshot.docChanges().forEach((change) => {
43
+ groupedResult[change.type].push(change.doc.data());
44
+ });
45
+ return [
46
+ ...groupedResult.added,
47
+ ...groupedResult.modified,
48
+ ...groupedResult.removed
49
+ ].length > 0 ? groupedResult : null;
50
+ };
51
+ //#endregion
52
+ //#region src/firestore/utils/ref.utils.ts
53
+ var getRef = (db, fullUrl) => {
54
+ return fullUrl.replace(/^\/|\/$/g, "").split("/").map((element, index) => {
55
+ return index % 2 === 0 ? ["collection", element] : ["doc", element];
56
+ }).reduce((_db, value) => {
57
+ const [method, pathPart] = value;
58
+ if (method === "doc" && "doc" in _db) return _db.doc(pathPart);
59
+ if (method === "collection" && "collection" in _db) return _db.collection(pathPart);
60
+ return _db;
61
+ }, db);
62
+ };
63
+ //#endregion
64
+ //#region src/constraints/firebase.constraints.ts
65
+ var SharedQueryConstraints = /* @__PURE__ */ function(SharedQueryConstraints) {
66
+ SharedQueryConstraints["START_AT"] = "startAt";
67
+ SharedQueryConstraints["START_AFTER"] = "startAfter";
68
+ SharedQueryConstraints["END_AT"] = "endAt";
69
+ SharedQueryConstraints["END_BEFORE"] = "endBefore";
70
+ return SharedQueryConstraints;
71
+ }({});
72
+ var RealtimeQueryConstraints = /* @__PURE__ */ function(RealtimeQueryConstraints) {
73
+ RealtimeQueryConstraints["ORDER_BY_CHILD"] = "orderByChild";
74
+ RealtimeQueryConstraints["ORDER_BY_KEY"] = "orderByKey";
75
+ RealtimeQueryConstraints["ORDER_BY_VALUE"] = "orderByValue";
76
+ RealtimeQueryConstraints["LIMIT_TO_FIRST"] = "limitToFirst";
77
+ RealtimeQueryConstraints["LIMIT_TO_LAST"] = "limitToLast";
78
+ RealtimeQueryConstraints["EQUAL_TO"] = "equalTo";
79
+ return RealtimeQueryConstraints;
80
+ }({});
81
+ var FirestoreQueryConstraints = /* @__PURE__ */ function(FirestoreQueryConstraints) {
82
+ FirestoreQueryConstraints["WHERE"] = "where";
83
+ FirestoreQueryConstraints["ORDER_BY"] = "orderBy";
84
+ FirestoreQueryConstraints["LIMIT"] = "limit";
85
+ return FirestoreQueryConstraints;
86
+ }({});
87
+ var $where = (fieldPath, opStr, value) => {
88
+ return {
89
+ toString: () => `${FirestoreQueryConstraints.WHERE}_${fieldPath}${opStr}${value}`,
90
+ type: FirestoreQueryConstraints.WHERE,
91
+ values: [
92
+ fieldPath,
93
+ opStr,
94
+ value
95
+ ]
96
+ };
97
+ };
98
+ var $orderBy = (fieldPath, directionStr) => {
99
+ return {
100
+ toString: () => `${FirestoreQueryConstraints.ORDER_BY}_${fieldPath}_${directionStr || ""}`,
101
+ type: FirestoreQueryConstraints.ORDER_BY,
102
+ values: [fieldPath, directionStr]
103
+ };
104
+ };
105
+ var $limit = (num) => {
106
+ return {
107
+ toString: () => `${FirestoreQueryConstraints.LIMIT}_${num}`,
108
+ type: FirestoreQueryConstraints.LIMIT,
109
+ values: [num]
110
+ };
111
+ };
112
+ var startEndConstraintsImpl = (constraintType) => {
113
+ return (...docOrFields) => {
114
+ if (docOrFields.length === 1 && docOrFields[0].query) return {
115
+ toString: () => `${constraintType}_${docOrFields[0].query?._query?.memoizedTarget?.memoizedCanonicalId}`,
116
+ type: constraintType,
117
+ values: [docOrFields]
118
+ };
119
+ return {
120
+ toString: () => `${constraintType}_${docOrFields}`,
121
+ type: constraintType,
122
+ values: [docOrFields]
123
+ };
124
+ };
125
+ };
126
+ var $startAt = startEndConstraintsImpl(SharedQueryConstraints.START_AT);
127
+ var $startAfter = startEndConstraintsImpl(SharedQueryConstraints.START_AFTER);
128
+ var $endAt = startEndConstraintsImpl(SharedQueryConstraints.END_AT);
129
+ var $endBefore = startEndConstraintsImpl(SharedQueryConstraints.END_BEFORE);
130
+ var $orderByChild = (path) => {
131
+ return {
132
+ toString: () => `${RealtimeQueryConstraints.ORDER_BY_CHILD}_${path}`,
133
+ type: RealtimeQueryConstraints.ORDER_BY_CHILD,
134
+ values: [path]
135
+ };
136
+ };
137
+ var $orderByKey = () => {
138
+ return {
139
+ toString: () => `${RealtimeQueryConstraints.ORDER_BY_KEY}`,
140
+ type: RealtimeQueryConstraints.ORDER_BY_KEY,
141
+ values: []
142
+ };
143
+ };
144
+ var $orderByValue = () => {
145
+ return {
146
+ toString: () => `${RealtimeQueryConstraints.ORDER_BY_VALUE}`,
147
+ type: RealtimeQueryConstraints.ORDER_BY_VALUE,
148
+ values: []
149
+ };
150
+ };
151
+ var $limitToFirst = (num) => {
152
+ return {
153
+ toString: () => `${RealtimeQueryConstraints.LIMIT_TO_FIRST}_${num}`,
154
+ type: RealtimeQueryConstraints.LIMIT_TO_FIRST,
155
+ values: [num]
156
+ };
157
+ };
158
+ var $limitToLast = (num) => {
159
+ return {
160
+ toString: () => `${RealtimeQueryConstraints.LIMIT_TO_LAST}_${num}`,
161
+ type: RealtimeQueryConstraints.LIMIT_TO_LAST,
162
+ values: [num]
163
+ };
164
+ };
165
+ var $equalTo = (value) => {
166
+ return {
167
+ toString: () => `${RealtimeQueryConstraints.LIMIT_TO_LAST}_${value}`,
168
+ type: RealtimeQueryConstraints.EQUAL_TO,
169
+ values: [value]
170
+ };
171
+ };
172
+ //#endregion
173
+ //#region src/firestore/utils/constraints.utils.ts
174
+ var applyFireStoreAdminConstraint = (collectionRef, { type, values }) => {
175
+ switch (type) {
176
+ case FirestoreQueryConstraints.WHERE: {
177
+ const [fieldPath, strOp, value] = values;
178
+ return collectionRef.where(fieldPath, strOp, value);
179
+ }
180
+ case FirestoreQueryConstraints.ORDER_BY: {
181
+ const [field, ord] = values;
182
+ return collectionRef.orderBy(field, ord);
183
+ }
184
+ case FirestoreQueryConstraints.LIMIT: {
185
+ const [limitValue] = values;
186
+ return collectionRef.limit(limitValue);
187
+ }
188
+ case SharedQueryConstraints.START_AT: {
189
+ const [docOrFields] = values;
190
+ return collectionRef.startAt(docOrFields);
191
+ }
192
+ case SharedQueryConstraints.START_AFTER: {
193
+ const [docOrFields] = values;
194
+ return collectionRef.startAfter(docOrFields);
195
+ }
196
+ case SharedQueryConstraints.END_AT: {
197
+ const [docOrFields] = values;
198
+ return collectionRef.endAt(docOrFields);
199
+ }
200
+ case SharedQueryConstraints.END_BEFORE: {
201
+ const [docOrFields] = values;
202
+ return collectionRef.endBefore(docOrFields);
203
+ }
204
+ default: throw new Error(`Unknown method ${type}`);
205
+ }
206
+ };
207
+ var applyFireStoreAdminConstraints = (collectionRef, constraints) => {
208
+ return constraints.reduce((collection, constraint) => {
209
+ return applyFireStoreAdminConstraint(collection, constraint);
210
+ }, collectionRef);
211
+ };
212
+ //#endregion
213
+ //#region src/utils/misc.ts
214
+ var getStatus = (res) => {
215
+ return Array.isArray(res) && res.length === 0 || res == null ? "emptyResource" : "success";
216
+ };
217
+ var isDocOrQuery = (fullUrl) => {
218
+ return fullUrl.replace(/^\/|\/$/g, "").split("/").length % 2 === 0 ? "doc" : "query";
219
+ };
220
+ //#endregion
221
+ //#region src/firestore/firestore.sockets.ts
222
+ var firestoreAdminSockets = (database) => {
223
+ return new SocketAdapter({
224
+ name: "firebase-admin-firestore",
225
+ defaultConnected: true
226
+ }).setConnector(({ socket, onReconnect, onListen, onEvent, onError }) => {
227
+ const connect = () => {
228
+ throw new Error("Connect function is not implemented for Firestore Admin socket.");
229
+ };
230
+ const disconnect = () => {
231
+ throw new Error("Cannot disconnect from Firestore database socket. Use 'app.delete()' instead.");
232
+ };
233
+ const reconnect = () => {
234
+ onReconnect({
235
+ disconnect,
236
+ connect
237
+ });
238
+ };
239
+ const listen = (listener, callback) => {
240
+ const fullUrl = socket.url + listener.topic;
241
+ const { options } = listener;
242
+ let pathRef = getRef(database, fullUrl);
243
+ if (pathRef instanceof CollectionReference) pathRef = applyFireStoreAdminConstraints(pathRef, options?.constraints || []);
244
+ let unsubscribe = () => {};
245
+ let unmount = () => {};
246
+ let clearListeners = () => {};
247
+ unsubscribe = pathRef.onSnapshot((snapshot) => {
248
+ const getSnapshotData = (s) => s.data() ? {
249
+ ...s.data(),
250
+ __key: s.id
251
+ } : null;
252
+ const response = snapshot instanceof DocumentSnapshot ? getSnapshotData(snapshot) : getOrderedResultFirestore(snapshot);
253
+ const status = getStatus(response);
254
+ const groupedResult = options?.groupByChangeType === true ? getGroupedResultFirestore(snapshot) : null;
255
+ const extra = {
256
+ ref: pathRef,
257
+ snapshot,
258
+ unsubscribe,
259
+ groupedResult,
260
+ status
261
+ };
262
+ callback({
263
+ data: response,
264
+ extra
265
+ });
266
+ onEvent({
267
+ topic: listener.topic,
268
+ data: response,
269
+ extra
270
+ });
271
+ }, (error) => {
272
+ onError({ error });
273
+ });
274
+ unmount = onListen({
275
+ listener,
276
+ callback,
277
+ onUnmount: unsubscribe
278
+ });
279
+ clearListeners = () => {
280
+ unsubscribe();
281
+ unmount();
282
+ };
283
+ return clearListeners;
284
+ };
285
+ const emit = async () => {
286
+ throw new Error("Cannot emit from Firestore database socket.");
287
+ };
288
+ return {
289
+ connect,
290
+ reconnect,
291
+ disconnect,
292
+ emit,
293
+ listen
294
+ };
295
+ });
296
+ };
297
+ //#endregion
298
+ //#region src/firestore/firestore.methods.ts
299
+ var getFirestoreAdminMethods = ({ database, url, onSuccess, onError, onResponseStart, onRequestStart, onRequestEnd, onResponseEnd }) => {
300
+ const [cleanUrl] = url.split("?");
301
+ const methods = {
302
+ getDoc: async () => {
303
+ const path = getRef(database, cleanUrl);
304
+ const snapshot = await path.get();
305
+ const result = snapshot.data() ? {
306
+ ...snapshot.data(),
307
+ __key: snapshot.id
308
+ } : null;
309
+ return {
310
+ result,
311
+ status: result ? "success" : "emptyResource",
312
+ extra: {
313
+ ref: path,
314
+ snapshot
315
+ }
316
+ };
317
+ },
318
+ getDocs: async ({ constraints = [] }) => {
319
+ const path = getRef(database, cleanUrl);
320
+ const querySnapshot = await applyFireStoreAdminConstraints(path, constraints).get();
321
+ const result = getOrderedResultFirestore(querySnapshot);
322
+ return {
323
+ result,
324
+ status: getStatus(result),
325
+ extra: {
326
+ ref: path,
327
+ snapshot: querySnapshot
328
+ }
329
+ };
330
+ },
331
+ setDoc: async ({ data, options }) => {
332
+ const path = getRef(database, cleanUrl);
333
+ const merge = options?.merge === true;
334
+ return {
335
+ result: await path.set(data, { merge }),
336
+ status: "success",
337
+ extra: { ref: path }
338
+ };
339
+ },
340
+ addDoc: async ({ data }) => {
341
+ const docRef = await getRef(database, cleanUrl).add(data);
342
+ return {
343
+ result: {
344
+ ...data,
345
+ __key: docRef.id
346
+ },
347
+ status: "success",
348
+ extra: { ref: docRef }
349
+ };
350
+ },
351
+ updateDoc: async ({ data }) => {
352
+ const path = getRef(database, cleanUrl);
353
+ await path.update(data);
354
+ return {
355
+ result: data,
356
+ status: "success",
357
+ extra: { ref: path }
358
+ };
359
+ },
360
+ deleteDoc: async () => {
361
+ const path = getRef(database, cleanUrl);
362
+ await path.delete();
363
+ return {
364
+ result: null,
365
+ status: "success",
366
+ extra: { ref: path }
367
+ };
368
+ }
369
+ };
370
+ return async (methodName, data) => {
371
+ try {
372
+ onRequestStart();
373
+ const { result, status, extra } = await methods[methodName](data);
374
+ onRequestEnd();
375
+ onResponseStart();
376
+ onSuccess({
377
+ data: result,
378
+ status,
379
+ extra
380
+ });
381
+ onResponseEnd();
382
+ } catch (error) {
383
+ onRequestEnd();
384
+ onResponseStart();
385
+ onError({
386
+ error,
387
+ status: "error",
388
+ extra: {}
389
+ });
390
+ onResponseEnd();
391
+ }
392
+ };
393
+ };
394
+ //#endregion
395
+ //#region src/realtime/utils/result.utils.ts
396
+ var getOrderedResultRealtime = (snapshot) => {
397
+ const res = [];
398
+ snapshot.forEach((child) => {
399
+ res.push({
400
+ ...child.val(),
401
+ __key: child.key
402
+ });
403
+ });
404
+ return res.length > 0 ? res : null;
405
+ };
406
+ //#endregion
407
+ //#region src/realtime/utils/constraints.utils.ts
408
+ var applyRealtimeAdminConstraint = (ref, { type, values }) => {
409
+ switch (type) {
410
+ case RealtimeQueryConstraints.ORDER_BY_CHILD: {
411
+ const [value] = values;
412
+ return ref.orderByChild(value);
413
+ }
414
+ case RealtimeQueryConstraints.ORDER_BY_KEY: return ref.orderByKey();
415
+ case RealtimeQueryConstraints.ORDER_BY_VALUE: return ref.orderByValue();
416
+ case SharedQueryConstraints.START_AT: {
417
+ const [[value]] = values;
418
+ return ref.startAt(value);
419
+ }
420
+ case SharedQueryConstraints.START_AFTER: {
421
+ const [[value]] = values;
422
+ return ref.startAfter(value);
423
+ }
424
+ case SharedQueryConstraints.END_AT: {
425
+ const [[value]] = values;
426
+ return ref.endAt(value);
427
+ }
428
+ case SharedQueryConstraints.END_BEFORE: {
429
+ const [[value]] = values;
430
+ return ref.endBefore(value);
431
+ }
432
+ case RealtimeQueryConstraints.LIMIT_TO_FIRST: {
433
+ const [value] = values;
434
+ return ref.limitToFirst(value);
435
+ }
436
+ case RealtimeQueryConstraints.LIMIT_TO_LAST: {
437
+ const [value] = values;
438
+ return ref.limitToLast(value);
439
+ }
440
+ case RealtimeQueryConstraints.EQUAL_TO: {
441
+ const [value] = values;
442
+ return ref.equalTo(value);
443
+ }
444
+ default: throw new Error(`Unknown method ${type}`);
445
+ }
446
+ };
447
+ var applyRealtimeAdminConstraints = (ref, constraints) => {
448
+ return constraints.reduce((collection, constraint) => {
449
+ return applyRealtimeAdminConstraint(collection, constraint);
450
+ }, ref);
451
+ };
452
+ //#endregion
453
+ //#region src/realtime/realtime.methods.ts
454
+ var getRealtimeDbAdminMethods = ({ database, url, onSuccess, onError, onResponseStart, onRequestStart, onRequestEnd, onResponseEnd }) => {
455
+ const [fullUrl] = url.split("?");
456
+ const path = database.ref(fullUrl);
457
+ const methods = {
458
+ get: async ({ constraints = [] }) => {
459
+ const docOrQuery = isDocOrQuery(fullUrl);
460
+ const snapshot = await applyRealtimeAdminConstraints(path, constraints).get();
461
+ const res = docOrQuery === "doc" ? snapshot.val() : getOrderedResultRealtime(snapshot);
462
+ return {
463
+ result: res,
464
+ status: getStatus(res),
465
+ extra: {
466
+ ref: path,
467
+ snapshot
468
+ }
469
+ };
470
+ },
471
+ set: async ({ data }) => {
472
+ await path.set(data);
473
+ return {
474
+ result: data,
475
+ status: "success",
476
+ extra: { ref: path }
477
+ };
478
+ },
479
+ push: async ({ data }) => {
480
+ const resRef = await path.push(data);
481
+ return {
482
+ result: {
483
+ ...data,
484
+ __key: resRef.key
485
+ },
486
+ status: "success",
487
+ extra: {
488
+ ref: resRef,
489
+ key: resRef.key
490
+ }
491
+ };
492
+ },
493
+ update: async ({ data }) => {
494
+ await path.update(data);
495
+ return {
496
+ result: data,
497
+ status: "success",
498
+ extra: { ref: path }
499
+ };
500
+ },
501
+ remove: async () => {
502
+ await path.remove();
503
+ return {
504
+ result: null,
505
+ status: "success",
506
+ extra: { ref: path }
507
+ };
508
+ }
509
+ };
510
+ return async (methodName, data) => {
511
+ try {
512
+ onRequestStart();
513
+ const { result, status, extra } = await methods[methodName](data);
514
+ onRequestEnd();
515
+ onResponseStart();
516
+ onSuccess({
517
+ data: result,
518
+ status,
519
+ extra
520
+ });
521
+ onResponseEnd();
522
+ } catch (error) {
523
+ onRequestEnd();
524
+ onResponseStart();
525
+ onError({
526
+ error,
527
+ status: "error",
528
+ extra: {}
529
+ });
530
+ onResponseEnd();
531
+ }
532
+ };
533
+ };
534
+ //#endregion
535
+ //#region src/realtime/realtime.sockets.ts
536
+ var realtimeSocketsAdmin = (database) => {
537
+ return new SocketAdapter({
538
+ name: "firebase-admin-realtime",
539
+ defaultConnected: true
540
+ }).setConnector(({ socket, onReconnect, onConnect, onConnected, onDisconnect, onDisconnected, onListen, onEvent, onError }) => {
541
+ const connect = async () => {
542
+ if (onConnect()) {
543
+ database.goOnline();
544
+ onConnected();
545
+ }
546
+ };
547
+ const disconnect = async () => {
548
+ database.goOffline();
549
+ onDisconnect();
550
+ onDisconnected();
551
+ };
552
+ const reconnect = () => {
553
+ onReconnect({
554
+ disconnect,
555
+ connect
556
+ });
557
+ };
558
+ const listen = (listener, callback) => {
559
+ const fullUrl = socket.url + listener.topic;
560
+ const path = database.ref(fullUrl);
561
+ const { options } = listener;
562
+ const onlyOnce = options?.onlyOnce || false;
563
+ const q = applyRealtimeAdminConstraints(path, options?.constraints || []);
564
+ q[onlyOnce === true ? "once" : "on"]("value", (snapshot) => {
565
+ const getSnapshotData = (s) => s.val() ? {
566
+ ...s.val(),
567
+ __key: s.key
568
+ } : null;
569
+ const response = isDocOrQuery(fullUrl) === "doc" ? getSnapshotData(snapshot) : getOrderedResultRealtime(snapshot);
570
+ const extra = {
571
+ ref: path,
572
+ snapshot,
573
+ status: getStatus(response)
574
+ };
575
+ callback({
576
+ data: response,
577
+ extra
578
+ });
579
+ onEvent({
580
+ topic: listener.topic,
581
+ data: response,
582
+ extra
583
+ });
584
+ }, (error) => {
585
+ onError({ error });
586
+ });
587
+ const unsubscribe = () => q.off("value");
588
+ const unmount = onListen({
589
+ listener,
590
+ callback,
591
+ onUnmount: unsubscribe
592
+ });
593
+ const clearListeners = () => {
594
+ unsubscribe();
595
+ unmount();
596
+ };
597
+ return clearListeners;
598
+ };
599
+ const emit = async () => {
600
+ throw new Error("Cannot emit from Realtime database socket.");
601
+ };
602
+ return {
603
+ connect,
604
+ reconnect,
605
+ disconnect,
606
+ listen,
607
+ emit
608
+ };
609
+ });
610
+ };
611
+ //#endregion
612
+ //#region src/adapter/adapter.sockets.ts
613
+ var FirebaseSocketsAdminAdapter = (database) => {
614
+ if (database instanceof Firestore) return firestoreAdminSockets(database);
615
+ return realtimeSocketsAdmin(database);
616
+ };
617
+ //#endregion
618
+ //#region src/adapter/adapter.ts
619
+ var FirebaseAdminAdapter = (database) => {
620
+ return new Adapter({
621
+ name: "firebase-admin",
622
+ defaultMethod: "getDoc",
623
+ defaultExtra: {},
624
+ systemErrorStatus: "error",
625
+ systemErrorExtra: {}
626
+ }).setFetcher(async ({ request, onSuccess, onError, onResponseStart, onResponseEnd, onRequestStart, onRequestEnd }) => {
627
+ const fullUrl = `${request.client.url}${request.endpoint}`;
628
+ if (database instanceof Firestore) {
629
+ const { method = FirestoreMethods.getDocs, queryParams, payload, options } = request;
630
+ const availableMethods = getFirestoreAdminMethods({
631
+ database,
632
+ url: fullUrl,
633
+ onSuccess,
634
+ onError,
635
+ onRequestStart,
636
+ onResponseEnd,
637
+ onResponseStart,
638
+ onRequestEnd
639
+ });
640
+ if (!Object.values(FirestoreMethods).includes(method)) throw new Error(`Cannot find method ${method} in Firestore available methods.`);
641
+ availableMethods(method, {
642
+ constraints: queryParams?.constraints ? queryParams.constraints : [],
643
+ payload,
644
+ options
645
+ });
646
+ } else {
647
+ const { method = RealtimeDBMethods.get, queryParams, payload, options } = request;
648
+ const availableMethods = getRealtimeDbAdminMethods({
649
+ database,
650
+ url: fullUrl,
651
+ onSuccess,
652
+ onError,
653
+ onRequestStart,
654
+ onResponseEnd,
655
+ onResponseStart,
656
+ onRequestEnd
657
+ });
658
+ if (!Object.values(RealtimeDBMethods).includes(method)) throw new Error(`Cannot find method ${method} in Realtime database available methods.`);
659
+ availableMethods(method, {
660
+ constraints: queryParams?.constraints ? queryParams.constraints : [],
661
+ options,
662
+ payload
663
+ });
664
+ }
665
+ });
666
+ };
667
+ //#endregion
668
+ export { $endAt, $endBefore, $equalTo, $limit, $limitToFirst, $limitToLast, $orderBy, $orderByChild, $orderByKey, $orderByValue, $startAfter, $startAt, $where, FirebaseAdminAdapter, FirebaseSocketsAdminAdapter, FirestoreMethods, FirestoreQueryConstraints, RealtimeDBMethods, RealtimeQueryConstraints, SharedQueryConstraints, applyFireStoreAdminConstraint, applyFireStoreAdminConstraints, applyRealtimeAdminConstraint, applyRealtimeAdminConstraints, firestoreAdminSockets, getFirestoreAdminMethods, getGroupedResultFirestore, getOrderedResultFirestore, getOrderedResultRealtime, getRealtimeDbAdminMethods, getRef, realtimeSocketsAdmin };
669
+
670
+ //# sourceMappingURL=index.mjs.map