/* -------------------------------------------------------------------------------------------- * Copyright (c) Jan Dolejsi. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.postJson = exports.getText = exports.getFile = exports.getJson = void 0; /* eslint-disable @typescript-eslint/no-explicit-any */ const request = require("request"); const fs = require("fs"); function getJson(url) { return new Promise((resolve, reject) => { request.get(url, { json: true }, (error, httpResponse, body) => { if (error) { reject(error); } else { if (httpResponse && httpResponse.statusCode !== 200) { reject("HTTP status code " + httpResponse.statusCode); } else { resolve(body); } } }); }); } exports.getJson = getJson; function getFile(url, localFilePath) { const localFile = fs.createWriteStream(localFilePath); return new Promise((resolve, reject) => { request.get(url) .on('response', function (response) { console.log("Downloading %s. Content-type: %s, Status code: %d", url, response.headers['content-type'], response.statusCode); if (response.statusCode >= 400) { reject(new Error("Downloading VAL binaries failed with HTTP status code " + response.statusCode)); } }) .on('error', function (err) { reject(err); }) .pipe(localFile) .on("close", () => resolve()); }); } exports.getFile = getFile; function getText(url) { return new Promise((resolve, reject) => { request.get(url, (error, httpResponse, body) => { if (error) { reject(error); } else { if (httpResponse && httpResponse.statusCode !== 200) { reject("HTTP status code " + httpResponse.statusCode); } else { resolve(body); } } }); }); } exports.getText = getText; function postJson(url, content) { return new Promise((resolve, reject) => { request.post(url, { body: content, json: true }, (error, httpResponse, body) => { if (error) { reject(error); } else { if (httpResponse && httpResponse.statusCode > 204) { reject("HTTP status code " + httpResponse.statusCode); } else { resolve(body); } } }); }); } exports.postJson = postJson; //# sourceMappingURL=httpUtils.js.map