@@ -15,19 +15,43 @@ export const revalidate = 0
1515 * GET /api/templates/approved/sanitized
1616 * Returns all approved templates with their sanitized JSONs, names, and descriptions
1717 * Requires internal API secret authentication via X-API-Key header
18+ *
19+ * Example usage:
20+ * curl -X GET https://your-domain.com/api/templates/approved/sanitized \
21+ * -H "X-API-Key: your_internal_api_secret"
1822 */
1923export async function GET ( request : NextRequest ) {
2024 const requestId = generateRequestId ( )
2125
2226 try {
27+ // Log incoming request details
28+ const url = new URL ( request . url )
29+ const hasApiKey = ! ! request . headers . get ( 'x-api-key' )
30+
31+ logger . info ( `[${ requestId } ] Incoming request to /api/templates/approved/sanitized` , {
32+ method : request . method ,
33+ url : url . pathname ,
34+ fullUrl : url . toString ( ) ,
35+ hasApiKey,
36+ userAgent : request . headers . get ( 'user-agent' ) ,
37+ origin : request . headers . get ( 'origin' ) ,
38+ } )
39+
2340 // Check internal API key authentication
2441 const authResult = checkInternalApiKey ( request )
2542 if ( ! authResult . success ) {
26- logger . warn ( `[${ requestId } ] Unauthorized access to approved sanitized templates: ${ authResult . error } ` )
27- return NextResponse . json ( { error : authResult . error } , { status : 401 } )
43+ logger . warn ( `[${ requestId } ] Authentication failed for approved sanitized templates` , {
44+ error : authResult . error ,
45+ hasApiKey,
46+ howToUse : 'Add header: X-API-Key: <INTERNAL_API_SECRET>' ,
47+ } )
48+ return NextResponse . json ( {
49+ error : authResult . error ,
50+ hint : 'Include X-API-Key header with INTERNAL_API_SECRET value'
51+ } , { status : 401 } )
2852 }
2953
30- logger . info ( `[${ requestId } ] Fetching all approved templates with sanitized JSON ` )
54+ logger . info ( `[${ requestId } ] Authentication successful, fetching approved templates` )
3155
3256 // Fetch all approved templates
3357 const approvedTemplates = await db
@@ -37,11 +61,15 @@ export async function GET(request: NextRequest) {
3761 details : templates . details ,
3862 state : templates . state ,
3963 tags : templates . tags ,
64+ requiredCredentials : templates . requiredCredentials ,
4065 } )
4166 . from ( templates )
4267 . where ( eq ( templates . status , 'approved' ) )
4368
44- logger . info ( `[${ requestId } ] Found ${ approvedTemplates . length } approved templates` )
69+ logger . info ( `[${ requestId } ] Found ${ approvedTemplates . length } approved templates` , {
70+ templateIds : approvedTemplates . map ( t => t . id ) . slice ( 0 , 5 ) , // Log first 5 IDs
71+ totalCount : approvedTemplates . length ,
72+ } )
4573
4674 // Process each template to sanitize for copilot
4775 const sanitizedTemplates = approvedTemplates . map ( ( template ) => {
@@ -58,6 +86,7 @@ export async function GET(request: NextRequest) {
5886 name : template . name ,
5987 description,
6088 tags : template . tags ,
89+ requiredCredentials : template . requiredCredentials ,
6190 sanitizedJson : copilotSanitized ,
6291 }
6392 } catch ( error ) {
@@ -70,18 +99,47 @@ export async function GET(request: NextRequest) {
7099 } ) . filter ( ( t ) : t is NonNullable < typeof t > => t !== null )
71100
72101 logger . info (
73- `[${ requestId } ] Successfully sanitized ${ sanitizedTemplates . length } templates for copilot`
102+ `[${ requestId } ] Successfully sanitized ${ sanitizedTemplates . length } templates for copilot` ,
103+ {
104+ totalTemplates : sanitizedTemplates . length ,
105+ templateNames : sanitizedTemplates . map ( t => t . name ) . slice ( 0 , 5 ) , // Log first 5 names
106+ }
74107 )
75108
76- return NextResponse . json ( {
109+ const response = {
77110 templates : sanitizedTemplates ,
78111 count : sanitizedTemplates . length ,
112+ }
113+
114+ logger . info ( `[${ requestId } ] Sending response` , {
115+ responseSize : JSON . stringify ( response ) . length ,
116+ templateCount : sanitizedTemplates . length ,
79117 } )
118+
119+ return NextResponse . json ( response )
80120 } catch ( error ) {
81121 logger . error ( `[${ requestId } ] Error fetching approved sanitized templates` , {
82122 error : error instanceof Error ? error . message : String ( error ) ,
123+ stack : error instanceof Error ? error . stack : undefined ,
83124 } )
84- return NextResponse . json ( { error : 'Internal server error' } , { status : 500 } )
125+ return NextResponse . json ( {
126+ error : 'Internal server error' ,
127+ requestId,
128+ } , { status : 500 } )
85129 }
86130}
87131
132+ // Add a helpful OPTIONS handler for CORS preflight
133+ export async function OPTIONS ( request : NextRequest ) {
134+ const requestId = generateRequestId ( )
135+ logger . info ( `[${ requestId } ] OPTIONS request received for /api/templates/approved/sanitized` )
136+
137+ return new NextResponse ( null , {
138+ status : 200 ,
139+ headers : {
140+ 'Access-Control-Allow-Methods' : 'GET, OPTIONS' ,
141+ 'Access-Control-Allow-Headers' : 'X-API-Key, Content-Type' ,
142+ } ,
143+ } )
144+ }
145+
0 commit comments