Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting alert type and app id to camel case in the CloudEvent #1236

Merged
merged 2 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions 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';
Expand Down Expand Up @@ -174,4 +175,39 @@ describe('alerts', () => {
expect(appId).to.be.equal(myOpts.appId);
});
});

describe('convertAlertAndApp', () => {
const event: CloudEvent<string> = {
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',
});
});
});
});
23 changes: 22 additions & 1 deletion src/v2/providers/alerts/alerts.ts
Expand Up @@ -207,7 +207,7 @@ export function onAlertPublished<T extends { ['@type']: string } = any>(
const [opts, alertType, appId] = getOptsAndAlertTypeAndApp(alertTypeOrOpts);

const func = (raw: CloudEvent<unknown>) => {
return handler(raw as AlertEvent<T>);
return handler(convertAlertAndApp(raw) as AlertEvent<T>);
};

func.run = handler;
Expand Down Expand Up @@ -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<unknown>
): CloudEvent<unknown> {
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;
}
14 changes: 11 additions & 3 deletions src/v2/providers/alerts/appDistribution.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -253,7 +257,9 @@ export function onNewTesterIosDevicePublished(
const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);

const func = (raw: CloudEvent<unknown>) => {
return handler(raw as AppDistributionEvent<NewTesterDevicePayload>);
return handler(
convertAlertAndApp(raw) as AppDistributionEvent<NewTesterDevicePayload>
);
};

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

const func = (raw: CloudEvent<unknown>) => {
return handler(raw as AppDistributionEvent<InAppFeedbackPayload>);
return handler(
convertAlertAndApp(raw) as AppDistributionEvent<InAppFeedbackPayload>
);
};

func.run = handler;
Expand Down
8 changes: 6 additions & 2 deletions src/v2/providers/alerts/billing.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -167,7 +171,7 @@ export function onOperation<T>(
}

const func = (raw: CloudEvent<unknown>) => {
return handler(raw as BillingEvent<T>);
return handler(convertAlertAndApp(raw) as BillingEvent<T>);
};

func.run = handler;
Expand Down
8 changes: 6 additions & 2 deletions src/v2/providers/alerts/crashlytics.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -631,7 +635,7 @@ export function onOperation<T>(
);

const func = (raw: CloudEvent<unknown>) => {
return handler(raw as CrashlyticsEvent<T>);
return handler(convertAlertAndApp(raw) as CrashlyticsEvent<T>);
};

func.run = handler;
Expand Down
10 changes: 8 additions & 2 deletions src/v2/providers/alerts/performance.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -142,7 +146,9 @@ export function onThresholdAlertPublished(
const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);

const func = (raw: CloudEvent<unknown>) => {
const event = raw as PerformanceEvent<ThresholdAlertPayload>;
const event = convertAlertAndApp(raw) as PerformanceEvent<
ThresholdAlertPayload
>;
const convertedPayload = convertPayload(event.data.payload);
event.data.payload = convertedPayload;
return handler(event);
Expand Down