From 65e66a21388ffe08f923e194d28295090f9cf195 Mon Sep 17 00:00:00 2001 From: Cole Rogers Date: Mon, 19 Sep 2022 20:08:22 -0400 Subject: [PATCH] Converting alert type and app id to camel case in the CloudEvent (#1236) * converting to camel case * add fix for generic alerts --- spec/v2/providers/alerts/alerts.spec.ts | 36 ++++++++++++++++++++++ src/v2/providers/alerts/alerts.ts | 23 +++++++++++++- src/v2/providers/alerts/appDistribution.ts | 14 +++++++-- src/v2/providers/alerts/billing.ts | 8 +++-- src/v2/providers/alerts/crashlytics.ts | 8 +++-- src/v2/providers/alerts/performance.ts | 10 ++++-- 6 files changed, 89 insertions(+), 10 deletions(-) diff --git a/spec/v2/providers/alerts/alerts.spec.ts b/spec/v2/providers/alerts/alerts.spec.ts index 8c4741f43..592b2839e 100644 --- a/spec/v2/providers/alerts/alerts.spec.ts +++ b/spec/v2/providers/alerts/alerts.spec.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import { CloudEvent } from '../../../../src/v2'; import * as options from '../../../../src/v2/options'; import * as alerts from '../../../../src/v2/providers/alerts'; import { FULL_ENDPOINT, FULL_OPTIONS } from '../fixtures'; @@ -174,4 +175,39 @@ describe('alerts', () => { expect(appId).to.be.equal(myOpts.appId); }); }); + + describe('convertAlertAndApp', () => { + const event: CloudEvent = { + specversion: '1.0', + id: 'id', + source: 'source', + type: 'type', + time: 'now', + data: 'data', + }; + + it('should leave event unchanged if alerttype & appid are missing', () => { + const raw = { ...event }; + + const converted = alerts.convertAlertAndApp(raw); + + expect(raw).to.deep.eq(converted); + }); + + it('should convert alerttype & appid when present', () => { + const raw = { + ...event, + alerttype: 'my-alert', + appid: 'my-app', + }; + + const converted = alerts.convertAlertAndApp(raw); + + expect(converted).to.deep.eq({ + ...event, + alertType: 'my-alert', + appId: 'my-app', + }); + }); + }); }); diff --git a/src/v2/providers/alerts/alerts.ts b/src/v2/providers/alerts/alerts.ts index 545e9bbcf..c021c6ebd 100644 --- a/src/v2/providers/alerts/alerts.ts +++ b/src/v2/providers/alerts/alerts.ts @@ -207,7 +207,7 @@ export function onAlertPublished( const [opts, alertType, appId] = getOptsAndAlertTypeAndApp(alertTypeOrOpts); const func = (raw: CloudEvent) => { - return handler(raw as AlertEvent); + return handler(convertAlertAndApp(raw) as AlertEvent); }; func.run = handler; @@ -271,3 +271,24 @@ export function getOptsAndAlertTypeAndApp( } return [opts, alertType, appId]; } + +/** + * Helper function to covert alert type & app id in the CloudEvent to camel case. + * @internal + */ +export function convertAlertAndApp( + raw: CloudEvent +): CloudEvent { + const event = { ...raw }; + + if ('alerttype' in event) { + (event as any).alertType = (event as any).alerttype; + delete (event as any).alerttype; + } + if ('appid' in event) { + (event as any).appId = (event as any).appid; + delete (event as any).appid; + } + + return event; +} diff --git a/src/v2/providers/alerts/appDistribution.ts b/src/v2/providers/alerts/appDistribution.ts index 695bfc4df..3a9e7107c 100644 --- a/src/v2/providers/alerts/appDistribution.ts +++ b/src/v2/providers/alerts/appDistribution.ts @@ -28,7 +28,11 @@ import { CloudEvent, CloudFunction } from '../../core'; import * as options from '../../options'; import { Expression } from '../../params'; -import { FirebaseAlertData, getEndpointAnnotation } from './alerts'; +import { + convertAlertAndApp, + FirebaseAlertData, + getEndpointAnnotation, +} from './alerts'; /** * The internal payload object for adding a new tester device to app distribution. @@ -253,7 +257,9 @@ export function onNewTesterIosDevicePublished( const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler); const func = (raw: CloudEvent) => { - return handler(raw as AppDistributionEvent); + return handler( + convertAlertAndApp(raw) as AppDistributionEvent + ); }; func.run = handler; @@ -326,7 +332,9 @@ export function onInAppFeedbackPublished( const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler); const func = (raw: CloudEvent) => { - return handler(raw as AppDistributionEvent); + return handler( + convertAlertAndApp(raw) as AppDistributionEvent + ); }; func.run = handler; diff --git a/src/v2/providers/alerts/billing.ts b/src/v2/providers/alerts/billing.ts index 4d42af79b..955fd8791 100644 --- a/src/v2/providers/alerts/billing.ts +++ b/src/v2/providers/alerts/billing.ts @@ -25,9 +25,13 @@ * @packageDocumentation */ -import { FirebaseAlertData, getEndpointAnnotation } from '.'; import { CloudEvent, CloudFunction } from '../../core'; import * as options from '../../options'; +import { + convertAlertAndApp, + FirebaseAlertData, + getEndpointAnnotation, +} from './alerts'; /** * The internal payload object for billing plan updates. @@ -167,7 +171,7 @@ export function onOperation( } const func = (raw: CloudEvent) => { - return handler(raw as BillingEvent); + return handler(convertAlertAndApp(raw) as BillingEvent); }; func.run = handler; diff --git a/src/v2/providers/alerts/crashlytics.ts b/src/v2/providers/alerts/crashlytics.ts index af3d58bdc..1e6345b41 100644 --- a/src/v2/providers/alerts/crashlytics.ts +++ b/src/v2/providers/alerts/crashlytics.ts @@ -25,10 +25,14 @@ * @packageDocumentation */ -import { FirebaseAlertData, getEndpointAnnotation } from '.'; import { CloudEvent, CloudFunction } from '../../core'; import * as options from '../../options'; import { Expression } from '../../params'; +import { + convertAlertAndApp, + FirebaseAlertData, + getEndpointAnnotation, +} from './alerts'; /** Generic Crashlytics issue interface */ export interface Issue { @@ -631,7 +635,7 @@ export function onOperation( ); const func = (raw: CloudEvent) => { - return handler(raw as CrashlyticsEvent); + return handler(convertAlertAndApp(raw) as CrashlyticsEvent); }; func.run = handler; diff --git a/src/v2/providers/alerts/performance.ts b/src/v2/providers/alerts/performance.ts index 7fa82439f..bc4ab22c9 100644 --- a/src/v2/providers/alerts/performance.ts +++ b/src/v2/providers/alerts/performance.ts @@ -25,9 +25,13 @@ * @packageDocumentation */ -import { FirebaseAlertData, getEndpointAnnotation } from '.'; import { CloudEvent, CloudFunction } from '../../core'; import { EventHandlerOptions } from '../../options'; +import { + convertAlertAndApp, + FirebaseAlertData, + getEndpointAnnotation, +} from './alerts'; /** * The internal payload object for a performance threshold alert. @@ -142,7 +146,9 @@ export function onThresholdAlertPublished( const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler); const func = (raw: CloudEvent) => { - const event = raw as PerformanceEvent; + const event = convertAlertAndApp(raw) as PerformanceEvent< + ThresholdAlertPayload + >; const convertedPayload = convertPayload(event.data.payload); event.data.payload = convertedPayload; return handler(event);