Skip to content

Commit 67dea13

Browse files
committed
Tweak error responses and API structure
1 parent 378db89 commit 67dea13

File tree

19 files changed

+263
-156
lines changed

19 files changed

+263
-156
lines changed

auth/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ List of Auth hooks:
1818
const [user, loading, error] = useAuthState(auth);
1919
```
2020

21-
Returns the `firebase.User` (if logged in), a boolean to indicate whether the the user is still being loaded and any `firebase.FirebaseError` returned by Firebase when trying to load the user.
21+
Retrieve and monitor the authentication state from Firebase.
2222

2323
The `useAuthState` hook takes the following parameters:
2424

2525
- `auth`: `firebase.auth.Auth` instance for the app you would like to monitor
2626

27+
Returns:
28+
29+
- `user`: The `firebase.User` if logged in, or `void` if not
30+
- `loading`: A `boolean` to indicate whether the the authentication state is still being loaded
31+
- `error`: Any `firebase.auth.Error` returned by Firebase when trying to load the user, or `void` if there is no error
32+
2733
#### Full Example
2834

2935
```js

auth/helpers/index.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

auth/index.js.flow

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @flow
22
import type { FirebaseUser as User } from 'firebase';
3-
import typeof { FirebaseError } from 'firebase';
43
import type { Auth } from 'firebase/auth';
4+
import typeof { Error as AuthError } from 'firebase/auth';
55

6-
type LoadingHook<T> = [T | void, boolean, FirebaseError | void];
6+
type LoadingHook<T> = [T | void, boolean, AuthError | void];
77

88
export type AuthStateHook = LoadingHook<User>;
99
declare export function useAuthState(auth: Auth): AuthStateHook;

auth/useAuthState.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { auth, FirebaseError, User } from 'firebase';
1+
import { auth, User } from 'firebase';
22
import { useEffect } from 'react';
3-
import { transformError } from './helpers';
43
import { LoadingHook, useLoadingValue } from '../util';
54

6-
export type AuthStateHook = LoadingHook<User, FirebaseError>;
5+
export type AuthStateHook = LoadingHook<User, auth.Error>;
76

87
export default (auth: auth.Auth): AuthStateHook => {
9-
const { error, loading, setError, setValue, value } = useLoadingValue<User>(
10-
() => auth.currentUser
11-
);
8+
const { error, loading, setError, setValue, value } = useLoadingValue<
9+
User,
10+
auth.Error
11+
>(() => auth.currentUser);
1212

1313
useEffect(
1414
() => {
15-
const listener = auth.onAuthStateChanged(setValue, (error: auth.Error) =>
16-
setError(transformError(error))
17-
);
15+
const listener = auth.onAuthStateChanged(setValue, setError);
1816

1917
return () => {
2018
listener();

database/README.md

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useList } from 'react-firebase-hooks/database';
1515
List of Realtime Database hooks:
1616

1717
- [useList](#uselistref)
18-
- [useListKeys](#uselistkeyst)
18+
- [useListKeys](#uselistkeys)
1919
- [useListVals](#uselistvals)
2020
- [useObject](#useobjectref)
2121
- [useObjectVal](#useobjectval)
@@ -26,12 +26,18 @@ List of Realtime Database hooks:
2626
const [snapshots, loading, error] = useList(reference);
2727
```
2828

29-
Returns an array of `firebase.database.DataSnapshot` (if a reference is specified), a `boolean` to indicate if the data is still being loaded and any `firebase.FirebaseError` returned by Firebase when trying to load the data.
29+
Retrieve and monitor a list value in the Firebase Realtime Database.
3030

3131
The `useList` hook takes the following parameters:
3232

3333
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
3434

35+
Returns:
36+
37+
- `snapshots`: an array of `firebase.database.DataSnapshot`, or `void` if no reference is supplied
38+
- `loading`: a `boolean` to indicate if the data is still being loaded
39+
- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error
40+
3541
#### Full Example
3642

3743
```js
@@ -67,38 +73,57 @@ const DatabaseList = () => {
6773
const [keys, loading, error] = useListKeys(reference);
6874
```
6975

70-
As `useList`, but this hook returns a list of the `firebase.database.DataSnapshot.key` values, rather than the the `firebase.database.DataSnapshot`s themselves.
76+
As `useList`, but this hooks extracts the `firebase.database.DataSnapshot.key` values, rather than the the `firebase.database.DataSnapshot`s themselves.
7177

7278
The `useListKeys` hook takes the following parameters:
7379

7480
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
7581

82+
Returns:
83+
84+
- `keys`: an array of `string`, or `void` if no reference is supplied
85+
- `loading`: a `boolean` to indicate if the data is still being loaded
86+
- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error
87+
7688
### useListVals
7789

7890
```
79-
const [values, loading, error] = useListVals<T>(reference, keyField);
91+
const [values, loading, error] = useListVals<T>(reference, options);
8092
```
8193

82-
As `useList`, but this hook returns a typed list of the `firebase.database.DataSnapshot.val()` values, rather than the the
83-
`DataSnapshot`s themselves.
94+
As `useList`, but this hook extracts a typed list of the `firebase.database.DataSnapshot.val()` values, rather than the the
95+
`firebase.database.DataSnapshot`s themselves.
8496

8597
The `useListVals` hook takes the following parameters:
8698

8799
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
88-
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.
100+
- `options`: (optional) `Object` with the following parameters:
101+
- `keyField`: (optional) `string` field name that should be populated with the `firebase.firestore.QuerySnapshot.id` property in the returned values
102+
103+
Returns:
104+
105+
- `values`: an array of `T`, or `void` if no reference is supplied
106+
- `loading`: a `boolean` to indicate if the data is still being loaded
107+
- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error
89108

90109
### useObject
91110

92111
```
93112
const [snapshot, loading, error] = useObject(reference);
94113
```
95114

96-
Returns a `firebase.database.DataSnapshot` (if a reference is specified), a `boolean` to indicate if the data is still being loaded and any `firebase.FirebaseError` returned by Firebase when trying to load the data.
115+
Retrieve and monitor an object or primitive value in the Firebase Realtime Database.
97116

98117
The `useObject` hook takes the following parameters:
99118

100119
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
101120

121+
Returns:
122+
123+
- `snapshot`: a `firebase.database.DataSnapshot`, or `void` if no reference is supplied
124+
- `loading`: a `boolean` to indicate if the data is still being loaded
125+
- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error
126+
102127
#### Full Example
103128

104129
```js
@@ -122,13 +147,20 @@ const DatabaseValue = () => {
122147
### useObjectVal
123148

124149
```
125-
const [value, loading, error] = useObjectVal<T>(reference, keyField);
150+
const [value, loading, error] = useObjectVal<T>(reference, options);
126151
```
127152

128-
As `useObject`, but this hook returns the typed contents of `DataSnapshot.val()` rather than the
129-
`DataSnapshot` itself.
153+
As `useObject`, but this hook returns the typed contents of `firebase.database.DataSnapshot.val()`, rather than the the
154+
`firebase.database.DataSnapshot` itself.
130155

131156
The `useObjectVal` hook takes the following parameters:
132157

133158
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
134-
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.
159+
- `options`: (optional) `Object` with the following parameters:
160+
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.
161+
162+
Returns:
163+
164+
- `value`: a `T`, or `void` if no reference is supplied
165+
- `loading`: a `boolean` to indicate if the data is still being loaded
166+
- `error`: Any `firebase.FirebaseError` returned by Firebase when trying to load the data, or `void` if there is no error

database/index.js.flow

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ declare export function useList(query?: Query | null): ListHook;
1414
declare export function useListKeys(query?: Query | null): ListKeysHook;
1515
declare export function useListVals<T>(
1616
query?: Query | null,
17-
keyField?: string
17+
options?: {
18+
keyField?: string,
19+
}
1820
): ListValsHook<T>;
1921
declare export function useObject(query?: Query | null): ObjectHook;
20-
declare export function useObjectVal<T>(query?: Query | null): ObjectValHook<T>;
22+
declare export function useObjectVal<T>(
23+
query?: Query | null,
24+
options?: {
25+
keyField?: string,
26+
}
27+
): ObjectValHook<T>;

database/useList.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,16 @@ export const useListKeys = (query?: database.Query | null): ListKeysHook => {
8181

8282
export const useListVals = <T>(
8383
query?: database.Query | null,
84-
keyField?: string
84+
options?: {
85+
keyField?: string;
86+
}
8587
): ListValsHook<T> => {
8688
const [value, loading, error] = useList(query);
8789
return [
8890
value
89-
? value.map(snapshot => snapshotToData(snapshot, keyField))
91+
? value.map(snapshot =>
92+
snapshotToData(snapshot, options ? options.keyField : undefined)
93+
)
9094
: undefined,
9195
loading,
9296
error,

database/useObject.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export type ObjectValHook<T> = LoadingHook<T, FirebaseError>;
88

99
export const useObject = (query?: database.Query | null): ObjectHook => {
1010
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
11-
database.DataSnapshot
11+
database.DataSnapshot,
12+
FirebaseError
1213
>();
1314
const ref = useIsEqualRef(query, reset);
1415

@@ -34,8 +35,16 @@ export const useObject = (query?: database.Query | null): ObjectHook => {
3435

3536
export const useObjectVal = <T>(
3637
query?: database.Query | null,
37-
keyField?: string
38+
options?: {
39+
keyField?: string;
40+
}
3841
): ObjectValHook<T> => {
3942
const [value, loading, error] = useObject(query);
40-
return [value ? snapshotToData(value, keyField) : undefined, loading, error];
43+
return [
44+
value
45+
? snapshotToData(value, options ? options.keyField : undefined)
46+
: undefined,
47+
loading,
48+
error,
49+
];
4150
};

0 commit comments

Comments
 (0)