|
| 1 | +import { getEndpointAnnotation, FirebaseAlertData } from '.'; |
| 2 | +import { CloudEvent, CloudFunction } from '../../core'; |
| 3 | +import * as options from '../../options'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The internal payload object for billing plan updates. |
| 7 | + * Payload is wrapped inside a FirebaseAlertData object. |
| 8 | + */ |
| 9 | +export interface PlanUpdatePayload { |
| 10 | + ['@type']: 'com.google.firebase.firebasealerts.PlanUpdatePayload'; |
| 11 | + billingPlan: string; |
| 12 | + principalEmail: string; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * The internal payload object for billing plan automated updates. |
| 17 | + * Payload is wrapped inside a FirebaseAlertData object. |
| 18 | + */ |
| 19 | +export interface PlanAutomatedUpdatePayload { |
| 20 | + ['@type']: 'com.google.firebase.firebasealerts.PlanAutomatedUpdatePayload'; |
| 21 | + billingPlan: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface WithAlertType { |
| 25 | + alertType: string; |
| 26 | +} |
| 27 | +/** |
| 28 | + * A custom CloudEvent for billing Firebase Alerts (with custom extension attributes). |
| 29 | + */ |
| 30 | +export type BillingEvent<T> = CloudEvent<FirebaseAlertData<T>, WithAlertType>; |
| 31 | + |
| 32 | +/** @internal */ |
| 33 | +export const planUpdateAlert = 'billing.planUpdate'; |
| 34 | +/** @internal */ |
| 35 | +export const automatedPlanUpdateAlert = 'billing.automatedPlanUpdate'; |
| 36 | + |
| 37 | +/** |
| 38 | + * Declares a function that can handle a billing plan update event. |
| 39 | + */ |
| 40 | +export function onPlanUpdatePublished( |
| 41 | + handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any> |
| 42 | +): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>; |
| 43 | +export function onPlanUpdatePublished( |
| 44 | + opts: options.EventHandlerOptions, |
| 45 | + handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any> |
| 46 | +): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>; |
| 47 | +export function onPlanUpdatePublished( |
| 48 | + optsOrHandler: |
| 49 | + | options.EventHandlerOptions |
| 50 | + | ((event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>), |
| 51 | + handler?: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any> |
| 52 | +): CloudFunction<FirebaseAlertData<PlanUpdatePayload>> { |
| 53 | + return onOperation<PlanUpdatePayload>( |
| 54 | + planUpdateAlert, |
| 55 | + optsOrHandler, |
| 56 | + handler |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Declares a function that can handle an automated billing plan update event. |
| 62 | + */ |
| 63 | +export function onAutomatedPlanUpdatePublished( |
| 64 | + handler: ( |
| 65 | + event: BillingEvent<PlanAutomatedUpdatePayload> |
| 66 | + ) => any | Promise<any> |
| 67 | +): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>; |
| 68 | +export function onAutomatedPlanUpdatePublished( |
| 69 | + opts: options.EventHandlerOptions, |
| 70 | + handler: ( |
| 71 | + event: BillingEvent<PlanAutomatedUpdatePayload> |
| 72 | + ) => any | Promise<any> |
| 73 | +): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>; |
| 74 | +export function onAutomatedPlanUpdatePublished( |
| 75 | + optsOrHandler: |
| 76 | + | options.EventHandlerOptions |
| 77 | + | ((event: BillingEvent<PlanAutomatedUpdatePayload>) => any | Promise<any>), |
| 78 | + handler?: ( |
| 79 | + event: BillingEvent<PlanAutomatedUpdatePayload> |
| 80 | + ) => any | Promise<any> |
| 81 | +): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>> { |
| 82 | + return onOperation<PlanAutomatedUpdatePayload>( |
| 83 | + automatedPlanUpdateAlert, |
| 84 | + optsOrHandler, |
| 85 | + handler |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +/** @internal */ |
| 90 | +export function onOperation<T>( |
| 91 | + alertType: string, |
| 92 | + optsOrHandler: |
| 93 | + | options.EventHandlerOptions |
| 94 | + | ((event: BillingEvent<T>) => any | Promise<any>), |
| 95 | + handler: (event: BillingEvent<T>) => any | Promise<any> |
| 96 | +): CloudFunction<FirebaseAlertData<T>> { |
| 97 | + if (typeof optsOrHandler === 'function') { |
| 98 | + handler = optsOrHandler as (event: BillingEvent<T>) => any | Promise<any>; |
| 99 | + optsOrHandler = {}; |
| 100 | + } |
| 101 | + |
| 102 | + const func = (raw: CloudEvent<unknown>) => { |
| 103 | + return handler(raw as BillingEvent<T>); |
| 104 | + }; |
| 105 | + |
| 106 | + func.run = handler; |
| 107 | + func.__endpoint = getEndpointAnnotation(optsOrHandler, alertType); |
| 108 | + |
| 109 | + return func; |
| 110 | +} |
0 commit comments