1+ import { ExtendedRequest } from '@/types'
12import { publish } from '@/events/lib/hydro'
23import { hydroNames } from '@/events/lib/schema'
34import { createLogger } from '@/observability/logger'
@@ -9,7 +10,7 @@ const logger = createLogger(import.meta.url)
910 * Returns null if the request should continue, or an error response object if validation failed
1011 */
1112export async function handleExternalSearchAnalytics (
12- req : any ,
13+ req : ExtendedRequest ,
1314 searchContext : string ,
1415) : Promise < { error : string ; status : number } | null > {
1516 const host = req . headers [ 'x-host' ] || req . headers . host
@@ -73,12 +74,12 @@ export async function handleExternalSearchAnalytics(
7374 version : '1.0.0' ,
7475 created : new Date ( ) . toISOString ( ) ,
7576 hostname : normalizedHost ,
76- path : '' ,
77- search : '' ,
77+ path : req ?. context ?. path || '' ,
78+ search : 'REDACTED ' ,
7879 hash : '' ,
79- path_language : 'en' ,
80- path_version : '' ,
81- path_product : '' ,
80+ path_language : req ?. context ?. language || 'en' ,
81+ path_version : req ?. context ?. version || '' ,
82+ path_product : req ?. context ?. product || '' ,
8283 path_article : '' ,
8384 } ,
8485 search_query : 'REDACTED' ,
@@ -90,7 +91,7 @@ export async function handleExternalSearchAnalytics(
9091 await publish ( analyticsPayload )
9192 } catch ( error ) {
9293 // Don't fail the request if analytics fails
93- console . error ( 'Failed to send search analytics:' , error )
94+ logger . error ( 'Failed to send search analytics:' , { error } )
9495 }
9596
9697 return null
@@ -146,16 +147,12 @@ function stripPort(host: string): string {
146147 return hostname
147148}
148149
149- interface ExternalAPIRequestLike {
150- headers : Record < string , string | undefined >
151- }
152-
153150/**
154151 * Determines if a request is likely from an external API client rather than a browser
155152 * Uses multiple heuristics to detect programmatic vs browser requests
156153 */
157154const userAgentRegex = / ^ ( c u r l | w g e t | p y t h o n - r e q u e s t s | a x i o s | n o d e - f e t c h | G o - h t t p - c l i e n t | o k h t t p ) / i
158- function isExternalAPIRequest ( req : ExternalAPIRequestLike ) : boolean {
155+ function isExternalAPIRequest ( req : ExtendedRequest ) : boolean {
159156 const headers = req . headers
160157
161158 // Browser security headers that APIs typically don't send
0 commit comments