Skip to content

Commit 2102689

Browse files
committed
address pr comments
1 parent d2c4a56 commit 2102689

File tree

4 files changed

+73
-113
lines changed

4 files changed

+73
-113
lines changed

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

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import { expect } from 'chai';
2-
import * as sinon from 'sinon';
3-
import * as config from '../../../../src/config';
42
import { CloudEvent, CloudFunction } from '../../../../src/v2/core';
53
import * as options from '../../../../src/v2/options';
64
import * as alerts from '../../../../src/v2/providers/alerts';
7-
import {
8-
BASIC_ENDPOINT,
9-
BASIC_OPTIONS,
10-
FULL_ENDPOINT,
11-
FULL_OPTIONS,
12-
} from '../helpers';
5+
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';
136

147
const ALERT_TYPE = 'new-alert-type';
158
const APPID = '123456789';
169

1710
function getMockFunction(): CloudFunction<alerts.FirebaseAlertData<String>> {
1811
const func = (raw: CloudEvent<unknown>) => 42;
1912
func.run = (event: CloudEvent<alerts.FirebaseAlertData<String>>) => 42;
20-
func.__trigger = 'silence the transpiler';
2113
func.__endpoint = {};
2214
return func;
2315
}
@@ -43,15 +35,15 @@ describe('alerts', () => {
4335
it('should create the function with opts', () => {
4436
const result = alerts.onAlertPublished(
4537
{
46-
...BASIC_OPTIONS,
38+
...FULL_OPTIONS,
4739
alertType: ALERT_TYPE,
4840
appId: APPID,
4941
},
5042
() => 42
5143
);
5244

5345
expect(result.__endpoint).to.deep.equal({
54-
...BASIC_ENDPOINT,
46+
...FULL_ENDPOINT,
5547
eventTrigger: {
5648
eventType: alerts.eventType,
5749
eventFilters: {
@@ -72,24 +64,20 @@ describe('alerts', () => {
7264
});
7365
});
7466

75-
describe('defineEndpoint', () => {
76-
let configStub: sinon.SinonStub;
77-
67+
describe('getEndpointAnnotation', () => {
7868
beforeEach(() => {
7969
process.env.GCLOUD_PROJECT = 'aProject';
80-
configStub = sinon.stub(config, 'firebaseConfig');
8170
});
8271

8372
afterEach(() => {
8473
options.setGlobalOptions({});
8574
delete process.env.GCLOUD_PROJECT;
86-
configStub.restore();
8775
});
8876

8977
it('should define the endpoint without appId and opts', () => {
9078
const func = getMockFunction();
9179

92-
alerts.defineEndpoint(func, {}, ALERT_TYPE);
80+
func.__endpoint = alerts.getEndpointAnnotation({}, ALERT_TYPE);
9381

9482
expect(func.__endpoint).to.deep.equal({
9583
platform: 'gcfv2',
@@ -104,35 +92,20 @@ describe('alerts', () => {
10492
});
10593
});
10694

107-
it('should define the endpoint without appId, with opts', () => {
108-
const func = getMockFunction();
109-
110-
alerts.defineEndpoint(func, { ...BASIC_OPTIONS }, ALERT_TYPE);
111-
112-
expect(func.__endpoint).to.deep.equal({
113-
...BASIC_ENDPOINT,
114-
eventTrigger: {
115-
eventType: alerts.eventType,
116-
eventFilters: {
117-
alertType: ALERT_TYPE,
118-
},
119-
retry: false,
120-
},
121-
});
122-
});
123-
124-
it('should define the endpoint with appId', () => {
95+
it('should define a complex endpoint without appId', () => {
12596
const func = getMockFunction();
12697

127-
alerts.defineEndpoint(func, { ...BASIC_OPTIONS }, ALERT_TYPE, APPID);
98+
func.__endpoint = alerts.getEndpointAnnotation(
99+
{ ...FULL_OPTIONS },
100+
ALERT_TYPE
101+
);
128102

129103
expect(func.__endpoint).to.deep.equal({
130-
...BASIC_ENDPOINT,
104+
...FULL_ENDPOINT,
131105
eventTrigger: {
132106
eventType: alerts.eventType,
133107
eventFilters: {
134108
alertType: ALERT_TYPE,
135-
appId: APPID,
136109
},
137110
retry: false,
138111
},
@@ -142,20 +115,19 @@ describe('alerts', () => {
142115
it('should define a complex endpoint', () => {
143116
const func = getMockFunction();
144117

145-
alerts.defineEndpoint(
146-
func,
118+
func.__endpoint = alerts.getEndpointAnnotation(
147119
{ ...FULL_OPTIONS },
148-
'new-alert-type',
149-
'123456789'
120+
ALERT_TYPE,
121+
APPID
150122
);
151123

152124
expect(func.__endpoint).to.deep.equal({
153125
...FULL_ENDPOINT,
154126
eventTrigger: {
155127
eventType: alerts.eventType,
156128
eventFilters: {
157-
alertType: 'new-alert-type',
158-
appId: '123456789',
129+
alertType: ALERT_TYPE,
130+
appId: APPID,
159131
},
160132
retry: false,
161133
},
@@ -174,7 +146,11 @@ describe('alerts', () => {
174146
};
175147
const func = getMockFunction();
176148

177-
alerts.defineEndpoint(func, specificOpts, ALERT_TYPE, APPID);
149+
func.__endpoint = alerts.getEndpointAnnotation(
150+
specificOpts,
151+
ALERT_TYPE,
152+
APPID
153+
);
178154

179155
expect(func.__endpoint).to.deep.equal({
180156
platform: 'gcfv2',

spec/v2/providers/helpers.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import * as options from '../../../src/v2/options';
22

3-
export const BASIC_OPTIONS: options.EventHandlerOptions = {
4-
labels: {
5-
someKey: 'someValue',
6-
},
7-
region: 'us-east1',
8-
};
9-
10-
export const BASIC_ENDPOINT = {
11-
platform: 'gcfv2',
12-
region: ['us-east1'],
13-
labels: {
14-
someKey: 'someValue',
15-
},
16-
};
17-
183
export const FULL_OPTIONS: options.GlobalOptions = {
194
region: 'us-west1',
205
memory: '512MB',

src/v2/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface TriggerAnnotation {
5252
* A CloudEventBase is the base of a cross-platform format for encoding a serverless event.
5353
* More information can be found in https://github.com/cloudevents/spec
5454
*/
55-
export interface CloudEventBase<T> {
55+
interface CloudEventBase<T> {
5656
/** Version of the CloudEvents spec for this event. */
5757
readonly specversion: '1.0';
5858

src/v2/providers/alerts/alerts.ts

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@ import { ManifestEndpoint } from '../../../common/manifest';
22
import { CloudEvent, CloudFunction } from '../../core';
33
import * as options from '../../options';
44

5-
/** Data */
5+
/**
6+
* The data object that is emitted from Firebase Alerts inside the CloudEvent
7+
*/
68
export interface FirebaseAlertData<T = any> {
79
createTime: string;
810
endTime: string;
911
payload: T;
1012
}
1113

14+
interface WithAlertTypeAndApp {
15+
alertType: string;
16+
appId?: string;
17+
}
18+
/**
19+
* A custom CloudEvent for Firebase Alerts with custom extension attributes defined
20+
*/
21+
export type AlertEvent<T> = CloudEvent<
22+
FirebaseAlertData<T>,
23+
WithAlertTypeAndApp
24+
>;
25+
1226
/** @internal */
1327
export const eventType = 'firebase.firebasealerts.alerts.v1.published';
1428

@@ -25,24 +39,18 @@ export type AlertType =
2539
| 'appDistribution.newTesterIosDevice'
2640
| string;
2741

28-
/** Options */
42+
/**
43+
* Configuration for Firebase Alert functions
44+
*/
2945
export interface FirebaseAlertOptions extends options.EventHandlerOptions {
3046
alertType: AlertType;
3147
appId?: string;
3248
}
3349

34-
/** Cloud Event Type */
35-
interface WithAlertTypeAndApp {
36-
alertType: string;
37-
appId?: string;
38-
}
39-
export type AlertEvent<T> = CloudEvent<
40-
FirebaseAlertData<T>,
41-
WithAlertTypeAndApp
42-
>;
43-
44-
/** Handlers */
45-
/** Handle an alert published */
50+
/**
51+
* Declares a function that can handle Firebase Alerts from CloudEvents
52+
* @param alertTypeOrOpts the alert type or Firebase Alert function configuration
53+
*/
4654
export function onAlertPublished<T extends { ['@type']: string } = any>(
4755
alertTypeOrOpts: AlertType | FirebaseAlertOptions,
4856
handler: (event: AlertEvent<T>) => any | Promise<any>
@@ -56,57 +64,48 @@ export function onAlertPublished<T extends { ['@type']: string } = any>(
5664
};
5765

5866
func.run = handler;
59-
60-
// TypeScript doesn't recognize defineProperty as adding a property and complains
61-
// that __endpoint doesn't exist. We can either cast to any and lose all type safety
62-
// or we can just assign a meaningless value before calling defineProperty.
63-
func.__trigger = 'silence the transpiler';
64-
func.__endpoint = {} as ManifestEndpoint;
65-
defineEndpoint(func, opts, alertType, appId);
67+
func.__endpoint = getEndpointAnnotation(opts, alertType, appId);
6668

6769
return func;
6870
}
6971

7072
/**
7173
* @internal
72-
* Helper function for defining an endpoint for alert handling functions
74+
* Helper function for getting the endpoint annotation used in alert handling functions
7375
*/
74-
export function defineEndpoint(
75-
func: CloudFunction<FirebaseAlertData<any>>,
76+
export function getEndpointAnnotation(
7677
opts: options.EventHandlerOptions,
7778
alertType: string,
7879
appId?: string
79-
): void {
80-
Object.defineProperty(func, '__endpoint', {
81-
get: () => {
82-
const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
83-
const specificOpts = options.optionsToEndpoint(opts);
84-
85-
const endpoint: ManifestEndpoint = {
86-
platform: 'gcfv2',
87-
...baseOpts,
88-
...specificOpts,
89-
labels: {
90-
...baseOpts?.labels,
91-
...specificOpts?.labels,
92-
},
93-
eventTrigger: {
94-
eventType,
95-
eventFilters: {
96-
alertType,
97-
},
98-
retry: false,
99-
},
100-
};
101-
if (appId) {
102-
endpoint.eventTrigger.eventFilters.appId = appId;
103-
}
104-
return endpoint;
80+
): ManifestEndpoint {
81+
const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
82+
const specificOpts = options.optionsToEndpoint(opts);
83+
const endpoint: ManifestEndpoint = {
84+
platform: 'gcfv2',
85+
...baseOpts,
86+
...specificOpts,
87+
labels: {
88+
...baseOpts?.labels,
89+
...specificOpts?.labels,
90+
},
91+
eventTrigger: {
92+
eventType,
93+
eventFilters: {
94+
alertType,
95+
},
96+
retry: false,
10597
},
106-
});
98+
};
99+
if (appId) {
100+
endpoint.eventTrigger.eventFilters.appId = appId;
101+
}
102+
return endpoint;
107103
}
108104

109-
/** @internal */
105+
/**
106+
* @internal
107+
* Helper function to parse the function opts, alert type, and appId
108+
*/
110109
export function getOptsAndAlertTypeAndApp(
111110
alertTypeOrOpts: AlertType | FirebaseAlertOptions
112111
): [options.EventHandlerOptions, string, string | undefined] {

0 commit comments

Comments
 (0)