polymatica-api
v1.0.15
Published
Polymatica api
Downloads
10
Maintainers
Readme
Example usage
This is a simple example of getting data from a server Polymatica Platform
const {
PolymaticaApi,
DataOptionMethod,
DataOptionSortDirection,
} = require('polymatica-api');
const poly = new PolymaticaApi('SERVER_URL');
poly.setAPIToken('TOKEN');
poly.getData(
[
poly
.fromById(51106)
.key('data')
.method(DataOptionMethod.Aggregate)
.groupById(656892)
.sumById(656894)
.sortById(656892, DataOptionSortDirection.Asc)
.make()
],
true,
).then((r) => {
const data = r.get('data');
if (data.valid) {
console.table(data.rows);
} else {
console.error(data.error);
}
});
Run widget from Polymatica Dashboards
<polymatica-widget-legacy></polymatica-widget-legacy>
<script src="https://cdn.jsdelivr.net/npm/polymatica-widget-component-legacy@latest/polymatica-widget-component-legacy.min.js"></script>
<script src="polymatica-api.js"></script>
<script>
const poly = new PolymaticaApi.PolymaticaApi('/');
const component = document.querySelector('polymatica-widget-legacy');
poly.loadDashboardWidget(3796).then((r) => {
r.runLegacy(component);
});
</script>
For api to work from a browser, proxy through your server:
const path = require('path');
const express = require('express');
const nunjucks = require('nunjucks');
const { createProxyMiddleware } = require('http-proxy-middleware');
const token = 'Token {{ POLYMATICA_TOKEN }}';
const targetServer = '{{ POLYMATICA_URL }}';
const app = express();
const port = 3000;
nunjucks.configure('src', {
autoescape: true,
express: app
});
app.use(['/api', '/dashboard/api'], createProxyMiddleware({
target: targetServer,
changeOrigin: true,
headers: {
authorization: token,
},
}));
app.use(express.static('src/public'));
app.get("/polymatica-api.js", function( req, res ) {
res.sendFile(path.join(__dirname, "node_modules/polymatica-api/index.js"));
});
app.get('/', async (req, res) => {
res.render('index.html');
})
app.listen(port, () => {
console.log(`Running on a port ${port}`)
})