diff --git a/src/adaptermanager.js b/src/adaptermanager.js index 231a8936a69..48c320548a0 100644 --- a/src/adaptermanager.js +++ b/src/adaptermanager.js @@ -3,6 +3,7 @@ import { flatten, getBidderCodes, getDefinedParams, shuffle } from './utils'; import { mapSizes } from './sizeMapping'; import { processNativeAdUnitParams, nativeAdapters } from './native'; +import { newBidder } from './adapters/bidderFactory'; var utils = require('./utils.js'); var CONSTANTS = require('./constants.json'); @@ -194,6 +195,13 @@ function transformHeightWidth(adUnit) { return sizesObj; } +function getSupportedMediaTypes(bidderCode) { + let result = []; + if (exports.videoAdapters.includes(bidderCode)) result.push('video'); + if (nativeAdapters.includes(bidderCode)) result.push('native'); + return result; +} + exports.videoAdapters = []; // added by adapterLoader for now exports.registerBidAdapter = function (bidAdaptor, bidderCode, {supportedMediaTypes = []} = {}) { @@ -220,14 +228,24 @@ exports.aliasBidAdapter = function (bidderCode, alias) { if (typeof existingAlias === 'undefined') { var bidAdaptor = _bidderRegistry[bidderCode]; - if (typeof bidAdaptor === 'undefined') { utils.logError('bidderCode "' + bidderCode + '" is not an existing bidder.', 'adaptermanager.aliasBidAdapter'); } else { try { - let newAdapter = new bidAdaptor.constructor(); - newAdapter.setBidderCode(alias); - this.registerBidAdapter(newAdapter, alias); + let newAdapter; + let supportedMediaTypes = getSupportedMediaTypes(bidderCode); + // Have kept old code to support backward compatibilitiy. + // Remove this if loop when all adapters are supporting bidderFactory. i.e When Prebid.js is 1.0 + if (bidAdaptor.constructor.prototype != Object.prototype) { + newAdapter = new bidAdaptor.constructor(); + newAdapter.setBidderCode(alias); + } else { + let spec = bidAdaptor.getSpec(); + newAdapter = newBidder(Object.assign({}, spec, { code: alias })); + } + this.registerBidAdapter(newAdapter, alias, { + supportedMediaTypes + }); } catch (e) { utils.logError(bidderCode + ' bidder does not currently support aliasing.', 'adaptermanager.aliasBidAdapter'); } diff --git a/src/adapters/bidderFactory.js b/src/adapters/bidderFactory.js index 641af863f15..becc4afc029 100644 --- a/src/adapters/bidderFactory.js +++ b/src/adapters/bidderFactory.js @@ -138,6 +138,9 @@ export function registerBidder(spec) { */ export function newBidder(spec) { return Object.assign(new Adapter(spec.code), { + getSpec: function() { + return Object.freeze(spec); + }, callBids: function(bidderRequest) { if (!Array.isArray(bidderRequest.bids)) { return; diff --git a/test/spec/unit/core/adapterManager_spec.js b/test/spec/unit/core/adapterManager_spec.js index b829aa9b5b2..10bd6074021 100644 --- a/test/spec/unit/core/adapterManager_spec.js +++ b/test/spec/unit/core/adapterManager_spec.js @@ -3,6 +3,7 @@ import AdapterManager from 'src/adaptermanager'; import { getAdUnits } from 'test/fixtures/fixtures'; import CONSTANTS from 'src/constants.json'; import * as utils from 'src/utils'; +import { registerBidder } from 'src/adapters/bidderFactory'; const CONFIG = { enabled: true, @@ -81,4 +82,58 @@ describe('adapterManager tests', () => { expect(spy.called).to.equal(false); }); }) + + describe('aliasBidderAdaptor', function() { + const CODE = 'sampleBidder'; + + // Note: remove this describe once Prebid is 1.0 + describe('old way', function() { + let originalRegistry; + + function SampleAdapter() { + return Object.assign(this, { + callBids: sinon.stub(), + setBidderCode: sinon.stub() + }); + } + + before(() => { + originalRegistry = AdapterManager.bidderRegistry; + AdapterManager.bidderRegistry[CODE] = new SampleAdapter(); + }); + + after(() => { + AdapterManager.bidderRegistry = originalRegistry; + }); + + it('should add alias to registry', () => { + const alias = 'testalias'; + AdapterManager.aliasBidAdapter(CODE, alias); + expect(AdapterManager.bidderRegistry).to.have.property(alias); + }); + }); + + describe('using bidderFactory', function() { + let spec; + + beforeEach(() => { + spec = { + code: CODE, + isBidRequestValid: () => {}, + buildRequests: () => {}, + interpretResponse: () => {}, + getUserSyncs: () => {} + }; + }); + + it('should add alias to registry when original adapter is using bidderFactory', function() { + let thisSpec = Object.assign(spec, { supportedMediaTypes: ['video'] }); + registerBidder(thisSpec); + const alias = 'aliasBidder'; + AdapterManager.aliasBidAdapter(CODE, alias); + expect(AdapterManager.bidderRegistry).to.have.property(alias); + expect(AdapterManager.videoAdapters).to.include(alias); + }); + }); + }); });