mirror of
				https://github.com/cp6/my-idlers.git
				synced 2025-11-03 23:59:09 +00:00 
			
		
		
		
	
		
			
	
	
		
			26 lines
		
	
	
	
		
			794 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			26 lines
		
	
	
	
		
			794 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 
								 | 
							
								"use strict";
							 | 
						||
| 
								 | 
							
								Object.defineProperty(exports, "__esModule", { value: true });
							 | 
						||
| 
								 | 
							
								const types_1 = require("./types");
							 | 
						||
| 
								 | 
							
								const parseBody = (response, responseType, parseJson, encoding) => {
							 | 
						||
| 
								 | 
							
								    const { rawBody } = response;
							 | 
						||
| 
								 | 
							
								    try {
							 | 
						||
| 
								 | 
							
								        if (responseType === 'text') {
							 | 
						||
| 
								 | 
							
								            return rawBody.toString(encoding);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if (responseType === 'json') {
							 | 
						||
| 
								 | 
							
								            return rawBody.length === 0 ? '' : parseJson(rawBody.toString());
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if (responseType === 'buffer') {
							 | 
						||
| 
								 | 
							
								            return rawBody;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        throw new types_1.ParseError({
							 | 
						||
| 
								 | 
							
								            message: `Unknown body type '${responseType}'`,
							 | 
						||
| 
								 | 
							
								            name: 'Error'
							 | 
						||
| 
								 | 
							
								        }, response);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    catch (error) {
							 | 
						||
| 
								 | 
							
								        throw new types_1.ParseError(error, response);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								exports.default = parseBody;
							 |