oneuptime
Version:
OneUptime is a JS package that tracks error event and send logs from your applications to your oneuptime dashboard.
137 lines (109 loc) • 3.62 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: lib/helpers.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: lib/helpers.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @fileoverview HTTP wrapper functions module.
* @author HackerBay, Inc.
* @module helpers
* @see module:config
* @see module:logger
*/
'use strict';
const axios = require('axios');
const { API_URL } = require('./config');
const logger = require('./logger');
/** The request headers. */
const headers = {
'Content-Type': 'application/json',
};
/** Handle request error.
* @param {Object} - The error object of the request.
* @default
*/
const defaultErrorHandler = error => {
logger.debug(error.config);
if (error.response) {
logger.debug(error.response.data);
logger.debug(error.response.status);
logger.debug(error.response.headers);
throw error.response.data;
} else {
if (error.request) {
logger.debug(error.request);
} else {
logger.debug('Error', error.message);
}
}
throw error;
};
/**
* Get request data with axios.
* @param {string} apiUrl - The url of the api.
* @param {string} url - The endpoint of the request.
* @param {string} key - The api key of the endpoint.
* @param {Function} success - The request success callback.
* @param {Function} error - The request error callback.
* @return {Promise} The request promise.
*/
const get = (apiUrl, url, key, success, error = defaultErrorHandler) => {
headers['apiKey'] = key;
return axios({
method: 'get',
url: `${apiUrl || API_URL}/${url}`,
headers,
}).then(success, error);
};
/**
* Post request data with axios.
* @param {string} apiUrl - The url of the api.
* @param {string} url - The endpoint of the request.
* @param {Object} data - The data of endpoint.
* @param {string} key - The api key of the endpoint.
* @param {Function} success - The request success callback.
* @param {Function} error - The request error callback.
* @return {Promise} The request promise.
*/
const post = (apiUrl, url, data, key, success, error = defaultErrorHandler) => {
headers['apiKey'] = key;
return axios({
method: 'post',
url: `${apiUrl || API_URL}/${url}`,
headers,
data,
}).then(success, error);
};
module.exports = {
get,
post,
defaultErrorHandler,
};
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-api.html">api</a></li><li><a href="module-config.html">config</a></li><li><a href="module-helpers.html">helpers</a></li><li><a href="module-logger.html">logger</a></li><li><a href="module-server-monitor.html">server-monitor</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Fri Jan 08 2021 00:53:57 GMT+0100 (West Africa Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>