Skip to content

Commit

Permalink
feat(storage): add useMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Dec 21, 2021
1 parent 31b3142 commit dbb25e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,24 @@ Returns:
- `loading`: `true` while fetching the download URL; `false` if the download URL was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useMetadata

Returns the metadata of a Google Cloud Storage object

```javascript
const [metadata, loading, error] = useMetadata(storageReference);
```

Params:

- `reference`: Reference to a Google Cloud Storage object

Returns:

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

## Development

### Build
Expand Down
1 change: 1 addition & 0 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./useBytes";
export * from "./useDownloadURL";
export * from "./useMetadata";
19 changes: 19 additions & 0 deletions src/storage/useMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FullMetadata, getMetadata, StorageError, StorageReference } from "firebase/storage";
import { ValueHookResult } from "../common";
import { useOnce } from "../internal/useOnce";
import { isStorageRefEqual } from "./internal";

export type UseMetadataResult = ValueHookResult<FullMetadata, StorageError>;

/**
* Returns the metadata of a Google Cloud Storage object
*
* @param {StorageReference | undefined | null} reference Reference to a Google Cloud Storage object
* @returns {UseMetadataResult} Metadata, loading state, and error
* * value: Metadata; `undefined` if metadata is currently being fetched, or an error occurred
* * loading: `true` while fetching the metadata; `false` if the metadata was fetched successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useMetadata(reference: StorageReference | undefined | null): UseMetadataResult {
return useOnce(reference ?? undefined, getMetadata, isStorageRefEqual);
}

0 comments on commit dbb25e0

Please sign in to comment.