1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/api/admin/nodes.js

147 lines
4.4 KiB
JavaScript
Raw Normal View History

2014-11-04 12:34:49 +01:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
2014-11-04 12:34:49 +01:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var apiUtils = require("../util");
2018-04-15 12:18:10 +02:00
var runtimeAPI;
2014-11-04 12:34:49 +01:00
module.exports = {
2018-04-15 12:18:10 +02:00
init: function(_runtimeAPI) {
runtimeAPI = _runtimeAPI;
},
2014-11-04 12:34:49 +01:00
getAll: function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user
}
2014-11-04 12:34:49 +01:00
if (req.get("accept") == "application/json") {
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.getNodeList(opts).then(function(list) {
res.json(list);
})
2014-11-04 12:34:49 +01:00
} else {
2018-04-15 12:18:10 +02:00
opts.lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
runtimeAPI.nodes.getNodeConfigs(opts).then(function(configs) {
res.send(configs);
})
2014-11-04 12:34:49 +01:00
}
},
2014-11-04 12:34:49 +01:00
post: function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
module: req.body.module,
version: req.body.version
2014-11-04 12:34:49 +01:00
}
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.addModule(opts).then(function(info) {
res.json(info);
}).catch(function(err) {
2018-04-15 12:18:10 +02:00
apiUtils.rejectHandler(req,res,err);
})
2014-11-04 12:34:49 +01:00
},
2014-11-04 12:34:49 +01:00
delete: function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
module: req.params[0]
2014-11-04 12:34:49 +01:00
}
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.removeModule(opts).then(function(info) {
res.status(204).end();
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
2014-11-04 12:34:49 +01:00
},
getSet: function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
id: req.params[0] + "/" + req.params[2]
}
if (req.get("accept") === "application/json") {
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.getNodeInfo(opts).then(function(result) {
res.send(result);
2018-04-15 12:18:10 +02:00
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
2014-11-04 12:34:49 +01:00
} else {
2018-04-15 12:18:10 +02:00
opts.lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
runtimeAPI.nodes.getNodeConfig(opts).then(function(result) {
return res.json(result);
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
2014-11-04 12:34:49 +01:00
}
},
getModule: function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
module: req.params[0]
}
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.getModuleInfo(opts).then(function(result) {
res.send(result);
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
},
putSet: function(req,res) {
2014-11-04 12:34:49 +01:00
var body = req.body;
if (!body.hasOwnProperty("enabled")) {
2018-04-15 12:18:10 +02:00
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
2015-07-15 23:43:24 +02:00
res.status(400).json({error:"invalid_request", message:"Invalid request"});
2014-11-04 12:34:49 +01:00
return;
}
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
id: req.params[0] + "/" + req.params[2],
enabled: body.enabled
}
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.setNodeSetState(opts).then(function(result) {
res.send(result);
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
},
putModule: function(req,res) {
var body = req.body;
if (!body.hasOwnProperty("enabled")) {
2018-04-15 12:18:10 +02:00
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
2015-07-15 23:43:24 +02:00
res.status(400).json({error:"invalid_request", message:"Invalid request"});
return;
}
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
module: req.params[0],
enabled: body.enabled
}
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.setModuleState(opts).then(function(result) {
res.send(result);
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
},
getIcons: function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user
}
2018-04-15 12:18:10 +02:00
runtimeAPI.nodes.getIconList(opts).then(function(list) {
res.json(list);
});
2014-12-08 17:26:54 +01:00
}
2018-04-15 12:18:10 +02:00
};