Repository URL to install this package:
|
Version:
1.1.9 ▾
|
const axios = require('axios');
const Validation = require('./validation');
const Version = require('./version');
class API {
constructor(apiKey) {
this.apiKey = apiKey;
this.endpoint = "https://alerts.c1.fbinhouse.se";
this.defaultRepo = null;
this.defaultExpiry = null;
this.defaultMeta = {};
this.defaultAlertType = null;
this.verbose = false;
}
getBuildDetails() {
return Version;
}
setVerbose(verbose) {
this.verbose = verbose;
}
setEndpoint(endpoint) {
this.endpoint = endpoint;
}
setDefaultRepository(user, name, type='bitbucket') {
this.defaultRepo = {
type,
user,
name
}
}
setDefaultExpiry(seconds) {
this.defaultExpiry = seconds;
}
setDefaultMeta(meta) {
this.defaultMeta = meta;
}
setDefaultAlertType(alertType) {
this.defaultAlertType = alertType;
}
_sendAlert(alertIdent, status, alertType=null, meta={}, expiry=null, repo=null) {
return Promise.resolve().then(() => {
if (alertType === null) {
alertType = this.defaultAlertType;
}
if (repo === null) {
repo = this.defaultRepo;
}
if (expiry === null) {
expiry = this.defaultExpiry;
}
meta = Object.assign({}, this.defaultMeta, meta);
Validation.validateAlertType(alertType);
alertIdent = Validation.validateAlertIdent(alertIdent);
// console.log(alertIdent, alertIdent.length);
Validation.validateStatus(status);
Validation.validateMeta(meta);
Validation.validateExpiry(expiry);
Validation.validateRepo(repo);
let body = {
status,
meta
}
if (expiry !== null) {
body.expiry = expiry;
}
if (repo !== null) {
body.repo = repo;
}
return this._sendToAPI("POST", "/alert/" + alertType + "/" + encodeURIComponent(alertIdent), body);
});
}
_resetAlerts(alertType) {
return Promise.resolve().then(() => {
if (alertType !== null) {
Validation.validateAlertType(alertType);
return this._sendToAPI("POST", "/reset/" + alertType);
} else {
return this._sendToAPI("POST", "/reset");
}
})
}
_sendToAPI(method, path, body) {
return axios.request({
timeout: 15 * 1000,
headers: {'authorization': this.apiKey, 'content-type': 'application/json'},
method,
url: this.endpoint + path,
data: body,
maxContentLength: 1024 * 10,
transformRequest: (data, headers) => {
return JSON.stringify(data);
},
transformResponse: (data) => {
try {
return JSON.parse(data);
} catch(e) {
return {
errors: [
{
message: "Non JSON response: " + data
}
]
}
}
},
validateStatus: (status) => true,
}).then(response => {
// Check for errors
if ('errors' in response.data) {
// console.warn('Errors', response.data.errors);
throw new Error(response.data.errors[0].message);
} else if ('data' in response.data) {
if (response.data.data === "OK") {
return;
} else {
// console.warn(response);
throw new Error('Received data response but not "OK": ' + response.data.data);
}
} else {
// console.warn(response);
throw new Error('Received unexpected response format');
}
})
}
_logProblem(e) {
if (this.verbose) {
console.warn(e);
}
return Promise.resolve();
}
// Public interfaces that return a promise
sendOKAlert(alertIdent, alertType=null, meta={}, expiry=null, repo=null) {
return this._sendAlert(alertIdent, "OK", alertType, meta, expiry, repo).catch(e => {
return this._logProblem(e);
});
}
sendWarningAlert(alertIdent, alertType=null, meta={}, expiry=null, repo=null) {
return this._sendAlert(alertIdent, "WARNING", alertType, meta, expiry, repo).catch(e => {
return this._logProblem(e);
});
}
sendCriticalAlert(alertIdent, alertType=null, meta={}, expiry=null, repo=null) {
return this._sendAlert(alertIdent, "CRITICAL", alertType, meta, expiry, repo).catch(e => {
return this._logProblem(e);
});
}
resetAllAlertsOfType(alertType) {
return this._resetAlerts(alertType).catch(e => {
return this._logProblem(e);
});
}
resetAllAlerts() {
return this._resetAlerts(null).catch(e => {
return this._logProblem(e);
});
}
}
module.exports = API;