@@ -22,7 +22,10 @@ const SIM_WEBHOOK_TOKEN_HEADER = 'x-sim-webhook-token'
2222export const instantlyHandler : WebhookProviderHandler = {
2323 verifyAuth ( { request, requestId, providerConfig } : AuthContext ) : NextResponse | null {
2424 const secretToken = providerConfig . secretToken as string | undefined
25- if ( ! secretToken ) return null
25+ if ( ! secretToken ) {
26+ logger . warn ( `[${ requestId } ] Instantly webhook secret token is missing` )
27+ return new NextResponse ( 'Unauthorized' , { status : 401 } )
28+ }
2629
2730 if ( ! verifyTokenAuth ( request , secretToken , SIM_WEBHOOK_TOKEN_HEADER ) ) {
2831 logger . warn ( `[${ requestId } ] Unauthorized Instantly webhook request` )
@@ -85,9 +88,9 @@ export const instantlyHandler: WebhookProviderHandler = {
8588 async createSubscription ( ctx : SubscriptionContext ) : Promise < SubscriptionResult | undefined > {
8689 const { webhook, requestId } = ctx
8790 const providerConfig = getProviderConfig ( webhook )
88- const apiKey = providerConfig . apiKey as string | undefined
91+ const apiKey = providerConfig . triggerApiKey as string | undefined
8992 const triggerId = providerConfig . triggerId as string | undefined
90- const campaignId = providerConfig . triggerCampaignId as string | undefined
93+ const campaignId = optionalId ( providerConfig . triggerCampaignId )
9194
9295 if ( ! apiKey ?. trim ( ) ) {
9396 throw new Error ( 'Instantly API Key is required.' )
@@ -119,8 +122,8 @@ export const instantlyHandler: WebhookProviderHandler = {
119122 } ,
120123 }
121124
122- if ( campaignId ?. trim ( ) ) {
123- requestBody . campaign = campaignId . trim ( )
125+ if ( campaignId ) {
126+ requestBody . campaign = campaignId
124127 }
125128
126129 logger . info ( `[${ requestId } ] Creating Instantly webhook` , {
@@ -133,7 +136,7 @@ export const instantlyHandler: WebhookProviderHandler = {
133136 const response = await fetch ( instantlyUrl ( '/api/v2/webhooks' ) , {
134137 method : 'POST' ,
135138 headers : {
136- Authorization : `Bearer ${ apiKey } ` ,
139+ Authorization : `Bearer ${ apiKey . trim ( ) } ` ,
137140 'Content-Type' : 'application/json' ,
138141 } ,
139142 body : JSON . stringify ( requestBody ) ,
@@ -179,7 +182,7 @@ export const instantlyHandler: WebhookProviderHandler = {
179182
180183 try {
181184 const providerConfig = getProviderConfig ( webhook )
182- const apiKey = providerConfig . apiKey as string | undefined
185+ const apiKey = providerConfig . triggerApiKey as string | undefined
183186 const externalId = providerConfig . externalId as string | undefined
184187
185188 if ( ! apiKey ?. trim ( ) || ! externalId ?. trim ( ) ) {
@@ -193,11 +196,11 @@ export const instantlyHandler: WebhookProviderHandler = {
193196 }
194197
195198 const response = await fetch (
196- instantlyUrl ( `/api/v2/webhooks/${ encodeURIComponent ( externalId ) } ` ) ,
199+ instantlyUrl ( `/api/v2/webhooks/${ encodeURIComponent ( externalId . trim ( ) ) } ` ) ,
197200 {
198201 method : 'DELETE' ,
199202 headers : {
200- Authorization : `Bearer ${ apiKey } ` ,
203+ Authorization : `Bearer ${ apiKey . trim ( ) } ` ,
201204 } ,
202205 }
203206 )
@@ -254,6 +257,13 @@ function toBooleanOrNull(value: unknown): boolean | null {
254257 return typeof value === 'boolean' ? value : null
255258}
256259
260+ function optionalId ( value : unknown ) : string | undefined {
261+ if ( typeof value !== 'string' ) return undefined
262+ const trimmed = value . trim ( )
263+ if ( trimmed === '' || trimmed === '-' ) return undefined
264+ return trimmed
265+ }
266+
257267function isRecord ( value : unknown ) : value is Record < string , unknown > {
258268 return Boolean ( value ) && typeof value === 'object' && ! Array . isArray ( value )
259269}
0 commit comments