Skip to content

Specific interface for provider enhancement (2/3) #1022

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

Merged
merged 12 commits into from
Jan 27, 2022
Merged
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"./v2/pubsub": "./lib/v2/providers/pubsub.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/appDistribution": "./lib/v2/providers/alerts/appDistribution.js",
"./v2/alerts/billing": "./lib/v2/providers/alerts/billing.js"
},
"typesVersions": {
"*": {
Expand Down Expand Up @@ -122,6 +123,9 @@
],
"v2/alerts/appDistribution": [
"lib/v2/providers/alerts/appDistribution"
],
"v2/alerts/billing": [
"lib/v2/providers/alerts/billing"
]
}
},
Expand Down
126 changes: 126 additions & 0 deletions spec/v2/providers/alerts/billing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { expect } from 'chai';
import * as alerts from '../../../../src/v2/providers/alerts';
import * as billing from '../../../../src/v2/providers/alerts/billing';
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';

const ALERT_TYPE = 'new-alert-type';
const myHandler = () => 42;

describe('billing', () => {
describe('onPlanUpdatePublished', () => {
it('should create a function with only handler', () => {
const func = billing.onPlanUpdatePublished(myHandler);

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

it('should create a function with opts & handler', () => {
const func = billing.onPlanUpdatePublished(
{ ...FULL_OPTIONS },
myHandler
);

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

describe('onAutomatedPlanUpdatePublished', () => {
it('should create a function with only handler', () => {
const func = billing.onAutomatedPlanUpdatePublished(myHandler);

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

it('should create a function with opts & handler', () => {
const func = billing.onAutomatedPlanUpdatePublished(
{ ...FULL_OPTIONS },
myHandler
);

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

describe('onOperation', () => {
it('should create a function with alertType only', () => {
const func = billing.onOperation(ALERT_TYPE, myHandler, undefined);

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

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

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

it('should create a function with a run method', () => {
const func = billing.onOperation(ALERT_TYPE, (event) => event, undefined);

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

expect(res).to.equal('input');
});
});
});
110 changes: 110 additions & 0 deletions src/v2/providers/alerts/billing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { getEndpointAnnotation, FirebaseAlertData } from '.';
import { CloudEvent, CloudFunction } from '../../core';
import * as options from '../../options';

/**
* The internal payload object for billing plan updates.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface PlanUpdatePayload {
['@type']: 'com.google.firebase.firebasealerts.PlanUpdatePayload';
billingPlan: string;
principalEmail: string;
}

/**
* The internal payload object for billing plan automated updates.
* Payload is wrapped inside a FirebaseAlertData object.
*/
export interface PlanAutomatedUpdatePayload {
['@type']: 'com.google.firebase.firebasealerts.PlanAutomatedUpdatePayload';
billingPlan: string;
}

interface WithAlertType {
alertType: string;
}
/**
* A custom CloudEvent for billing Firebase Alerts (with custom extension attributes).
*/
export type BillingEvent<T> = CloudEvent<FirebaseAlertData<T>, WithAlertType>;

/** @internal */
export const planUpdateAlert = 'billing.planUpdate';
/** @internal */
export const automatedPlanUpdateAlert = 'billing.automatedPlanUpdate';

/**
* Declares a function that can handle a billing plan update event.
*/
export function onPlanUpdatePublished(
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
export function onPlanUpdatePublished(
opts: options.EventHandlerOptions,
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
export function onPlanUpdatePublished(
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>),
handler?: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>> {
return onOperation<PlanUpdatePayload>(
planUpdateAlert,
optsOrHandler,
handler
);
}

/**
* Declares a function that can handle an automated billing plan update event.
*/
export function onAutomatedPlanUpdatePublished(
handler: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
export function onAutomatedPlanUpdatePublished(
opts: options.EventHandlerOptions,
handler: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
export function onAutomatedPlanUpdatePublished(
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<PlanAutomatedUpdatePayload>) => any | Promise<any>),
handler?: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>> {
return onOperation<PlanAutomatedUpdatePayload>(
automatedPlanUpdateAlert,
optsOrHandler,
handler
);
}

/** @internal */
export function onOperation<T>(
alertType: string,
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<T>) => any | Promise<any>),
handler: (event: BillingEvent<T>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<T>> {
if (typeof optsOrHandler === 'function') {
handler = optsOrHandler as (event: BillingEvent<T>) => any | Promise<any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woo (event: BillingEvent<T>) => any | Promise<any>; is used thrice! This meets the threshold for DRY for me:

type BillingEventHandler<T> = (event: BillingEvent<T>) => any | Promise<any>;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that looks way better, thanks for the suggestion!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taeold Removed based on typing feedback below

optsOrHandler = {};
}

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

func.run = handler;
func.__endpoint = getEndpointAnnotation(optsOrHandler, alertType);

return func;
}
3 changes: 2 additions & 1 deletion src/v2/providers/alerts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as appDistribution from './appDistribution';
import * as billing from './billing';

export { appDistribution };
export { appDistribution, billing };
export * from './alerts';
26 changes: 26 additions & 0 deletions v2/alerts/billing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The MIT License (MIT)
//
// Copyright (c) 2021 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// This file is not part of the firebase-functions SDK. It is used to silence the
// imports eslint plugin until it can understand import paths defined by node
// package exports.
// For more information, see github.com/import-js/eslint-plugin-import/issues/1810