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

Move module message catalogs under runtime api

This commit is contained in:
Nick O'Leary 2018-04-30 10:57:14 +01:00
parent fc4edde6e6
commit 42516206d9
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
9 changed files with 155 additions and 25 deletions

View File

@ -36,7 +36,7 @@ RED.i18n = (function() {
}
},
loadCatalog: function(namespace,done) {
loadNodeCatalog: function(namespace,done) {
var languageList = i18n.functions.toLanguages(i18n.detectLanguage());
var toLoad = languageList.length;
languageList.forEach(function(lang) {
@ -45,7 +45,7 @@ RED.i18n = (function() {
"Accept":"application/json"
},
cache: false,
url: 'locales/'+namespace+'?lng='+lang,
url: 'nodes/'+namespace+'/messages?lng='+lang,
success: function(data) {
i18n.addResourceBundle(lang,namespace,data);
toLoad--;
@ -68,7 +68,7 @@ RED.i18n = (function() {
"Accept":"application/json"
},
cache: false,
url: 'locales/nodes?lng='+lang,
url: 'nodes/messages?lng='+lang,
success: function(data) {
var namespaces = Object.keys(data);
namespaces.forEach(function(ns) {

View File

@ -282,7 +282,7 @@ var RED = (function() {
var id = m.id;
RED.nodes.addNodeSet(m);
addedTypes = addedTypes.concat(m.types);
RED.i18n.loadCatalog(id, function() {
RED.i18n.loadNodeCatalog(id, function() {
$.get('nodes/'+id, function(data) {
$("body").append(data);
});

View File

@ -46,6 +46,8 @@ module.exports = {
// Nodes
adminApp.get("/nodes",needsPermission("nodes.read"),nodes.getAll,apiUtil.errorHandler);
adminApp.post("/nodes",needsPermission("nodes.write"),nodes.post,apiUtil.errorHandler);
adminApp.get(/\/nodes\/messages/,needsPermission("nodes.read"),nodes.getModuleCatalogs,apiUtil.errorHandler);
adminApp.get(/\/nodes\/((@[^\/]+\/)?[^\/]+\/[^\/]+)\/messages/,needsPermission("nodes.read"),nodes.getModuleCatalog,apiUtil.errorHandler);
adminApp.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.read"),nodes.getModule,apiUtil.errorHandler);
adminApp.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.write"),nodes.putModule,apiUtil.errorHandler);
adminApp.delete(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,needsPermission("nodes.write"),nodes.delete,apiUtil.errorHandler);

View File

@ -135,6 +135,31 @@ module.exports = {
},
getModuleCatalog: function(req,res) {
var opts = {
user: req.user,
module: req.params[0],
lang: req.query.lng
}
runtimeAPI.nodes.getModuleCatalog(opts).then(function(result) {
res.json(result);
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
},
getModuleCatalogs: function(req,res) {
var opts = {
user: req.user,
lang: req.query.lng
}
runtimeAPI.nodes.getModuleCatalogs(opts).then(function(result) {
res.json(result);
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
},
getIcons: function(req,res) {
var opts = {
user: req.user

View File

@ -83,7 +83,6 @@ module.exports = {
// Locales
var locales = require("./locales");
locales.init(runtimeAPI);
editorApp.get('/locales/nodes',locales.getAllNodes,apiUtil.errorHandler);
editorApp.get(/locales\/(.+)\/?$/,locales.get,apiUtil.errorHandler);
// Library

View File

@ -38,20 +38,5 @@ module.exports = {
});
i18n.i.setLng(prevLang);
},
getAllNodes: function(req,res) {
var lngs = req.query.lng;
var opts = {
user: req.user
}
runtimeAPI.nodes.getNodeList(opts).then(function(nodeList) {
var result = {};
nodeList.forEach(function(n) {
if (n.module !== "node-red") {
result[n.id] = i18n.catalog(n.id,lngs)||{};
}
});
res.json(result);
})
}
}

View File

@ -350,13 +350,55 @@ var api = module.exports = {
},
/**
* TODO: getModuleCatalogs
* Gets all registered module message catalogs
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.lang - the i18n language to return. If not set, uses runtime default (en-US)
* @return {Promise<Object>} - the message catalogs
* @memberof RED.nodes
*/
getModuleCatalogs: function() {},
getModuleCatalogs: function(opts) {
return new Promise(function(resolve,reject) {
var namespace = opts.module;
var lang = opts.lang;
var prevLang = runtime.i18n.i.lng();
// Trigger a load from disk of the language if it is not the default
runtime.i18n.i.setLng(lang, function(){
var nodeList = runtime.nodes.getNodeList();
var result = {};
nodeList.forEach(function(n) {
if (n.module !== "node-red") {
result[n.id] = runtime.i18n.catalog(n.id,lang)||{};
}
});
resolve(result);
});
runtime.i18n.i.setLng(prevLang);
});
},
/**
* TODO: getModuleCatalog
* Gets a modules message catalog
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.module - the module
* @param {User} opts.lang - the i18n language to return. If not set, uses runtime default (en-US)
* @return {Promise<Object>} - the message catalog
* @memberof RED.nodes
*/
getModuleCatalog: function() {},
getModuleCatalog: function(opts) {
return new Promise(function(resolve,reject) {
var namespace = opts.module;
var lang = opts.lang;
var prevLang = runtime.i18n.i.lng();
// Trigger a load from disk of the language if it is not the default
runtime.i18n.i.setLng(lang, function(){
var catalog = runtime.i18n.catalog(namespace,lang);
resolve(catalog||{});
});
runtime.i18n.i.setLng(prevLang);
});
},
/**
* Gets the list of all icons available in the modules installed within the runtime

View File

@ -66,7 +66,8 @@ describe("api/admin/index", function() {
sinon.stub(nodes,"delete",stubApp);
sinon.stub(nodes,"getSet",stubApp);
sinon.stub(nodes,"putSet",stubApp);
sinon.stub(nodes,"getModuleCatalog",stubApp);
sinon.stub(nodes,"getModuleCatalogs",stubApp);
});
after(function() {
mockList.forEach(function(m) {
@ -87,6 +88,8 @@ describe("api/admin/index", function() {
nodes.delete.restore();
nodes.getSet.restore();
nodes.putSet.restore();
nodes.getModuleCatalog.restore();
nodes.getModuleCatalogs.restore();
});
@ -281,5 +284,36 @@ describe("api/admin/index", function() {
done();
})
});
it('GET /nodes/messages', function(done) {
request(app).get("/nodes/messages").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('nodes.read',1);
done();
})
});
it('GET /nodes/module/set/messages', function(done) {
request(app).get("/nodes/module/set/messages").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('nodes.read',1);
lastRequest.params.should.have.property(0,'module/set');
done();
})
});
it('GET /nodes/@scope/module/set/messages', function(done) {
request(app).get("/nodes/@scope/module/set/messages").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('nodes.read',1);
lastRequest.params.should.have.property(0,'@scope/module/set');
done();
})
});
});
});

View File

@ -32,6 +32,8 @@ describe("api/admin/nodes", function() {
app.use(bodyParser.json());
app.get("/nodes",nodes.getAll);
app.post("/nodes",nodes.post);
app.get(/\/nodes\/messages/,nodes.getModuleCatalogs);
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+\/[^\/]+)\/messages/,nodes.getModuleCatalog);
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,nodes.getModule);
app.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,nodes.putModule);
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)\/([^\/]+)$/,nodes.getSet);
@ -431,4 +433,45 @@ describe("api/admin/nodes", function() {
});
});
});
describe('get module messages', function() {
it('returns message catalog', function(done) {
nodes.init({
nodes:{
getModuleCatalog: function(opts) {
return Promise.resolve(opts);
}
}
});
request(app)
.get('/nodes/module/set/messages')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.eql({ module: 'module/set' });
done();
});
});
it('returns all node catalogs', function(done) {
nodes.init({
nodes:{
getModuleCatalogs: function(opts) {
return Promise.resolve({a:1});
}
}
});
request(app)
.get('/nodes/messages')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.eql({a:1});
done();
});
});
})
});