@@ -81,6 +81,7 @@ export class ResourceCollection<
8181 constructor ( rsc : R , ctx : Response | H3Event | Record < string , any > )
8282 constructor ( rsc : R , ctx ?: Response | H3Event | Record < string , any > ) {
8383 super ( )
84+ if ( ctx ) ResourceCollection . ctx = ctx
8485 this . resource = rsc
8586
8687 if ( ctx ) {
@@ -333,17 +334,31 @@ export class ResourceCollection<
333334 return this
334335 }
335336
337+ /**
338+ * Build a response object, optionally accepting a raw response to mutate in withResponse.
339+ */
336340 response ( ) : ServerResponse < CollectionBody < R > >
341+ /**
342+ * Build a response object, optionally accepting a raw response to mutate in withResponse.
343+ * @param res Optional raw response object (e.g. Express Response or H3Event res)
344+ */
337345 response ( res : H3Event [ 'res' ] ) : ServerResponse < CollectionBody < R > >
338- response ( res ?: H3Event [ 'res' ] ) : ServerResponse < CollectionBody < R > > {
339- const rawResponse = res ?? this . res as never
346+ response ( res : Response ) : ServerResponse < CollectionBody < R > >
347+ /**
348+ * Build a response object, optionally accepting a raw response to mutate in withResponse.
349+ *
350+ * @param res Optional raw response object (e.g. Express Response or H3Event res)
351+ * @returns
352+ */
353+ response ( res ?: H3Event [ 'res' ] | Response ) : ServerResponse < CollectionBody < R > > {
354+ const rawResponse = res ?? this . res ?? ( this . ctx as any ) ?. res as never
340355
341356 return this . runResponse ( {
342357 ensureJson : ( ) => this . json ( ) ,
343358 rawResponse,
344359 body : ( ) => this . body ,
345360 createServerResponse : ( raw , body ) => {
346- const response = new ServerResponse ( raw , body )
361+ const response = new ServerResponse ( raw as never , body )
347362 this . withResponseContext = {
348363 response,
349364 raw,
@@ -389,7 +404,7 @@ export class ResourceCollection<
389404 return this . runThen ( {
390405 ensureJson : ( ) => this . json ( ) ,
391406 body : ( ) => this . body ,
392- rawResponse : this . res ,
407+ rawResponse : this . res ?? ( ResourceCollection . ctx as any ) ?. res as never ,
393408 createServerResponse : ( raw , body ) => {
394409 const response = new ServerResponse ( raw as never , body )
395410 this . withResponseContext = {
@@ -422,7 +437,7 @@ export class ResourceCollection<
422437 return this . runThen ( {
423438 ensureJson : ( ) => this . json ( ) ,
424439 body : ( ) => this . body ,
425- rawResponse : this . res ,
440+ rawResponse : this . res ?? ( ResourceCollection . ctx as any ) ?. res as never ,
426441 createServerResponse : ( raw , body ) => {
427442 const response = new ServerResponse ( raw as never , body )
428443 this . withResponseContext = {
@@ -452,7 +467,7 @@ export class ResourceCollection<
452467 return this . runThen ( {
453468 ensureJson : ( ) => this . json ( ) ,
454469 body : ( ) => this . body ,
455- rawResponse : this . res ,
470+ rawResponse : this . res ?? ( ResourceCollection . ctx as any ) ?. res as never ,
456471 createServerResponse : ( raw , body ) => {
457472 const response = new ServerResponse ( raw as never , body )
458473 this . withResponseContext = {
0 commit comments