Skip to content

Commit

Permalink
Generic interface for provider enhancement (#1020) (#1024)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
colerogers authored Jan 31, 2022
1 parent 7645a69 commit 8e21f77
Show file tree
Hide file tree
Showing 18 changed files with 1,839 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Parallelizes network calls that occur when validating authorization for onCall handlers.
- Adds new regions to V2 API
- Fixes bug where the emulator crashed when given app without an `options` property.
- Adds new alerting providers
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
"./v2/https": "./lib/v2/providers/https.js",
"./v2/params": "./lib/v2/params/index.js",
"./v2/pubsub": "./lib/v2/providers/pubsub.js",
"./v2/storage": "./lib/v2/providers/storage.js"
"./v2/storage": "./lib/v2/providers/storage.js",
"./v2/alerts": "./lib/v2/providers/alerts/index.js",
"./v2/alerts/appDistribution": "./lib/v2/providers/alerts/appDistribution.js",
"./v2/alerts/billing": "./lib/v2/providers/alerts/billing.js",
"./v2/alerts/crashlytics": "./lib/v2/providers/alerts/crashlytics.js"
},
"typesVersions": {
"*": {
Expand Down Expand Up @@ -114,6 +118,18 @@
],
"v2/storage": [
"lib/v2/providers/storage"
],
"v2/alerts": [
"lib/v2/providers/alerts"
],
"v2/alerts/appDistribution": [
"lib/v2/providers/alerts/appDistribution"
],
"v2/alerts/billing": [
"lib/v2/providers/alerts/billing"
],
"v2/alerts/crashlytics": [
"lib/v2/providers/alerts/crashlytics"
]
}
},
Expand Down
183 changes: 183 additions & 0 deletions spec/v2/providers/alerts/alerts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { expect } from 'chai';
import * as options from '../../../../src/v2/options';
import * as alerts from '../../../../src/v2/providers/alerts';
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';

const ALERT_TYPE = 'new-alert-type';
const APPID = '123456789';

describe('alerts', () => {
describe('onAlertPublished', () => {
it('should create the function without opts', () => {
const result = alerts.onAlertPublished(ALERT_TYPE, () => 42);

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should create the function with opts', () => {
const result = alerts.onAlertPublished(
{
...FULL_OPTIONS,
alertType: ALERT_TYPE,
appId: APPID,
},
() => 42
);

expect(result.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
appId: APPID,
},
retry: false,
},
});
});

it('should have a .run method', () => {
const func = alerts.onAlertPublished(ALERT_TYPE, (event) => event);

const res = func.run('input' as any);

expect(res).to.equal('input');
});
});

describe('getEndpointAnnotation', () => {
beforeEach(() => {
process.env.GCLOUD_PROJECT = 'aProject';
});

afterEach(() => {
options.setGlobalOptions({});
delete process.env.GCLOUD_PROJECT;
});

it('should define the endpoint without appId and opts', () => {
expect(alerts.getEndpointAnnotation({}, ALERT_TYPE)).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should define a complex endpoint without appId', () => {
expect(
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE)
).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should define a complex endpoint', () => {
expect(
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE, APPID)
).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
appId: APPID,
},
retry: false,
},
});
});

it('should merge global & specific opts', () => {
options.setGlobalOptions({
concurrency: 20,
region: 'europe-west1',
minInstances: 1,
});
const specificOpts = {
region: 'us-west1',
minInstances: 3,
};

expect(
alerts.getEndpointAnnotation(specificOpts, ALERT_TYPE, APPID)
).to.deep.equal({
platform: 'gcfv2',
labels: {},
concurrency: 20,
region: ['us-west1'],
minInstances: 3,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
appId: APPID,
},
retry: false,
},
});
});
});

describe('getOptsAndAlertTypeAndApp', () => {
it('should parse a string', () => {
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(
ALERT_TYPE
);

expect(opts).to.deep.equal({});
expect(alertType).to.equal(ALERT_TYPE);
expect(appId).to.be.undefined;
});

it('should parse an options object without appId', () => {
const myOpts: alerts.FirebaseAlertOptions = {
alertType: ALERT_TYPE,
region: 'us-west1',
};

const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(alertType).to.equal(myOpts.alertType);
expect(appId).to.be.undefined;
});

it('should parse an options object with appId', () => {
const myOpts: alerts.FirebaseAlertOptions = {
alertType: ALERT_TYPE,
appId: APPID,
region: 'us-west1',
};

const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(alertType).to.equal(myOpts.alertType);
expect(appId).to.be.equal(myOpts.appId);
});
});
});
127 changes: 127 additions & 0 deletions spec/v2/providers/alerts/appDistribution.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { expect } from 'chai';
import * as alerts from '../../../../src/v2/providers/alerts';
import * as appDistribution from '../../../../src/v2/providers/alerts/appDistribution';
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';

const APPID = '123456789';
const myHandler = () => 42;

describe('appDistribution', () => {
describe('onNewTesterIosDevicePublished', () => {
it('should create a function with alertType & appId', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
APPID,
myHandler
);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
appId: APPID,
},
retry: false,
},
});
});

it('should create a function with opts', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
{ ...FULL_OPTIONS },
myHandler
);

expect(func.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
},
retry: false,
},
});
});

it('should create a function with appid in opts', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
{ ...FULL_OPTIONS, appId: APPID },
myHandler
);

expect(func.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
appId: APPID,
},
retry: false,
},
});
});

it('should create a function without opts or appId', () => {
const func = appDistribution.onNewTesterIosDevicePublished(myHandler);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
},
retry: false,
},
});
});

it('should create a function with a run method', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
APPID,
(event) => event
);

const res = func.run('input' as any);

expect(res).to.equal('input');
});
});

describe('getOptsAndApp', () => {
it('should parse a string', () => {
const [opts, appId] = appDistribution.getOptsAndApp(APPID);

expect(opts).to.deep.equal({});
expect(appId).to.equal(APPID);
});

it('should parse an options object without appId', () => {
const myOpts: appDistribution.AppDistributionOptions = {
region: 'us-west1',
};

const [opts, appId] = appDistribution.getOptsAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(appId).to.be.undefined;
});

it('should parse an options object with appId', () => {
const myOpts: appDistribution.AppDistributionOptions = {
appId: APPID,
region: 'us-west1',
};

const [opts, appId] = appDistribution.getOptsAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(appId).to.equal(APPID);
});
});
});
Loading

0 comments on commit 8e21f77

Please sign in to comment.