Skip to content

Commit

Permalink
feat: expose devalue transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 19, 2023
1 parent 46e7716 commit 7c1ce18
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/firestore/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,24 @@ export function extractRefs(

return dataAndRefs
}

/**
* Custom stringifier for [devalue](https://github.com/Rich-Harris/devalue) to support Firestore Timestamp and GeoPoint
* on SSR.
*/
export const devalueCustomStringifiers = {
TimeStamp: (data: unknown) => data instanceof Timestamp && data.toJSON(),
GeoPoint: (data: unknown) => data instanceof GeoPoint && data.toJSON(),
}

/**
* Custom parsers for [devalue](https://github.com/Rich-Harris/devalue) to support Firestore Timestamp and GeoPoint on
* SSR.
*/
export const devalueCustomParsers = {
TimeStamp: (data: ReturnType<Timestamp['toJSON']>) =>
new Timestamp(data.seconds, data.nanoseconds),

GeoPoint: (data: ReturnType<GeoPoint['toJSON']>) =>
new GeoPoint(data.latitude, data.longitude),
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export type {
*/
export { useCollection, useDocument, useFirestore } from './firestore'
export { firestoreOptionsDefaults as globalFirestoreOptions } from './firestore/bind'
export { firestoreDefaultConverter } from './firestore/utils'
export {
firestoreDefaultConverter,
devalueCustomParsers,
devalueCustomStringifiers,
} from './firestore/utils'
export type {
UseCollectionOptions,
UseDocumentOptions,
Expand Down

0 comments on commit 7c1ce18

Please sign in to comment.