nginx-proxy-manager/frontend/js/app/api.js

689 lines
19 KiB
JavaScript
Raw Normal View History

2018-06-19 18:48:14 -04:00
const $ = require('jquery');
const _ = require('underscore');
const Tokens = require('./tokens');
/**
* @param {String} message
* @param {*} debug
2019-01-03 05:27:43 -05:00
* @param {Number} code
2018-06-19 18:48:14 -04:00
* @constructor
*/
const ApiError = function (message, debug, code) {
let temp = Error.call(this, message);
temp.name = this.name = 'ApiError';
2018-06-19 18:48:14 -04:00
this.stack = temp.stack;
this.message = temp.message;
this.debug = debug;
this.code = code;
};
ApiError.prototype = Object.create(Error.prototype, {
constructor: {
value: ApiError,
writable: true,
configurable: true
}
});
/**
*
* @param {String} verb
* @param {String} path
* @param {Object} [data]
* @param {Object} [options]
* @returns {Promise}
*/
function fetch(verb, path, data, options) {
2018-06-19 18:48:14 -04:00
options = options || {};
return new Promise(function (resolve, reject) {
let api_url = '/api/';
let url = api_url + path;
let token = Tokens.getTopToken();
2018-07-23 01:12:24 -04:00
if ((typeof options.contentType === 'undefined' || options.contentType.match(/json/im)) && typeof data === 'object') {
data = JSON.stringify(data);
}
2018-06-19 18:48:14 -04:00
$.ajax({
url: url,
data: typeof data === 'object' ? JSON.stringify(data) : data,
type: verb,
dataType: 'json',
2018-07-23 01:12:24 -04:00
contentType: options.contentType || 'application/json; charset=UTF-8',
processData: options.processData || true,
2018-06-19 18:48:14 -04:00
crossDomain: true,
timeout: options.timeout ? options.timeout : 30000,
2018-06-19 18:48:14 -04:00
xhrFields: {
withCredentials: true
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + (token ? token.t : null));
},
success: function (data, textStatus, response) {
let total = response.getResponseHeader('X-Dataset-Total');
if (total !== null) {
resolve({
data: data,
pagination: {
total: parseInt(total, 10),
offset: parseInt(response.getResponseHeader('X-Dataset-Offset'), 10),
limit: parseInt(response.getResponseHeader('X-Dataset-Limit'), 10)
}
});
} else {
resolve(response);
}
},
error: function (xhr, status, error_thrown) {
let code = 400;
if (typeof xhr.responseJSON !== 'undefined' && typeof xhr.responseJSON.error !== 'undefined' && typeof xhr.responseJSON.error.message !== 'undefined') {
error_thrown = xhr.responseJSON.error.message;
code = xhr.responseJSON.error.code || 500;
}
reject(new ApiError(error_thrown, xhr.responseText, code));
}
});
});
}
/**
*
* @param {Array} expand
* @returns {String}
*/
function makeExpansionString(expand) {
2018-06-19 18:48:14 -04:00
let items = [];
_.forEach(expand, function (exp) {
items.push(encodeURIComponent(exp));
});
return items.join(',');
}
2018-07-08 21:22:10 -04:00
/**
* @param {String} path
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
function getAllObjects(path, expand, query) {
2018-07-08 21:22:10 -04:00
let params = [];
if (typeof expand === 'object' && expand !== null && expand.length) {
params.push('expand=' + makeExpansionString(expand));
}
if (typeof query === 'string') {
params.push('query=' + query);
}
return fetch('get', path + (params.length ? '?' + params.join('&') : ''));
}
function FileUpload(path, fd) {
2018-07-23 01:12:24 -04:00
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
let token = Tokens.getTopToken();
xhr.open('POST', '/api/' + path);
xhr.overrideMimeType('text/plain');
xhr.setRequestHeader('Authorization', 'Bearer ' + (token ? token.t : null));
xhr.send(fd);
xhr.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE) {
if (xhr.status !== 200 && xhr.status !== 201) {
reject(new Error('Upload failed: ' + xhr.status));
} else {
resolve(xhr.responseText);
}
}
};
});
}
2018-06-19 18:48:14 -04:00
module.exports = {
status: function () {
return fetch('get', '');
},
Tokens: {
/**
* @param {String} identity
* @param {String} secret
* @param {Boolean} [wipe] Will wipe the stack before adding to it again if login was successful
* @returns {Promise}
*/
login: function (identity, secret, wipe) {
return fetch('post', 'tokens', {identity: identity, secret: secret})
.then(response => {
if (response.token) {
if (wipe) {
Tokens.clearTokens();
}
// Set storage token
Tokens.addToken(response.token);
return response.token;
} else {
Tokens.clearTokens();
throw(new Error('No token returned'));
}
});
},
/**
* @returns {Promise}
*/
refresh: function () {
return fetch('get', 'tokens')
.then(response => {
if (response.token) {
Tokens.setCurrentToken(response.token);
return response.token;
} else {
Tokens.clearTokens();
throw(new Error('No token returned'));
}
});
}
},
Users: {
/**
* @param {Number|String} user_id
2018-06-19 18:48:14 -04:00
* @param {Array} [expand]
* @returns {Promise}
*/
getById: function (user_id, expand) {
return fetch('get', 'users/' + user_id + (typeof expand === 'object' && expand.length ? '?expand=' + makeExpansionString(expand) : ''));
},
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
2018-07-08 21:22:10 -04:00
return getAllObjects('users', expand, query);
2018-06-19 18:48:14 -04:00
},
/**
* @param {Object} data
* @returns {Promise}
*/
create: function (data) {
return fetch('post', 'users', data);
},
/**
* @param {Object} data
2019-01-03 05:27:43 -05:00
* @param {Number} data.id
2018-06-19 18:48:14 -04:00
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'users/' + id, data);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-06-19 18:48:14 -04:00
* @returns {Promise}
*/
delete: function (id) {
return fetch('delete', 'users/' + id);
},
/**
*
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-06-19 18:48:14 -04:00
* @param {Object} auth
* @returns {Promise}
*/
setPassword: function (id, auth) {
return fetch('put', 'users/' + id + '/auth', auth);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-06-19 18:48:14 -04:00
* @returns {Promise}
*/
loginAs: function (id) {
return fetch('post', 'users/' + id + '/login');
2018-07-04 18:27:25 -04:00
},
/**
*
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-04 18:27:25 -04:00
* @param {Object} perms
* @returns {Promise}
*/
setPermissions: function (id, perms) {
return fetch('put', 'users/' + id + '/permissions', perms);
2018-06-19 18:48:14 -04:00
}
},
2018-07-08 21:22:10 -04:00
Nginx: {
ProxyHosts: {
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
return getAllObjects('nginx/proxy-hosts', expand, query);
},
/**
* @param {Object} data
*/
create: function (data) {
return fetch('post', 'nginx/proxy-hosts', data);
2018-07-17 18:35:49 -04:00
},
/**
* @param {Object} data
2019-01-03 05:27:43 -05:00
* @param {Number} data.id
2018-07-17 18:35:49 -04:00
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'nginx/proxy-hosts/' + id, data);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-17 18:35:49 -04:00
* @returns {Promise}
*/
delete: function (id) {
return fetch('delete', 'nginx/proxy-hosts/' + id);
2019-01-03 05:27:43 -05:00
},
/**
* @param {Number} id
* @returns {Promise}
*/
get: function (id) {
return fetch('get', 'nginx/proxy-hosts/' + id);
},
/**
* @param {Number} id
* @returns {Promise}
*/
enable: function (id) {
return fetch('post', 'nginx/proxy-hosts/' + id + '/enable');
},
/**
* @param {Number} id
* @returns {Promise}
*/
disable: function (id) {
return fetch('post', 'nginx/proxy-hosts/' + id + '/disable');
2018-07-08 21:22:10 -04:00
}
},
RedirectionHosts: {
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
return getAllObjects('nginx/redirection-hosts', expand, query);
2018-07-23 01:12:24 -04:00
},
/**
* @param {Object} data
*/
create: function (data) {
return fetch('post', 'nginx/redirection-hosts', data);
},
/**
* @param {Object} data
2019-01-03 05:27:43 -05:00
* @param {Number} data.id
2018-07-23 01:12:24 -04:00
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'nginx/redirection-hosts/' + id, data);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-23 01:12:24 -04:00
* @returns {Promise}
*/
delete: function (id) {
return fetch('delete', 'nginx/redirection-hosts/' + id);
},
/**
* @param {Number} id
* @returns {Promise}
*/
get: function (id) {
return fetch('get', 'nginx/redirection-hosts/' + id);
},
2018-07-23 01:12:24 -04:00
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-23 01:12:24 -04:00
* @param {FormData} form_data
* @params {Promise}
*/
setCerts: function (id, form_data) {
2018-07-24 02:56:39 -04:00
return FileUpload('nginx/redirection-hosts/' + id + '/certificates', form_data);
2019-01-03 05:27:43 -05:00
},
/**
* @param {Number} id
* @returns {Promise}
*/
enable: function (id) {
return fetch('post', 'nginx/redirection-hosts/' + id + '/enable');
},
/**
* @param {Number} id
* @returns {Promise}
*/
disable: function (id) {
return fetch('post', 'nginx/redirection-hosts/' + id + '/disable');
2018-07-08 21:22:10 -04:00
}
},
Streams: {
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
return getAllObjects('nginx/streams', expand, query);
2018-07-23 01:12:24 -04:00
},
/**
* @param {Object} data
*/
create: function (data) {
return fetch('post', 'nginx/streams', data);
},
/**
* @param {Object} data
2019-01-03 05:27:43 -05:00
* @param {Number} data.id
2018-07-23 01:12:24 -04:00
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'nginx/streams/' + id, data);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-23 01:12:24 -04:00
* @returns {Promise}
*/
delete: function (id) {
return fetch('delete', 'nginx/streams/' + id);
2019-01-03 05:27:43 -05:00
},
/**
* @param {Number} id
* @returns {Promise}
*/
get: function (id) {
return fetch('get', 'nginx/streams/' + id);
},
2019-01-03 05:27:43 -05:00
/**
* @param {Number} id
* @returns {Promise}
*/
enable: function (id) {
return fetch('post', 'nginx/streams/' + id + '/enable');
},
/**
* @param {Number} id
* @returns {Promise}
*/
disable: function (id) {
return fetch('post', 'nginx/streams/' + id + '/disable');
2018-07-08 21:22:10 -04:00
}
},
DeadHosts: {
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
return getAllObjects('nginx/dead-hosts', expand, query);
2018-07-23 01:12:24 -04:00
},
/**
* @param {Object} data
*/
create: function (data) {
return fetch('post', 'nginx/dead-hosts', data);
},
/**
* @param {Object} data
2019-01-03 05:27:43 -05:00
* @param {Number} data.id
2018-07-23 01:12:24 -04:00
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'nginx/dead-hosts/' + id, data);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-23 01:12:24 -04:00
* @returns {Promise}
*/
delete: function (id) {
return fetch('delete', 'nginx/dead-hosts/' + id);
},
/**
* @param {Number} id
* @returns {Promise}
*/
get: function (id) {
return fetch('get', 'nginx/dead-hosts/' + id);
},
2018-07-23 01:12:24 -04:00
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-23 01:12:24 -04:00
* @param {FormData} form_data
* @params {Promise}
*/
setCerts: function (id, form_data) {
2018-07-24 02:56:39 -04:00
return FileUpload('nginx/dead-hosts/' + id + '/certificates', form_data);
2019-01-03 05:27:43 -05:00
},
/**
* @param {Number} id
* @returns {Promise}
*/
enable: function (id) {
return fetch('post', 'nginx/dead-hosts/' + id + '/enable');
},
/**
* @param {Number} id
* @returns {Promise}
*/
disable: function (id) {
return fetch('post', 'nginx/dead-hosts/' + id + '/disable');
2018-07-08 21:22:10 -04:00
}
2018-07-23 01:12:24 -04:00
},
2018-07-24 02:56:39 -04:00
AccessLists: {
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
return getAllObjects('nginx/access-lists', expand, query);
},
2018-07-23 01:12:24 -04:00
2018-07-24 02:56:39 -04:00
/**
* @param {Object} data
*/
create: function (data) {
return fetch('post', 'nginx/access-lists', data);
},
2018-07-23 01:12:24 -04:00
2018-07-24 02:56:39 -04:00
/**
* @param {Object} data
2019-01-03 05:27:43 -05:00
* @param {Number} data.id
2018-07-24 02:56:39 -04:00
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'nginx/access-lists/' + id, data);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-07-24 02:56:39 -04:00
* @returns {Promise}
*/
delete: function (id) {
return fetch('delete', 'nginx/access-lists/' + id);
}
},
Certificates: {
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
return getAllObjects('nginx/certificates', expand, query);
},
/**
* @param {Object} data
*/
create: function (data) {
return fetch('post', 'nginx/certificates', data);
},
/**
* @param {Object} data
2019-01-03 05:27:43 -05:00
* @param {Number} data.id
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'nginx/certificates/' + id, data);
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
* @returns {Promise}
*/
delete: function (id) {
return fetch('delete', 'nginx/certificates/' + id);
2018-08-07 06:27:20 -04:00
},
/**
2019-01-03 05:27:43 -05:00
* @param {Number} id
2018-08-07 06:27:20 -04:00
* @param {FormData} form_data
* @params {Promise}
*/
upload: function (id, form_data) {
return FileUpload('nginx/certificates/' + id + '/upload', form_data);
2018-08-08 02:58:21 -04:00
},
/**
* @param {FormData} form_data
* @params {Promise}
*/
validate: function (form_data) {
return FileUpload('nginx/certificates/validate', form_data);
},
/**
* @param {Number} id
* @returns {Promise}
*/
renew: function (id) {
return fetch('post', 'nginx/certificates/' + id + '/renew');
}
}
2018-07-08 21:22:10 -04:00
},
AuditLog: {
/**
* @param {Array} [expand]
* @param {String} [query]
* @returns {Promise}
*/
getAll: function (expand, query) {
return getAllObjects('audit-log', expand, query);
}
},
Reports: {
/**
* @returns {Promise}
*/
getHostStats: function () {
return fetch('get', 'reports/hosts');
2018-07-08 21:22:10 -04:00
}
},
Settings: {
/**
* @param {String} setting_id
* @returns {Promise}
*/
getById: function (setting_id) {
return fetch('get', 'settings/' + setting_id);
},
/**
* @returns {Promise}
*/
getAll: function () {
return getAllObjects('settings');
},
/**
* @param {Object} data
* @param {Number} data.id
* @returns {Promise}
*/
update: function (data) {
let id = data.id;
delete data.id;
return fetch('put', 'settings/' + id, data);
}
2018-06-19 18:48:14 -04:00
}
};