mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 07:04:53 +09:00
Use lib* prefix for the modules impored from the lib package to help distinguish them more from ordinary variables. Fix up the naming convention for files to be consistently lowercase_underscore for JavaScript files.
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
"use strict";
|
|
const path = require("path");
|
|
|
|
|
|
function addApiRoutes(app, items, endpointHitCounter) {
|
|
|
|
/**
|
|
* GET endpoint to read the masters current inventory of items.
|
|
*
|
|
* @memberof clusterioMaster
|
|
* @instance
|
|
* @alias api/inventory
|
|
* @returns {object[]} JSON [{name:"iron-plate", count:100},{name:"copper-plate",count:5}]
|
|
*/
|
|
app.get("/api/inventory", function(req, res) {
|
|
endpointHitCounter.labels(req.route.path).inc();
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
// Check it and send it
|
|
var inventory = [];
|
|
for (let [name, count] of items._items) {
|
|
inventory.push({ name, count });
|
|
}
|
|
res.type("json");
|
|
res.send(JSON.stringify(inventory));
|
|
});
|
|
|
|
/**
|
|
* GET endpoint to read the masters inventory as an object with key:value pairs
|
|
*
|
|
* @memberof clusterioMaster
|
|
* @instance
|
|
* @alias api/inventoryAsObject
|
|
* @returns {object} JSON {"iron-plate":100, "copper-plate":5}
|
|
*/
|
|
app.get("/api/inventoryAsObject", function(req, res) {
|
|
endpointHitCounter.labels(req.route.path).inc();
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
// Check it and send it
|
|
res.type("json");
|
|
res.send(JSON.stringify(items.serialise()));
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
addApiRoutes,
|
|
};
|