1.0.15 • Published 1 year ago

polymatica-api v1.0.15

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Example usage

build: success license: MIT

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}`)
})
1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago