node-red-contrib-micropub/micropub-create.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-02-06 14:14:39 +00:00
var Micropub = require('micropub-helper');
module.exports = function (RED) {
function MicropubNode(config) {
RED.nodes.createNode(this, config);
this.endpoint = RED.nodes.getNode(config.endpoint);
2019-02-06 19:51:38 +00:00
const node = this;
2019-02-06 14:14:39 +00:00
node.on('input', function (msg) {
try {
const micropub = new Micropub({
micropubEndpoint: node.endpoint.credentials.micropub_endpoint,
token: node.endpoint.credentials.auth_token
});
2019-02-06 19:51:38 +00:00
let entry = {};
if (typeof msg.payload === 'string') {
entry.type = ['h-entry'];
entry.properties = {
content: [{
value: msg.payload
}]
};
} else if (msg.payload.hasOwnProperty('type') && msg.payload.hasOwnProperty('properties')) {
entry = msg.payload;
}
msg.payload = entry;
msg.location = micropub.postMicropub(entry);
2019-02-06 14:14:39 +00:00
node.send(msg);
} catch (e) {
node.error(e);
}
});
}
RED.nodes.registerType("micropub-create", MicropubNode);
2019-02-06 19:51:38 +00:00
};
2019-02-06 14:14:39 +00:00