Skip to content

Commit 65e66a2

Browse files
authored
Converting alert type and app id to camel case in the CloudEvent (#1236)
* converting to camel case * add fix for generic alerts
1 parent c18e832 commit 65e66a2

File tree

6 files changed

+89
-10
lines changed

6 files changed

+89
-10
lines changed

spec/v2/providers/alerts/alerts.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import { CloudEvent } from '../../../../src/v2';
23
import * as options from '../../../../src/v2/options';
34
import * as alerts from '../../../../src/v2/providers/alerts';
45
import { FULL_ENDPOINT, FULL_OPTIONS } from '../fixtures';
@@ -174,4 +175,39 @@ describe('alerts', () => {
174175
expect(appId).to.be.equal(myOpts.appId);
175176
});
176177
});
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+
});
177213
});

src/v2/providers/alerts/alerts.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export function onAlertPublished<T extends { ['@type']: string } = any>(
207207
const [opts, alertType, appId] = getOptsAndAlertTypeAndApp(alertTypeOrOpts);
208208

209209
const func = (raw: CloudEvent<unknown>) => {
210-
return handler(raw as AlertEvent<T>);
210+
return handler(convertAlertAndApp(raw) as AlertEvent<T>);
211211
};
212212

213213
func.run = handler;
@@ -271,3 +271,24 @@ export function getOptsAndAlertTypeAndApp(
271271
}
272272
return [opts, alertType, appId];
273273
}
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+
}

src/v2/providers/alerts/appDistribution.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
import { CloudEvent, CloudFunction } from '../../core';
2929
import * as options from '../../options';
3030
import { Expression } from '../../params';
31-
import { FirebaseAlertData, getEndpointAnnotation } from './alerts';
31+
import {
32+
convertAlertAndApp,
33+
FirebaseAlertData,
34+
getEndpointAnnotation,
35+
} from './alerts';
3236

3337
/**
3438
* The internal payload object for adding a new tester device to app distribution.
@@ -253,7 +257,9 @@ export function onNewTesterIosDevicePublished(
253257
const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);
254258

255259
const func = (raw: CloudEvent<unknown>) => {
256-
return handler(raw as AppDistributionEvent<NewTesterDevicePayload>);
260+
return handler(
261+
convertAlertAndApp(raw) as AppDistributionEvent<NewTesterDevicePayload>
262+
);
257263
};
258264

259265
func.run = handler;
@@ -326,7 +332,9 @@ export function onInAppFeedbackPublished(
326332
const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);
327333

328334
const func = (raw: CloudEvent<unknown>) => {
329-
return handler(raw as AppDistributionEvent<InAppFeedbackPayload>);
335+
return handler(
336+
convertAlertAndApp(raw) as AppDistributionEvent<InAppFeedbackPayload>
337+
);
330338
};
331339

332340
func.run = handler;

src/v2/providers/alerts/billing.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
* @packageDocumentation
2626
*/
2727

28-
import { FirebaseAlertData, getEndpointAnnotation } from '.';
2928
import { CloudEvent, CloudFunction } from '../../core';
3029
import * as options from '../../options';
30+
import {
31+
convertAlertAndApp,
32+
FirebaseAlertData,
33+
getEndpointAnnotation,
34+
} from './alerts';
3135

3236
/**
3337
* The internal payload object for billing plan updates.
@@ -167,7 +171,7 @@ export function onOperation<T>(
167171
}
168172

169173
const func = (raw: CloudEvent<unknown>) => {
170-
return handler(raw as BillingEvent<T>);
174+
return handler(convertAlertAndApp(raw) as BillingEvent<T>);
171175
};
172176

173177
func.run = handler;

src/v2/providers/alerts/crashlytics.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
* @packageDocumentation
2626
*/
2727

28-
import { FirebaseAlertData, getEndpointAnnotation } from '.';
2928
import { CloudEvent, CloudFunction } from '../../core';
3029
import * as options from '../../options';
3130
import { Expression } from '../../params';
31+
import {
32+
convertAlertAndApp,
33+
FirebaseAlertData,
34+
getEndpointAnnotation,
35+
} from './alerts';
3236

3337
/** Generic Crashlytics issue interface */
3438
export interface Issue {
@@ -631,7 +635,7 @@ export function onOperation<T>(
631635
);
632636

633637
const func = (raw: CloudEvent<unknown>) => {
634-
return handler(raw as CrashlyticsEvent<T>);
638+
return handler(convertAlertAndApp(raw) as CrashlyticsEvent<T>);
635639
};
636640

637641
func.run = handler;

src/v2/providers/alerts/performance.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
* @packageDocumentation
2626
*/
2727

28-
import { FirebaseAlertData, getEndpointAnnotation } from '.';
2928
import { CloudEvent, CloudFunction } from '../../core';
3029
import { EventHandlerOptions } from '../../options';
30+
import {
31+
convertAlertAndApp,
32+
FirebaseAlertData,
33+
getEndpointAnnotation,
34+
} from './alerts';
3135

3236
/**
3337
* The internal payload object for a performance threshold alert.
@@ -142,7 +146,9 @@ export function onThresholdAlertPublished(
142146
const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);
143147

144148
const func = (raw: CloudEvent<unknown>) => {
145-
const event = raw as PerformanceEvent<ThresholdAlertPayload>;
149+
const event = convertAlertAndApp(raw) as PerformanceEvent<
150+
ThresholdAlertPayload
151+
>;
146152
const convertedPayload = convertPayload(event.data.payload);
147153
event.data.payload = convertedPayload;
148154
return handler(event);

0 commit comments

Comments
 (0)