1 | import * as firestore from "firebase-admin/firestore";
|
2 | import { ParamsOf } from "../../common/params";
|
3 | import { Change, CloudEvent, CloudFunction } from "../core";
|
4 | import { EventHandlerOptions } from "../options";
|
5 | import { Expression } from "../../params";
|
6 | export { Change };
|
7 | /** A Firestore DocumentSnapshot */
|
8 | export type DocumentSnapshot = firestore.DocumentSnapshot;
|
9 | /** A Firestore QueryDocumentSnapshot */
|
10 | export type QueryDocumentSnapshot = firestore.QueryDocumentSnapshot;
|
11 | /**
|
12 | * AuthType defines the possible values for the authType field in a Firestore event with auth context.
|
13 | * - service_account: a non-user principal used to identify a workload or machine user.
|
14 | * - api_key: a non-user client API key.
|
15 | * - system: an obscured identity used when Cloud Platform or another system triggered the event. Examples include a database record which was deleted based on a TTL.
|
16 | * - unauthenticated: an unauthenticated action.
|
17 | * - unknown: a general type to capture all other principals not captured in the other auth types.
|
18 | */
|
19 | export type AuthType = "service_account" | "api_key" | "system" | "unauthenticated" | "unknown";
|
20 | /** A CloudEvent that contains a DocumentSnapshot or a Change<DocumentSnapshot> */
|
21 | export interface FirestoreEvent<T, Params = Record<string, string>> extends CloudEvent<T> {
|
22 | /** The location of the Firestore instance */
|
23 | location: string;
|
24 | /** The project identifier */
|
25 | project: string;
|
26 | /** The Firestore database */
|
27 | database: string;
|
28 | /** The Firestore namespace */
|
29 | namespace: string;
|
30 | /** The document path */
|
31 | document: string;
|
32 | /**
|
33 | * An object containing the values of the path patterns.
|
34 | * Only named capture groups will be populated - {key}, {key=*}, {key=**}
|
35 | */
|
36 | params: Params;
|
37 | }
|
38 | export interface FirestoreAuthEvent<T, Params = Record<string, string>> extends FirestoreEvent<T, Params> {
|
39 | /** The type of principal that triggered the event */
|
40 | authType: AuthType;
|
41 | /** The unique identifier for the principal */
|
42 | authId?: string;
|
43 | }
|
44 | /** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace. */
|
45 | export interface DocumentOptions<Document extends string = string> extends EventHandlerOptions {
|
46 | /** The document path */
|
47 | document: Document | Expression<string>;
|
48 | /** The Firestore database */
|
49 | database?: string | Expression<string>;
|
50 | /** The Firestore namespace */
|
51 | namespace?: string | Expression<string>;
|
52 | }
|
53 | /**
|
54 | * Event handler that triggers when a document is created, updated, or deleted in Firestore.
|
55 | *
|
56 | * @param document - The Firestore document path to trigger on.
|
57 | * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
|
58 | */
|
59 | export declare function onDocumentWritten<Document extends string>(document: Document, handler: (event: FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
60 | /**
|
61 | * Event handler that triggers when a document is created, updated, or deleted in Firestore.
|
62 | *
|
63 | * @param opts - Options that can be set on an individual event-handling function.
|
64 | * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
|
65 | */
|
66 | export declare function onDocumentWritten<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
67 | /**
|
68 | * Event handler that triggers when a document is created, updated, or deleted in Firestore.
|
69 | * This trigger also provides the authentication context of the principal who triggered the event.
|
70 | *
|
71 | * @param document - The Firestore document path to trigger on.
|
72 | * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
|
73 | */
|
74 | export declare function onDocumentWrittenWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
75 | /**
|
76 | * Event handler that triggers when a document is created, updated, or deleted in Firestore.
|
77 | * This trigger also provides the authentication context of the principal who triggered the event.
|
78 | *
|
79 | * @param opts - Options that can be set on an individual event-handling function.
|
80 | * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
|
81 | */
|
82 | export declare function onDocumentWrittenWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
83 | /**
|
84 | * Event handler that triggers when a document is created in Firestore.
|
85 | *
|
86 | * @param document - The Firestore document path to trigger on.
|
87 | * @param handler - Event handler which is run every time a Firestore create occurs.
|
88 | */
|
89 | export declare function onDocumentCreated<Document extends string>(document: Document, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|
90 | /**
|
91 | * Event handler that triggers when a document is created in Firestore.
|
92 | *
|
93 | * @param opts - Options that can be set on an individual event-handling function.
|
94 | * @param handler - Event handler which is run every time a Firestore create occurs.
|
95 | */
|
96 | export declare function onDocumentCreated<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|
97 | /**
|
98 | * Event handler that triggers when a document is created in Firestore.
|
99 | * This trigger also provides the authentication context of the principal who triggered the event.
|
100 | *
|
101 | * @param document - The Firestore document path to trigger on.
|
102 | * @param handler - Event handler which is run every time a Firestore create occurs.
|
103 | */
|
104 | export declare function onDocumentCreatedWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|
105 | /**
|
106 | * Event handler that triggers when a document is created in Firestore.
|
107 | * This trigger also provides the authentication context of the principal who triggered the event.
|
108 | *
|
109 | * @param opts - Options that can be set on an individual event-handling function.
|
110 | * @param handler - Event handler which is run every time a Firestore create occurs.
|
111 | */
|
112 | export declare function onDocumentCreatedWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|
113 | /**
|
114 | * Event handler that triggers when a document is updated in Firestore.
|
115 | *
|
116 | * @param document - The Firestore document path to trigger on.
|
117 | * @param handler - Event handler which is run every time a Firestore update occurs.
|
118 | */
|
119 | export declare function onDocumentUpdated<Document extends string>(document: Document, handler: (event: FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
120 | /**
|
121 | * Event handler that triggers when a document is updated in Firestore.
|
122 | *
|
123 | * @param opts - Options that can be set on an individual event-handling function.
|
124 | * @param handler - Event handler which is run every time a Firestore update occurs.
|
125 | */
|
126 | export declare function onDocumentUpdated<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
127 | /**
|
128 | * Event handler that triggers when a document is updated in Firestore.
|
129 | * This trigger also provides the authentication context of the principal who triggered the event.
|
130 | *
|
131 | * @param document - The Firestore document path to trigger on.
|
132 | * @param handler - Event handler which is run every time a Firestore update occurs.
|
133 | */
|
134 | export declare function onDocumentUpdatedWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
135 | /**
|
136 | * Event handler that triggers when a document is updated in Firestore.
|
137 | * This trigger also provides the authentication context of the principal who triggered the event.
|
138 | *
|
139 | * @param opts - Options that can be set on an individual event-handling function.
|
140 | * @param handler - Event handler which is run every time a Firestore update occurs.
|
141 | */
|
142 | export declare function onDocumentUpdatedWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
|
143 | /**
|
144 | * Event handler that triggers when a document is deleted in Firestore.
|
145 | *
|
146 | * @param document - The Firestore document path to trigger on.
|
147 | * @param handler - Event handler which is run every time a Firestore delete occurs.
|
148 | */
|
149 | export declare function onDocumentDeleted<Document extends string>(document: Document, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|
150 | /**
|
151 | * Event handler that triggers when a document is deleted in Firestore.
|
152 | *
|
153 | * @param opts - Options that can be set on an individual event-handling function.
|
154 | * @param handler - Event handler which is run every time a Firestore delete occurs.
|
155 | */
|
156 | export declare function onDocumentDeleted<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|
157 | /**
|
158 | * Event handler that triggers when a document is deleted in Firestore.
|
159 | * This trigger also provides the authentication context of the principal who triggered the event.
|
160 | *
|
161 | * @param document - The Firestore document path to trigger on.
|
162 | * @param handler - Event handler which is run every time a Firestore delete occurs.
|
163 | */
|
164 | export declare function onDocumentDeletedWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|
165 | /**
|
166 | * Event handler that triggers when a document is deleted in Firestore.
|
167 | * This trigger also provides the authentication context of the principal who triggered the event.
|
168 | *
|
169 | * @param opts - Options that can be set on an individual event-handling function.
|
170 | * @param handler - Event handler which is run every time a Firestore delete occurs.
|
171 | */
|
172 | export declare function onDocumentDeletedWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
|