1+ import { DocumentData , DocumentReference , DocumentSnapshot , GeoPoint , QuerySnapshot , Timestamp } from "firebase-admin/firestore" ;
2+ import { CloudEvent } from "firebase-functions" ;
3+ import { FirestoreEvent , QueryDocumentSnapshot } from "firebase-functions/firestore" ;
4+
5+ export function serializeCloudEvent ( event : CloudEvent < unknown > ) : any {
6+ return {
7+ specversion : event . specversion ,
8+ id : event . id ,
9+ source : event . source ,
10+ subject : event . subject ,
11+ type : event . type ,
12+ time : event . time ,
13+ } ;
14+ }
15+
16+ export function serializeFirestoreEvent ( event : FirestoreEvent < unknown > , data : any ) : any {
17+ return {
18+ ...serializeCloudEvent ( event ) ,
19+ location : event . location ,
20+ project : event . project ,
21+ database : event . database ,
22+ namespace : event . namespace ,
23+ document : event . document ,
24+ params : event . params ,
25+ data,
26+ } ;
27+ }
28+
29+ export function serializeQuerySnapshot ( snapshot : QuerySnapshot ) : any {
30+ return {
31+ docs : snapshot . docs . map ( serializeQueryDocumentSnapshot ) ,
32+ } ;
33+ }
34+
35+ export function serializeQueryDocumentSnapshot ( snapshot : QueryDocumentSnapshot ) : any {
36+ return serializeDocumentSnapshot ( snapshot ) ;
37+ }
38+
39+ export function serializeDocumentSnapshot ( snapshot : DocumentSnapshot ) : any {
40+ return {
41+ exists : snapshot . exists ,
42+ ref : serializeDocumentReference ( snapshot . ref ) ,
43+ id : snapshot . id ,
44+ createTime : serializeTimestamp ( snapshot . createTime ) ,
45+ updateTime : serializeTimestamp ( snapshot . updateTime ) ,
46+ data : serializeDocumentData ( snapshot . data ( ) ?? { } ) ,
47+ } ;
48+ }
49+
50+ export function serializeGeoPoint ( geoPoint : GeoPoint ) : any {
51+ return {
52+ _type : "geopoint" ,
53+ latitude : geoPoint . latitude ,
54+ longitude : geoPoint . longitude ,
55+ } ;
56+ }
57+
58+ export function serializeTimestamp ( timestamp ?: Timestamp ) : any {
59+ if ( ! timestamp ) {
60+ return null ;
61+ }
62+
63+ return {
64+ _type : "timestamp" ,
65+ seconds : timestamp . seconds ,
66+ nanoseconds : timestamp . nanoseconds ,
67+ iso : timestamp . toDate ( ) . toISOString ( ) ,
68+ } ;
69+ }
70+
71+ export function serializeDocumentReference ( reference : DocumentReference ) : any {
72+ return {
73+ _type : "reference" ,
74+ path : reference . path ,
75+ id : reference . id ,
76+ } ;
77+ }
78+
79+ function serializeDocumentData ( data : DocumentData ) : any {
80+ const result : Record < string , unknown > = { } ;
81+ for ( const [ key , value ] of Object . entries ( data ) ) {
82+ if ( value instanceof Timestamp ) {
83+ result [ key ] = serializeTimestamp ( value ) ;
84+ } else if ( value instanceof GeoPoint ) {
85+ result [ key ] = serializeGeoPoint ( value ) ;
86+ } else if ( value instanceof DocumentReference ) {
87+ result [ key ] = serializeDocumentReference ( value ) ;
88+ } else if ( Array . isArray ( value ) ) {
89+ result [ key ] = value . map ( serializeDocumentData ) ;
90+ } else if ( typeof value === "object" && value !== null ) {
91+ result [ key ] = serializeDocumentData ( value ) ;
92+ } else {
93+ result [ key ] = value ;
94+ }
95+ }
96+ return result ;
97+ }
0 commit comments