File tree 6 files changed +89
-10
lines changed
6 files changed +89
-10
lines changed Original file line number Diff line number Diff line change 1
1
import { expect } from 'chai' ;
2
+ import { CloudEvent } from '../../../../src/v2' ;
2
3
import * as options from '../../../../src/v2/options' ;
3
4
import * as alerts from '../../../../src/v2/providers/alerts' ;
4
5
import { FULL_ENDPOINT , FULL_OPTIONS } from '../fixtures' ;
@@ -174,4 +175,39 @@ describe('alerts', () => {
174
175
expect ( appId ) . to . be . equal ( myOpts . appId ) ;
175
176
} ) ;
176
177
} ) ;
178
+
179
+ describe ( 'convertAlertAndApp' , ( ) => {
180
+ const event : CloudEvent < string > = {
181
+ specversion : '1.0' ,
182
+ id : 'id' ,
183
+ source : 'source' ,
184
+ type : 'type' ,
185
+ time : 'now' ,
186
+ data : 'data' ,
187
+ } ;
188
+
189
+ it ( 'should leave event unchanged if alerttype & appid are missing' , ( ) => {
190
+ const raw = { ...event } ;
191
+
192
+ const converted = alerts . convertAlertAndApp ( raw ) ;
193
+
194
+ expect ( raw ) . to . deep . eq ( converted ) ;
195
+ } ) ;
196
+
197
+ it ( 'should convert alerttype & appid when present' , ( ) => {
198
+ const raw = {
199
+ ...event ,
200
+ alerttype : 'my-alert' ,
201
+ appid : 'my-app' ,
202
+ } ;
203
+
204
+ const converted = alerts . convertAlertAndApp ( raw ) ;
205
+
206
+ expect ( converted ) . to . deep . eq ( {
207
+ ...event ,
208
+ alertType : 'my-alert' ,
209
+ appId : 'my-app' ,
210
+ } ) ;
211
+ } ) ;
212
+ } ) ;
177
213
} ) ;
Original file line number Diff line number Diff line change @@ -207,7 +207,7 @@ export function onAlertPublished<T extends { ['@type']: string } = any>(
207
207
const [ opts , alertType , appId ] = getOptsAndAlertTypeAndApp ( alertTypeOrOpts ) ;
208
208
209
209
const func = ( raw : CloudEvent < unknown > ) => {
210
- return handler ( raw as AlertEvent < T > ) ;
210
+ return handler ( convertAlertAndApp ( raw ) as AlertEvent < T > ) ;
211
211
} ;
212
212
213
213
func . run = handler ;
@@ -271,3 +271,24 @@ export function getOptsAndAlertTypeAndApp(
271
271
}
272
272
return [ opts , alertType , appId ] ;
273
273
}
274
+
275
+ /**
276
+ * Helper function to covert alert type & app id in the CloudEvent to camel case.
277
+ * @internal
278
+ */
279
+ export function convertAlertAndApp (
280
+ raw : CloudEvent < unknown >
281
+ ) : CloudEvent < unknown > {
282
+ const event = { ...raw } ;
283
+
284
+ if ( 'alerttype' in event ) {
285
+ ( event as any ) . alertType = ( event as any ) . alerttype ;
286
+ delete ( event as any ) . alerttype ;
287
+ }
288
+ if ( 'appid' in event ) {
289
+ ( event as any ) . appId = ( event as any ) . appid ;
290
+ delete ( event as any ) . appid ;
291
+ }
292
+
293
+ return event ;
294
+ }
Original file line number Diff line number Diff line change 28
28
import { CloudEvent , CloudFunction } from '../../core' ;
29
29
import * as options from '../../options' ;
30
30
import { Expression } from '../../params' ;
31
- import { FirebaseAlertData , getEndpointAnnotation } from './alerts' ;
31
+ import {
32
+ convertAlertAndApp ,
33
+ FirebaseAlertData ,
34
+ getEndpointAnnotation ,
35
+ } from './alerts' ;
32
36
33
37
/**
34
38
* The internal payload object for adding a new tester device to app distribution.
@@ -253,7 +257,9 @@ export function onNewTesterIosDevicePublished(
253
257
const [ opts , appId ] = getOptsAndApp ( appIdOrOptsOrHandler ) ;
254
258
255
259
const func = ( raw : CloudEvent < unknown > ) => {
256
- return handler ( raw as AppDistributionEvent < NewTesterDevicePayload > ) ;
260
+ return handler (
261
+ convertAlertAndApp ( raw ) as AppDistributionEvent < NewTesterDevicePayload >
262
+ ) ;
257
263
} ;
258
264
259
265
func . run = handler ;
@@ -326,7 +332,9 @@ export function onInAppFeedbackPublished(
326
332
const [ opts , appId ] = getOptsAndApp ( appIdOrOptsOrHandler ) ;
327
333
328
334
const func = ( raw : CloudEvent < unknown > ) => {
329
- return handler ( raw as AppDistributionEvent < InAppFeedbackPayload > ) ;
335
+ return handler (
336
+ convertAlertAndApp ( raw ) as AppDistributionEvent < InAppFeedbackPayload >
337
+ ) ;
330
338
} ;
331
339
332
340
func . run = handler ;
Original file line number Diff line number Diff line change 25
25
* @packageDocumentation
26
26
*/
27
27
28
- import { FirebaseAlertData , getEndpointAnnotation } from '.' ;
29
28
import { CloudEvent , CloudFunction } from '../../core' ;
30
29
import * as options from '../../options' ;
30
+ import {
31
+ convertAlertAndApp ,
32
+ FirebaseAlertData ,
33
+ getEndpointAnnotation ,
34
+ } from './alerts' ;
31
35
32
36
/**
33
37
* The internal payload object for billing plan updates.
@@ -167,7 +171,7 @@ export function onOperation<T>(
167
171
}
168
172
169
173
const func = ( raw : CloudEvent < unknown > ) => {
170
- return handler ( raw as BillingEvent < T > ) ;
174
+ return handler ( convertAlertAndApp ( raw ) as BillingEvent < T > ) ;
171
175
} ;
172
176
173
177
func . run = handler ;
Original file line number Diff line number Diff line change 25
25
* @packageDocumentation
26
26
*/
27
27
28
- import { FirebaseAlertData , getEndpointAnnotation } from '.' ;
29
28
import { CloudEvent , CloudFunction } from '../../core' ;
30
29
import * as options from '../../options' ;
31
30
import { Expression } from '../../params' ;
31
+ import {
32
+ convertAlertAndApp ,
33
+ FirebaseAlertData ,
34
+ getEndpointAnnotation ,
35
+ } from './alerts' ;
32
36
33
37
/** Generic Crashlytics issue interface */
34
38
export interface Issue {
@@ -631,7 +635,7 @@ export function onOperation<T>(
631
635
) ;
632
636
633
637
const func = ( raw : CloudEvent < unknown > ) => {
634
- return handler ( raw as CrashlyticsEvent < T > ) ;
638
+ return handler ( convertAlertAndApp ( raw ) as CrashlyticsEvent < T > ) ;
635
639
} ;
636
640
637
641
func . run = handler ;
Original file line number Diff line number Diff line change 25
25
* @packageDocumentation
26
26
*/
27
27
28
- import { FirebaseAlertData , getEndpointAnnotation } from '.' ;
29
28
import { CloudEvent , CloudFunction } from '../../core' ;
30
29
import { EventHandlerOptions } from '../../options' ;
30
+ import {
31
+ convertAlertAndApp ,
32
+ FirebaseAlertData ,
33
+ getEndpointAnnotation ,
34
+ } from './alerts' ;
31
35
32
36
/**
33
37
* The internal payload object for a performance threshold alert.
@@ -142,7 +146,9 @@ export function onThresholdAlertPublished(
142
146
const [ opts , appId ] = getOptsAndApp ( appIdOrOptsOrHandler ) ;
143
147
144
148
const func = ( raw : CloudEvent < unknown > ) => {
145
- const event = raw as PerformanceEvent < ThresholdAlertPayload > ;
149
+ const event = convertAlertAndApp ( raw ) as PerformanceEvent <
150
+ ThresholdAlertPayload
151
+ > ;
146
152
const convertedPayload = convertPayload ( event . data . payload ) ;
147
153
event . data . payload = convertedPayload ;
148
154
return handler ( event ) ;
You can’t perform that action at this time.
0 commit comments