@windingtree/wt-write-api
Version:
API to write data to the Winding Tree platform
74 lines (65 loc) • 2.17 kB
JavaScript
const request = require('xhr-request-promise');
const HOTEL_RESOURCE_TYPE = 'hotel';
let _requestLib = request;
/* Send a publication request to the notification service. */
async function publish (notificationsUri, notification) {
const separator = notificationsUri.endsWith('/') ? '' : '/';
await _requestLib(`${notificationsUri}${separator}notifications`, {
method: 'POST',
json: true,
responseType: 'text', // Without this, the library expects JSON in return.
body: notification,
});
}
function publishForHotel (notificationsUri, wtDirectory, hotelAddress, scope) {
return publish(notificationsUri, {
wtDirectory, // TODO https://github.com/windingtree/wt-write-api/issues/154
resourceType: HOTEL_RESOURCE_TYPE,
resourceAddress: hotelAddress,
scope: scope,
});
}
/**
* Publish notification about hotel creation.
*
* @param {String} notificationsUri
* @param {String} wtDirectory
* @param {String} hotelAddress
* @return {Promise<void>}
*/
module.exports.publishHotelCreated = function (notificationsUri, wtDirectory, hotelAddress) {
return publishForHotel(notificationsUri, wtDirectory, hotelAddress, { action: 'create' });
};
/**
* Publish notification about hotel deletion.
*
* @param {String} notificationsUri
* @param {String} wtDirectory
* @param {String} hotelAddress
* @return {Promise<void>}
*/
module.exports.publishHotelDeleted = function (notificationsUri, wtDirectory, hotelAddress) {
return publishForHotel(notificationsUri, wtDirectory, hotelAddress, { action: 'delete' });
};
/**
* Publish notification about hotel update.
*
* @param {String} notificationsUri
* @param {String} wtDirectory
* @param {String} hotelAddress
* @param {String[]} subjects (optional)
* @return {Promise<void>}
*/
module.exports.publishHotelUpdated = function (notificationsUri, wtDirectory, hotelAddress, subjects) {
return publishForHotel(notificationsUri, wtDirectory, hotelAddress, {
action: 'update',
subjects: subjects,
});
};
/**
* Allow requestLib to be overwritten from outside for mocking
* purposes.
*/
module.exports.setRequestLib = function (lib) {
_requestLib = lib;
};