@hyper-fetch/firebase-admin 9.9.0 → 10.1.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/README.md +111 -63
- package/__tests__/features/constraints/constraints.base.spec.ts +44 -0
- package/__tests__/features/constraints/listener-extra.types.spec.ts +14 -0
- package/__tests__/features/constraints/query-params.types.spec.ts +83 -0
- package/__tests__/features/firestore/admin/constraints.admin.spec.ts +1 -0
- package/__tests__/features/firestore/admin/sockets.base.spec.ts +4 -1
- package/__tests__/features/firestore/shared/constraints.shared.tests.ts +87 -6
- package/__tests__/features/firestore/shared/methods/get-doc.test.suite.ts +15 -0
- package/__tests__/features/firestore/shared/methods/get-docs.test.suite.ts +1 -1
- package/__tests__/features/firestore/shared/methods/on-snapshot.test.suite.ts +79 -5
- package/__tests__/features/firestore/shared/methods/update-doc.test.suite.ts +1 -0
- package/__tests__/features/firestore/shared/methods.shared.tests.ts +2 -2
- package/__tests__/features/realtime/shared/constraints.shared.tests.ts +58 -8
- package/__tests__/features/realtime/shared/methods/get.test.suite.ts +23 -0
- package/__tests__/features/realtime/shared/methods/on-value.test.suite.ts +108 -5
- package/__tests__/features/realtime/shared/methods/push.test.suite.ts +3 -3
- package/__tests__/features/realtime/shared/methods/remove.test.suite.ts +3 -3
- package/__tests__/features/realtime/shared/methods/update.test.suite.ts +3 -3
- package/__tests__/shared/request-events.shared.ts +1 -4
- package/dist/adapter/adapter.d.ts +19 -0
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/adapter/adapter.sockets.d.ts +17 -0
- package/dist/adapter/adapter.sockets.d.ts.map +1 -1
- package/dist/adapter/types/adapter.firestore.types.d.ts +28 -6
- package/dist/adapter/types/adapter.firestore.types.d.ts.map +1 -1
- package/dist/adapter/types/adapter.realtime.types.d.ts +26 -6
- package/dist/adapter/types/adapter.realtime.types.d.ts.map +1 -1
- package/dist/adapter/types/firestore.socket.types.d.ts +6 -4
- package/dist/adapter/types/firestore.socket.types.d.ts.map +1 -1
- package/dist/adapter/types/realtime.socket.types.d.ts +2 -4
- package/dist/adapter/types/realtime.socket.types.d.ts.map +1 -1
- package/dist/constraints/constraints.builder.d.ts +98 -0
- package/dist/constraints/constraints.builder.d.ts.map +1 -0
- package/dist/constraints/firebase.constraints.d.ts +109 -1
- package/dist/constraints/firebase.constraints.d.ts.map +1 -1
- package/dist/constraints/index.d.ts +1 -0
- package/dist/constraints/index.d.ts.map +1 -1
- package/dist/firestore/firestore.sockets.d.ts.map +1 -1
- package/dist/index.mjs +277 -94
- package/dist/index.mjs.map +1 -1
- package/dist/realtime/realtime.methods.d.ts +7 -5
- package/dist/realtime/realtime.methods.d.ts.map +1 -1
- package/dist/realtime/realtime.sockets.d.ts.map +1 -1
- package/dist/utils/misc.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.sockets.ts +17 -0
- package/src/adapter/adapter.ts +77 -46
- package/src/adapter/types/adapter.firestore.types.ts +40 -7
- package/src/adapter/types/adapter.realtime.types.ts +39 -7
- package/src/adapter/types/firestore.socket.types.ts +10 -2
- package/src/adapter/types/realtime.socket.types.ts +6 -3
- package/src/constraints/constraints.builder.ts +214 -0
- package/src/constraints/firebase.constraints.ts +111 -3
- package/src/constraints/index.ts +1 -0
- package/src/firestore/firestore.methods.ts +8 -8
- package/src/firestore/firestore.sockets.ts +65 -56
- package/src/firestore/utils/constraints.utils.ts +4 -4
- package/src/realtime/realtime.methods.ts +19 -20
- package/src/realtime/realtime.sockets.ts +57 -52
- package/src/realtime/utils/constraints.utils.ts +10 -10
- package/src/utils/misc.ts +1 -1
- package/vite.config.ts +2 -0
|
@@ -11,6 +11,12 @@ import type { Database } from "firebase-admin/database";
|
|
|
11
11
|
import { applyRealtimeAdminConstraints, getOrderedResultRealtime } from "realtime/index";
|
|
12
12
|
import { getStatus, isDocOrQuery } from "utils";
|
|
13
13
|
|
|
14
|
+
type DataType = {
|
|
15
|
+
constraints: PermittedConstraints<RealtimePermittedMethods, RealtimeConstraintsUnion | SharedQueryConstraints>[];
|
|
16
|
+
payload: any;
|
|
17
|
+
options: Record<string, any>;
|
|
18
|
+
};
|
|
19
|
+
|
|
14
20
|
export const getRealtimeDbAdminMethods = ({
|
|
15
21
|
database,
|
|
16
22
|
url,
|
|
@@ -29,18 +35,11 @@ export const getRealtimeDbAdminMethods = ({
|
|
|
29
35
|
onRequestStart: Awaited<ReturnType<typeof getAdapterBindings>>["onRequestStart"];
|
|
30
36
|
onRequestEnd: Awaited<ReturnType<typeof getAdapterBindings>>["onRequestEnd"];
|
|
31
37
|
onResponseEnd: Awaited<ReturnType<typeof getAdapterBindings>>["onResponseEnd"];
|
|
32
|
-
}): ((
|
|
33
|
-
methodName: RealtimeDBMethodsUnion,
|
|
34
|
-
data: {
|
|
35
|
-
constraints: PermittedConstraints<RealtimePermittedMethods, RealtimeConstraintsUnion | SharedQueryConstraints>[];
|
|
36
|
-
payload: any;
|
|
37
|
-
options: Record<string, any>;
|
|
38
|
-
},
|
|
39
|
-
) => Promise<void>) => {
|
|
38
|
+
}): ((methodName: RealtimeDBMethodsUnion, data: DataType) => Promise<void>) => {
|
|
40
39
|
const [fullUrl] = url.split("?");
|
|
41
40
|
const path = database.ref(fullUrl);
|
|
42
41
|
const methods = {
|
|
43
|
-
get: async ({ constraints = [] }) => {
|
|
42
|
+
get: async ({ constraints = [] }: DataType) => {
|
|
44
43
|
const docOrQuery = isDocOrQuery(fullUrl);
|
|
45
44
|
const q = applyRealtimeAdminConstraints(path, constraints);
|
|
46
45
|
const snapshot = await q.get();
|
|
@@ -48,17 +47,17 @@ export const getRealtimeDbAdminMethods = ({
|
|
|
48
47
|
const status = getStatus(res);
|
|
49
48
|
return { result: res, status, extra: { ref: path, snapshot } };
|
|
50
49
|
},
|
|
51
|
-
set: async ({
|
|
52
|
-
await path.set(
|
|
53
|
-
return { result:
|
|
50
|
+
set: async ({ payload }: DataType) => {
|
|
51
|
+
await path.set(payload);
|
|
52
|
+
return { result: payload, status: "success", extra: { ref: path } };
|
|
54
53
|
},
|
|
55
|
-
push: async ({
|
|
56
|
-
const resRef = await path.push(
|
|
57
|
-
return { result: { ...
|
|
54
|
+
push: async ({ payload }: DataType) => {
|
|
55
|
+
const resRef = await path.push(payload);
|
|
56
|
+
return { result: { ...payload, __key: resRef.key }, status: "success", extra: { ref: resRef, key: resRef.key } };
|
|
58
57
|
},
|
|
59
|
-
update: async ({
|
|
60
|
-
await path.update(
|
|
61
|
-
return { result:
|
|
58
|
+
update: async ({ payload }: DataType) => {
|
|
59
|
+
await path.update(payload);
|
|
60
|
+
return { result: payload, status: "success", extra: { ref: path } };
|
|
62
61
|
},
|
|
63
62
|
remove: async () => {
|
|
64
63
|
await path.remove();
|
|
@@ -66,10 +65,10 @@ export const getRealtimeDbAdminMethods = ({
|
|
|
66
65
|
},
|
|
67
66
|
};
|
|
68
67
|
|
|
69
|
-
return async (methodName: RealtimeDBMethodsUnion, data) => {
|
|
68
|
+
return async (methodName: RealtimeDBMethodsUnion, data: DataType) => {
|
|
70
69
|
try {
|
|
71
70
|
onRequestStart();
|
|
72
|
-
const { result, status, extra } = await methods[methodName](data
|
|
71
|
+
const { result, status, extra } = await methods[methodName](data);
|
|
73
72
|
onRequestEnd();
|
|
74
73
|
onResponseStart();
|
|
75
74
|
onSuccess({ data: result, status, extra });
|
|
@@ -3,6 +3,8 @@ import type { RealtimeAdminOnValueMethodExtra, RealtimeAdminSocketAdapterType }
|
|
|
3
3
|
import type { Database, DataSnapshot } from "firebase-admin/database";
|
|
4
4
|
import { getStatus, isDocOrQuery } from "utils";
|
|
5
5
|
|
|
6
|
+
import { createRealtimeConstraints, getConstraints, resolveConstraints } from "../constraints";
|
|
7
|
+
import type { RealtimePermittedMethods } from "../constraints";
|
|
6
8
|
import { applyRealtimeAdminConstraints, getOrderedResultRealtime } from "./utils";
|
|
7
9
|
|
|
8
10
|
export const realtimeSocketsAdmin = (database: Database) => {
|
|
@@ -11,71 +13,74 @@ export const realtimeSocketsAdmin = (database: Database) => {
|
|
|
11
13
|
name: "firebase-admin-realtime",
|
|
12
14
|
defaultConnected: true,
|
|
13
15
|
}) as unknown as RealtimeAdminSocketAdapterType
|
|
14
|
-
)
|
|
15
|
-
(
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
)
|
|
17
|
+
.setListenerQueryParamsResolver(resolveConstraints(createRealtimeConstraints))
|
|
18
|
+
.setConnector(
|
|
19
|
+
({ socket, onReconnect, onConnect, onConnected, onDisconnect, onDisconnected, onListen, onEvent, onError }) => {
|
|
20
|
+
const connect = async () => {
|
|
21
|
+
const connection = await onConnect();
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
if (connection) {
|
|
24
|
+
database.goOnline();
|
|
25
|
+
onConnected();
|
|
26
|
+
}
|
|
27
|
+
};
|
|
24
28
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
const disconnect = async () => {
|
|
30
|
+
database.goOffline();
|
|
31
|
+
onDisconnect();
|
|
32
|
+
onDisconnected();
|
|
33
|
+
};
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
const reconnect = () => {
|
|
36
|
+
onReconnect({ disconnect, connect });
|
|
37
|
+
};
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
const listen: RealtimeAdminSocketAdapterType["listen"] = (listener, callback) => {
|
|
40
|
+
const fullUrl = socket.url + listener.topic;
|
|
41
|
+
const path = database.ref(fullUrl);
|
|
42
|
+
const { options } = listener;
|
|
43
|
+
const onlyOnce = options?.onlyOnce || false;
|
|
44
|
+
// `options.constraints` is the deprecated place for constraints, kept working in v10
|
|
45
|
+
const q = applyRealtimeAdminConstraints(
|
|
46
|
+
path,
|
|
47
|
+
getConstraints<RealtimePermittedMethods>(listener.queryParams ?? options?.constraints),
|
|
48
|
+
);
|
|
49
|
+
const method = onlyOnce === true ? "once" : "on";
|
|
50
|
+
const onValue = (snapshot: DataSnapshot) => {
|
|
45
51
|
const getSnapshotData = (s: DataSnapshot) => (s.val() ? { ...s.val(), __key: s.key } : null);
|
|
46
52
|
const response =
|
|
47
53
|
isDocOrQuery(fullUrl) === "doc" ? getSnapshotData(snapshot) : getOrderedResultRealtime(snapshot);
|
|
48
54
|
const status = getStatus(response);
|
|
49
55
|
const extra: RealtimeAdminOnValueMethodExtra = { ref: path, snapshot, status };
|
|
50
|
-
callback({ data: response, extra });
|
|
51
56
|
onEvent({ topic: listener.topic, data: response, extra });
|
|
52
|
-
}
|
|
53
|
-
(error) => {
|
|
57
|
+
};
|
|
58
|
+
q[method]("value", onValue, (error) => {
|
|
54
59
|
onError({ error });
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
});
|
|
61
|
+
// Detach only this listener's callback, other subscriptions on the same path keep receiving updates
|
|
62
|
+
const unsubscribe = () => q.off("value", onValue);
|
|
63
|
+
const unmount = onListen({ listener, callback, onUnmount: unsubscribe });
|
|
59
64
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
const clearListeners = () => {
|
|
66
|
+
unsubscribe();
|
|
67
|
+
unmount();
|
|
68
|
+
};
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
return clearListeners;
|
|
71
|
+
};
|
|
67
72
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
const emit = async () => {
|
|
74
|
+
throw new Error("Cannot emit from Realtime database socket.");
|
|
75
|
+
};
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
return {
|
|
78
|
+
connect,
|
|
79
|
+
reconnect,
|
|
80
|
+
disconnect,
|
|
81
|
+
listen,
|
|
82
|
+
emit,
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
);
|
|
81
86
|
};
|
|
@@ -15,20 +15,20 @@ export const applyRealtimeAdminConstraint = (ref: Query, { type, values }: Realt
|
|
|
15
15
|
return ref.orderByValue();
|
|
16
16
|
}
|
|
17
17
|
case SharedQueryConstraints.START_AT: {
|
|
18
|
-
const [[value]] = values;
|
|
19
|
-
return ref.startAt(value);
|
|
18
|
+
const [[value, key]] = values;
|
|
19
|
+
return ref.startAt(value, key);
|
|
20
20
|
}
|
|
21
21
|
case SharedQueryConstraints.START_AFTER: {
|
|
22
|
-
const [[value]] = values;
|
|
23
|
-
return ref.startAfter(value);
|
|
22
|
+
const [[value, key]] = values;
|
|
23
|
+
return ref.startAfter(value, key);
|
|
24
24
|
}
|
|
25
25
|
case SharedQueryConstraints.END_AT: {
|
|
26
|
-
const [[value]] = values;
|
|
27
|
-
return ref.endAt(value);
|
|
26
|
+
const [[value, key]] = values;
|
|
27
|
+
return ref.endAt(value, key);
|
|
28
28
|
}
|
|
29
29
|
case SharedQueryConstraints.END_BEFORE: {
|
|
30
|
-
const [[value]] = values;
|
|
31
|
-
return ref.endBefore(value);
|
|
30
|
+
const [[value, key]] = values;
|
|
31
|
+
return ref.endBefore(value, key);
|
|
32
32
|
}
|
|
33
33
|
case RealtimeQueryConstraints.LIMIT_TO_FIRST: {
|
|
34
34
|
const [value] = values;
|
|
@@ -39,8 +39,8 @@ export const applyRealtimeAdminConstraint = (ref: Query, { type, values }: Realt
|
|
|
39
39
|
return ref.limitToLast(value);
|
|
40
40
|
}
|
|
41
41
|
case RealtimeQueryConstraints.EQUAL_TO: {
|
|
42
|
-
const [value] = values;
|
|
43
|
-
return ref.equalTo(value);
|
|
42
|
+
const [value, key] = values;
|
|
43
|
+
return ref.equalTo(value, key);
|
|
44
44
|
}
|
|
45
45
|
default:
|
|
46
46
|
throw new Error(`Unknown method ${type}`);
|
package/src/utils/misc.ts
CHANGED
package/vite.config.ts
CHANGED
|
@@ -32,6 +32,8 @@ export default defineConfig({
|
|
|
32
32
|
provider: "v8",
|
|
33
33
|
},
|
|
34
34
|
environment: "node",
|
|
35
|
+
// Every spec file seeds and clears the same collections on one shared emulator, so files must run one at a time
|
|
36
|
+
fileParallelism: false,
|
|
35
37
|
globals: true,
|
|
36
38
|
include: ["__tests__/**/*.spec.{ts,tsx}", "src/**/*.spec.{ts,tsx}"],
|
|
37
39
|
setupFiles: ["./__tests__/vitest.setup.ts"],
|