Skip to content

Commit ea634a8

Browse files
authored
Change type info to be inheritance friendly. (#1091)
Unwind the cleverness of CloudEvent<EventType, Extensions>. Types now match the approved format of the Python API. CloudFunction now templatizes the CloudEvent rather than the EventType so that we can keep typing to include subtypes of events both in code complete and in firebase-functions-test.
1 parent af51156 commit ea634a8

File tree

7 files changed

+82
-113
lines changed

7 files changed

+82
-113
lines changed

src/v2/core.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface TriggerAnnotation {
5757
* A CloudEventBase is the base of a cross-platform format for encoding a serverless event.
5858
* More information can be found in https://github.com/cloudevents/spec
5959
*/
60-
interface CloudEventBase<T> {
60+
export interface CloudEvent<T> {
6161
/** Version of the CloudEvents spec for this event. */
6262
readonly specversion: '1.0';
6363

@@ -78,32 +78,14 @@ interface CloudEventBase<T> {
7878

7979
/** Information about this specific event. */
8080
data: T;
81-
82-
/**
83-
* A map of template parameter name to value for subject strings.
84-
*
85-
* This map is only available on some event types that allow templates
86-
* in the subject string, such as Firestore. When listening to a document
87-
* template "/users/{uid}", an event with subject "/documents/users/1234"
88-
* would have a params of {"uid": "1234"}.
89-
*
90-
* Params are generated inside the firebase-functions SDK and are not
91-
* part of the CloudEvents spec nor the payload that a Cloud Function
92-
* actually receives.
93-
*/
94-
params?: Record<string, string>;
9581
}
9682

97-
/**
98-
* A CloudEvent with custom extension attributes
99-
*/
100-
export type CloudEvent<T = any, Ext = {}> = CloudEventBase<T> & Ext;
10183
/** A handler for CloudEvents. */
102-
export interface CloudFunction<T> {
84+
export interface CloudFunction<EventType extends CloudEvent<unknown>> {
10385
(raw: CloudEvent<unknown>): any | Promise<any>;
10486

10587
__trigger?: unknown;
10688
__endpoint: ManifestEndpoint;
10789

108-
run(event: CloudEvent<T>): any | Promise<any>;
90+
run(event: EventType): any | Promise<any>;
10991
}

src/v2/providers/alerts/alerts.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@ export interface FirebaseAlertData<T = any> {
1414
payload: T;
1515
}
1616

17-
interface WithAlertTypeAndApp {
17+
/**
18+
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
19+
*/
20+
export interface AlertEvent<T> extends CloudEvent<FirebaseAlertData<T>> {
1821
/** The type of the alerts that got triggered. */
1922
alertType: string;
2023
/**
2124
* The Firebase App ID that’s associated with the alert. This is optional,
2225
* and only present when the alert is targeting at a specific Firebase App.
2326
*/
2427
appId?: string;
28+
29+
/** Data for an AlertEvent is a FirebaseAlertData with a given payload. */
30+
data: FirebaseAlertData<T>;
2531
}
26-
/**
27-
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
28-
*/
29-
export type AlertEvent<T> = CloudEvent<
30-
FirebaseAlertData<T>,
31-
WithAlertTypeAndApp
32-
>;
3332

3433
/** @internal */
3534
export const eventType = 'google.firebase.firebasealerts.alerts.v1.published';
@@ -63,13 +62,11 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
6362
export function onAlertPublished<T extends { ['@type']: string } = any>(
6463
alertTypeOrOpts: AlertType | FirebaseAlertOptions,
6564
handler: (event: AlertEvent<T>) => any | Promise<any>
66-
): CloudFunction<FirebaseAlertData<T>> {
65+
): CloudFunction<AlertEvent<T>> {
6766
const [opts, alertType, appId] = getOptsAndAlertTypeAndApp(alertTypeOrOpts);
6867

6968
const func = (raw: CloudEvent<unknown>) => {
70-
return handler(
71-
raw as CloudEvent<FirebaseAlertData<T>, WithAlertTypeAndApp>
72-
);
69+
return handler(raw as AlertEvent<T>);
7370
};
7471

7572
func.run = handler;

src/v2/providers/alerts/appDistribution.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@ export interface NewTesterDevicePayload {
1414
testerDeviceIdentifier: string;
1515
}
1616

17-
interface WithAlertTypeAndApp {
17+
/**
18+
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
19+
*/
20+
export interface AppDistributionEvent<T>
21+
extends CloudEvent<FirebaseAlertData<T>> {
1822
/** The type of the alerts that got triggered. */
1923
alertType: string;
2024
/** The Firebase App ID that’s associated with the alert. */
2125
appId: string;
2226
}
23-
/**
24-
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
25-
*/
26-
export type AppDistributionEvent<T> = CloudEvent<
27-
FirebaseAlertData<T>,
28-
WithAlertTypeAndApp
29-
>;
3027

3128
/** @internal */
3229
export const newTesterIosDeviceAlert = 'appDistribution.newTesterIosDevice';
@@ -45,19 +42,19 @@ export function onNewTesterIosDevicePublished(
4542
handler: (
4643
event: AppDistributionEvent<NewTesterDevicePayload>
4744
) => any | Promise<any>
48-
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>;
45+
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>>;
4946
export function onNewTesterIosDevicePublished(
5047
appId: string,
5148
handler: (
5249
event: AppDistributionEvent<NewTesterDevicePayload>
5350
) => any | Promise<any>
54-
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>;
51+
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>>;
5552
export function onNewTesterIosDevicePublished(
5653
opts: AppDistributionOptions,
5754
handler: (
5855
event: AppDistributionEvent<NewTesterDevicePayload>
5956
) => any | Promise<any>
60-
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>;
57+
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>>;
6158
export function onNewTesterIosDevicePublished(
6259
appIdOrOptsOrHandler:
6360
| string
@@ -68,7 +65,7 @@ export function onNewTesterIosDevicePublished(
6865
handler?: (
6966
event: AppDistributionEvent<NewTesterDevicePayload>
7067
) => any | Promise<any>
71-
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>> {
68+
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>> {
7269
if (typeof appIdOrOptsOrHandler === 'function') {
7370
handler = appIdOrOptsOrHandler as (
7471
event: AppDistributionEvent<NewTesterDevicePayload>

src/v2/providers/alerts/billing.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ export interface PlanAutomatedUpdatePayload {
2828
notificationType: string;
2929
}
3030

31-
interface WithAlertType {
32-
/** The type of the alerts that got triggered. */
33-
alertType: string;
34-
}
3531
/**
3632
* A custom CloudEvent for billing Firebase Alerts (with custom extension attributes).
3733
*/
38-
export type BillingEvent<T> = CloudEvent<FirebaseAlertData<T>, WithAlertType>;
34+
export interface BillingEvent<T> extends CloudEvent<FirebaseAlertData<T>> {
35+
/** The type of the alerts that got triggered. */
36+
alertType: string;
37+
}
3938

4039
/** @internal */
4140
export const planUpdateAlert = 'billing.planUpdate';
@@ -47,17 +46,17 @@ export const planAutomatedUpdateAlert = 'billing.planAutomatedUpdate';
4746
*/
4847
export function onPlanUpdatePublished(
4948
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
50-
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
49+
): CloudFunction<BillingEvent<PlanUpdatePayload>>;
5150
export function onPlanUpdatePublished(
5251
opts: options.EventHandlerOptions,
5352
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
54-
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
53+
): CloudFunction<BillingEvent<PlanUpdatePayload>>;
5554
export function onPlanUpdatePublished(
5655
optsOrHandler:
5756
| options.EventHandlerOptions
5857
| ((event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>),
5958
handler?: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
60-
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>> {
59+
): CloudFunction<BillingEvent<PlanUpdatePayload>> {
6160
return onOperation<PlanUpdatePayload>(
6261
planUpdateAlert,
6362
optsOrHandler,
@@ -72,21 +71,21 @@ export function onPlanAutomatedUpdatePublished(
7271
handler: (
7372
event: BillingEvent<PlanAutomatedUpdatePayload>
7473
) => any | Promise<any>
75-
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
74+
): CloudFunction<BillingEvent<PlanAutomatedUpdatePayload>>;
7675
export function onPlanAutomatedUpdatePublished(
7776
opts: options.EventHandlerOptions,
7877
handler: (
7978
event: BillingEvent<PlanAutomatedUpdatePayload>
8079
) => any | Promise<any>
81-
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
80+
): CloudFunction<BillingEvent<PlanAutomatedUpdatePayload>>;
8281
export function onPlanAutomatedUpdatePublished(
8382
optsOrHandler:
8483
| options.EventHandlerOptions
8584
| ((event: BillingEvent<PlanAutomatedUpdatePayload>) => any | Promise<any>),
8685
handler?: (
8786
event: BillingEvent<PlanAutomatedUpdatePayload>
8887
) => any | Promise<any>
89-
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>> {
88+
): CloudFunction<BillingEvent<PlanAutomatedUpdatePayload>> {
9089
return onOperation<PlanAutomatedUpdatePayload>(
9190
planAutomatedUpdateAlert,
9291
optsOrHandler,
@@ -101,7 +100,7 @@ export function onOperation<T>(
101100
| options.EventHandlerOptions
102101
| ((event: BillingEvent<T>) => any | Promise<any>),
103102
handler: (event: BillingEvent<T>) => any | Promise<any>
104-
): CloudFunction<FirebaseAlertData<T>> {
103+
): CloudFunction<BillingEvent<T>> {
105104
if (typeof optsOrHandler === 'function') {
106105
handler = optsOrHandler as (event: BillingEvent<T>) => any | Promise<any>;
107106
optsOrHandler = {};

0 commit comments

Comments
 (0)