@@ -794,6 +794,19 @@ export class Prince {
794794 configurable : true ,
795795 } ) ;
796796
797+ // Only parse body if it hasn't been parsed by middleware already
798+ if ( [ "POST" , "PUT" , "PATCH" ] . includes ( req . method ) && ! req . parsedBody ) {
799+ const parsed = await this . parseBody ( req ) ;
800+ if ( parsed ) {
801+ if ( typeof parsed === "object" && "files" in parsed && "fields" in parsed ) {
802+ req . parsedBody = parsed . fields ;
803+ req . files = parsed . files ;
804+ } else {
805+ req . parsedBody = parsed ;
806+ }
807+ }
808+ }
809+
797810 // Call onBeforeHandle hooks
798811 for ( const hook of this . onBeforeHandleHooks ) {
799812 await hook ( req , pathname , method ) ;
@@ -809,16 +822,6 @@ export class Prince {
809822 if ( result instanceof Response ) return result ;
810823 }
811824
812- // Parse body here — after all middleware has run — so middleware like
813- // validate() can read/clone the raw stream first without it being consumed.
814- // If middleware already set req.parsedBody (e.g. validate), skip parsing.
815- if ( [ "POST" , "PUT" , "PATCH" ] . includes ( req . method ) && ! req . parsedBody ) {
816- const parsed = await this . parseBody ( req ) ;
817- if ( parsed !== null ) {
818- req . parsedBody = parsed ;
819- }
820- }
821-
822825 const res = await handler ( req ) ;
823826 if ( res instanceof Response ) return res ;
824827 if ( res instanceof ResponseBuilder ) return res . build ( ) ;
0 commit comments