@@ -5,32 +5,28 @@ import { read_only_form_data } from './read_only_form_data.js';
5
5
* @param {import('types/helper').Headers } headers
6
6
*/
7
7
export function parse_body ( raw , headers ) {
8
- if ( ! raw ) return raw ;
8
+ if ( ! raw || typeof raw !== 'string' ) return raw ;
9
9
10
- if ( typeof raw === 'string' ) {
11
- const [ type , ...directives ] = headers [ 'content-type' ] . split ( / ; \s * / ) ;
10
+ const [ type , ...directives ] = headers [ 'content-type' ] . split ( / ; \s * / ) ;
12
11
13
- switch ( type ) {
14
- case 'text/plain' :
15
- return raw ;
12
+ switch ( type ) {
13
+ case 'text/plain' :
14
+ return raw ;
16
15
17
- case 'application/json' :
18
- return JSON . parse ( raw ) ;
16
+ case 'application/json' :
17
+ return JSON . parse ( raw ) ;
19
18
20
- case 'application/x-www-form-urlencoded' :
21
- return get_urlencoded ( raw ) ;
19
+ case 'application/x-www-form-urlencoded' :
20
+ return get_urlencoded ( raw ) ;
22
21
23
- case 'multipart/form-data' : {
24
- const boundary = directives . find ( ( directive ) => directive . startsWith ( 'boundary=' ) ) ;
25
- if ( ! boundary ) throw new Error ( 'Missing boundary' ) ;
26
- return get_multipart ( raw , boundary . slice ( 'boundary=' . length ) ) ;
27
- }
28
- default :
29
- throw new Error ( `Invalid Content-Type ${ type } ` ) ;
22
+ case 'multipart/form-data' : {
23
+ const boundary = directives . find ( ( directive ) => directive . startsWith ( 'boundary=' ) ) ;
24
+ if ( ! boundary ) throw new Error ( 'Missing boundary' ) ;
25
+ return get_multipart ( raw , boundary . slice ( 'boundary=' . length ) ) ;
30
26
}
27
+ default :
28
+ throw new Error ( `Invalid Content-Type ${ type } ` ) ;
31
29
}
32
-
33
- return raw ;
34
30
}
35
31
36
32
/** @param {string } text */
0 commit comments