UNPKG

forest-express

Version:

Official package for all Forest Express Lianas

67 lines (66 loc) 2.69 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var timeoutError = require('../utils/timeout-error'); var ApimapSender = /*#__PURE__*/function () { function ApimapSender(_ref) { var forestUrl = _ref.forestUrl, logger = _ref.logger, superagentRequest = _ref.superagentRequest; (0, _classCallCheck2["default"])(this, ApimapSender); this.forestUrl = forestUrl; this.superagentRequest = superagentRequest; this.logger = logger; } (0, _createClass2["default"])(ApimapSender, [{ key: "handleResult", value: function handleResult(result) { if (!result) return; if ([200, 202, 204].includes(result.status)) { if (result.body && result.body.warning) { this.logger.warn(result.body.warning); } } else if (result.status === 0) { this.logger.warn('Cannot send the apimap to Forest. Are you online?'); } else if (result.status === 404) { this.logger.error('Cannot find the project related to the envSecret you configured. Can you check on Forest that you copied it properly in the Forest initialization?'); } else if (result.status === 503) { this.logger.warn('Forest is in maintenance for a few minutes. We are upgrading your experience in the forest. We just need a few more minutes to get it right.'); } else { this.logger.error('An error occured with the apimap sent to Forest. Please contact support@forestadmin.com for further investigations.'); } } }, { key: "_send", value: function _send(envSecret, data, path) { var _this = this; var url = "".concat(this.forestUrl, "/").concat(path); return this.superagentRequest.post(url).set('forest-secret-key', envSecret).send(data)["catch"](function (error) { var message = timeoutError(url, error); if (message) { throw new Error(message); } else { throw error; } }).then(function (result) { _this.handleResult(result); return result; }); } }, { key: "send", value: function send(envSecret, apimap) { return this._send(envSecret, apimap, 'forest/apimaps'); } }, { key: "checkHash", value: function checkHash(envSecret, schemaFileHash) { return this._send(envSecret, { schemaFileHash: schemaFileHash }, 'forest/apimaps/hashcheck'); } }]); return ApimapSender; }(); module.exports = ApimapSender;