'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.wrapWebhook = wrapWebhook;
exports.wrapWebhookCollection = wrapWebhookCollection;
var _cloneDeep = require('lodash/cloneDeep');
var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
var _freezeSys = require('contentful-sdk-core/freeze-sys');
var _freezeSys2 = _interopRequireDefault(_freezeSys);
var _enhanceWithMethods = require('../enhance-with-methods');
var _enhanceWithMethods2 = _interopRequireDefault(_enhanceWithMethods);
var _toPlainObject = require('contentful-sdk-core/mixins/to-plain-object');
var _toPlainObject2 = _interopRequireDefault(_toPlainObject);
var _errorHandler = require('../error-handler');
var _errorHandler2 = _interopRequireDefault(_errorHandler);
var _instanceActions = require('../instance-actions');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @memberof Webhook
* @typedef Webhook
* @prop {Meta.Sys} sys - System metadata
* @prop {string} name
* @prop {string} url - Url which the webhook will call
* @prop {string} httpBasicUsername - Username for basic HTTP authentication
* @prop {string} httpBasicPassword - Password for basic HTTP authentication
* @prop {object} headers - Key value pairs of additional headers to be sent with every webhook call.
* @prop {array} topics - Topics which this webhook should be subscribed to. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhooks/create-a-webhook for more details
* @prop {function(): Object} toPlainObject() - Returns this Webhook as a plain JS object
*/
/**
* Webhook instances
* @namespace Webhook
*/
function createWebhookApi(http) {
return {
/**
* Sends an update to the server with any changes made to the object's properties
* @memberof Webhook
* @func update
* @return {Promise<Webhook>} Object returned from the server with updated changes.
* @example
* webhook.name = 'New name'
* webhook.update()
* .then(webhook => console.log(webhook.name))
*/
update: (0, _instanceActions.createUpdateEntity)({
http: http,
entityPath: 'webhook_definitions',
wrapperMethod: wrapWebhook
}),
/**
* Deletes this object on the server.
* @memberof Webhook
* @func delete
* @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled.
* @example
* webhook.delete()
* .catch(err => console.log(err))
*/
delete: (0, _instanceActions.createDeleteEntity)({
http: http,
entityPath: 'webhook_definitions'
}),
/**
* List of the most recent webhook calls. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhook-calls/webhook-call-overviews for more details.
* @memberof Webhook
* @func getCalls
* @return {Promise<object>} Promise for list of calls
* @example
* webhook.getCalls()
* .then(calls => console.log(calls.items))
*/
getCalls: function getCalls() {
return http.get('webhooks/' + this.sys.id + '/calls').then(function (response) {
return response.data;
}, _errorHandler2.default);
},
/**
* Webhook call with specific id. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhook-calls/webhook-call-overviews for more details
* @memberof Webhook
* @func getCalls
* @return {Promise<object>} Promise for call details
* @example
* webhook.getCall('kjsdk1jeasd')
* .then(call => console.log(call))
*/
getCall: function getCall(id) {
return http.get('webhooks/' + this.sys.id + '/calls/' + id).then(function (response) {
return response.data;
}, _errorHandler2.default);
},
/**
* Overview of the health of webhook calls. See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/webhook-calls/webhook-call-overviews for more details.
* @memberof Webhook
* @func getHealth
* @return {Promise<object>} Promise for health info
* @example
* webhook.getHealth()
* .then(health => console.log(health))
*/
getHealth: function getHealth() {
return http.get('webhooks/' + this.sys.id + '/health').then(function (response) {
return response.data;
}, _errorHandler2.default);
}
};
}
/**
* @private
* @param {Object} http - HTTP client instance
* @param {Object} data - Raw webhook data
* @return {Webhook} Wrapped webhook data
*/
function wrapWebhook(http, data) {
var webhook = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
(0, _enhanceWithMethods2.default)(webhook, createWebhookApi(http));
return (0, _freezeSys2.default)(webhook);
}
/**
* @memberof Webhook
* @typedef WebhookCollection
* @prop {number} total
* @prop {number} skip
* @prop {number} limit
* @prop {Array<Webhook.Webhook>} items
* @prop {function(): Object} toPlainObject() - Returns this Webhook collection as a plain JS object
*/
/**
* @private
* @param {Object} http - HTTP client instance
* @param {Object} data - Raw webhook collection data
* @return {WebhookCollection} Wrapped webhook collection data
*/
function wrapWebhookCollection(http, data) {
var webhooks = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
webhooks.items = webhooks.items.map(function (entity) {
return wrapWebhook(http, entity);
});
return (0, _freezeSys2.default)(webhooks);
}