2018-04-15 12:18:10 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
/**
|
2018-12-01 00:01:09 +01:00
|
|
|
* @mixin @node-red/runtime_library
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-18 18:09:31 +02:00
|
|
|
var runtime;
|
|
|
|
|
|
|
|
var api = module.exports = {
|
|
|
|
init: function(_runtime) {
|
|
|
|
runtime = _runtime;
|
|
|
|
},
|
2018-04-15 12:18:10 +02:00
|
|
|
|
|
|
|
/**
|
2018-04-18 18:09:31 +02:00
|
|
|
* Gets an entry from the library.
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
2019-04-25 12:32:09 +02:00
|
|
|
* @param {String} opts.library - the library
|
2018-04-18 18:09:31 +02:00
|
|
|
* @param {String} opts.type - the type of entry
|
|
|
|
* @param {String} opts.path - the path of the entry
|
|
|
|
* @return {Promise<String|Object>} - resolves when complete
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_library
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2018-04-18 18:09:31 +02:00
|
|
|
getEntry: function(opts) {
|
|
|
|
return new Promise(function(resolve,reject) {
|
2019-04-25 12:32:09 +02:00
|
|
|
runtime.library.getEntry(opts.library,opts.type,opts.path).then(function(result) {
|
|
|
|
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path});
|
2018-04-18 18:09:31 +02:00
|
|
|
return resolve(result);
|
|
|
|
}).catch(function(err) {
|
|
|
|
if (err) {
|
2019-04-25 12:32:09 +02:00
|
|
|
runtime.log.warn(runtime.log._("api.library.error-load-entry",{library:opts.library,type:opts.type,path:opts.path,message:err.toString()}));
|
2018-04-18 18:09:31 +02:00
|
|
|
if (err.code === 'forbidden') {
|
|
|
|
err.status = 403;
|
|
|
|
return reject(err);
|
|
|
|
} else if (err.code === "not_found") {
|
|
|
|
err.status = 404;
|
|
|
|
} else {
|
|
|
|
err.status = 400;
|
|
|
|
}
|
2019-04-25 12:32:09 +02:00
|
|
|
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path,error:err.code});
|
2018-04-18 18:09:31 +02:00
|
|
|
return reject(err);
|
|
|
|
}
|
2019-04-25 12:32:09 +02:00
|
|
|
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,error:"not_found"});
|
2018-04-18 18:09:31 +02:00
|
|
|
var error = new Error();
|
|
|
|
error.code = "not_found";
|
|
|
|
error.status = 404;
|
|
|
|
return reject(error);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2018-04-15 12:18:10 +02:00
|
|
|
/**
|
2018-04-18 18:09:31 +02:00
|
|
|
* Saves an entry to the library
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
2019-04-25 12:32:09 +02:00
|
|
|
* @param {String} opts.library - the library
|
2018-04-18 18:09:31 +02:00
|
|
|
* @param {String} opts.type - the type of entry
|
|
|
|
* @param {String} opts.path - the path of the entry
|
|
|
|
* @param {Object} opts.meta - any meta data associated with the entry
|
|
|
|
* @param {String} opts.body - the body of the entry
|
|
|
|
* @return {Promise} - resolves when complete
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_library
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2018-04-18 18:09:31 +02:00
|
|
|
saveEntry: function(opts) {
|
|
|
|
return new Promise(function(resolve,reject) {
|
2019-04-25 12:32:09 +02:00
|
|
|
runtime.library.saveEntry(opts.library,opts.type,opts.path,opts.meta,opts.body).then(function() {
|
2018-04-18 18:09:31 +02:00
|
|
|
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path});
|
|
|
|
return resolve();
|
|
|
|
}).catch(function(err) {
|
|
|
|
runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()}));
|
2018-04-24 23:13:35 +02:00
|
|
|
if (err.code === 'forbidden') {
|
2018-04-18 18:09:31 +02:00
|
|
|
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"});
|
|
|
|
err.status = 403;
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()});
|
|
|
|
var error = new Error();
|
|
|
|
error.status = 400;
|
|
|
|
return reject(error);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|