@@ -28,7 +28,8 @@ export { Request, Response };
28
28
29
29
const WILDCARD_REGEX = new RegExp ( '{[^/{}]*}' , 'g' ) ;
30
30
31
- /** Wire format for an event
31
+ /**
32
+ * Wire format for an event.
32
33
* @internal
33
34
*/
34
35
export interface Event {
@@ -41,56 +42,78 @@ export interface Event {
41
42
data : any ;
42
43
}
43
44
44
- /** The context in which an event occurred.
45
+ /**
46
+ * The context in which an event occurred.
47
+ *
45
48
* An EventContext describes:
46
49
* - The time an event occurred.
47
50
* - A unique identifier of the event.
48
51
* - The resource on which the event occurred, if applicable.
49
- * - Authorization of the request that triggered the event, if applicable and available.
52
+ * - Authorization of the request that triggered the event, if applicable and
53
+ * available.
50
54
*/
51
55
export interface EventContext {
52
- /** ID of the event */
56
+ /**
57
+ * Firebase auth variable for the user whose action triggered the function.
58
+ * Field will be null for unauthenticated users, and will not exist for admin
59
+ * users. Only available for database functions.
60
+ */
61
+ auth ?: {
62
+ token : object ;
63
+ uid : string ;
64
+ } ;
65
+ /**
66
+ * Type of authentication for the triggering action. Only available for
67
+ * database functions.
68
+ */
69
+ authType ?: 'ADMIN' | 'USER' | 'UNAUTHENTICATED' ;
70
+ /**
71
+ * ID of the event
72
+ */
53
73
eventId : string ;
54
- /** Timestamp for when the event occured (ISO string) */
55
- timestamp : string ;
56
- /** Type of event */
74
+ /**
75
+ * Type of event
76
+ */
57
77
eventType : string ;
58
- /** Resource that triggered the event */
59
- resource : Resource ;
60
- /** Key-value pairs that represent the values of wildcards in a database reference
61
- * Cannot be accessed while inside the handler namespace.
78
+ /**
79
+ * Key-value pairs that represent the values of wildcards in a database
80
+ * reference. Cannot be accessed while inside the handler namespace.
62
81
*/
63
82
params : { [ option : string ] : any } ;
64
- /** Type of authentication for the triggering action, valid value are: 'ADMIN', 'USER',
65
- * 'UNAUTHENTICATED'. Only available for database functions.
83
+ /**
84
+ * Resource that triggered the event
66
85
*/
67
- authType ?: 'ADMIN' | 'USER' | 'UNAUTHENTICATED' ;
68
- /** Firebase auth variable for the user whose action triggered the function. Field will be
69
- * null for unauthenticated users, and will not exist for admin users. Only available
70
- * for database functions.
86
+ resource : Resource ;
87
+ /**
88
+ * Timestamp for when the event ocurred (ISO 8601 string)
71
89
*/
72
- auth ?: {
73
- uid : string ;
74
- token : object ;
75
- } ;
90
+ timestamp : string ;
76
91
}
77
92
78
- /** Change describes a change of state - "before" represents the state prior
79
- * to the event, "after" represents the state after the event.
93
+ /**
94
+ * Change describes a change of state - "before" represents the state prior to
95
+ * the event, "after" represents the state after the event.
80
96
*/
81
97
export class Change < T > {
82
98
constructor ( public before : T , public after : T ) { }
83
99
}
84
100
85
- /** ChangeJson is the JSON format used to construct a Change object. */
101
+ /**
102
+ * ChangeJson is the JSON format used to construct a Change object.
103
+ */
86
104
export interface ChangeJson {
87
- /** Key-value pairs representing state of data before the change.
88
- * If `fieldMask` is set, then only fields that changed are present in `before` .
105
+ /**
106
+ * Key-value pairs representing state of data after the change .
89
107
*/
90
- before ?: any ;
91
- /** Key-value pairs representing state of data after the change. */
92
108
after ?: any ;
93
- /** Comma-separated string that represents names of field that changed. */
109
+ /**
110
+ * Key-value pairs representing state of data before the change. If
111
+ * `fieldMask` is set, then only fields that changed are present in `before`.
112
+ */
113
+ before ?: any ;
114
+ /**
115
+ * Comma-separated string that represents names of field that changed.
116
+ */
94
117
fieldMask ?: string ;
95
118
}
96
119
@@ -99,13 +122,17 @@ export namespace Change {
99
122
return x as T ;
100
123
}
101
124
102
- /** Factory method for creating a Change from a `before` object and an `after` object. */
125
+ /**
126
+ * Factory method for creating a Change from a `before` object and an `after`
127
+ * object.
128
+ */
103
129
export function fromObjects < T > ( before : T , after : T ) {
104
130
return new Change ( before , after ) ;
105
131
}
106
132
107
- /** Factory method for creating a Change from a JSON and an optional customizer function to be
108
- * applied to both the `before` and the `after` fields.
133
+ /**
134
+ * Factory method for creating a Change from a JSON and an optional customizer
135
+ * function to be applied to both the `before` and the `after` fields.
109
136
*/
110
137
export function fromJSON < T > (
111
138
json : ChangeJson ,
@@ -121,7 +148,9 @@ export namespace Change {
121
148
) ;
122
149
}
123
150
124
- /** @internal */
151
+ /**
152
+ * @internal
153
+ */
125
154
export function applyFieldMask (
126
155
sparseBefore : any ,
127
156
after : any ,
@@ -141,8 +170,10 @@ export namespace Change {
141
170
}
142
171
}
143
172
144
- /** Resource is a standard format for defining a resource (google.rpc.context.AttributeContext.Resource).
145
- * In Cloud Functions, it is the resource that triggered the function - such as a storage bucket.
173
+ /**
174
+ * Resource is a standard format for defining a resource
175
+ * (google.rpc.context.AttributeContext.Resource). In Cloud Functions, it is the
176
+ * resource that triggered the function - such as a storage bucket.
146
177
*/
147
178
export interface Resource {
148
179
service : string ;
@@ -151,83 +182,96 @@ export interface Resource {
151
182
labels ?: { [ tag : string ] : string } ;
152
183
}
153
184
154
- /** TriggerAnnotated is used internally by the firebase CLI to understand what type of Cloud Function to deploy. */
185
+ /**
186
+ * TriggerAnnotated is used internally by the firebase CLI to understand what
187
+ * type of Cloud Function to deploy.
188
+ */
155
189
export interface TriggerAnnotated {
156
190
__trigger : {
157
- httpsTrigger ?: { } ;
191
+ availableMemoryMb ?: number ;
158
192
eventTrigger ?: {
159
193
eventType : string ;
160
194
resource : string ;
161
195
service : string ;
162
196
} ;
197
+ httpsTrigger ?: { } ;
163
198
labels ?: { [ key : string ] : string } ;
164
199
regions ?: string [ ] ;
165
- timeout ?: string ;
166
- availableMemoryMb ?: number ;
167
200
schedule ?: Schedule ;
201
+ timeout ?: string ;
168
202
} ;
169
203
}
170
204
171
- /** A Runnable has a `run` method which directly invokes the user-defined function - useful for unit testing. */
205
+ /**
206
+ * A Runnable has a `run` method which directly invokes the user-defined
207
+ * function - useful for unit testing.
208
+ */
172
209
export interface Runnable < T > {
173
210
run : ( data : T , context : any ) => PromiseLike < any > | any ;
174
211
}
175
212
176
213
/**
177
- * An HttpsFunction is both an object that exports its trigger definitions at __trigger and
178
- * can be called as a function that takes an express.js Request and Response object.
214
+ * An HttpsFunction is both an object that exports its trigger definitions at
215
+ * __trigger and can be called as a function that takes an express.js Request
216
+ * and Response object.
179
217
*/
180
218
export type HttpsFunction = TriggerAnnotated &
181
219
( ( req : Request , resp : Response ) => void ) ;
182
220
183
221
/**
184
- * A CloudFunction is both an object that exports its trigger definitions at __trigger and
185
- * can be called as a function using the raw JS API for Google Cloud Functions.
222
+ * A CloudFunction is both an object that exports its trigger definitions at
223
+ * __trigger and can be called as a function using the raw JS API for Google
224
+ * Cloud Functions.
186
225
*/
187
226
export type CloudFunction < T > = Runnable < T > &
188
227
TriggerAnnotated &
189
228
( ( input : any , context ?: any ) => PromiseLike < any > | any ) ;
190
229
191
- /** @internal */
230
+ /**
231
+ * @internal
232
+ */
192
233
export interface MakeCloudFunctionArgs < EventData > {
193
- // TODO should remove `provider` and require a fully qualified `eventType`
194
- // once all providers have migrated to new format.
195
- provider : string ;
196
- eventType : string ;
197
- triggerResource : ( ) => string ;
198
- service : string ;
234
+ after ?: ( raw : Event ) => void ;
235
+ before ?: ( raw : Event ) => void ;
236
+ contextOnlyHandler ?: ( context : EventContext ) => PromiseLike < any > | any ;
199
237
dataConstructor ?: ( raw : Event ) => EventData ;
238
+ eventType : string ;
200
239
handler ?: ( data : EventData , context : EventContext ) => PromiseLike < any > | any ;
201
- contextOnlyHandler ?: ( context : EventContext ) => PromiseLike < any > | any ;
202
- before ?: ( raw : Event ) => void ;
203
- after ?: ( raw : Event ) => void ;
240
+ labels ?: { [ key : string ] : any } ;
204
241
legacyEventType ?: string ;
205
242
options ?: { [ key : string ] : any } ;
206
- labels ?: { [ key : string ] : any } ;
243
+ /*
244
+ * TODO: should remove `provider` and require a fully qualified `eventType`
245
+ * once all providers have migrated to new format.
246
+ */
247
+ provider : string ;
248
+ service : string ;
249
+ triggerResource : ( ) => string ;
207
250
}
208
251
209
- /** @internal */
252
+ /**
253
+ * @internal
254
+ */
210
255
export function makeCloudFunction < EventData > ( {
211
- provider,
212
- eventType,
213
- triggerResource,
214
- service,
256
+ after = ( ) => { } ,
257
+ before = ( ) => { } ,
258
+ contextOnlyHandler,
215
259
dataConstructor = ( raw : Event ) => raw . data ,
260
+ eventType,
216
261
handler,
217
- contextOnlyHandler,
218
- before = ( ) => {
219
- return ;
220
- } ,
221
- after = ( ) => {
222
- return ;
223
- } ,
262
+ labels = { } ,
224
263
legacyEventType,
225
264
options = { } ,
226
- labels = { } ,
265
+ provider,
266
+ service,
267
+ triggerResource,
227
268
} : MakeCloudFunctionArgs < EventData > ) : CloudFunction < EventData > {
228
269
const cloudFunction : any = ( data : any , context : any ) => {
229
270
if ( legacyEventType && context . eventType === legacyEventType ) {
230
- // v1beta1 event flow has different format for context, transform them to new format.
271
+ /*
272
+ * v1beta1 event flow has different format for context, transform them to
273
+ * new format.
274
+ */
231
275
context . eventType = provider + '.' + eventType ;
232
276
context . resource = {
233
277
service,
0 commit comments