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