@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.
Files changed (62) hide show
  1. package/README.md +111 -63
  2. package/__tests__/features/constraints/constraints.base.spec.ts +44 -0
  3. package/__tests__/features/constraints/listener-extra.types.spec.ts +14 -0
  4. package/__tests__/features/constraints/query-params.types.spec.ts +83 -0
  5. package/__tests__/features/firestore/admin/constraints.admin.spec.ts +1 -0
  6. package/__tests__/features/firestore/admin/sockets.base.spec.ts +4 -1
  7. package/__tests__/features/firestore/shared/constraints.shared.tests.ts +87 -6
  8. package/__tests__/features/firestore/shared/methods/get-doc.test.suite.ts +15 -0
  9. package/__tests__/features/firestore/shared/methods/get-docs.test.suite.ts +1 -1
  10. package/__tests__/features/firestore/shared/methods/on-snapshot.test.suite.ts +79 -5
  11. package/__tests__/features/firestore/shared/methods/update-doc.test.suite.ts +1 -0
  12. package/__tests__/features/firestore/shared/methods.shared.tests.ts +2 -2
  13. package/__tests__/features/realtime/shared/constraints.shared.tests.ts +58 -8
  14. package/__tests__/features/realtime/shared/methods/get.test.suite.ts +23 -0
  15. package/__tests__/features/realtime/shared/methods/on-value.test.suite.ts +108 -5
  16. package/__tests__/features/realtime/shared/methods/push.test.suite.ts +3 -3
  17. package/__tests__/features/realtime/shared/methods/remove.test.suite.ts +3 -3
  18. package/__tests__/features/realtime/shared/methods/update.test.suite.ts +3 -3
  19. package/__tests__/shared/request-events.shared.ts +1 -4
  20. package/dist/adapter/adapter.d.ts +19 -0
  21. package/dist/adapter/adapter.d.ts.map +1 -1
  22. package/dist/adapter/adapter.sockets.d.ts +17 -0
  23. package/dist/adapter/adapter.sockets.d.ts.map +1 -1
  24. package/dist/adapter/types/adapter.firestore.types.d.ts +28 -6
  25. package/dist/adapter/types/adapter.firestore.types.d.ts.map +1 -1
  26. package/dist/adapter/types/adapter.realtime.types.d.ts +26 -6
  27. package/dist/adapter/types/adapter.realtime.types.d.ts.map +1 -1
  28. package/dist/adapter/types/firestore.socket.types.d.ts +6 -4
  29. package/dist/adapter/types/firestore.socket.types.d.ts.map +1 -1
  30. package/dist/adapter/types/realtime.socket.types.d.ts +2 -4
  31. package/dist/adapter/types/realtime.socket.types.d.ts.map +1 -1
  32. package/dist/constraints/constraints.builder.d.ts +98 -0
  33. package/dist/constraints/constraints.builder.d.ts.map +1 -0
  34. package/dist/constraints/firebase.constraints.d.ts +109 -1
  35. package/dist/constraints/firebase.constraints.d.ts.map +1 -1
  36. package/dist/constraints/index.d.ts +1 -0
  37. package/dist/constraints/index.d.ts.map +1 -1
  38. package/dist/firestore/firestore.sockets.d.ts.map +1 -1
  39. package/dist/index.mjs +277 -94
  40. package/dist/index.mjs.map +1 -1
  41. package/dist/realtime/realtime.methods.d.ts +7 -5
  42. package/dist/realtime/realtime.methods.d.ts.map +1 -1
  43. package/dist/realtime/realtime.sockets.d.ts.map +1 -1
  44. package/dist/utils/misc.d.ts.map +1 -1
  45. package/package.json +1 -1
  46. package/src/adapter/adapter.sockets.ts +17 -0
  47. package/src/adapter/adapter.ts +77 -46
  48. package/src/adapter/types/adapter.firestore.types.ts +40 -7
  49. package/src/adapter/types/adapter.realtime.types.ts +39 -7
  50. package/src/adapter/types/firestore.socket.types.ts +10 -2
  51. package/src/adapter/types/realtime.socket.types.ts +6 -3
  52. package/src/constraints/constraints.builder.ts +214 -0
  53. package/src/constraints/firebase.constraints.ts +111 -3
  54. package/src/constraints/index.ts +1 -0
  55. package/src/firestore/firestore.methods.ts +8 -8
  56. package/src/firestore/firestore.sockets.ts +65 -56
  57. package/src/firestore/utils/constraints.utils.ts +4 -4
  58. package/src/realtime/realtime.methods.ts +19 -20
  59. package/src/realtime/realtime.sockets.ts +57 -52
  60. package/src/realtime/utils/constraints.utils.ts +10 -10
  61. package/src/utils/misc.ts +1 -1
  62. 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 ({ data }: { data?: any }) => {
52
- await path.set(data);
53
- return { result: data, status: "success", extra: { ref: path } };
50
+ set: async ({ payload }: DataType) => {
51
+ await path.set(payload);
52
+ return { result: payload, status: "success", extra: { ref: path } };
54
53
  },
55
- push: async ({ data }: { data?: any }) => {
56
- const resRef = await path.push(data);
57
- return { result: { ...data, __key: resRef.key }, status: "success", extra: { ref: resRef, key: resRef.key } };
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 ({ data }: { data?: any }) => {
60
- await path.update(data);
61
- return { result: data, status: "success", extra: { ref: path } };
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 as any);
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
- ).setConnector(
15
- ({ socket, onReconnect, onConnect, onConnected, onDisconnect, onDisconnected, onListen, onEvent, onError }) => {
16
- const connect = async () => {
17
- const connection = await onConnect();
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
- if (connection) {
20
- database.goOnline();
21
- onConnected();
22
- }
23
- };
23
+ if (connection) {
24
+ database.goOnline();
25
+ onConnected();
26
+ }
27
+ };
24
28
 
25
- const disconnect = async () => {
26
- database.goOffline();
27
- onDisconnect();
28
- onDisconnected();
29
- };
29
+ const disconnect = async () => {
30
+ database.goOffline();
31
+ onDisconnect();
32
+ onDisconnected();
33
+ };
30
34
 
31
- const reconnect = () => {
32
- onReconnect({ disconnect, connect });
33
- };
35
+ const reconnect = () => {
36
+ onReconnect({ disconnect, connect });
37
+ };
34
38
 
35
- const listen: RealtimeAdminSocketAdapterType["listen"] = (listener, callback) => {
36
- const fullUrl = socket.url + listener.topic;
37
- const path = database.ref(fullUrl);
38
- const { options } = listener;
39
- const onlyOnce = options?.onlyOnce || false;
40
- const q = applyRealtimeAdminConstraints(path, options?.constraints || []);
41
- const method = onlyOnce === true ? "once" : "on";
42
- q[method](
43
- "value",
44
- (snapshot) => {
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
- const unsubscribe = () => q.off("value");
58
- const unmount = onListen({ listener, callback, onUnmount: unsubscribe });
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
- const clearListeners = () => {
61
- unsubscribe();
62
- unmount();
63
- };
65
+ const clearListeners = () => {
66
+ unsubscribe();
67
+ unmount();
68
+ };
64
69
 
65
- return clearListeners;
66
- };
70
+ return clearListeners;
71
+ };
67
72
 
68
- const emit = async () => {
69
- throw new Error("Cannot emit from Realtime database socket.");
70
- };
73
+ const emit = async () => {
74
+ throw new Error("Cannot emit from Realtime database socket.");
75
+ };
71
76
 
72
- return {
73
- connect,
74
- reconnect,
75
- disconnect,
76
- listen,
77
- emit,
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
@@ -1,4 +1,4 @@
1
- export const getStatus = (res: any) => {
1
+ export const getStatus = (res: any): "success" | "emptyResource" => {
2
2
  // Deliberate == instead of ===
3
3
  return (Array.isArray(res) && res.length === 0) || res == null ? "emptyResource" : "success";
4
4
  };
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"],