@tatsuokaniwa/swr-firestore 1.1.2 → 1.2.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 +7 -3
- package/dist/hooks/useDoc.d.ts +1 -1
- package/dist/hooks/useGetDoc.d.ts +1 -1
- package/dist/index.js +16 -30
- package/dist/index.umd.cjs +15 -29
- package/dist/util/type.d.ts +6 -2
- package/package.json +2 -4
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://github.com/t-k/swr-firestore/actions/workflows/test.yaml)
|
|
5
5
|
[](https://codecov.io/gh/t-k/swr-firestore)
|
|
6
6
|
|
|
7
|
-
React Hooks library for Firestore, built using the Firebase v9 modular SDK. It utilizes the [`useSWRSubscription`](https://swr.vercel.app/
|
|
7
|
+
React Hooks library for Firestore, built using the Firebase v9 modular SDK. It utilizes the [`useSWRSubscription`](https://swr.vercel.app/docs/subscription) function from SWR library to enable subscription-based data fetching and caching.
|
|
8
8
|
|
|
9
9
|
Inspired by [swr-firestore-v9](https://www.npmjs.com/package/swr-firestore-v9)
|
|
10
10
|
|
|
@@ -114,6 +114,10 @@ type KeyParams<T> =
|
|
|
114
114
|
// `Paths` means object's property path, including nested object
|
|
115
115
|
where?: [Paths<T>, Parameters<typeof where>[1], ValueOf<T> | unknown][]
|
|
116
116
|
orderBy?: [Paths<T>, Parameters<typeof orderBy>[1]][]
|
|
117
|
+
startAt?: Parameters<typeof startAt>;
|
|
118
|
+
startAfter?: Parameters<typeof startAfter>;
|
|
119
|
+
endAt?: Parameters<typeof endAt>;
|
|
120
|
+
endBefore?: Parameters<typeof endBefore>;
|
|
117
121
|
limit?: number
|
|
118
122
|
// Array of field names that should be parsed as dates.
|
|
119
123
|
parseDates?: Paths<T>[]
|
|
@@ -168,7 +172,7 @@ Wrapper for getCountFromServer for collection
|
|
|
168
172
|
|
|
169
173
|
#### Parameters
|
|
170
174
|
|
|
171
|
-
- `params`: KeyParams except `
|
|
175
|
+
- `params`: KeyParams except `parseDates` | null
|
|
172
176
|
- `swrOptions`: [Options for SWR hook](https://swr.vercel.app/docs/api#options) except `fetcher`
|
|
173
177
|
|
|
174
178
|
#### Return values
|
|
@@ -213,7 +217,7 @@ Wrapper for getCountFromServer for collectionGroup
|
|
|
213
217
|
|
|
214
218
|
#### Parameters
|
|
215
219
|
|
|
216
|
-
- `params`: KeyParams except `
|
|
220
|
+
- `params`: KeyParams except `parseDates` | null
|
|
217
221
|
- `swrOptions`: [Options for SWR hook](https://swr.vercel.app/docs/api#options) except `fetcher`
|
|
218
222
|
|
|
219
223
|
#### Return values
|
package/dist/hooks/useDoc.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ import type { FirestoreError } from "firebase/firestore";
|
|
|
2
2
|
import type { SWRSubscriptionResponse } from "swr/subscription";
|
|
3
3
|
import type { DocumentData, KeyParams } from "../util/type";
|
|
4
4
|
import type { SWRConfiguration } from "swr";
|
|
5
|
-
declare const useDoc: <T>(params: Omit<KeyParams<T>, "
|
|
5
|
+
declare const useDoc: <T>(params: Omit<KeyParams<T>, "where" | "orderBy" | "limit"> | null, swrOptions?: Omit<SWRConfiguration, "fetcher">) => SWRSubscriptionResponse<DocumentData<T>, FirestoreError>;
|
|
6
6
|
export default useDoc;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { SWRConfiguration } from "swr";
|
|
2
2
|
import type { DocumentData, GetDocKeyParams } from "../util/type";
|
|
3
|
-
declare const useGetDoc: <T>(params: Omit<GetDocKeyParams<T>, "
|
|
3
|
+
declare const useGetDoc: <T>(params: Omit<GetDocKeyParams<T>, "where" | "orderBy" | "limit"> | null, swrOptions?: Omit<SWRConfiguration, "fetcher">) => import("swr/_internal").SWRResponse<DocumentData<T> | undefined, any, {
|
|
4
4
|
use: import("swr/_internal").Middleware[];
|
|
5
5
|
errorRetryInterval?: number | undefined;
|
|
6
6
|
errorRetryCount?: number | undefined;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { collection, getFirestore, query, where, orderBy, limit, onSnapshot, getCountFromServer, collectionGroup, doc, getDocFromCache, getDoc, getDocsFromCache, getDocs } from "firebase/firestore";
|
|
1
|
+
import { collection, getFirestore, query, where, orderBy, startAt, startAfter, endAt, endBefore, limit, onSnapshot, getCountFromServer, collectionGroup, doc, getDocFromCache, getDoc, getDocsFromCache, getDocs } from "firebase/firestore";
|
|
2
2
|
import { get, set } from "lodash-es";
|
|
3
3
|
import useSWRSubscription from "swr/subscription";
|
|
4
4
|
import useSWR from "swr";
|
|
@@ -39,8 +39,7 @@ const serializeMiddleware = (useSWRNext) => {
|
|
|
39
39
|
const useCollection = (params, swrOptions) => {
|
|
40
40
|
return useSWRSubscription(params, (_, { next }) => {
|
|
41
41
|
if (params == null) {
|
|
42
|
-
return
|
|
43
|
-
};
|
|
42
|
+
return;
|
|
44
43
|
}
|
|
45
44
|
const { path, parseDates } = params;
|
|
46
45
|
const converter = getFirestoreConverter(parseDates);
|
|
@@ -49,8 +48,8 @@ const useCollection = (params, swrOptions) => {
|
|
|
49
48
|
if (isQueryConstraintParams(params)) {
|
|
50
49
|
q = query(ref, ...params.queryConstraints);
|
|
51
50
|
} else {
|
|
52
|
-
const { where: w, orderBy: o, limit: l } = params;
|
|
53
|
-
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...l ? [limit(l)] : []);
|
|
51
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
52
|
+
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...s ? [startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [limit(l)] : []);
|
|
54
53
|
}
|
|
55
54
|
const unsub = onSnapshot(q.withConverter(converter), (qs) => {
|
|
56
55
|
next(null, qs.docs.map((x) => x.data()));
|
|
@@ -61,10 +60,6 @@ const useCollection = (params, swrOptions) => {
|
|
|
61
60
|
}, { ...swrOptions, use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []] });
|
|
62
61
|
};
|
|
63
62
|
const useCollectionCount = (params, swrOptions) => {
|
|
64
|
-
let swrKey = params;
|
|
65
|
-
if (params != null && isQueryConstraintParams(params)) {
|
|
66
|
-
swrKey = JSON.parse(JSON.stringify(params));
|
|
67
|
-
}
|
|
68
63
|
const fetcher = async () => {
|
|
69
64
|
if (params == null) {
|
|
70
65
|
return;
|
|
@@ -75,13 +70,13 @@ const useCollectionCount = (params, swrOptions) => {
|
|
|
75
70
|
if (isQueryConstraintParams(params)) {
|
|
76
71
|
q = query(ref, ...params.queryConstraints);
|
|
77
72
|
} else {
|
|
78
|
-
const { where: w, limit: l } = params;
|
|
79
|
-
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...l ? [limit(l)] : []);
|
|
73
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
74
|
+
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...s ? [startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [limit(l)] : []);
|
|
80
75
|
}
|
|
81
76
|
const sn = await getCountFromServer(q);
|
|
82
77
|
return sn.data().count;
|
|
83
78
|
};
|
|
84
|
-
return useSWR(
|
|
79
|
+
return useSWR(params, fetcher, {
|
|
85
80
|
...swrOptions,
|
|
86
81
|
use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []]
|
|
87
82
|
});
|
|
@@ -89,8 +84,7 @@ const useCollectionCount = (params, swrOptions) => {
|
|
|
89
84
|
const useCollectionGroup = (params, swrOptions) => {
|
|
90
85
|
return useSWRSubscription(params, (_, { next }) => {
|
|
91
86
|
if (params == null) {
|
|
92
|
-
return
|
|
93
|
-
};
|
|
87
|
+
return;
|
|
94
88
|
}
|
|
95
89
|
const { path, parseDates } = params;
|
|
96
90
|
const converter = getFirestoreConverter(parseDates);
|
|
@@ -99,8 +93,8 @@ const useCollectionGroup = (params, swrOptions) => {
|
|
|
99
93
|
if (isQueryConstraintParams(params)) {
|
|
100
94
|
q = query(ref, ...params.queryConstraints);
|
|
101
95
|
} else {
|
|
102
|
-
const { where: w, orderBy: o, limit: l } = params;
|
|
103
|
-
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...l ? [limit(l)] : []);
|
|
96
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
97
|
+
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...s ? [startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [limit(l)] : []);
|
|
104
98
|
}
|
|
105
99
|
const unsub = onSnapshot(q.withConverter(converter), (qs) => {
|
|
106
100
|
next(null, qs.docs.map((x) => x.data()));
|
|
@@ -111,10 +105,6 @@ const useCollectionGroup = (params, swrOptions) => {
|
|
|
111
105
|
}, { ...swrOptions, use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []] });
|
|
112
106
|
};
|
|
113
107
|
const useCollectionGroupCount = (params, swrOptions) => {
|
|
114
|
-
let swrKey = params;
|
|
115
|
-
if (params != null && isQueryConstraintParams(params)) {
|
|
116
|
-
swrKey = JSON.parse(JSON.stringify(params));
|
|
117
|
-
}
|
|
118
108
|
const fetcher = async () => {
|
|
119
109
|
if (params == null) {
|
|
120
110
|
return;
|
|
@@ -125,13 +115,13 @@ const useCollectionGroupCount = (params, swrOptions) => {
|
|
|
125
115
|
if (isQueryConstraintParams(params)) {
|
|
126
116
|
q = query(ref, ...params.queryConstraints);
|
|
127
117
|
} else {
|
|
128
|
-
const { where: w, limit: l } = params;
|
|
129
|
-
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...l ? [limit(l)] : []);
|
|
118
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
119
|
+
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...s ? [startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [limit(l)] : []);
|
|
130
120
|
}
|
|
131
121
|
const sn = await getCountFromServer(q);
|
|
132
122
|
return sn.data().count;
|
|
133
123
|
};
|
|
134
|
-
return useSWR(
|
|
124
|
+
return useSWR(params, fetcher, {
|
|
135
125
|
...swrOptions,
|
|
136
126
|
use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []]
|
|
137
127
|
});
|
|
@@ -171,10 +161,6 @@ const useGetDoc = (params, swrOptions) => {
|
|
|
171
161
|
});
|
|
172
162
|
};
|
|
173
163
|
const useGetDocs = (params, swrOptions) => {
|
|
174
|
-
let swrKey = params;
|
|
175
|
-
if (params != null && isQueryConstraintParams(params)) {
|
|
176
|
-
swrKey = JSON.parse(JSON.stringify(params));
|
|
177
|
-
}
|
|
178
164
|
const fetcher = async () => {
|
|
179
165
|
if (params == null) {
|
|
180
166
|
return;
|
|
@@ -186,14 +172,14 @@ const useGetDocs = (params, swrOptions) => {
|
|
|
186
172
|
if (isQueryConstraintParams(params)) {
|
|
187
173
|
q = query(ref, ...params.queryConstraints);
|
|
188
174
|
} else {
|
|
189
|
-
const { where: w, orderBy: o, limit: l } = params;
|
|
190
|
-
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...l ? [limit(l)] : []);
|
|
175
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
176
|
+
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...s ? [startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [limit(l)] : []);
|
|
191
177
|
}
|
|
192
178
|
const getFn = params.useOfflineCache ? getDocsFromCache : getDocs;
|
|
193
179
|
const sn = await getFn(q.withConverter(converter));
|
|
194
180
|
return sn.docs.map((x) => x.data());
|
|
195
181
|
};
|
|
196
|
-
return useSWR(
|
|
182
|
+
return useSWR(params, fetcher, {
|
|
197
183
|
...swrOptions,
|
|
198
184
|
use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []]
|
|
199
185
|
});
|
package/dist/index.umd.cjs
CHANGED
|
@@ -39,8 +39,7 @@
|
|
|
39
39
|
const useCollection = (params, swrOptions) => {
|
|
40
40
|
return useSWRSubscription(params, (_, { next }) => {
|
|
41
41
|
if (params == null) {
|
|
42
|
-
return
|
|
43
|
-
};
|
|
42
|
+
return;
|
|
44
43
|
}
|
|
45
44
|
const { path, parseDates } = params;
|
|
46
45
|
const converter = getFirestoreConverter(parseDates);
|
|
@@ -49,8 +48,8 @@
|
|
|
49
48
|
if (isQueryConstraintParams(params)) {
|
|
50
49
|
q = firestore.query(ref, ...params.queryConstraints);
|
|
51
50
|
} else {
|
|
52
|
-
const { where: w, orderBy: o, limit: l } = params;
|
|
53
|
-
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...l ? [firestore.limit(l)] : []);
|
|
51
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
52
|
+
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...s ? [firestore.startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [firestore.startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [firestore.endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [firestore.endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [firestore.limit(l)] : []);
|
|
54
53
|
}
|
|
55
54
|
const unsub = firestore.onSnapshot(q.withConverter(converter), (qs) => {
|
|
56
55
|
next(null, qs.docs.map((x) => x.data()));
|
|
@@ -61,10 +60,6 @@
|
|
|
61
60
|
}, { ...swrOptions, use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []] });
|
|
62
61
|
};
|
|
63
62
|
const useCollectionCount = (params, swrOptions) => {
|
|
64
|
-
let swrKey = params;
|
|
65
|
-
if (params != null && isQueryConstraintParams(params)) {
|
|
66
|
-
swrKey = JSON.parse(JSON.stringify(params));
|
|
67
|
-
}
|
|
68
63
|
const fetcher = async () => {
|
|
69
64
|
if (params == null) {
|
|
70
65
|
return;
|
|
@@ -75,13 +70,13 @@
|
|
|
75
70
|
if (isQueryConstraintParams(params)) {
|
|
76
71
|
q = firestore.query(ref, ...params.queryConstraints);
|
|
77
72
|
} else {
|
|
78
|
-
const { where: w, limit: l } = params;
|
|
79
|
-
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...l ? [firestore.limit(l)] : []);
|
|
73
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
74
|
+
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...s ? [firestore.startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [firestore.startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [firestore.endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [firestore.endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [firestore.limit(l)] : []);
|
|
80
75
|
}
|
|
81
76
|
const sn = await firestore.getCountFromServer(q);
|
|
82
77
|
return sn.data().count;
|
|
83
78
|
};
|
|
84
|
-
return useSWR(
|
|
79
|
+
return useSWR(params, fetcher, {
|
|
85
80
|
...swrOptions,
|
|
86
81
|
use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []]
|
|
87
82
|
});
|
|
@@ -89,8 +84,7 @@
|
|
|
89
84
|
const useCollectionGroup = (params, swrOptions) => {
|
|
90
85
|
return useSWRSubscription(params, (_, { next }) => {
|
|
91
86
|
if (params == null) {
|
|
92
|
-
return
|
|
93
|
-
};
|
|
87
|
+
return;
|
|
94
88
|
}
|
|
95
89
|
const { path, parseDates } = params;
|
|
96
90
|
const converter = getFirestoreConverter(parseDates);
|
|
@@ -99,8 +93,8 @@
|
|
|
99
93
|
if (isQueryConstraintParams(params)) {
|
|
100
94
|
q = firestore.query(ref, ...params.queryConstraints);
|
|
101
95
|
} else {
|
|
102
|
-
const { where: w, orderBy: o, limit: l } = params;
|
|
103
|
-
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...l ? [firestore.limit(l)] : []);
|
|
96
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
97
|
+
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...s ? [firestore.startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [firestore.startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [firestore.endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [firestore.endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [firestore.limit(l)] : []);
|
|
104
98
|
}
|
|
105
99
|
const unsub = firestore.onSnapshot(q.withConverter(converter), (qs) => {
|
|
106
100
|
next(null, qs.docs.map((x) => x.data()));
|
|
@@ -111,10 +105,6 @@
|
|
|
111
105
|
}, { ...swrOptions, use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []] });
|
|
112
106
|
};
|
|
113
107
|
const useCollectionGroupCount = (params, swrOptions) => {
|
|
114
|
-
let swrKey = params;
|
|
115
|
-
if (params != null && isQueryConstraintParams(params)) {
|
|
116
|
-
swrKey = JSON.parse(JSON.stringify(params));
|
|
117
|
-
}
|
|
118
108
|
const fetcher = async () => {
|
|
119
109
|
if (params == null) {
|
|
120
110
|
return;
|
|
@@ -125,13 +115,13 @@
|
|
|
125
115
|
if (isQueryConstraintParams(params)) {
|
|
126
116
|
q = firestore.query(ref, ...params.queryConstraints);
|
|
127
117
|
} else {
|
|
128
|
-
const { where: w, limit: l } = params;
|
|
129
|
-
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...l ? [firestore.limit(l)] : []);
|
|
118
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
119
|
+
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...s ? [firestore.startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [firestore.startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [firestore.endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [firestore.endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [firestore.limit(l)] : []);
|
|
130
120
|
}
|
|
131
121
|
const sn = await firestore.getCountFromServer(q);
|
|
132
122
|
return sn.data().count;
|
|
133
123
|
};
|
|
134
|
-
return useSWR(
|
|
124
|
+
return useSWR(params, fetcher, {
|
|
135
125
|
...swrOptions,
|
|
136
126
|
use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []]
|
|
137
127
|
});
|
|
@@ -171,10 +161,6 @@
|
|
|
171
161
|
});
|
|
172
162
|
};
|
|
173
163
|
const useGetDocs = (params, swrOptions) => {
|
|
174
|
-
let swrKey = params;
|
|
175
|
-
if (params != null && isQueryConstraintParams(params)) {
|
|
176
|
-
swrKey = JSON.parse(JSON.stringify(params));
|
|
177
|
-
}
|
|
178
164
|
const fetcher = async () => {
|
|
179
165
|
if (params == null) {
|
|
180
166
|
return;
|
|
@@ -186,14 +172,14 @@
|
|
|
186
172
|
if (isQueryConstraintParams(params)) {
|
|
187
173
|
q = firestore.query(ref, ...params.queryConstraints);
|
|
188
174
|
} else {
|
|
189
|
-
const { where: w, orderBy: o, limit: l } = params;
|
|
190
|
-
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...l ? [firestore.limit(l)] : []);
|
|
175
|
+
const { where: w, orderBy: o, startAt: s, startAfter: sa, endAt: e, endBefore: eb, limit: l } = params;
|
|
176
|
+
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...s ? [firestore.startAt(...Array.isArray(s) ? s : [s])] : [], ...sa ? [firestore.startAfter(...Array.isArray(sa) ? sa : [sa])] : [], ...e ? [firestore.endAt(...Array.isArray(e) ? e : [e])] : [], ...eb ? [firestore.endBefore(...Array.isArray(eb) ? eb : [eb])] : [], ...l ? [firestore.limit(l)] : []);
|
|
191
177
|
}
|
|
192
178
|
const getFn = params.useOfflineCache ? firestore.getDocsFromCache : firestore.getDocs;
|
|
193
179
|
const sn = await getFn(q.withConverter(converter));
|
|
194
180
|
return sn.docs.map((x) => x.data());
|
|
195
181
|
};
|
|
196
|
-
return useSWR(
|
|
182
|
+
return useSWR(params, fetcher, {
|
|
197
183
|
...swrOptions,
|
|
198
184
|
use: [serializeMiddleware, ...(swrOptions == null ? void 0 : swrOptions.use) ?? []]
|
|
199
185
|
});
|
package/dist/util/type.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { orderBy, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint, where } from "firebase/firestore";
|
|
1
|
+
import type { endAt, endBefore, orderBy, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint, startAfter, startAt, where } from "firebase/firestore";
|
|
2
2
|
import type { QueryDocumentSnapshot } from "firebase/firestore";
|
|
3
3
|
type Join<K, P> = K extends string | number ? P extends string | number ? `${K}${"" extends P ? "" : "."}${P}` : never : never;
|
|
4
4
|
type Prev = [
|
|
@@ -33,6 +33,10 @@ export type ValueOf<T> = T[keyof T];
|
|
|
33
33
|
export type QueryParams<T> = {
|
|
34
34
|
where?: [Paths<T>, Parameters<typeof where>[1], ValueOf<T> | unknown][];
|
|
35
35
|
orderBy?: [Paths<T>, Parameters<typeof orderBy>[1]][];
|
|
36
|
+
startAt?: Parameters<typeof startAt>;
|
|
37
|
+
startAfter?: Parameters<typeof startAfter>;
|
|
38
|
+
endAt?: Parameters<typeof endAt>;
|
|
39
|
+
endBefore?: Parameters<typeof endBefore>;
|
|
36
40
|
limit?: number;
|
|
37
41
|
};
|
|
38
42
|
export type QueryConstraintParams = {
|
|
@@ -43,7 +47,7 @@ type BaseParams<T> = {
|
|
|
43
47
|
parseDates?: Paths<T>[];
|
|
44
48
|
};
|
|
45
49
|
export type KeyParams<T> = BaseParams<T> & (QueryParams<T> | QueryConstraintParams);
|
|
46
|
-
export type KeyParamsForCount<T> = BaseParams<T> & (Omit<QueryParams<T>, "
|
|
50
|
+
export type KeyParamsForCount<T> = BaseParams<T> & (Omit<QueryParams<T>, "parseDates"> | QueryConstraintParams);
|
|
47
51
|
export type GetDocKeyParams<T> = KeyParams<T> & {
|
|
48
52
|
useOfflineCache?: boolean;
|
|
49
53
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tatsuokaniwa/swr-firestore",
|
|
3
3
|
"description": "React Hooks library for Firestore using SWR's subscription feature.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/t-k/swr-firestore.git"
|
|
@@ -45,11 +45,9 @@
|
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@faker-js/faker": "^7.6.0",
|
|
48
|
-
"@playwright/test": "^1.31.2",
|
|
49
48
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
50
49
|
"@testing-library/jest-dom": "^5.16.5",
|
|
51
50
|
"@testing-library/react": "^14.0.0",
|
|
52
|
-
"@testing-library/user-event": "^14.4.3",
|
|
53
51
|
"@types/lodash-es": "^4.17.7",
|
|
54
52
|
"@types/react": "^18.0.28",
|
|
55
53
|
"@types/react-dom": "^18.0.11",
|
|
@@ -68,7 +66,7 @@
|
|
|
68
66
|
"swr": "^2.1.0 <3.0.0",
|
|
69
67
|
"typescript": "^5.0.2",
|
|
70
68
|
"vite-tsconfig-paths": "^4.0.7",
|
|
71
|
-
"vitest": "^0.
|
|
69
|
+
"vitest": "^0.30.0"
|
|
72
70
|
},
|
|
73
71
|
"peerDependencies": {
|
|
74
72
|
"firebase": "^9.11.0 < 10.0.0",
|