@tatsuokaniwa/swr-firestore 0.1.0 → 0.2.1
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 +70 -0
- package/dist/hooks/useGetDoc.d.ts +4 -0
- package/dist/hooks/useGetDocs.d.ts +4 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +31 -2
- package/dist/index.umd.cjs +29 -0
- package/dist/util/type.d.ts +3 -0
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
# swr-firestore
|
|
2
2
|
|
|
3
|
+
[](https://badge.fury.io/js/@tatsuokaniwa%2Fswr-firestore)
|
|
4
|
+
[](https://github.com/t-k/swr-firestore/actions/workflows/test.yaml)
|
|
5
|
+
|
|
3
6
|
React Hooks library for Firestore, built using the Firebase v9 modular SDK. It utilizes the [`useSWRSubscription`](https://swr.vercel.app/ja/docs/subscription) function from SWR library to enable subscription-based data fetching and caching.
|
|
4
7
|
|
|
8
|
+
Inspired by [swr-firestore-v9](https://www.npmjs.com/package/swr-firestore-v9)
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# if you use NPM
|
|
14
|
+
npm i --save @tatsuokaniwa/swr-firestore
|
|
15
|
+
# if you use Yarn
|
|
16
|
+
yarn add @tatsuokaniwa/swr-firestore
|
|
17
|
+
# if you use pnpm
|
|
18
|
+
pnpm i @tatsuokaniwa/swr-firestore
|
|
19
|
+
```
|
|
20
|
+
|
|
5
21
|
## Usage
|
|
6
22
|
|
|
7
23
|
```tsx
|
|
@@ -179,6 +195,60 @@ const { data, error } = useDoc<Post>({
|
|
|
179
195
|
});
|
|
180
196
|
```
|
|
181
197
|
|
|
198
|
+
### `useGetDocs(params, swrOptions)`
|
|
199
|
+
|
|
200
|
+
Fetch documents with firestore's [getDocs](https://firebase.google.com/docs/reference/js/firestore_.md#getdocs) function
|
|
201
|
+
|
|
202
|
+
#### Parameters
|
|
203
|
+
|
|
204
|
+
- `params`: KeyParams | null
|
|
205
|
+
- `swrOptions`: [Options for SWR hook](https://swr.vercel.app/docs/api#options) except `fetcher`
|
|
206
|
+
|
|
207
|
+
#### Return values
|
|
208
|
+
|
|
209
|
+
Returns [`SWRResponse`](https://swr.vercel.app/docs/api#return-values)
|
|
210
|
+
|
|
211
|
+
- `data`: data for given path's collection
|
|
212
|
+
- `error`: FirestoreError | Error
|
|
213
|
+
- `isLoading`: if there's an ongoing request and no "loaded data". Fallback data and previous data are not considered "loaded data"
|
|
214
|
+
- `isValidating`: if there's a request or revalidation loading
|
|
215
|
+
- `mutate(data?, options?)`: function to mutate the cached data (details)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { useGetDocs } from "@tatsuokaniwa/swr-firestore";
|
|
220
|
+
|
|
221
|
+
const { data, error } = useGetDocs<Post>({
|
|
222
|
+
path: `Posts`,
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
### `useGetDoc(params, swrOptions)`
|
|
226
|
+
|
|
227
|
+
Fetch the document with firestore's [getDoc](https://firebase.google.com/docs/reference/js/firestore_.md#getdoc) function
|
|
228
|
+
|
|
229
|
+
#### Parameters
|
|
230
|
+
|
|
231
|
+
- `params`: KeyParams except `where`, `orderBy`, `limit` | null
|
|
232
|
+
- `swrOptions`: [Options for SWR hook](https://swr.vercel.app/docs/api#options) except `fetcher`
|
|
233
|
+
|
|
234
|
+
#### Return values
|
|
235
|
+
|
|
236
|
+
Returns [`SWRResponse`](https://swr.vercel.app/docs/api#return-values)
|
|
237
|
+
|
|
238
|
+
- `data`: data for given path's document
|
|
239
|
+
- `error`: FirestoreError | Error
|
|
240
|
+
- `isLoading`: if there's an ongoing request and no "loaded data". Fallback data and previous data are not considered "loaded data"
|
|
241
|
+
- `isValidating`: if there's a request or revalidation loading
|
|
242
|
+
- `mutate(data?, options?)`: function to mutate the cached data (details)
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
import { useGetDoc } from "@tatsuokaniwa/swr-firestore";
|
|
246
|
+
|
|
247
|
+
const { data, error } = useGetDoc<Post>({
|
|
248
|
+
path: `Posts/${postId}`,
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
182
252
|
## Testing
|
|
183
253
|
|
|
184
254
|
Before running the test, you need to install the [Firebase tools](https://firebase.google.com/docs/cli).
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SWRHook } from "swr";
|
|
2
|
+
import type { DocumentData, GetDocKeyParams } from "../util/type";
|
|
3
|
+
declare const useGetDoc: <T>(params: Omit<GetDocKeyParams<T>, "where" | "orderBy" | "limit"> | null, swrOptions?: Omit<Parameters<SWRHook>[2], "fetcher">) => import("swr/_internal").SWRResponse<DocumentData<T> | undefined, any, Required<Omit<Partial<import("swr/_internal").PublicConfiguration<unknown, unknown, import("swr/_internal").BareFetcher<unknown>>> | undefined, "fetcher">>>;
|
|
4
|
+
export default useGetDoc;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SWRHook } from "swr";
|
|
2
|
+
import type { DocumentData, GetDocKeyParams } from "../util/type";
|
|
3
|
+
declare const useGetDocs: <T>(params: GetDocKeyParams<T> | null, swrOptions?: Omit<Parameters<SWRHook>[2], "fetcher">) => import("swr/_internal").SWRResponse<DocumentData<T>[] | undefined, any, Required<Omit<Partial<import("swr/_internal").PublicConfiguration<unknown, unknown, import("swr/_internal").BareFetcher<unknown>>> | undefined, "fetcher">>>;
|
|
4
|
+
export default useGetDocs;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,5 +4,7 @@ import useCollectionCount from "./hooks/useCollectionCount";
|
|
|
4
4
|
import useCollectionGroup from "./hooks/useCollectionGroup";
|
|
5
5
|
import useCollectionGroupCount from "./hooks/useCollectionGroupCount";
|
|
6
6
|
import useDoc from "./hooks/useDoc";
|
|
7
|
+
import useGetDoc from "./hooks/useGetDoc";
|
|
8
|
+
import useGetDocs from "./hooks/useGetDocs";
|
|
7
9
|
export type { DocumentData, KeyParams };
|
|
8
|
-
export { useCollection, useCollectionCount, useCollectionGroup, useCollectionGroupCount, useDoc, };
|
|
10
|
+
export { useCollection, useCollectionCount, useCollectionGroup, useCollectionGroupCount, useDoc, useGetDoc, useGetDocs, };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { collection, getFirestore, query, where, orderBy, limit, onSnapshot, getCountFromServer, collectionGroup, doc } from "firebase/firestore";
|
|
1
|
+
import { collection, getFirestore, query, where, orderBy, limit, onSnapshot, getCountFromServer, collectionGroup, doc, getDocFromCache, getDoc, getDocsFromCache, getDocs } from "firebase/firestore";
|
|
2
2
|
import React, { createContext, useContext, useEffect, useLayoutEffect, useRef, useMemo, useCallback, useDebugValue } from "react";
|
|
3
3
|
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
4
4
|
var isArray$c = Array.isArray;
|
|
@@ -4418,10 +4418,39 @@ const useDoc = (params) => {
|
|
|
4418
4418
|
return () => unsub && unsub();
|
|
4419
4419
|
});
|
|
4420
4420
|
};
|
|
4421
|
+
const useGetDoc = (params, swrOptions) => {
|
|
4422
|
+
const fetcher = async (key) => {
|
|
4423
|
+
const { path, ...option } = key;
|
|
4424
|
+
const converter = getFirestoreConverter(option == null ? void 0 : option.parseDates);
|
|
4425
|
+
const ref = doc(getFirestore(), path);
|
|
4426
|
+
const getFn = option.useOfflineCache ? getDocFromCache : getDoc;
|
|
4427
|
+
const sn = await getFn(ref.withConverter(converter));
|
|
4428
|
+
return sn.data();
|
|
4429
|
+
};
|
|
4430
|
+
return useSWR(params, fetcher, swrOptions);
|
|
4431
|
+
};
|
|
4432
|
+
const useGetDocs = (params, swrOptions) => {
|
|
4433
|
+
const fetcher = async (key) => {
|
|
4434
|
+
const { path, ...option } = key;
|
|
4435
|
+
const converter = getFirestoreConverter(option == null ? void 0 : option.parseDates);
|
|
4436
|
+
const ref = collection(getFirestore(), path);
|
|
4437
|
+
let q;
|
|
4438
|
+
if (option) {
|
|
4439
|
+
const { where: w, orderBy: o, limit: l } = option;
|
|
4440
|
+
q = query(ref, ...(w ? w : []).map((q2) => where(...q2)), ...(o ? o : []).map((q2) => orderBy(...q2)), ...l ? [limit(l)] : []);
|
|
4441
|
+
}
|
|
4442
|
+
const getFn = option.useOfflineCache ? getDocsFromCache : getDocs;
|
|
4443
|
+
const sn = await getFn((q ?? ref).withConverter(converter));
|
|
4444
|
+
return sn.docs.map((x) => x.data());
|
|
4445
|
+
};
|
|
4446
|
+
return useSWR(params, fetcher, swrOptions);
|
|
4447
|
+
};
|
|
4421
4448
|
export {
|
|
4422
4449
|
useCollection,
|
|
4423
4450
|
useCollectionCount,
|
|
4424
4451
|
useCollectionGroup,
|
|
4425
4452
|
useCollectionGroupCount,
|
|
4426
|
-
useDoc
|
|
4453
|
+
useDoc,
|
|
4454
|
+
useGetDoc,
|
|
4455
|
+
useGetDocs
|
|
4427
4456
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -4420,10 +4420,39 @@
|
|
|
4420
4420
|
return () => unsub && unsub();
|
|
4421
4421
|
});
|
|
4422
4422
|
};
|
|
4423
|
+
const useGetDoc = (params, swrOptions) => {
|
|
4424
|
+
const fetcher = async (key) => {
|
|
4425
|
+
const { path, ...option } = key;
|
|
4426
|
+
const converter = getFirestoreConverter(option == null ? void 0 : option.parseDates);
|
|
4427
|
+
const ref = firestore.doc(firestore.getFirestore(), path);
|
|
4428
|
+
const getFn = option.useOfflineCache ? firestore.getDocFromCache : firestore.getDoc;
|
|
4429
|
+
const sn = await getFn(ref.withConverter(converter));
|
|
4430
|
+
return sn.data();
|
|
4431
|
+
};
|
|
4432
|
+
return useSWR(params, fetcher, swrOptions);
|
|
4433
|
+
};
|
|
4434
|
+
const useGetDocs = (params, swrOptions) => {
|
|
4435
|
+
const fetcher = async (key) => {
|
|
4436
|
+
const { path, ...option } = key;
|
|
4437
|
+
const converter = getFirestoreConverter(option == null ? void 0 : option.parseDates);
|
|
4438
|
+
const ref = firestore.collection(firestore.getFirestore(), path);
|
|
4439
|
+
let q;
|
|
4440
|
+
if (option) {
|
|
4441
|
+
const { where: w, orderBy: o, limit: l } = option;
|
|
4442
|
+
q = firestore.query(ref, ...(w ? w : []).map((q2) => firestore.where(...q2)), ...(o ? o : []).map((q2) => firestore.orderBy(...q2)), ...l ? [firestore.limit(l)] : []);
|
|
4443
|
+
}
|
|
4444
|
+
const getFn = option.useOfflineCache ? firestore.getDocsFromCache : firestore.getDocs;
|
|
4445
|
+
const sn = await getFn((q ?? ref).withConverter(converter));
|
|
4446
|
+
return sn.docs.map((x) => x.data());
|
|
4447
|
+
};
|
|
4448
|
+
return useSWR(params, fetcher, swrOptions);
|
|
4449
|
+
};
|
|
4423
4450
|
exports2.useCollection = useCollection;
|
|
4424
4451
|
exports2.useCollectionCount = useCollectionCount;
|
|
4425
4452
|
exports2.useCollectionGroup = useCollectionGroup;
|
|
4426
4453
|
exports2.useCollectionGroupCount = useCollectionGroupCount;
|
|
4427
4454
|
exports2.useDoc = useDoc;
|
|
4455
|
+
exports2.useGetDoc = useGetDoc;
|
|
4456
|
+
exports2.useGetDocs = useGetDocs;
|
|
4428
4457
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
4429
4458
|
});
|
package/dist/util/type.d.ts
CHANGED
|
@@ -8,4 +8,7 @@ export type KeyParams<T> = {
|
|
|
8
8
|
limit?: number;
|
|
9
9
|
parseDates?: Extract<keyof T, string>[];
|
|
10
10
|
};
|
|
11
|
+
export type GetDocKeyParams<T> = KeyParams<T> & {
|
|
12
|
+
useOfflineCache?: boolean;
|
|
13
|
+
};
|
|
11
14
|
export type DocumentData<T> = T & Pick<QueryDocumentSnapshot, "exists" | "id" | "ref">;
|
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": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/t-k/swr-firestore.git"
|
|
@@ -60,7 +60,6 @@
|
|
|
60
60
|
"eslint": "^8.36.0",
|
|
61
61
|
"eslint-plugin-react": "^7.32.2",
|
|
62
62
|
"firebase": "^9.17.1",
|
|
63
|
-
"firebase-tools": "^11.24.1",
|
|
64
63
|
"jsdom": "^21.1.1",
|
|
65
64
|
"prettier": "^2.8.5",
|
|
66
65
|
"react": "^18.2.0",
|