Skip to content

Commit 316301a

Browse files
committed
Consolidate hook options
1 parent a1f2b64 commit 316301a

File tree

7 files changed

+24
-28
lines changed

7 files changed

+24
-28
lines changed

database/helpers/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import firebase from 'firebase/app';
22

3+
export type ValOptions = {
4+
keyField?: string;
5+
refField?: string;
6+
};
7+
38
const isObject = (val: any) =>
49
val != null && typeof val === 'object' && Array.isArray(val) === false;
510

database/useList.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import firebase from 'firebase/app';
22
import { useEffect, useMemo } from 'react';
3-
import { snapshotToData } from './helpers';
3+
import { snapshotToData, ValOptions } from './helpers';
44
import useListReducer from './helpers/useListReducer';
55
import { LoadingHook, useIsEqualRef } from '../util';
66

@@ -131,10 +131,7 @@ export const useListKeys = (
131131

132132
export const useListVals = <T>(
133133
query?: firebase.database.Query | null,
134-
options?: {
135-
keyField?: string;
136-
refField?: string;
137-
}
134+
options?: ValOptions,
138135
): ListValsHook<T> => {
139136
const keyField = options ? options.keyField : undefined;
140137
const refField = options ? options.refField : undefined;

database/useObject.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import firebase from 'firebase/app';
22
import { useEffect, useMemo } from 'react';
3-
import { snapshotToData } from './helpers';
3+
import { snapshotToData, ValOptions } from './helpers';
44
import { LoadingHook, useIsEqualRef, useLoadingValue } from '../util';
55

66
export type ObjectHook = LoadingHook<
@@ -38,10 +38,7 @@ export const useObject = (
3838

3939
export const useObjectVal = <T>(
4040
query?: firebase.database.Query | null,
41-
options?: {
42-
keyField?: string;
43-
refField?: string;
44-
}
41+
options?: ValOptions
4542
): ObjectValHook<T> => {
4643
const keyField = options ? options.keyField : undefined;
4744
const refField = options ? options.refField : undefined;

firestore/helpers/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import firebase from 'firebase/app';
22

3+
export type Options = {
4+
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
5+
};
6+
export type DataOptions = Options & {
7+
idField?: string;
8+
refField?: string;
9+
};
10+
311
export const snapshotToData = (
412
snapshot: firebase.firestore.DocumentSnapshot,
513
idField?: string,

firestore/useCollection.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import firebase from 'firebase/app';
22
import { useEffect, useMemo } from 'react';
3-
import { snapshotToData } from './helpers';
3+
import { DataOptions, Options, snapshotToData } from './helpers';
44
import { LoadingHook, useIsEqualRef, useLoadingValue } from '../util';
55

66
export type CollectionHook<T> = LoadingHook<
@@ -11,9 +11,7 @@ export type CollectionDataHook<T> = LoadingHook<T[], Error>;
1111

1212
export const useCollection = <T>(
1313
query?: firebase.firestore.Query | null,
14-
options?: {
15-
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
16-
}
14+
options?: Options,
1715
): CollectionHook<T> => {
1816
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
1917
firebase.firestore.QuerySnapshot,
@@ -50,11 +48,7 @@ export const useCollection = <T>(
5048

5149
export const useCollectionData = <T>(
5250
query?: firebase.firestore.Query | null,
53-
options?: {
54-
idField?: string;
55-
refField?: string;
56-
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
57-
}
51+
options?: DataOptions
5852
): CollectionDataHook<T> => {
5953
const idField = options ? options.idField : undefined;
6054
const refField = options ? options.refField : undefined;

firestore/useDocument.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import firebase from 'firebase/app';
22
import { useEffect, useMemo } from 'react';
3-
import { snapshotToData } from './helpers';
3+
import { DataOptions, Options, snapshotToData } from './helpers';
44
import { LoadingHook, useIsEqualRef, useLoadingValue } from '../util';
55

66
export type DocumentHook<T> = LoadingHook<
@@ -11,9 +11,7 @@ export type DocumentDataHook<T> = LoadingHook<T, Error>;
1111

1212
export const useDocument = <T>(
1313
docRef?: firebase.firestore.DocumentReference | null,
14-
options?: {
15-
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
16-
}
14+
options?: Options
1715
): DocumentHook<T> => {
1816
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
1917
firebase.firestore.DocumentSnapshot,
@@ -50,11 +48,7 @@ export const useDocument = <T>(
5048

5149
export const useDocumentData = <T>(
5250
docRef?: firebase.firestore.DocumentReference | null,
53-
options?: {
54-
idField?: string;
55-
refField?: string;
56-
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
57-
}
51+
options?: DataOptions,
5852
): DocumentDataHook<T> => {
5953
const idField = options ? options.idField : undefined;
6054
const refField = options ? options.refField : undefined;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@types/react": "^17.0.0",
5353
"firebase": "^8.0.0",
5454
"path": "^0.12.7",
55+
"prettier": "2.2.1",
5556
"react": "^17.0.1",
5657
"rimraf": "^2.6.2",
5758
"rollup": "0.57.1",

0 commit comments

Comments
 (0)