@@ -107,40 +107,30 @@ export type Trigger<
107107 S extends TriggerStrategy = TriggerStrategy ,
108108> = ITrigger < S , PieceAuth , TriggerProps >
109109
110+ // TODO refactor and extract common logic
110111export const createTrigger = <
111112 TS extends TriggerStrategy ,
112113 PieceAuth extends PieceAuthProperty ,
113114 TriggerProps extends InputPropertyMap ,
114115> ( params : CreateTriggerParams < PieceAuth , TriggerProps , TS > ) => {
115- // Extract common defaults to reduce duplication
116- const commonDefaults = {
117- requireAuth : params . requireAuth ?? true ,
118- onStart : params . onStart ?? ( async ( ) => Promise . resolve ( ) ) ,
119- test : params . test ?? ( ( ) => Promise . resolve ( [ params . sampleData ] ) ) ,
120- handshakeNone : { strategy : WebhookHandshakeStrategy . NONE } as const ,
121- renewNone : { strategy : WebhookRenewStrategy . NONE } as const ,
122- defaultWebhookResponse : async ( ) => ( { status : 200 } ) ,
123- defaultOnRenew : async ( ) => Promise . resolve ( ) ,
124- }
125-
126116 switch ( params . type ) {
127117 case TriggerStrategy . WEBHOOK :
128118 return new ITrigger (
129119 params . name ,
130120 params . displayName ,
131121 params . description ,
132- commonDefaults . requireAuth ,
122+ params . requireAuth ?? true ,
133123 params . props ,
134124 params . type ,
135- params . handshakeConfiguration ?? commonDefaults . handshakeNone ,
136- params . onHandshake ?? commonDefaults . defaultWebhookResponse ,
137- params . renewConfiguration ?? commonDefaults . renewNone ,
138- params . onRenew ?? commonDefaults . defaultOnRenew ,
125+ params . handshakeConfiguration ?? { strategy : WebhookHandshakeStrategy . NONE } ,
126+ params . onHandshake ?? ( async ( ) => ( { status : 200 } ) ) ,
127+ params . renewConfiguration ?? { strategy : WebhookRenewStrategy . NONE } ,
128+ params . onRenew ?? ( async ( ) => Promise . resolve ( ) ) ,
139129 params . onEnable ,
140130 params . onDisable ,
141- commonDefaults . onStart ,
131+ params . onStart ?? ( async ( ) => Promise . resolve ( ) ) ,
142132 params . run ,
143- commonDefaults . test ,
133+ params . test ?? ( ( ) => Promise . resolve ( [ params . sampleData ] ) ) ,
144134 params . sampleData ,
145135 params . test ? TriggerTestStrategy . TEST_FUNCTION : TriggerTestStrategy . SIMULATION ,
146136 )
@@ -149,18 +139,18 @@ export const createTrigger = <
149139 params . name ,
150140 params . displayName ,
151141 params . description ,
152- commonDefaults . requireAuth ,
142+ params . requireAuth ?? true ,
153143 params . props ,
154144 params . type ,
155- commonDefaults . handshakeNone ,
156- commonDefaults . defaultWebhookResponse ,
157- commonDefaults . renewNone ,
158- commonDefaults . defaultOnRenew ,
145+ { strategy : WebhookHandshakeStrategy . NONE } ,
146+ async ( ) => ( { status : 200 } ) ,
147+ { strategy : WebhookRenewStrategy . NONE } ,
148+ ( async ( ) => Promise . resolve ( ) ) ,
159149 params . onEnable ,
160150 params . onDisable ,
161- commonDefaults . onStart ,
151+ params . onStart ?? ( async ( ) => Promise . resolve ( ) ) ,
162152 params . run ,
163- commonDefaults . test ,
153+ params . test ?? ( ( ) => Promise . resolve ( [ params . sampleData ] ) ) ,
164154 params . sampleData ,
165155 TriggerTestStrategy . TEST_FUNCTION ,
166156 )
@@ -169,18 +159,18 @@ export const createTrigger = <
169159 params . name ,
170160 params . displayName ,
171161 params . description ,
172- commonDefaults . requireAuth ,
162+ params . requireAuth ?? true ,
173163 params . props ,
174164 params . type ,
175- commonDefaults . handshakeNone ,
176- commonDefaults . defaultWebhookResponse ,
177- commonDefaults . renewNone ,
178- commonDefaults . defaultOnRenew ,
165+ { strategy : WebhookHandshakeStrategy . NONE } ,
166+ async ( ) => ( { status : 200 } ) ,
167+ { strategy : WebhookRenewStrategy . NONE } ,
168+ ( async ( ) => Promise . resolve ( ) ) ,
179169 params . onEnable ,
180170 params . onDisable ,
181- commonDefaults . onStart ,
171+ params . onStart ?? ( async ( ) => Promise . resolve ( ) ) ,
182172 params . run ,
183- commonDefaults . test ,
173+ params . test ?? ( ( ) => Promise . resolve ( [ params . sampleData ] ) ) ,
184174 params . sampleData ,
185175 ( isNil ( params . sampleData ) && isNil ( params . test ) ) ? TriggerTestStrategy . SIMULATION : TriggerTestStrategy . TEST_FUNCTION ,
186176 )
0 commit comments