Skip to content
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

Change type info to be inheritance friendly. #1091

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 3 additions & 21 deletions src/v2/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface TriggerAnnotation {
* A CloudEventBase is the base of a cross-platform format for encoding a serverless event.
* More information can be found in https://github.com/cloudevents/spec
*/
interface CloudEventBase<T> {
export interface CloudEvent<T> {
/** Version of the CloudEvents spec for this event. */
readonly specversion: '1.0';

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

/** Information about this specific event. */
data: T;

/**
* A map of template parameter name to value for subject strings.
*
* This map is only available on some event types that allow templates
* in the subject string, such as Firestore. When listening to a document
* template "/users/{uid}", an event with subject "/documents/users/1234"
* would have a params of {"uid": "1234"}.
*
* Params are generated inside the firebase-functions SDK and are not
* part of the CloudEvents spec nor the payload that a Cloud Function
* actually receives.
*/
params?: Record<string, string>;
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: params should probably only be present in Cloud Function types that actually support params.

}

/**
* A CloudEvent with custom extension attributes
*/
export type CloudEvent<T = any, Ext = {}> = CloudEventBase<T> & Ext;
/** A handler for CloudEvents. */
export interface CloudFunction<T> {
export interface CloudFunction<EventType extends CloudEvent<unknown>> {
(raw: CloudEvent<unknown>): any | Promise<any>;

__trigger?: unknown;
__endpoint: ManifestEndpoint;

run(event: CloudEvent<T>): any | Promise<any>;
run(event: EventType): any | Promise<any>;
}
21 changes: 9 additions & 12 deletions src/v2/providers/alerts/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ export interface FirebaseAlertData<T = any> {
payload: T;
}

interface WithAlertTypeAndApp {
/**
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
*/
export interface AlertEvent<T> extends CloudEvent<FirebaseAlertData<T>> {
/** The type of the alerts that got triggered. */
alertType: string;
/**
* The Firebase App ID that’s associated with the alert. This is optional,
* and only present when the alert is targeting at a specific Firebase App.
*/
appId?: string;

/** Data for an AlertEvent is a FirebaseAlertData with a given payload. */
data: FirebaseAlertData<T>;
}
/**
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
*/
export type AlertEvent<T> = CloudEvent<
FirebaseAlertData<T>,
WithAlertTypeAndApp
>;

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

const func = (raw: CloudEvent<unknown>) => {
return handler(
raw as CloudEvent<FirebaseAlertData<T>, WithAlertTypeAndApp>
);
return handler(raw as AlertEvent<T>);
};

func.run = handler;
Expand Down
21 changes: 9 additions & 12 deletions src/v2/providers/alerts/appDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ export interface NewTesterDevicePayload {
testerDeviceIdentifier: string;
}

interface WithAlertTypeAndApp {
/**
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
*/
export interface AppDistributionEvent<T>
extends CloudEvent<FirebaseAlertData<T>> {
/** The type of the alerts that got triggered. */
alertType: string;
/** The Firebase App ID that’s associated with the alert. */
appId: string;
}
/**
* A custom CloudEvent for Firebase Alerts (with custom extension attributes).
*/
export type AppDistributionEvent<T> = CloudEvent<
FirebaseAlertData<T>,
WithAlertTypeAndApp
>;

/** @internal */
export const newTesterIosDeviceAlert = 'appDistribution.newTesterIosDevice';
Expand All @@ -45,19 +42,19 @@ export function onNewTesterIosDevicePublished(
handler: (
event: AppDistributionEvent<NewTesterDevicePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>;
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>>;
export function onNewTesterIosDevicePublished(
appId: string,
handler: (
event: AppDistributionEvent<NewTesterDevicePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>;
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>>;
export function onNewTesterIosDevicePublished(
opts: AppDistributionOptions,
handler: (
event: AppDistributionEvent<NewTesterDevicePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>>;
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>>;
export function onNewTesterIosDevicePublished(
appIdOrOptsOrHandler:
| string
Expand All @@ -68,7 +65,7 @@ export function onNewTesterIosDevicePublished(
handler?: (
event: AppDistributionEvent<NewTesterDevicePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<NewTesterDevicePayload>> {
): CloudFunction<AppDistributionEvent<NewTesterDevicePayload>> {
if (typeof appIdOrOptsOrHandler === 'function') {
handler = appIdOrOptsOrHandler as (
event: AppDistributionEvent<NewTesterDevicePayload>
Expand Down
23 changes: 11 additions & 12 deletions src/v2/providers/alerts/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ export interface PlanAutomatedUpdatePayload {
notificationType: string;
}

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

/** @internal */
export const planUpdateAlert = 'billing.planUpdate';
Expand All @@ -47,17 +46,17 @@ export const planAutomatedUpdateAlert = 'billing.planAutomatedUpdate';
*/
export function onPlanUpdatePublished(
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
): CloudFunction<BillingEvent<PlanUpdatePayload>>;
export function onPlanUpdatePublished(
opts: options.EventHandlerOptions,
handler: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>>;
): CloudFunction<BillingEvent<PlanUpdatePayload>>;
export function onPlanUpdatePublished(
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>),
handler?: (event: BillingEvent<PlanUpdatePayload>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanUpdatePayload>> {
): CloudFunction<BillingEvent<PlanUpdatePayload>> {
return onOperation<PlanUpdatePayload>(
planUpdateAlert,
optsOrHandler,
Expand All @@ -72,21 +71,21 @@ export function onPlanAutomatedUpdatePublished(
handler: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
): CloudFunction<BillingEvent<PlanAutomatedUpdatePayload>>;
export function onPlanAutomatedUpdatePublished(
opts: options.EventHandlerOptions,
handler: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>>;
): CloudFunction<BillingEvent<PlanAutomatedUpdatePayload>>;
export function onPlanAutomatedUpdatePublished(
optsOrHandler:
| options.EventHandlerOptions
| ((event: BillingEvent<PlanAutomatedUpdatePayload>) => any | Promise<any>),
handler?: (
event: BillingEvent<PlanAutomatedUpdatePayload>
) => any | Promise<any>
): CloudFunction<FirebaseAlertData<PlanAutomatedUpdatePayload>> {
): CloudFunction<BillingEvent<PlanAutomatedUpdatePayload>> {
return onOperation<PlanAutomatedUpdatePayload>(
planAutomatedUpdateAlert,
optsOrHandler,
Expand All @@ -101,7 +100,7 @@ export function onOperation<T>(
| options.EventHandlerOptions
| ((event: BillingEvent<T>) => any | Promise<any>),
handler: (event: BillingEvent<T>) => any | Promise<any>
): CloudFunction<FirebaseAlertData<T>> {
): CloudFunction<BillingEvent<T>> {
if (typeof optsOrHandler === 'function') {
handler = optsOrHandler as (event: BillingEvent<T>) => any | Promise<any>;
optsOrHandler = {};
Expand Down