pbc/libs/api.js
2022-02-07 23:50:16 +01:00

69 lines
2.6 KiB
JavaScript

const logger = require('./logger.js');
const util = require('./util.js');
const constants = require('./constants.js');
const modep = require('./modep.js');
const endpoints = new Map();
function setEndpoints(url, data) {
var startTime = new Date();
var index = 0;
var elements = [];
for (var index = 0; index < data.length; index++) {
var element = data[index];
element.id = index;
setEndpoint(url + '/' + element.id, [constants.HTTP_GET, constants.HTTP_POST], element);
elements.push(element);
}
elements = elements.sort(function (a, b) {
return a.id - b.id;
});
setEndpoint(url, constants.HTTP_GET, elements);
logger.debug('setting up ' + elements.length + ' endpoint(s) for path \'' + url + '\' took ' + util.timeDiff(startTime) + 'ms');
}
function setEndpoint(url, types, method, args) {
logger.debug('setting up \'' + types + '\' endpoint \'' + url + '\'...');
endpoints.set(url, { types: types, method: method, args: args });
}
function getEndpoints() {
return endpoints;
}
// TODO: IMPLEMENT POST ENDPOINTS
function setupEndpoints() {
return new Promise(function (resolve, reject) {
modep.getBanks()
.then(function (banks) {
setEndpoint(constants.API_BANKS, constants.HTTP_GET, modep.getBanks);
for (var index = 0; index < banks.length; index++) {
var id = banks[index].id;
setEndpoint(constants.API_BANKS + '/' + id, constants.HTTP_GET, modep.getBankById, id)
}
})
.then(modep.getPedalboards)
.then(function (pedalboards) {
setEndpoint(constants.API_PEDALBOARDS, constants.HTTP_GET, modep.getPedalboards);
for (var index = 0; index < pedalboards.length; index++) {
var id = pedalboards[index].id;
setEndpoint(constants.API_PEDALBOARDS + '/' + id, constants.HTTP_GET, modep.getPedalboardById, id)
}
})
.then(modep.getCurrentPedals)
.then(function (pedals) {
setEndpoint(constants.API_PEDALS, constants.HTTP_GET, modep.getCurrentPedals);
for (var index = 0; index < pedals.length; index++) {
var id = pedals[index].id;
setEndpoint(constants.API_PEDALS + '/' + id, constants.HTTP_GET, modep.getCurrentPedalById, id)
}
})
.then(resolve)
.catch(reject);
});
}
module.exports = {
setupEndpoints,
getEndpoints
}