@@ -264,18 +264,31 @@ exports.getQuickbooks = async (req, res) => {
264
264
*/
265
265
exports . getNewYorkTimes = async ( req , res , next ) => {
266
266
const apiKey = process . env . NYT_KEY ;
267
+ const url = `https://api.nytimes.com/svc/books/v3/lists/current/young-adult-hardcover.json?api-key=${ apiKey } ` ;
267
268
try {
268
- const response = await fetch ( `http://api.nytimes.com/svc/books/v2/lists?list-name=young-adult&api-key=${ apiKey } ` ) ;
269
+ const response = await fetch ( url ) ;
270
+ const contentType = response . headers . get ( 'content-type' ) || '' ;
269
271
if ( ! response . ok ) {
270
- const error = await response . json ( ) ;
271
- throw new Error ( `New York Times API - ${ response . status } ${ response . statusText } ${ JSON . stringify ( error . fault ) } ` ) ;
272
+ const bodyText = await response . text ( ) ;
273
+ console . error ( `[NYT API] Error response:\nStatus: ${ response . status } ${ response . statusText } \nHeaders: ${ JSON . stringify ( [ ...response . headers ] ) } \nBody (first 500 chars):\n${ bodyText . slice ( 0 , 500 ) } ` ) ;
274
+ throw new Error ( `New York Times API - ${ response . status } ${ response . statusText } ` ) ;
275
+ }
276
+ if ( ! contentType . includes ( 'application/json' ) ) {
277
+ const bodyText = await response . text ( ) ;
278
+ console . error ( `[NYT API] Unexpected content-type: ${ contentType } \nBody (first 500 chars):\n${ bodyText . slice ( 0 , 500 ) } ` ) ;
279
+ throw new Error ( 'NYT API did not return JSON. Check your API key and endpoint.' ) ;
272
280
}
273
281
const data = await response . json ( ) ;
282
+ if ( ! data . results || ! data . results . books ) {
283
+ console . error ( '[NYT API] No "results.books" field in response:' , data ) ;
284
+ throw new Error ( 'NYT API response missing "results.books".' ) ;
285
+ }
274
286
res . render ( 'api/nyt' , {
275
287
title : 'New York Times API' ,
276
- books : data . results ,
288
+ books : data . results . books ,
277
289
} ) ;
278
290
} catch ( error ) {
291
+ console . error ( '[NYT API] Exception:' , error ) ;
279
292
next ( error ) ;
280
293
}
281
294
} ;
0 commit comments