Skip to content

Commit

Permalink
feat(messaging): add useMessagingToken (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Oct 17, 2021
1 parent ccf9926 commit 8bc1f4e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
/auth/
/database/
/firestore/
/messaging/
/storage/
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ CHANGELOG.md
/auth/
/database/
/firestore/
/messaging/
/storage/
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ This library consists of 4 modules with many hooks:
- [`useDocumentData`](#useDocumentData)
- [`useDocumentDataOnce`](#useDocumentataOnce)
- [`useDocumentOnce`](#useDocumentOnce)
- [`messaging`](#Messaging)
- [`useMessagingToken`](#useMessagingToken)
- [`storage`](#Storage)
- [`useDownloadURL`](#useDownloadURL)

Expand Down Expand Up @@ -323,6 +325,31 @@ Returns:
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

### Messaging

```javascript
import { ... } from 'react-firehooks/messaging';
```

#### useMessagingToken

Returns the messaging token. The token never updates.

```javascript
const [token, loading, error] = useMessagingToken(messaging, options);
```

Params:

- `messaging`: Firestore Messaging instance
- `options`: Options to configure how the token will be fetched

Returns:

- `value`: Messaging token; `undefined` if token is currently being fetched, or an error occurred
- `loading`: `true` while fetching the token; `false` if the token was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

### Storage

```javascript
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"require": "./lib/cjs/firestore/index.js",
"default": "./lib/esm/firestore/index.js"
},
"./messaging": {
"require": "./lib/cjs/messaging/index.js",
"default": "./lib/esm/messaging/index.js"
},
"./storage": {
"require": "./lib/cjs/storage/index.js",
"default": "./lib/esm/storage/index.js"
Expand All @@ -43,9 +47,10 @@
"react",
"hooks",
"firebase",
"firestore",
"database",
"auth",
"database",
"firestore",
"messaging",
"storage"
],
"author": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-modules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require("fs");

const modules = ["auth", "database", "firestore", "storage"];
const modules = ["auth", "database", "firestore", "messaging", "storage"];
for (const module of modules) {
const packageJson = {
main: `../lib/cjs/${module}/index.js`,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./auth";
export * from "./common";
export * from "./database";
export * from "./firestore";
export * from "./messaging";
export * from "./storage";
1 change: 1 addition & 0 deletions src/messaging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useMessagingToken";
30 changes: 30 additions & 0 deletions src/messaging/useMessagingToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Messaging, getToken, GetTokenOptions } from "firebase/messaging";
import { ValueHookResult } from "../common";
import { useOnce } from "../internal/useOnce";

export type UseMessagingTokenResult = ValueHookResult<string, Error>;

/**
* Options to configure how the token will be fetched
*/
export interface UseMessagingTokenOptions {
getTokenOptions?: GetTokenOptions;
}

/**
* Returns the messaging token. The token never updates.
*
* @param {Messaging} messaging Firebase Messaging instance
* @param {UseMessagingTokenOptions} options Options to configure how the token will be fetched
* @returns {UseMessagingTokenResult} Token, loading state, and error
* * value: Messaging token; `undefined` if token is currently being fetched, or an error occurred
* * loading: `true` while fetching the token; `false` if the token was fetched successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useMessagingToken(messaging: Messaging, options?: UseMessagingTokenOptions): UseMessagingTokenResult {
return useOnce(
messaging,
(m) => getToken(m, options?.getTokenOptions),
() => true
);
}

0 comments on commit 8bc1f4e

Please sign in to comment.