Skip to content

Commit 8e21f77

Browse files
authored
Generic interface for provider enhancement (#1020) (#1024)
* breaking out general interface * cleaning up exports * removing comments & references to specific interfaces * format * jsdoc comments * address pr comments * added param comment * addressing comments * Specific interface for provider enhancement (1/3) (#1021) * adding in app distro changes * removing comments & addings package refs * jsdoc comments * fix comments * adding periods to doc strings * fix wording * adding import/export statement * Specific interface for provider enhancement (2/3) (#1022) * adding in billing changes * removing comments & adding package refs * jsdoc comments * addressing pr comments * change handler doc string * changing to BillingEventHandler type * remove BillingEventHandler type * Specific interface for provider enhancement (3/3) (#1023) * adding in crashlytics changes * comments & adding package refs * address comments and make doc strings better * add opts.retry to event trigger
1 parent 7645a69 commit 8e21f77

18 files changed

+1839
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
- Parallelizes network calls that occur when validating authorization for onCall handlers.
22
- Adds new regions to V2 API
33
- Fixes bug where the emulator crashed when given app without an `options` property.
4+
- Adds new alerting providers

package.json

+17-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454
"./v2/https": "./lib/v2/providers/https.js",
5555
"./v2/params": "./lib/v2/params/index.js",
5656
"./v2/pubsub": "./lib/v2/providers/pubsub.js",
57-
"./v2/storage": "./lib/v2/providers/storage.js"
57+
"./v2/storage": "./lib/v2/providers/storage.js",
58+
"./v2/alerts": "./lib/v2/providers/alerts/index.js",
59+
"./v2/alerts/appDistribution": "./lib/v2/providers/alerts/appDistribution.js",
60+
"./v2/alerts/billing": "./lib/v2/providers/alerts/billing.js",
61+
"./v2/alerts/crashlytics": "./lib/v2/providers/alerts/crashlytics.js"
5862
},
5963
"typesVersions": {
6064
"*": {
@@ -114,6 +118,18 @@
114118
],
115119
"v2/storage": [
116120
"lib/v2/providers/storage"
121+
],
122+
"v2/alerts": [
123+
"lib/v2/providers/alerts"
124+
],
125+
"v2/alerts/appDistribution": [
126+
"lib/v2/providers/alerts/appDistribution"
127+
],
128+
"v2/alerts/billing": [
129+
"lib/v2/providers/alerts/billing"
130+
],
131+
"v2/alerts/crashlytics": [
132+
"lib/v2/providers/alerts/crashlytics"
117133
]
118134
}
119135
},
+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import { expect } from 'chai';
2+
import * as options from '../../../../src/v2/options';
3+
import * as alerts from '../../../../src/v2/providers/alerts';
4+
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';
5+
6+
const ALERT_TYPE = 'new-alert-type';
7+
const APPID = '123456789';
8+
9+
describe('alerts', () => {
10+
describe('onAlertPublished', () => {
11+
it('should create the function without opts', () => {
12+
const result = alerts.onAlertPublished(ALERT_TYPE, () => 42);
13+
14+
expect(result.__endpoint).to.deep.equal({
15+
platform: 'gcfv2',
16+
labels: {},
17+
eventTrigger: {
18+
eventType: alerts.eventType,
19+
eventFilters: {
20+
alertType: ALERT_TYPE,
21+
},
22+
retry: false,
23+
},
24+
});
25+
});
26+
27+
it('should create the function with opts', () => {
28+
const result = alerts.onAlertPublished(
29+
{
30+
...FULL_OPTIONS,
31+
alertType: ALERT_TYPE,
32+
appId: APPID,
33+
},
34+
() => 42
35+
);
36+
37+
expect(result.__endpoint).to.deep.equal({
38+
...FULL_ENDPOINT,
39+
eventTrigger: {
40+
eventType: alerts.eventType,
41+
eventFilters: {
42+
alertType: ALERT_TYPE,
43+
appId: APPID,
44+
},
45+
retry: false,
46+
},
47+
});
48+
});
49+
50+
it('should have a .run method', () => {
51+
const func = alerts.onAlertPublished(ALERT_TYPE, (event) => event);
52+
53+
const res = func.run('input' as any);
54+
55+
expect(res).to.equal('input');
56+
});
57+
});
58+
59+
describe('getEndpointAnnotation', () => {
60+
beforeEach(() => {
61+
process.env.GCLOUD_PROJECT = 'aProject';
62+
});
63+
64+
afterEach(() => {
65+
options.setGlobalOptions({});
66+
delete process.env.GCLOUD_PROJECT;
67+
});
68+
69+
it('should define the endpoint without appId and opts', () => {
70+
expect(alerts.getEndpointAnnotation({}, ALERT_TYPE)).to.deep.equal({
71+
platform: 'gcfv2',
72+
labels: {},
73+
eventTrigger: {
74+
eventType: alerts.eventType,
75+
eventFilters: {
76+
alertType: ALERT_TYPE,
77+
},
78+
retry: false,
79+
},
80+
});
81+
});
82+
83+
it('should define a complex endpoint without appId', () => {
84+
expect(
85+
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE)
86+
).to.deep.equal({
87+
...FULL_ENDPOINT,
88+
eventTrigger: {
89+
eventType: alerts.eventType,
90+
eventFilters: {
91+
alertType: ALERT_TYPE,
92+
},
93+
retry: false,
94+
},
95+
});
96+
});
97+
98+
it('should define a complex endpoint', () => {
99+
expect(
100+
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE, APPID)
101+
).to.deep.equal({
102+
...FULL_ENDPOINT,
103+
eventTrigger: {
104+
eventType: alerts.eventType,
105+
eventFilters: {
106+
alertType: ALERT_TYPE,
107+
appId: APPID,
108+
},
109+
retry: false,
110+
},
111+
});
112+
});
113+
114+
it('should merge global & specific opts', () => {
115+
options.setGlobalOptions({
116+
concurrency: 20,
117+
region: 'europe-west1',
118+
minInstances: 1,
119+
});
120+
const specificOpts = {
121+
region: 'us-west1',
122+
minInstances: 3,
123+
};
124+
125+
expect(
126+
alerts.getEndpointAnnotation(specificOpts, ALERT_TYPE, APPID)
127+
).to.deep.equal({
128+
platform: 'gcfv2',
129+
labels: {},
130+
concurrency: 20,
131+
region: ['us-west1'],
132+
minInstances: 3,
133+
eventTrigger: {
134+
eventType: alerts.eventType,
135+
eventFilters: {
136+
alertType: ALERT_TYPE,
137+
appId: APPID,
138+
},
139+
retry: false,
140+
},
141+
});
142+
});
143+
});
144+
145+
describe('getOptsAndAlertTypeAndApp', () => {
146+
it('should parse a string', () => {
147+
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(
148+
ALERT_TYPE
149+
);
150+
151+
expect(opts).to.deep.equal({});
152+
expect(alertType).to.equal(ALERT_TYPE);
153+
expect(appId).to.be.undefined;
154+
});
155+
156+
it('should parse an options object without appId', () => {
157+
const myOpts: alerts.FirebaseAlertOptions = {
158+
alertType: ALERT_TYPE,
159+
region: 'us-west1',
160+
};
161+
162+
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);
163+
164+
expect(opts).to.deep.equal({ region: 'us-west1' });
165+
expect(alertType).to.equal(myOpts.alertType);
166+
expect(appId).to.be.undefined;
167+
});
168+
169+
it('should parse an options object with appId', () => {
170+
const myOpts: alerts.FirebaseAlertOptions = {
171+
alertType: ALERT_TYPE,
172+
appId: APPID,
173+
region: 'us-west1',
174+
};
175+
176+
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);
177+
178+
expect(opts).to.deep.equal({ region: 'us-west1' });
179+
expect(alertType).to.equal(myOpts.alertType);
180+
expect(appId).to.be.equal(myOpts.appId);
181+
});
182+
});
183+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { expect } from 'chai';
2+
import * as alerts from '../../../../src/v2/providers/alerts';
3+
import * as appDistribution from '../../../../src/v2/providers/alerts/appDistribution';
4+
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';
5+
6+
const APPID = '123456789';
7+
const myHandler = () => 42;
8+
9+
describe('appDistribution', () => {
10+
describe('onNewTesterIosDevicePublished', () => {
11+
it('should create a function with alertType & appId', () => {
12+
const func = appDistribution.onNewTesterIosDevicePublished(
13+
APPID,
14+
myHandler
15+
);
16+
17+
expect(func.__endpoint).to.deep.equal({
18+
platform: 'gcfv2',
19+
labels: {},
20+
eventTrigger: {
21+
eventType: alerts.eventType,
22+
eventFilters: {
23+
alertType: appDistribution.newTesterIosDeviceAlert,
24+
appId: APPID,
25+
},
26+
retry: false,
27+
},
28+
});
29+
});
30+
31+
it('should create a function with opts', () => {
32+
const func = appDistribution.onNewTesterIosDevicePublished(
33+
{ ...FULL_OPTIONS },
34+
myHandler
35+
);
36+
37+
expect(func.__endpoint).to.deep.equal({
38+
...FULL_ENDPOINT,
39+
eventTrigger: {
40+
eventType: alerts.eventType,
41+
eventFilters: {
42+
alertType: appDistribution.newTesterIosDeviceAlert,
43+
},
44+
retry: false,
45+
},
46+
});
47+
});
48+
49+
it('should create a function with appid in opts', () => {
50+
const func = appDistribution.onNewTesterIosDevicePublished(
51+
{ ...FULL_OPTIONS, appId: APPID },
52+
myHandler
53+
);
54+
55+
expect(func.__endpoint).to.deep.equal({
56+
...FULL_ENDPOINT,
57+
eventTrigger: {
58+
eventType: alerts.eventType,
59+
eventFilters: {
60+
alertType: appDistribution.newTesterIosDeviceAlert,
61+
appId: APPID,
62+
},
63+
retry: false,
64+
},
65+
});
66+
});
67+
68+
it('should create a function without opts or appId', () => {
69+
const func = appDistribution.onNewTesterIosDevicePublished(myHandler);
70+
71+
expect(func.__endpoint).to.deep.equal({
72+
platform: 'gcfv2',
73+
labels: {},
74+
eventTrigger: {
75+
eventType: alerts.eventType,
76+
eventFilters: {
77+
alertType: appDistribution.newTesterIosDeviceAlert,
78+
},
79+
retry: false,
80+
},
81+
});
82+
});
83+
84+
it('should create a function with a run method', () => {
85+
const func = appDistribution.onNewTesterIosDevicePublished(
86+
APPID,
87+
(event) => event
88+
);
89+
90+
const res = func.run('input' as any);
91+
92+
expect(res).to.equal('input');
93+
});
94+
});
95+
96+
describe('getOptsAndApp', () => {
97+
it('should parse a string', () => {
98+
const [opts, appId] = appDistribution.getOptsAndApp(APPID);
99+
100+
expect(opts).to.deep.equal({});
101+
expect(appId).to.equal(APPID);
102+
});
103+
104+
it('should parse an options object without appId', () => {
105+
const myOpts: appDistribution.AppDistributionOptions = {
106+
region: 'us-west1',
107+
};
108+
109+
const [opts, appId] = appDistribution.getOptsAndApp(myOpts);
110+
111+
expect(opts).to.deep.equal({ region: 'us-west1' });
112+
expect(appId).to.be.undefined;
113+
});
114+
115+
it('should parse an options object with appId', () => {
116+
const myOpts: appDistribution.AppDistributionOptions = {
117+
appId: APPID,
118+
region: 'us-west1',
119+
};
120+
121+
const [opts, appId] = appDistribution.getOptsAndApp(myOpts);
122+
123+
expect(opts).to.deep.equal({ region: 'us-west1' });
124+
expect(appId).to.equal(APPID);
125+
});
126+
});
127+
});

0 commit comments

Comments
 (0)