@@ -19,7 +19,7 @@ const SUCCESS_NO_CONTENT = 204;
19
19
* This contains a few pieces of information about a file so
20
20
* we can properly construct a source URL for it.
21
21
*/
22
- export interface FilePathDetails {
22
+ interface FilePathDetails {
23
23
/** The URL or local file path */
24
24
filePath : string ;
25
25
/** This is derived from the `oas-normalize` `type` property. */
@@ -348,10 +348,30 @@ export async function handleAPIv1Res(res: Response, rejectOnJsonError = true) {
348
348
*
349
349
* If we receive non-JSON responses, we consider them errors and throw them.
350
350
*/
351
- export async function handleAPIv2Res < T extends Command > ( this : T , res : Response ) {
351
+ export async function handleAPIv2Res < T extends Command > (
352
+ this : T ,
353
+ res : Response ,
354
+ /**
355
+ * If we're making a request where we don't care about the body (e.g. a HEAD or DELETE request),
356
+ * we can skip parsing the JSON body using this flag.
357
+ */
358
+ skipJsonParsing = false ,
359
+ ) {
352
360
const contentType = res . headers . get ( 'content-type' ) || '' ;
353
361
const extension = mime . extension ( contentType ) || contentType . includes ( 'json' ) ? 'json' : false ;
354
- if ( extension === 'json' ) {
362
+ if ( res . status === SUCCESS_NO_CONTENT ) {
363
+ // to prevent a memory leak, we should still consume the response body? even though we don't use it?
364
+ // https://x.com/cramforce/status/1762142087930433999
365
+ const body = await res . text ( ) ;
366
+ this . debug ( `received status code ${ res . status } from ${ res . url } with no content: ${ body } ` ) ;
367
+ return { } ;
368
+ } else if ( skipJsonParsing ) {
369
+ // to prevent a memory leak, we should still consume the response body? even though we don't use it?
370
+ // https://x.com/cramforce/status/1762142087930433999
371
+ const body = await res . text ( ) ;
372
+ this . debug ( `received status code ${ res . status } from ${ res . url } and not parsing JSON b/c of flag: ${ body } ` ) ;
373
+ return { } ;
374
+ } else if ( extension === 'json' ) {
355
375
// TODO: type this better
356
376
// eslint-disable-next-line @typescript-eslint/no-explicit-any
357
377
const body = ( await res . json ( ) ) as any ;
@@ -361,10 +381,6 @@ export async function handleAPIv2Res<T extends Command>(this: T, res: Response)
361
381
}
362
382
return body ;
363
383
}
364
- if ( res . status === SUCCESS_NO_CONTENT ) {
365
- this . debug ( `received status code ${ res . status } from ${ res . url } with no content` ) ;
366
- return { } ;
367
- }
368
384
369
385
// If we receive a non-JSON response, it's likely an error.
370
386
// Let's debug the raw response body and throw it.
0 commit comments