All files / src/http/utility transmit.ts

100% Statements 18/18
100% Branches 0/0
100% Functions 5/5
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 5913x     13x                                 28x       28x         28x   28x 28x 27x 27x 27x   27x 23x     27x 27x   27x               28x 28x       13x  
import HTTP from 'http';
 
import HeaderMap from 'http/type/header-map';
import HttpHeader from 'http/enum/header';
import HttpMethod from 'http/enum/method';
import StatusCode from 'http/enum/status-code';
import ContentType from 'http/enum/content-type';
 
interface ResponseData {
	body: Buffer;
	headers: HeaderMap;
	status_code: StatusCode;
}
 
function transmit(
	url: string,
	method: HttpMethod,
	content_type: ContentType,
	data: Buffer
): Promise<ResponseData> {
	const headers = {
		[HttpHeader.ACCEPT]: content_type
	};
 
	const options = {
		method,
		headers
	};
 
	const request = HTTP.request(url, options);
 
	return new Promise((resolve, reject) => {
		request.on('response', (response) => {
			const headers = response.headers as HeaderMap;
			const status_code = response.statusCode as StatusCode;
			const chunks: Buffer[] = [];
 
			response.on('data', (data) => {
				chunks.push(data);
			});
 
			response.on('end', () => {
				const body = Buffer.concat(chunks);
 
				resolve({
					body,
					headers,
					status_code
				});
			});
		});
 
		request.on('error', reject);
		request.end(data);
	});
}
 
export default transmit;