Skip to content

Commit 3e30a1b

Browse files
authored
[chore] minor simplification of parse_body (#2043)
1 parent 635f632 commit 3e30a1b

File tree

1 file changed

+15
-19
lines changed
  • packages/kit/src/runtime/server/parse_body

1 file changed

+15
-19
lines changed

packages/kit/src/runtime/server/parse_body/index.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,28 @@ import { read_only_form_data } from './read_only_form_data.js';
55
* @param {import('types/helper').Headers} headers
66
*/
77
export function parse_body(raw, headers) {
8-
if (!raw) return raw;
8+
if (!raw || typeof raw !== 'string') return raw;
99

10-
if (typeof raw === 'string') {
11-
const [type, ...directives] = headers['content-type'].split(/;\s*/);
10+
const [type, ...directives] = headers['content-type'].split(/;\s*/);
1211

13-
switch (type) {
14-
case 'text/plain':
15-
return raw;
12+
switch (type) {
13+
case 'text/plain':
14+
return raw;
1615

17-
case 'application/json':
18-
return JSON.parse(raw);
16+
case 'application/json':
17+
return JSON.parse(raw);
1918

20-
case 'application/x-www-form-urlencoded':
21-
return get_urlencoded(raw);
19+
case 'application/x-www-form-urlencoded':
20+
return get_urlencoded(raw);
2221

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));
3026
}
27+
default:
28+
throw new Error(`Invalid Content-Type ${type}`);
3129
}
32-
33-
return raw;
3430
}
3531

3632
/** @param {string} text */

0 commit comments

Comments
 (0)