21
21
// SOFTWARE.
22
22
23
23
import { CloudEvent , CloudFunction } from "../core" ;
24
- import { ParamsOf } from "../../common/params" ;
24
+ import { ParamsOf , VarName } from "../../common/params" ;
25
25
import { EventHandlerOptions , getGlobalOptions , optionsToEndpoint } from "../options" ;
26
26
import { normalizePath } from "../../common/utilities/path" ;
27
27
import { wrapTraceContext } from "../trace" ;
@@ -88,16 +88,23 @@ export interface RawDataConnectEvent<T> extends CloudEvent<T> {
88
88
export type AuthType = "app_user" | "admin" | "unknown" ;
89
89
90
90
/** 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 ;
98
98
}
99
99
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 > {
101
108
/** The location of the Firebase Data Connect instance */
102
109
location : string ;
103
110
/** The project identifier */
@@ -126,9 +133,9 @@ export function onMutationExecuted<
126
133
> (
127
134
mutation : Mutation ,
128
135
handler : (
129
- event : DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > >
136
+ event : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Mutation > >
130
137
) => unknown | Promise < unknown >
131
- ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > > > ;
138
+ ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Mutation > > > ;
132
139
133
140
/**
134
141
* Event handler that triggers when a mutation is executed in Firebase Data Connect.
@@ -137,15 +144,15 @@ export function onMutationExecuted<
137
144
* @param handler - Event handler which is run every time a mutation is executed.
138
145
*/
139
146
export function onMutationExecuted <
140
- Mutation extends string ,
147
+ Options extends OperationOptions ,
141
148
Variables = unknown ,
142
149
ResponseData = unknown
143
150
> (
144
- opts : OperationOptions ,
151
+ opts : Options ,
145
152
handler : (
146
- event : DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > >
153
+ event : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Options > >
147
154
) => unknown | Promise < unknown >
148
- ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > > > ;
155
+ ) : CloudFunction < DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < Options > > > ;
149
156
150
157
/**
151
158
* Event handler that triggers when a mutation is executed in Firebase Data Connect.
@@ -154,16 +161,16 @@ export function onMutationExecuted<
154
161
* @param handler - Event handler which is run every time a mutation is executed.
155
162
*/
156
163
export function onMutationExecuted <
157
- Mutation extends string ,
164
+ PathPatternOrOptions extends string | OperationOptions < string , string , string > ,
158
165
Variables = unknown ,
159
166
ResponseData = unknown
160
167
> (
161
- mutationOrOpts : Mutation | OperationOptions ,
168
+ mutationOrOpts : PathPatternOrOptions ,
162
169
handler : (
163
- event : DataConnectEvent < MutationEventData < Variables , ResponseData > , ParamsOf < Mutation > >
170
+ event : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < PathPatternOrOptions > >
164
171
) => 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 ) ;
167
174
}
168
175
169
176
function getOpts ( mutationOrOpts : string | OperationOptions ) {
@@ -257,11 +264,11 @@ function makeParams<V, R>(
257
264
} ;
258
265
}
259
266
260
- function onOperation < V , R > (
267
+ function onOperation < Variables , ResponseData , PathPatternOrOptions > (
261
268
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 > > > {
265
272
const { service, connector, operation, opts } = getOpts ( mutationOrOpts ) ;
266
273
267
274
const servicePattern = new PathPattern ( service ) ;
@@ -270,14 +277,14 @@ function onOperation<V, R>(
270
277
271
278
// wrap the handler
272
279
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 ) ;
275
282
276
- const dataConnectEvent : DataConnectEvent < MutationEventData < V , R > > = {
283
+ const dataConnectEvent : DataConnectEvent < MutationEventData < Variables , ResponseData > , DataConnectParams < PathPatternOrOptions > > = {
277
284
...event ,
278
285
authType : event . authtype ,
279
286
authId : event . authid ,
280
- params,
287
+ params : params as DataConnectParams < PathPatternOrOptions > ,
281
288
} ;
282
289
delete ( dataConnectEvent as any ) . authtype ;
283
290
delete ( dataConnectEvent as any ) . authid ;
0 commit comments