-
Notifications
You must be signed in to change notification settings - Fork 211
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
Changes from all commits
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
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
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 { 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'); | ||
}); | ||
}); | ||
}); |
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,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>; | ||
optsOrHandler = {}; | ||
} | ||
|
||
const func = (raw: CloudEvent<unknown>) => { | ||
return handler(raw as BillingEvent<T>); | ||
}; | ||
|
||
func.run = handler; | ||
func.__endpoint = getEndpointAnnotation(optsOrHandler, alertType); | ||
|
||
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as appDistribution from './appDistribution'; | ||
import * as billing from './billing'; | ||
|
||
export { appDistribution }; | ||
export { appDistribution, billing }; | ||
export * from './alerts'; |
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