Skip to content

Commit

Permalink
[chore] minor simplification of parse_body (sveltejs#2043)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanJPNM authored Jul 30, 2021
1 parent 635f632 commit 3e30a1b
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions packages/kit/src/runtime/server/parse_body/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@ import { read_only_form_data } from './read_only_form_data.js';
* @param {import('types/helper').Headers} headers
*/
export function parse_body(raw, headers) {
if (!raw) return raw;
if (!raw || typeof raw !== 'string') return raw;

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

switch (type) {
case 'text/plain':
return raw;
switch (type) {
case 'text/plain':
return raw;

case 'application/json':
return JSON.parse(raw);
case 'application/json':
return JSON.parse(raw);

case 'application/x-www-form-urlencoded':
return get_urlencoded(raw);
case 'application/x-www-form-urlencoded':
return get_urlencoded(raw);

case 'multipart/form-data': {
const boundary = directives.find((directive) => directive.startsWith('boundary='));
if (!boundary) throw new Error('Missing boundary');
return get_multipart(raw, boundary.slice('boundary='.length));
}
default:
throw new Error(`Invalid Content-Type ${type}`);
case 'multipart/form-data': {
const boundary = directives.find((directive) => directive.startsWith('boundary='));
if (!boundary) throw new Error('Missing boundary');
return get_multipart(raw, boundary.slice('boundary='.length));
}
default:
throw new Error(`Invalid Content-Type ${type}`);
}

return raw;
}

/** @param {string} text */
Expand Down

0 comments on commit 3e30a1b

Please sign in to comment.