Skip to content

Commit 4efce55

Browse files
committed
refactor: create and export function handler types
1 parent 435f0f8 commit 4efce55

28 files changed

Lines changed: 689 additions & 605 deletions

src/v1/providers/analytics.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export const provider = "google.analytics";
3030
/** @internal */
3131
export const service = "app-measurement.com";
3232

33+
/** Handler used by {@link AnalyticsEventBuilder.onLog}. */
34+
export type OnLogHandler = (event: AnalyticsEvent, context: EventContext) => PromiseLike<any> | any;
35+
3336
/**
3437
* Registers a function to handle analytics events.
3538
*
@@ -75,9 +78,7 @@ export class AnalyticsEventBuilder {
7578
*
7679
* @returns A function that you can export and deploy.
7780
*/
78-
onLog(
79-
handler: (event: AnalyticsEvent, context: EventContext) => PromiseLike<any> | any
80-
): CloudFunction<AnalyticsEvent> {
81+
onLog(handler: OnLogHandler): CloudFunction<AnalyticsEvent> {
8182
const dataConstructor = (raw: LegacyEvent) => {
8283
return new AnalyticsEvent(raw.data);
8384
};

src/v1/providers/auth.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ export type { UserRecord, UserInfo };
5555

5656
export { HttpsError };
5757

58+
/** Handler used by {@link UserBuilder.onCreate}. */
59+
export type OnCreateHandler = (user: UserRecord, context: EventContext) => PromiseLike<any> | any;
60+
61+
/** Handler used by {@link UserBuilder.onDelete}. */
62+
export type OnDeleteHandler = OnCreateHandler;
63+
64+
/** Handler used by {@link UserBuilder.beforeCreate}. */
65+
export type BeforeCreateHandler = (
66+
user: AuthUserRecord,
67+
context: AuthEventContext
68+
) => MaybeAsync<BeforeCreateResponse | void>;
69+
70+
/** Handler used by {@link UserBuilder.beforeSignIn}. */
71+
export type BeforeSignInHandler = (
72+
user: AuthUserRecord,
73+
context: AuthEventContext
74+
) => MaybeAsync<BeforeSignInResponse | void>;
75+
76+
/** Handler used by {@link UserBuilder.beforeEmail}. */
77+
export type BeforeEmailHandler = (
78+
context: AuthEventContext
79+
) => MaybeAsync<BeforeEmailResponse | void>;
80+
81+
/** Handler used by {@link UserBuilder.beforeSms}. */
82+
export type BeforeSmsHandler = (context: AuthEventContext) => MaybeAsync<BeforeSmsResponse | void>;
83+
5884
/** @internal */
5985
export const provider = "google.firebase.auth";
6086
/** @internal */
@@ -126,9 +152,7 @@ export class UserBuilder {
126152
*
127153
* @public
128154
*/
129-
onCreate(
130-
handler: (user: UserRecord, context: EventContext) => PromiseLike<any> | any
131-
): CloudFunction<UserRecord> {
155+
onCreate(handler: OnCreateHandler): CloudFunction<UserRecord> {
132156
return this.onOperation(handler, "user.create");
133157
}
134158

@@ -139,9 +163,7 @@ export class UserBuilder {
139163
*
140164
* @public
141165
*/
142-
onDelete(
143-
handler: (user: UserRecord, context: EventContext) => PromiseLike<any> | any
144-
): CloudFunction<UserRecord> {
166+
onDelete(handler: OnDeleteHandler): CloudFunction<UserRecord> {
145167
return this.onOperation(handler, "user.delete");
146168
}
147169

@@ -152,12 +174,7 @@ export class UserBuilder {
152174
*
153175
* @public
154176
*/
155-
beforeCreate(
156-
handler: (
157-
user: AuthUserRecord,
158-
context: AuthEventContext
159-
) => MaybeAsync<BeforeCreateResponse | void>
160-
): BlockingFunction {
177+
beforeCreate(handler: BeforeCreateHandler): BlockingFunction {
161178
return this.beforeOperation(handler, "beforeCreate");
162179
}
163180

@@ -168,24 +185,15 @@ export class UserBuilder {
168185
*
169186
* @public
170187
*/
171-
beforeSignIn(
172-
handler: (
173-
user: AuthUserRecord,
174-
context: AuthEventContext
175-
) => MaybeAsync<BeforeSignInResponse | void>
176-
): BlockingFunction {
188+
beforeSignIn(handler: BeforeSignInHandler): BlockingFunction {
177189
return this.beforeOperation(handler, "beforeSignIn");
178190
}
179191

180-
beforeEmail(
181-
handler: (context: AuthEventContext) => MaybeAsync<BeforeEmailResponse | void>
182-
): BlockingFunction {
192+
beforeEmail(handler: BeforeEmailHandler): BlockingFunction {
183193
return this.beforeOperation(handler, "beforeSendEmail");
184194
}
185195

186-
beforeSms(
187-
handler: (context: AuthEventContext) => MaybeAsync<BeforeSmsResponse | void>
188-
): BlockingFunction {
196+
beforeSms(handler: BeforeSmsHandler): BlockingFunction {
189197
return this.beforeOperation(handler, "beforeSendSms");
190198
}
191199

src/v1/providers/database.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ import { expr } from "../../params";
3434

3535
export { DataSnapshot };
3636

37+
/** Handler used by {@link RefBuilder.onWrite}. */
38+
export type OnWriteHandler<Ref extends string = string> = (
39+
change: Change<DataSnapshot>,
40+
context: EventContext<ParamsOf<Ref>>
41+
) => PromiseLike<any> | any;
42+
43+
/** Handler used by {@link RefBuilder.onUpdate}. */
44+
export type OnUpdateHandler<Ref extends string = string> = OnWriteHandler<Ref>;
45+
46+
/** Handler used by {@link RefBuilder.onCreate}. */
47+
export type OnCreateHandler<Ref extends string = string> = (
48+
snapshot: DataSnapshot,
49+
context: EventContext<ParamsOf<Ref>>
50+
) => PromiseLike<any> | any;
51+
52+
/** Handler used by {@link RefBuilder.onDelete}. */
53+
export type OnDeleteHandler<Ref extends string = string> = OnCreateHandler<Ref>;
54+
3755
/** @internal */
3856
export const provider = "google.firebase.database";
3957
/** @internal */
@@ -182,12 +200,7 @@ export class RefBuilder<Ref extends string> {
182200
* write occurs.
183201
* @returns A function that you can export and deploy.
184202
*/
185-
onWrite(
186-
handler: (
187-
change: Change<DataSnapshot>,
188-
context: EventContext<ParamsOf<Ref>>
189-
) => PromiseLike<any> | any
190-
): CloudFunction<Change<DataSnapshot>> {
203+
onWrite(handler: OnWriteHandler<Ref>): CloudFunction<Change<DataSnapshot>> {
191204
return this.onOperation(handler, "ref.write", this.changeConstructor);
192205
}
193206

@@ -199,12 +212,7 @@ export class RefBuilder<Ref extends string> {
199212
* write occurs.
200213
* @returns A function which you can export and deploy.
201214
*/
202-
onUpdate(
203-
handler: (
204-
change: Change<DataSnapshot>,
205-
context: EventContext<ParamsOf<Ref>>
206-
) => PromiseLike<any> | any
207-
): CloudFunction<Change<DataSnapshot>> {
215+
onUpdate(handler: OnUpdateHandler<Ref>): CloudFunction<Change<DataSnapshot>> {
208216
return this.onOperation(handler, "ref.update", this.changeConstructor);
209217
}
210218

@@ -216,12 +224,7 @@ export class RefBuilder<Ref extends string> {
216224
* Firebase Realtime Database.
217225
* @returns A function that you can export and deploy.
218226
*/
219-
onCreate(
220-
handler: (
221-
snapshot: DataSnapshot,
222-
context: EventContext<ParamsOf<Ref>>
223-
) => PromiseLike<any> | any
224-
): CloudFunction<DataSnapshot> {
227+
onCreate(handler: OnCreateHandler<Ref>): CloudFunction<DataSnapshot> {
225228
const dataConstructor = (raw: LegacyEvent) => {
226229
const [dbInstance, path] = extractInstanceAndPath(
227230
raw.context.resource.name,
@@ -240,12 +243,7 @@ export class RefBuilder<Ref extends string> {
240243
* Firebase Realtime Database.
241244
* @returns A function that you can export and deploy.
242245
*/
243-
onDelete(
244-
handler: (
245-
snapshot: DataSnapshot,
246-
context: EventContext<ParamsOf<Ref>>
247-
) => PromiseLike<any> | any
248-
): CloudFunction<DataSnapshot> {
246+
onDelete(handler: OnDeleteHandler<Ref>): CloudFunction<DataSnapshot> {
249247
const dataConstructor = (raw: LegacyEvent) => {
250248
const [dbInstance, path] = extractInstanceAndPath(
251249
raw.context.resource.name,

src/v1/providers/firestore.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ export const defaultDatabase = "(default)";
4444
export type DocumentSnapshot = firestore.DocumentSnapshot;
4545
export type QueryDocumentSnapshot = firestore.QueryDocumentSnapshot;
4646

47+
/** Handler used by {@link DocumentBuilder.onWrite}. */
48+
export type OnWriteHandler<Path extends string = string> = (
49+
change: Change<DocumentSnapshot>,
50+
context: EventContext<ParamsOf<Path>>
51+
) => PromiseLike<any> | any;
52+
53+
/** Handler used by {@link DocumentBuilder.onUpdate}. */
54+
export type OnUpdateHandler<Path extends string = string> = (
55+
change: Change<QueryDocumentSnapshot>,
56+
context: EventContext<ParamsOf<Path>>
57+
) => PromiseLike<any> | any;
58+
59+
/** Handler used by {@link DocumentBuilder.onCreate}. */
60+
export type OnCreateHandler<Path extends string = string> = (
61+
snapshot: QueryDocumentSnapshot,
62+
context: EventContext<ParamsOf<Path>>
63+
) => PromiseLike<any> | any;
64+
65+
/** Handler used by {@link DocumentBuilder.onDelete}. */
66+
export type OnDeleteHandler<Path extends string = string> = OnCreateHandler<Path>;
67+
4768
/**
4869
* Select the Firestore document to listen to for events.
4970
* @param path Full database path to listen to. This includes the name of
@@ -166,42 +187,22 @@ export class DocumentBuilder<Path extends string> {
166187
}
167188

168189
/** Respond to all document writes (creates, updates, or deletes). */
169-
onWrite(
170-
handler: (
171-
change: Change<DocumentSnapshot>,
172-
context: EventContext<ParamsOf<Path>>
173-
) => PromiseLike<any> | any
174-
): CloudFunction<Change<DocumentSnapshot>> {
190+
onWrite(handler: OnWriteHandler<Path>): CloudFunction<Change<DocumentSnapshot>> {
175191
return this.onOperation(handler, "document.write", changeConstructor);
176192
}
177193

178194
/** Respond only to document updates. */
179-
onUpdate(
180-
handler: (
181-
change: Change<QueryDocumentSnapshot>,
182-
context: EventContext<ParamsOf<Path>>
183-
) => PromiseLike<any> | any
184-
): CloudFunction<Change<QueryDocumentSnapshot>> {
195+
onUpdate(handler: OnUpdateHandler<Path>): CloudFunction<Change<QueryDocumentSnapshot>> {
185196
return this.onOperation(handler, "document.update", changeConstructor);
186197
}
187198

188199
/** Respond only to document creations. */
189-
onCreate(
190-
handler: (
191-
snapshot: QueryDocumentSnapshot,
192-
context: EventContext<ParamsOf<Path>>
193-
) => PromiseLike<any> | any
194-
): CloudFunction<QueryDocumentSnapshot> {
200+
onCreate(handler: OnCreateHandler<Path>): CloudFunction<QueryDocumentSnapshot> {
195201
return this.onOperation(handler, "document.create", snapshotConstructor);
196202
}
197203

198204
/** Respond only to document deletions. */
199-
onDelete(
200-
handler: (
201-
snapshot: QueryDocumentSnapshot,
202-
context: EventContext<ParamsOf<Path>>
203-
) => PromiseLike<any> | any
204-
): CloudFunction<QueryDocumentSnapshot> {
205+
onDelete(handler: OnDeleteHandler<Path>): CloudFunction<QueryDocumentSnapshot> {
205206
return this.onOperation(handler, "document.delete", beforeSnapshotConstructor);
206207
}
207208

src/v1/providers/https.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import * as express from "express";
23+
import type * as express from "express";
2424

2525
import { convertIfPresent, convertInvoker } from "../../common/encoding";
2626
import {
@@ -40,30 +40,32 @@ import { wrapTraceContext } from "../../v2/trace";
4040
export { HttpsError };
4141
export type { Request, CallableContext, FunctionsErrorCode };
4242

43+
/** Handler used by {@link onRequest}. */
44+
export type OnRequestHandler = (req: Request, resp: express.Response) => void | Promise<void>;
45+
46+
/** Handler used by {@link onCall}. */
47+
export type OnCallHandler = (data: any, context: CallableContext) => any | Promise<any>;
48+
4349
/**
4450
* Handle HTTP requests.
4551
* @param handler A function that takes a request and response object,
4652
* same signature as an Express app.
4753
*/
48-
export function onRequest(
49-
handler: (req: Request, resp: express.Response) => void | Promise<void>
50-
): HttpsFunction {
54+
export function onRequest(handler: OnRequestHandler): HttpsFunction {
5155
return _onRequestWithOptions(handler, {});
5256
}
5357

5458
/**
5559
* Declares a callable method for clients to call using a Firebase SDK.
5660
* @param handler A method that takes a data and context and returns a value.
5761
*/
58-
export function onCall(
59-
handler: (data: any, context: CallableContext) => any | Promise<any>
60-
): HttpsFunction & Runnable<any> {
62+
export function onCall(handler: OnCallHandler): HttpsFunction & Runnable<any> {
6163
return _onCallWithOptions(handler, {});
6264
}
6365

6466
/** @internal */
6567
export function _onRequestWithOptions(
66-
handler: (req: Request, resp: express.Response) => void | Promise<void>,
68+
handler: OnRequestHandler,
6769
options: DeploymentOptions
6870
): HttpsFunction {
6971
// lets us add __endpoint without altering handler:
@@ -101,7 +103,7 @@ export function _onRequestWithOptions(
101103

102104
/** @internal */
103105
export function _onCallWithOptions(
104-
handler: (data: any, context: CallableContext) => any | Promise<any>,
106+
handler: OnCallHandler,
105107
options: DeploymentOptions
106108
): HttpsFunction & Runnable<any> {
107109
// fix the length of handler to make the call to handler consistent

src/v1/providers/pubsub.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export const provider = "google.pubsub";
3030
/** @internal */
3131
export const service = "pubsub.googleapis.com";
3232

33+
/** Handler used by {@link TopicBuilder.onPublish}. */
34+
export type OnPublishHandler = (message: Message, context: EventContext) => PromiseLike<any> | any;
35+
36+
/** Handler used by {@link ScheduleBuilder.onRun}. */
37+
export type OnRunHandler = (context: EventContext) => PromiseLike<any> | any;
38+
3339
/**
3440
* Registers a Cloud Function triggered when a Google Cloud Pub/Sub message
3541
* is sent to a specified topic.
@@ -79,9 +85,7 @@ export class TopicBuilder {
7985
* is published.
8086
* @returns A function that you can export and deploy.
8187
*/
82-
onPublish(
83-
handler: (message: Message, context: EventContext) => PromiseLike<any> | any
84-
): CloudFunction<Message> {
88+
onPublish(handler: OnPublishHandler): CloudFunction<Message> {
8589
return makeCloudFunction({
8690
handler,
8791
provider,
@@ -156,7 +160,7 @@ export class ScheduleBuilder {
156160
* scheduler job sends a Pub/Sub message.
157161
* @returns A function that you can export and deploy.
158162
*/
159-
onRun(handler: (context: EventContext) => PromiseLike<any> | any) {
163+
onRun(handler: OnRunHandler) {
160164
const cloudFunction = makeCloudFunction({
161165
contextOnlyHandler: handler,
162166
provider,

0 commit comments

Comments
 (0)