Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54 lines (40 sloc)
1.52 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const FIRST_INDEX = 0; | |
| /** | |
| * Parses input, depending on the supplied content type. Currently handles application/json and | |
| * application/x-www-form-urlencoded. Throws an error if the content type is not understood. May | |
| * also throw if an error occurs in parsing. | |
| * | |
| * @param {string} input The input. | |
| * @param {string} contentType Content type of the input. Defaults to application/json if not set. | |
| * If additional data such as a charset is present, it will be stripped. | |
| * @return {object} A collection of key and value pairs as supplied in the input. | |
| */ | |
| module.exports = ( input, contentType ) => { | |
| let processedContentType = contentType; | |
| // Default to JSON. | |
| if ( 'undefined' === typeof processedContentType ) { | |
| processedContentType = 'application/json'; | |
| } | |
| // Split the content type down to just the MIME type, in case there's encoding included there too. | |
| processedContentType = processedContentType.split( ';' )[ FIRST_INDEX ]; | |
| let data; | |
| switch ( processedContentType ) { | |
| case 'application/json': { | |
| data = JSON.parse( input ); | |
| break; | |
| } | |
| case 'application/x-www-form-urlencoded': { | |
| const querystring = require( 'querystring' ); | |
| data = querystring.parse( input ); | |
| break; | |
| } | |
| default: { | |
| throw new Error( | |
| 'Error: Invalid content-type. Please supply either application/json or ' + | |
| 'application/x-www-form-urlencoded.' | |
| ); | |
| } | |
| } // Switch contentType. | |
| return data; | |
| }; // Module.exports. |