- Notifications
You must be signed in to change notification settings - Fork 218
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
+269 −2
Merged
Changes from 1 commit
Commits
Show all changes
12 commits Select commit Hold shift + click to select a range
632bd50 adding in billing changes
colerogers 7b829dd Merge branch 'colerogers.alerts' into colerogers.billing
colerogers 11ee618 removing comments & adding package refs
colerogers 480c757 jsdoc comments
colerogers c11808a merge colerogers.alerts
colerogers 255dccd Merge branch 'colerogers.alerts' into colerogers.billing
colerogers e99f6ad Merge branch 'colerogers.alerts' into colerogers.billing
colerogers c7df9e7 addressing pr comments
colerogers 1749f56 change handler doc string
colerogers 26c62e9 changing to BillingEventHandler type
colerogers 6e1717a merge from alerts and fix import/export
colerogers 71dda6c remove BillingEventHandler type
colerogers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adding in billing changes
- Loading branch information
commit 632bd507fb07ccd13215daf1292298064d2cb059
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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 { BASIC_ENDPOINT, BASIC_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( | ||
| { ...BASIC_OPTIONS }, | ||
| myHandler | ||
| ); | ||
| | ||
| expect(func.__endpoint).to.deep.equal({ | ||
| ...BASIC_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( | ||
| { ...BASIC_OPTIONS }, | ||
| myHandler | ||
| ); | ||
| | ||
| expect(func.__endpoint).to.deep.equal({ | ||
| ...BASIC_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, | ||
| { ...BASIC_OPTIONS }, | ||
| myHandler | ||
| ); | ||
| | ||
| expect(func.__endpoint).to.deep.equal({ | ||
| ...BASIC_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'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { defineEndpoint, FirebaseAlertData } from '.'; | ||
| import { ManifestEndpoint } from '../../../common/manifest'; | ||
| import { CloudEvent, CloudFunction } from '../../core'; | ||
| import * as options from '../../options'; | ||
| | ||
| /** Data */ | ||
| // billing.planUpdate | ||
| export interface PlanUpdatePayload { | ||
| ['@type']: 'com.google.firebase.firebasealerts.PlanUpdatePayload'; | ||
| billingPlan: string; | ||
| principalEmail: string; | ||
| } | ||
| // billing.automatedPlanUpdate | ||
| export interface PlanAutomatedUpdatePayload { | ||
| ['@type']: 'com.google.firebase.firebasealerts.PlanAutomatedUpdatePayload'; | ||
| billingPlan: string; | ||
| } | ||
| | ||
| /** Events */ | ||
| /** @internal */ | ||
| export const planUpdateAlert = 'billing.planUpdate'; | ||
| /** @internal */ | ||
| export const automatedPlanUpdateAlert = 'billing.automatedPlanUpdate'; | ||
| | ||
| /** Cloud Event Type */ | ||
| interface WithAlertType { | ||
| alertType: string; | ||
| } | ||
| export type BillingEvent<T> = CloudEvent<FirebaseAlertData<T>, WithAlertType>; | ||
| | ||
| /** Handlers */ | ||
| 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 | ||
| ); | ||
| } | ||
| | ||
| 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>; | ||
| optsOrHandler = {}; | ||
| } | ||
| | ||
| const func = (raw: CloudEvent<unknown>) => { | ||
| return handler(raw as BillingEvent<T>); | ||
| }; | ||
| | ||
| func.run = handler; | ||
| | ||
| // TypeScript doesn't recognize defineProperty as adding a property and complains | ||
| // that __endpoint doesn't exist. We can either cast to any and lose all type safety | ||
| // or we can just assign a meaningless value before calling defineProperty. | ||
| func.__trigger = 'silence the transpiler'; | ||
| func.__endpoint = {} as ManifestEndpoint; | ||
colerogers marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| defineEndpoint(func, optsOrHandler, alertType, undefined); | ||
colerogers marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| return func; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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