@ttt-productions/query-core 0.13.2 → 0.14.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/dist/react/firestore/resubscribe.d.ts +23 -0
- package/dist/react/firestore/resubscribe.d.ts.map +1 -0
- package/dist/react/firestore/resubscribe.js +25 -0
- package/dist/react/firestore/resubscribe.js.map +1 -0
- package/dist/react/firestore/use-firestore-collection.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-collection.js +53 -21
- package/dist/react/firestore/use-firestore-collection.js.map +1 -1
- package/dist/react/firestore/use-firestore-doc.d.ts.map +1 -1
- package/dist/react/firestore/use-firestore-doc.js +54 -22
- package/dist/react/firestore/use-firestore-doc.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded resubscribe ladder for realtime Firestore listeners.
|
|
3
|
+
*
|
|
4
|
+
* A subscription that dies on a `permission-denied` listener error never retries on
|
|
5
|
+
* its own. In practice this fires transiently mid-session: the App Check token
|
|
6
|
+
* expires ~1h in and the SDK's silent refresh mint can fail (e.g. the refresh hits
|
|
7
|
+
* the reCAPTCHA bot-score throttle), so an active `onSnapshot` errors with
|
|
8
|
+
* `permission-denied` even though the user is still authorized. Without a retry the
|
|
9
|
+
* widget stays permanently dead until a full reload.
|
|
10
|
+
*
|
|
11
|
+
* These delays (ms) are the backoff between resubscribe attempts. After the last
|
|
12
|
+
* one the error surfaces exactly as it did before. While the ladder runs the
|
|
13
|
+
* consumer keeps its last-known data and reads `sourceState: 'connecting'`; a
|
|
14
|
+
* healthy snapshot resets the ladder so a later blip gets a fresh window.
|
|
15
|
+
*/
|
|
16
|
+
export declare const RESUBSCRIBE_DELAYS_MS: readonly [5000, 15000, 45000];
|
|
17
|
+
/**
|
|
18
|
+
* True for a Firestore `permission-denied` listener error — the only code the
|
|
19
|
+
* subscription hooks bounded-retry (a failed token refresh heals within seconds).
|
|
20
|
+
* Keys off the `FirestoreError.code`, never the message.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isPermissionDeniedError(error: unknown): boolean;
|
|
23
|
+
//# sourceMappingURL=resubscribe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resubscribe.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/resubscribe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,qBAAqB,+BAAmC,CAAC;AAEtE;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE/D"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded resubscribe ladder for realtime Firestore listeners.
|
|
3
|
+
*
|
|
4
|
+
* A subscription that dies on a `permission-denied` listener error never retries on
|
|
5
|
+
* its own. In practice this fires transiently mid-session: the App Check token
|
|
6
|
+
* expires ~1h in and the SDK's silent refresh mint can fail (e.g. the refresh hits
|
|
7
|
+
* the reCAPTCHA bot-score throttle), so an active `onSnapshot` errors with
|
|
8
|
+
* `permission-denied` even though the user is still authorized. Without a retry the
|
|
9
|
+
* widget stays permanently dead until a full reload.
|
|
10
|
+
*
|
|
11
|
+
* These delays (ms) are the backoff between resubscribe attempts. After the last
|
|
12
|
+
* one the error surfaces exactly as it did before. While the ladder runs the
|
|
13
|
+
* consumer keeps its last-known data and reads `sourceState: 'connecting'`; a
|
|
14
|
+
* healthy snapshot resets the ladder so a later blip gets a fresh window.
|
|
15
|
+
*/
|
|
16
|
+
export const RESUBSCRIBE_DELAYS_MS = [5_000, 15_000, 45_000];
|
|
17
|
+
/**
|
|
18
|
+
* True for a Firestore `permission-denied` listener error — the only code the
|
|
19
|
+
* subscription hooks bounded-retry (a failed token refresh heals within seconds).
|
|
20
|
+
* Keys off the `FirestoreError.code`, never the message.
|
|
21
|
+
*/
|
|
22
|
+
export function isPermissionDeniedError(error) {
|
|
23
|
+
return error?.code === 'permission-denied';
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=resubscribe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resubscribe.js","sourceRoot":"","sources":["../../../src/react/firestore/resubscribe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAU,CAAC;AAEtE;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAc;IACpD,OAAQ,KAA8C,EAAE,IAAI,KAAK,mBAAmB,CAAC;AACvF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-collection.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAKL,KAAK,YAAY,EAClB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"use-firestore-collection.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAKL,KAAK,YAAY,EAClB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EACV,0BAA0B,EAE1B,MAAM,EACN,eAAe,EAChB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,EAC5E,cAAc,EACd,QAAQ,EACR,WAAgB,EAChB,OAAc,EACd,SAAiB,EACjB,SAAS,EACT,MAAM,EACN,MAAM,GACP,EAAE,0BAA0B,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CA8HrF"}
|
|
@@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
|
|
|
3
3
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
4
4
|
import { collection, query, getDocs, onSnapshot, } from 'firebase/firestore';
|
|
5
5
|
import { useFirestoreDb } from './context.js';
|
|
6
|
+
import { RESUBSCRIBE_DELAYS_MS, isPermissionDeniedError } from './resubscribe.js';
|
|
6
7
|
/**
|
|
7
8
|
* Fetch all documents from a Firestore collection with optional realtime updates.
|
|
8
9
|
* Use this for small collections where you need all documents at once.
|
|
@@ -40,28 +41,59 @@ export function useFirestoreCollection({ collectionPath, queryKey, constraints =
|
|
|
40
41
|
return;
|
|
41
42
|
setSubscriptionError(null);
|
|
42
43
|
setSourceState('connecting');
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return
|
|
44
|
+
// Bounded resubscribe: a transient `permission-denied` (e.g. a failed mid-session
|
|
45
|
+
// App Check token refresh) is retried on the RESUBSCRIBE_DELAYS_MS ladder before the
|
|
46
|
+
// error is surfaced. `attempt` resets on any healthy snapshot; `disposed`/timers are
|
|
47
|
+
// cleaned up on unmount so a pending retry never fires after teardown.
|
|
48
|
+
let currentUnsubscribe = null;
|
|
49
|
+
let retryTimer = null;
|
|
50
|
+
let attempt = 0;
|
|
51
|
+
let disposed = false;
|
|
52
|
+
const start = () => {
|
|
53
|
+
if (disposed)
|
|
54
|
+
return;
|
|
55
|
+
const collectionRef = collection(db, collectionPath);
|
|
56
|
+
const q = constraints.length > 0
|
|
57
|
+
? query(collectionRef, ...constraints)
|
|
58
|
+
: collectionRef;
|
|
59
|
+
currentUnsubscribe = onSnapshot(q, { includeMetadataChanges: true }, (snapshot) => {
|
|
60
|
+
attempt = 0;
|
|
61
|
+
setSubscriptionError(null);
|
|
62
|
+
const items = snapshot.docs.map((docSnap) => {
|
|
63
|
+
const rawData = docSnap.data();
|
|
64
|
+
const dataWithId = { id: docSnap.id, ...rawData };
|
|
65
|
+
const data = select ? select(dataWithId) : dataWithId;
|
|
66
|
+
return data;
|
|
67
|
+
});
|
|
68
|
+
queryClient.setQueryData(queryKey, items);
|
|
69
|
+
const fromCache = snapshot.metadata?.fromCache ?? false;
|
|
70
|
+
setSourceState((prev) => (fromCache ? (prev === 'connecting' ? 'connecting' : 'offline') : 'live'));
|
|
71
|
+
}, (error) => {
|
|
72
|
+
// Heal a transient permission-denied by resubscribing on the backoff ladder,
|
|
73
|
+
// keeping the last-known data visible. Only surface the error once the ladder
|
|
74
|
+
// is exhausted; any other error surfaces immediately, as before.
|
|
75
|
+
if (isPermissionDeniedError(error) && attempt < RESUBSCRIBE_DELAYS_MS.length) {
|
|
76
|
+
currentUnsubscribe?.();
|
|
77
|
+
currentUnsubscribe = null;
|
|
78
|
+
const delay = RESUBSCRIBE_DELAYS_MS[attempt];
|
|
79
|
+
attempt += 1;
|
|
80
|
+
setSourceState('connecting');
|
|
81
|
+
retryTimer = setTimeout(start, delay);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
console.error('[useFirestoreCollection] Subscription error:', error);
|
|
85
|
+
setSubscriptionError(error);
|
|
86
|
+
setSourceState('error');
|
|
87
|
+
queryClient.setQueryData(queryKey, undefined);
|
|
54
88
|
});
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
});
|
|
64
|
-
return () => unsubscribe();
|
|
89
|
+
};
|
|
90
|
+
start();
|
|
91
|
+
return () => {
|
|
92
|
+
disposed = true;
|
|
93
|
+
if (retryTimer)
|
|
94
|
+
clearTimeout(retryTimer);
|
|
95
|
+
currentUnsubscribe?.();
|
|
96
|
+
};
|
|
65
97
|
// queryKey & constraints are tracked via their stringified forms above; select is intentionally
|
|
66
98
|
// excluded so an inline caller function doesn't force a re-subscribe on every render.
|
|
67
99
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-collection.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,uBAAuB,CAAC;AACtF,OAAO,EACL,UAAU,EACV,KAAK,EACL,OAAO,EACP,UAAU,GAEX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"use-firestore-collection.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-collection.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,uBAAuB,CAAC;AACtF,OAAO,EACL,UAAU,EACV,KAAK,EACL,OAAO,EACP,UAAU,GAEX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAQlF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,sBAAsB,CAAwC,EAC5E,cAAc,EACd,QAAQ,EACR,WAAW,GAAG,EAAE,EAChB,OAAO,GAAG,IAAI,EACd,SAAS,GAAG,KAAK,EACjB,SAAS,EACT,MAAM,EACN,MAAM,GACwB;IAC9B,MAAM,EAAE,GAAG,cAAc,EAAE,CAAC;IAC5B,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;IAErC,0BAA0B;IAC1B,wFA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C;YAC3B,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,EAAE,OAAO,IAAI,CAAC,SAAS;QAC9B,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;QAC3C,MAAM;KACP,CAAC,CAAC;IAEH,gFAAgF;IAChF,oEAAoE;IACpE,IAAI,SAAS,IAAI,iBAAiB,EAAE,CAAC;QACnC,OAAO;YACL,GAAG,WAAW;YACd,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,iBAAiB;YACxB,OAAO,EAAE,IAAI;YACb,cAAc,EAAE,IAAI;YACpB,SAAS,EAAE,KAAK;YAChB,SAAS,EAAE,KAAK;YAChB,SAAS,EAAE,KAAK;YAChB,MAAM,EAAE,OAAO;YACf,WAAW;SAC2C,CAAC;IAC3D,CAAC;IAED,OAAO,EAAE,GAAG,WAAW,EAAE,WAAW,EAAyD,CAAC;AAChG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-doc.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-doc.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAA2B,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"use-firestore-doc.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-doc.ts"],"names":[],"mappings":"AAGA,OAAO,EAA4B,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAA2B,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGhF,OAAO,KAAK,EACV,mBAAmB,EAEnB,MAAM,EACN,eAAe,EAChB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,EACrE,OAAO,EACP,QAAQ,EACR,OAAc,EACd,SAAiB,EACjB,SAAS,EACT,MAAM,EACN,MAAM,GACP,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,CAAC,CAAC,CAwHnF"}
|
|
@@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
|
|
|
3
3
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
4
4
|
import { doc, getDoc, onSnapshot } from 'firebase/firestore';
|
|
5
5
|
import { useFirestoreDb } from './context.js';
|
|
6
|
+
import { RESUBSCRIBE_DELAYS_MS, isPermissionDeniedError } from './resubscribe.js';
|
|
6
7
|
/**
|
|
7
8
|
* Fetch a single Firestore document with optional realtime updates.
|
|
8
9
|
* * @example
|
|
@@ -41,28 +42,59 @@ export function useFirestoreDoc({ docPath, queryKey, enabled = true, subscribe =
|
|
|
41
42
|
return;
|
|
42
43
|
setSubscriptionError(null);
|
|
43
44
|
setSourceState('connecting');
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
45
|
+
// Bounded resubscribe: a transient `permission-denied` (e.g. a failed mid-session
|
|
46
|
+
// App Check token refresh) is retried on the RESUBSCRIBE_DELAYS_MS ladder before the
|
|
47
|
+
// error is surfaced. `attempt` resets on any healthy snapshot; `disposed`/timers are
|
|
48
|
+
// cleaned up on unmount so a pending retry never fires after teardown.
|
|
49
|
+
let currentUnsubscribe = null;
|
|
50
|
+
let retryTimer = null;
|
|
51
|
+
let attempt = 0;
|
|
52
|
+
let disposed = false;
|
|
53
|
+
const start = () => {
|
|
54
|
+
if (disposed)
|
|
55
|
+
return;
|
|
56
|
+
const docRef = doc(db, docPath);
|
|
57
|
+
currentUnsubscribe = onSnapshot(docRef, { includeMetadataChanges: true }, (snapshot) => {
|
|
58
|
+
attempt = 0;
|
|
59
|
+
setSubscriptionError(null);
|
|
60
|
+
if (snapshot.exists()) {
|
|
61
|
+
const rawData = snapshot.data();
|
|
62
|
+
// Include id in data passed to select, so it can be renamed/transformed
|
|
63
|
+
const dataWithId = { id: snapshot.id, ...rawData };
|
|
64
|
+
const data = select ? select(dataWithId) : dataWithId;
|
|
65
|
+
queryClient.setQueryData(queryKey, data);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
queryClient.setQueryData(queryKey, null);
|
|
69
|
+
}
|
|
70
|
+
const fromCache = snapshot.metadata?.fromCache ?? false;
|
|
71
|
+
setSourceState((prev) => (fromCache ? (prev === 'connecting' ? 'connecting' : 'offline') : 'live'));
|
|
72
|
+
}, (error) => {
|
|
73
|
+
// Heal a transient permission-denied by resubscribing on the backoff ladder,
|
|
74
|
+
// keeping the last-known data visible. Only surface the error once the ladder
|
|
75
|
+
// is exhausted; any other error surfaces immediately, as before.
|
|
76
|
+
if (isPermissionDeniedError(error) && attempt < RESUBSCRIBE_DELAYS_MS.length) {
|
|
77
|
+
currentUnsubscribe?.();
|
|
78
|
+
currentUnsubscribe = null;
|
|
79
|
+
const delay = RESUBSCRIBE_DELAYS_MS[attempt];
|
|
80
|
+
attempt += 1;
|
|
81
|
+
setSourceState('connecting');
|
|
82
|
+
retryTimer = setTimeout(start, delay);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
console.error('[useFirestoreDoc] Subscription error:', error);
|
|
86
|
+
setSubscriptionError(error);
|
|
87
|
+
setSourceState('error');
|
|
88
|
+
queryClient.setQueryData(queryKey, undefined);
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
start();
|
|
92
|
+
return () => {
|
|
93
|
+
disposed = true;
|
|
94
|
+
if (retryTimer)
|
|
95
|
+
clearTimeout(retryTimer);
|
|
96
|
+
currentUnsubscribe?.();
|
|
97
|
+
};
|
|
66
98
|
// Note: select intentionally excluded to prevent re-subscribing on every render if the caller
|
|
67
99
|
// passes an inline function. queryKey is tracked via queryKeyMemo above.
|
|
68
100
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-firestore-doc.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-doc.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAqB,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"use-firestore-doc.js","sourceRoot":"","sources":["../../../src/react/firestore/use-firestore-doc.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAuB,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAqB,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAQlF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,eAAe,CAAwC,EACrE,OAAO,EACP,QAAQ,EACR,OAAO,GAAG,IAAI,EACd,SAAS,GAAG,KAAK,EACjB,SAAS,EACT,MAAM,EACN,MAAM,GACiB;IACvB,M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,iBAAiB,EAAE,CAAC;QACnC,OAAO;YACL,GAAG,KAAK;YACR,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,iBAAiB;YACxB,OAAO,EAAE,IAAI;YACb,cAAc,EAAE,IAAI;YACpB,SAAS,EAAE,KAAK;YAChB,SAAS,EAAE,KAAK;YAChB,SAAS,EAAE,KAAK;YAChB,MAAM,EAAE,OAAO;YACf,WAAW;SACgD,CAAC;IAChE,CAAC;IAED,OAAO,EAAE,GAAG,KAAK,EAAE,WAAW,EAA8D,CAAC;AAC/F,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttt-productions/query-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Shared TanStack Query (React Query) client defaults, query key helpers, and Firestore integration hooks for TTT Productions apps",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|