@schibstedspain/openads-appnexus-prebid
Version:
OpenAds AppNexus connector with Prebid features
305 lines (253 loc) • 8.24 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _events = require("./event/events");
var _timeout = require("./timeout/timeout");
var _Debouncer = _interopRequireDefault(require("./service/Debouncer"));
/* eslint-disable no-console */
/**
* @class
* @implements {AdLoadable}
* @implements {AdViewable}
* @implements {Logger}
*/
var AppNexusConnector =
/*#__PURE__*/
function () {
function AppNexusConnector(_ref) {
var pageOpts = _ref.pageOpts,
prebidConfig = _ref.prebidConfig,
logger = _ref.logger,
astClient = _ref.astClient,
adRepository = _ref.adRepository,
loggerProvider = _ref.loggerProvider,
prebidClient = _ref.prebidClient;
this._logger = logger;
this._astClient = astClient;
this._adRepository = adRepository;
this._loggerProvider = loggerProvider;
this._prebidClient = prebidClient;
this._pageOpts = pageOpts;
this._prebidConfig = prebidConfig;
if (this._pageOpts) {
this._astClient.setPageOpts(this._pageOpts);
}
this._prebidClient.setConfig({
config: this._prebidConfig
});
this._loadAdDebouncer = new _Debouncer.default({
onDebounce: this._onLoadAdDebounce.bind(this),
debounceTimeout: _timeout.TIMEOUT_DEBOUNCE
});
this._refreshDebouncer = new _Debouncer.default({
onDebounce: this._onRefreshDebounce.bind(this),
debounceTimeout: _timeout.TIMEOUT_DEBOUNCE
});
}
var _proto = AppNexusConnector.prototype;
_proto.loadAd = function loadAd(_ref2) {
var _this = this;
var id = _ref2.id,
specification = _ref2.specification;
this._logger.debug(this._logger.name, '| loadAd | id:', id, '| specification:', specification);
return Promise.resolve().then(function () {
return _this._adRepository.remove({
id: id
});
}).then(function () {
return _this._loadAdDebouncer.debounce({
input: {
id: id,
specification: specification
}
});
}).then(function () {
return _this._adRepository.find({
id: id
});
});
};
_proto.display = function display(_ref3) {
var _this2 = this;
var id = _ref3.id;
this._logger.debug(this._logger.name, '| display | id:', id);
return Promise.resolve().then(function () {
return _this2._astClient.showTag({
targetId: id
});
}).then(null);
};
_proto.refresh = function refresh(_ref4) {
var _this3 = this;
var id = _ref4.id,
specification = _ref4.specification;
this._logger.debug(this._logger.name, '| refresh | id:', id, '| specification:', specification);
return Promise.resolve().then(function () {
return _this3._adRepository.remove({
id: id
});
}).then(function () {
return _this3._refreshDebouncer.debounce({
input: {
id: id,
specification: specification
}
});
}).then(function () {
return _this3._adRepository.find({
id: id
});
});
}
/**
* @param {object} inputArray
* @returns {Promise<{appnexusInputsArray: Array, prebidUnitsArray: Array} | never | void>}
* @private
*/
;
_proto._onLoadAdDebounce = function _onLoadAdDebounce(loadAdInputs) {
var _this4 = this;
this._logger.debug(this._logger.name, '| _onLoadAdDebounce | loadAdInputs:', loadAdInputs);
return Promise.resolve(loadAdInputs).then(this._buildNormalizedInputs).then(function (normalizedInputs) {
_this4._logger.debug(_this4._logger.name, '| _onLoadAdDebounce | normalizedInputs:', normalizedInputs);
normalizedInputs.tags.forEach(function (input) {
return _this4._defineAppNexusTag({
id: input.id,
tag: input.data
});
});
if (normalizedInputs.adUnits.length > 0 && _this4._prebidClient) {
_this4._prebidClient.addAdUnits({
adUnits: normalizedInputs.adUnits
});
_this4._prebidClient.requestBids({
timeout: _timeout.TIMEOUT_PREBID,
bidsBackHandler: function bidsBackHandler() {
_this4._prebidClient.setTargetingForAst();
_this4._astClient.loadTags();
}
});
} else {
_this4._astClient.loadTags();
}
}).catch(function (error) {
return console.log(error);
});
};
_proto._onRefreshDebounce = function _onRefreshDebounce(refreshInputs) {
var _this5 = this;
this._logger.debug(this._logger.name, '| _onRefreshDebounce | refreshInputs:', refreshInputs);
return Promise.resolve(refreshInputs).then(this._buildNormalizedInputs).then(function (normalizedInputs) {
normalizedInputs.tags.forEach(function (tag) {
return _this5._astClient.modifyTag({
targetId: tag.id,
data: tag.data
});
});
if (normalizedInputs.adUnits.length > 0) {
_this5._prebidClient.requestBids({
adUnits: normalizedInputs.adUnits,
timeout: _timeout.TIMEOUT_PREBID,
bidsBackHandler: function bidsBackHandler() {
_this5._prebidClient.setTargetingForAst();
_this5._astClient.refresh(normalizedInputs.tags.map(function (input) {
return input.id;
}));
}
});
} else {
_this5._astClient.refresh(normalizedInputs.tags.map(function (input) {
return input.id;
}));
}
});
};
_proto._buildNormalizedInputs = function _buildNormalizedInputs(inputs) {
var formattedArray = {
tags: [],
adUnits: []
};
formattedArray.tags = inputs.map(function (input) {
return {
id: input.id,
data: input.specification.appnexus
};
});
formattedArray.adUnits = inputs.filter(function (maybePrebidInput) {
return maybePrebidInput.specification.prebid;
}).map(function (prebidInput) {
return prebidInput.specification.prebid;
});
return formattedArray;
};
_proto._defineAppNexusTag = function _defineAppNexusTag(_ref5) {
var id = _ref5.id,
tag = _ref5.tag;
this._logger.debug(this._logger.name, '| _defineAppNexusTag | id:', id, '| tag:', tag);
this._astClient.defineTag(tag);
this._astClient.onEvent({
event: _events.AD_AVAILABLE,
targetId: id,
callback: consumer(this._adRepository)(id)(_events.AD_AVAILABLE)
});
this._astClient.onEvent({
event: _events.AD_BAD_REQUEST,
targetId: id,
callback: consumer(this._adRepository)(id)(_events.AD_BAD_REQUEST)
});
this._astClient.onEvent({
event: _events.AD_ERROR,
targetId: id,
callback: consumer(this._adRepository)(id)(_events.AD_ERROR)
});
this._astClient.onEvent({
event: _events.AD_NO_BID,
targetId: id,
callback: consumer(this._adRepository)(id)(_events.AD_NO_BID)
});
this._astClient.onEvent({
event: _events.AD_REQUEST_FAILURE,
targetId: id,
callback: consumer(this._adRepository)(id)(_events.AD_REQUEST_FAILURE)
});
};
_proto.enableDebug = function enableDebug(_ref6) {
var debug = _ref6.debug;
this._astClient.debugMode({
debug: debug
});
this._loggerProvider.debugMode({
debug: debug
});
if (debug) {
this._logger.debug(this._logger.name, '| Enabled debug mode on AppNexusConnector | pageOpts:', this._pageOpts, '| prebidConfig:', this._prebidConfig);
}
};
(0, _createClass2.default)(AppNexusConnector, [{
key: "pageOpts",
get: function get() {
return this._pageOpts;
}
}]);
return AppNexusConnector;
}();
exports.default = AppNexusConnector;
var consumer = function consumer(adRepository) {
return function (id) {
return function (status) {
return function (data) {
return adRepository.save({
id: id,
adResponse: {
data: data,
status: status
}
});
};
};
};
};