2121// SOFTWARE.
2222
2323import { CloudEvent , CloudFunction } from "../core" ;
24- import { ParamsOf } from "../../common/params" ;
24+ import { ParamsOf , VarName } from "../../common/params" ;
2525import { EventHandlerOptions , getGlobalOptions , optionsToEndpoint } from "../options" ;
2626import { normalizePath } from "../../common/utilities/path" ;
2727import { wrapTraceContext } from "../trace" ;
@@ -88,16 +88,23 @@ export interface RawDataConnectEvent<T> extends CloudEvent<T> {
8888export type AuthType = "app_user" | "admin" | "unknown" ;
8989
9090/** OperationOptions extend EventHandlerOptions with a provided service, connector, and operation. */
91- export interface OperationOptions extends EventHandlerOptions {
92- /** Firebase Data Connect service ID */
93- service : string ;
94- /** Firebase Data Connect connector ID */
95- connector : string ;
96- /** Name of the operation */
97- operation : string ;
91+ export interface OperationOptions < Service extends string = string , Connector extends string = string , Operation extends string = string > extends EventHandlerOptions {
92+ /** Firebase Data Connect service ID */
93+ service ?: Service ;
94+ /** Firebase Data Connect connector ID */
95+ connector ?: Connector ;
96+ /** Name of the operation */
97+ operation ?: Operation ;
9898}
9999
100- export interface DataConnectEvent < T , Params = Record < string , string > > extends CloudEvent < T > {
100+ export type DataConnectParams < PathPatternOrOptions extends string | OperationOptions > =
101+ PathPatternOrOptions extends string
102+ ? ParamsOf < PathPatternOrOptions >
103+ : PathPatternOrOptions extends OperationOptions < infer Service extends string , infer Connector extends string , infer Operation extends string >
104+ ? Record < VarName < Service > | VarName < Connector > | VarName < Operation > , string >
105+ : never ;
106+
107+ export interface DataConnectEvent < T , Params extends Record < never , string > > extends CloudEvent < T > {
101108 /** The location of the Firebase Data Connect instance */
102109 location : string ;
103110 /** The project identifier */
@@ -126,9 +133,9 @@ export function onMutationExecuted<
126133> (
127134 mutation : Mutation ,
128135 handler : (
129- event : DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > >
136+ event : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Mutation > >
130137 ) => unknown | Promise < unknown >
131- ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > > > ;
138+ ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Mutation > > > ;
132139
133140/**
134141 * Event handler that triggers when a mutation is executed in Firebase Data Connect.
@@ -137,15 +144,15 @@ export function onMutationExecuted<
137144 * @param handler - Event handler which is run every time a mutation is executed.
138145 */
139146export function onMutationExecuted <
140- Mutation extends string ,
147+ Options extends OperationOptions ,
141148 Variables = unknown ,
142149 ResponseData = unknown
143150> (
144- opts : OperationOptions ,
151+ opts : Options ,
145152 handler : (
146- event : DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > >
153+ event : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Options > >
147154 ) => unknown | Promise < unknown >
148- ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > > > ;
155+ ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Options > > > ;
149156
150157/**
151158 * Event handler that triggers when a mutation is executed in Firebase Data Connect.
@@ -154,16 +161,16 @@ export function onMutationExecuted<
154161 * @param handler - Event handler which is run every time a mutation is executed.
155162 */
156163export function onMutationExecuted <
157- Mutation extends string ,
164+ PathPatternOrOptions extends string | OperationOptions < string , string , string > ,
158165 Variables = unknown ,
159166 ResponseData = unknown
160167> (
161- mutationOrOpts : Mutation | OperationOptions ,
168+ mutationOrOpts : PathPatternOrOptions ,
162169 handler : (
163- event : DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > >
170+ event : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < PathPatternOrOptions > >
164171 ) => unknown | Promise < unknown >
165- ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > > > {
166- return onOperation < Variables , ResponseData > ( mutationExecutedEventType , mutationOrOpts , handler ) ;
172+ ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < PathPatternOrOptions > > > {
173+ return onOperation < Variables , ResponseData , PathPatternOrOptions > ( mutationExecutedEventType , mutationOrOpts , handler ) ;
167174}
168175
169176function getOpts ( mutationOrOpts : string | OperationOptions ) {
@@ -257,11 +264,11 @@ function makeParams<V, R>(
257264 } ;
258265}
259266
260- function onOperation < V , R > (
267+ function onOperation < Variables , ResponseData , PathPatternOrOptions > (
261268 eventType : string ,
262- mutationOrOpts : string | OperationOptions ,
263- handler : ( event : DataConnectEvent < MutationEventData < V , R > > ) => any | Promise < any >
264- ) : CloudFunction < DataConnectEvent < MutationEventData < V , R > > > {
269+ mutationOrOpts : PathPatternOrOptions ,
270+ handler : ( event : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < PathPatternOrOptions > > ) => any | Promise < any >
271+ ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < PathPatternOrOptions > > > {
265272 const { service, connector, operation, opts } = getOpts ( mutationOrOpts ) ;
266273
267274 const servicePattern = new PathPattern ( service ) ;
@@ -270,14 +277,14 @@ function onOperation<V, R>(
270277
271278 // wrap the handler
272279 const func = ( raw : CloudEvent < unknown > ) => {
273- const event = raw as RawDataConnectEvent < MutationEventData < V , R > > ;
274- const params = makeParams < V , R > ( event , servicePattern , connectorPattern , operationPattern ) ;
280+ const event = raw as RawDataConnectEvent < MutationEventData < Variables , ResponseData > > ;
281+ const params = makeParams < Variables , ResponseData > ( event , servicePattern , connectorPattern , operationPattern ) ;
275282
276- const dataConnectEvent : DataConnectEvent < MutationEventData < V , R > > = {
283+ const dataConnectEvent : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < PathPatternOrOptions > > = {
277284 ...event ,
278285 authType : event . authtype ,
279286 authId : event . authid ,
280- params,
287+ params : params as DataConnectParams < PathPatternOrOptions > ,
281288 } ;
282289 delete ( dataConnectEvent as any ) . authtype ;
283290 delete ( dataConnectEvent as any ) . authid ;
0 commit comments