diff --git a/libraries/biddoInvamiaUtils/index.js b/libraries/biddoInvamiaUtils/index.js new file mode 100644 index 00000000000..0d7c4b1b683 --- /dev/null +++ b/libraries/biddoInvamiaUtils/index.js @@ -0,0 +1,70 @@ +/** + * Helper function to build request payload for banner ads. + * @param {Object} bidRequest - The bid request object. + * @param {string} endpointUrl - The endpoint URL specific to the bidder. + * @returns {Array} An array of server requests. + */ +export function buildBannerRequests(bidRequest, endpointUrl) { + const serverRequests = []; + const sizes = bidRequest.mediaTypes.banner.sizes; + + sizes.forEach(([width, height]) => { + bidRequest.params.requestedSizes = [width, height]; + + const payload = { + ctype: 'div', + pzoneid: bidRequest.params.zoneId, + width, + height, + }; + + const payloadString = Object.keys(payload) + .map((key) => `${key}=${encodeURIComponent(payload[key])}`) + .join('&'); + + serverRequests.push({ + method: 'GET', + url: endpointUrl, + data: payloadString, + bidderRequest: bidRequest, + }); + }); + + return serverRequests; +} + +/** + * Helper function to interpret server response for banner ads. + * @param {Object} serverResponse - The server response object. + * @param {Object} bidderRequest - The matched bid request for this response. + * @returns {Array} An array of bid responses. + */ +export function interpretBannerResponse(serverResponse, bidderRequest) { + const response = serverResponse.body; + const bidResponses = []; + + if (response && response.template && response.template.html) { + const { bidId } = bidderRequest; + const [width, height] = bidderRequest.params.requestedSizes; + + const bidResponse = { + requestId: bidId, + cpm: response.hb.cpm, + creativeId: response.banner.hash, + currency: 'USD', + netRevenue: response.hb.netRevenue, + ttl: 600, + ad: response.template.html, + mediaType: 'banner', + meta: { + advertiserDomains: response.hb.adomains || [], + }, + width, + height, + }; + + bidResponses.push(bidResponse); + } + + return bidResponses; +} diff --git a/modules/biddoBidAdapter.js b/modules/biddoBidAdapter.js index cf39c572629..6bfa0ac6ef8 100644 --- a/modules/biddoBidAdapter.js +++ b/modules/biddoBidAdapter.js @@ -1,5 +1,6 @@ import {registerBidder} from '../src/adapters/bidderFactory.js'; import {BANNER} from '../src/mediaTypes.js'; +import { buildBannerRequests, interpretBannerResponse } from '../libraries/biddoInvamiaUtils/index.js'; /** * @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest @@ -12,86 +13,15 @@ const ENDPOINT_URL = 'https://ad.adopx.net/delivery/impress'; export const spec = { code: BIDDER_CODE, supportedMediaTypes: [BANNER], - /** - * Determines whether or not the given bid request is valid. - * - * @param {BidRequest} bidRequest The bid request params to validate. - * @return boolean True if this is a valid bid request, and false otherwise. - */ - isBidRequestValid: function(bidRequest) { + isBidRequestValid: function (bidRequest) { return !!bidRequest.params.zoneId; }, - /** - * Make a server request from the list of BidRequests. - * - * @param {Array} validBidRequests an array of bid requests - * @return ServerRequest Info describing the request to the server. - */ - buildRequests: function(validBidRequests) { - let serverRequests = []; - - validBidRequests.forEach(bidRequest => { - const sizes = bidRequest.mediaTypes.banner.sizes; - - sizes.forEach(([width, height]) => { - bidRequest.params.requestedSizes = [width, height]; - - const payload = { - ctype: 'div', - pzoneid: bidRequest.params.zoneId, - width, - height, - }; - - const payloadString = Object.keys(payload).map(k => k + '=' + encodeURIComponent(payload[k])).join('&'); - - serverRequests.push({ - method: 'GET', - url: ENDPOINT_URL, - data: payloadString, - bidderRequest: bidRequest, - }); - }); - }); - - return serverRequests; + buildRequests: function (validBidRequests) { + return validBidRequests.flatMap((bidRequest) => buildBannerRequests(bidRequest, ENDPOINT_URL)); }, - /** - * Unpack the response from the server into a list of bids. - * - * @param {ServerResponse} serverResponse A successful response from the server. - * @param {BidRequest} bidderRequest A matched bid request for this response. - * @return Array An array of bids which were nested inside the server. - */ - interpretResponse: function(serverResponse, {bidderRequest}) { - const response = serverResponse.body; - const bidResponses = []; - - if (response && response.template && response.template.html) { - const {bidId} = bidderRequest; - const [width, height] = bidderRequest.params.requestedSizes; - - const bidResponse = { - requestId: bidId, - cpm: response.hb.cpm, - creativeId: response.banner.hash, - currency: 'USD', - netRevenue: response.hb.netRevenue, - ttl: 600, - ad: response.template.html, - mediaType: 'banner', - meta: { - advertiserDomains: response.hb.adomains || [], - }, - width, - height, - }; - - bidResponses.push(bidResponse); - } - - return bidResponses; + interpretResponse: function (serverResponse, { bidderRequest }) { + return interpretBannerResponse(serverResponse, bidderRequest); }, -} +}; registerBidder(spec); diff --git a/modules/invamiaBidAdapter.js b/modules/invamiaBidAdapter.js index 96af163ca4f..7f9a40e1473 100644 --- a/modules/invamiaBidAdapter.js +++ b/modules/invamiaBidAdapter.js @@ -1,5 +1,6 @@ import {registerBidder} from '../src/adapters/bidderFactory.js'; import {BANNER} from '../src/mediaTypes.js'; +import { buildBannerRequests, interpretBannerResponse } from '../libraries/biddoInvamiaUtils/index.js'; /** * @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest @@ -12,86 +13,15 @@ const ENDPOINT_URL = 'https://ad.invamia.com/delivery/impress'; export const spec = { code: BIDDER_CODE, supportedMediaTypes: [BANNER], - /** - * Determines whether or not the given bid request is valid. - * - * @param {BidRequest} bidRequest The bid request params to validate. - * @return boolean True if this is a valid bid request, and false otherwise. - */ - isBidRequestValid: function(bidRequest) { + isBidRequestValid: function (bidRequest) { return !!bidRequest.params.zoneId; }, - /** - * Make a server request from the list of BidRequests. - * - * @param {Array} validBidRequests an array of bid requests - * @return ServerRequest Info describing the request to the server. - */ - buildRequests: function(validBidRequests) { - let serverRequests = []; - - validBidRequests.forEach(bidRequest => { - const sizes = bidRequest.mediaTypes.banner.sizes; - - sizes.forEach(([width, height]) => { - bidRequest.params.requestedSizes = [width, height]; - - const payload = { - ctype: 'div', - pzoneid: bidRequest.params.zoneId, - width, - height, - }; - - const payloadString = Object.keys(payload).map(k => k + '=' + encodeURIComponent(payload[k])).join('&'); - - serverRequests.push({ - method: 'GET', - url: ENDPOINT_URL, - data: payloadString, - bidderRequest: bidRequest, - }); - }); - }); - - return serverRequests; + buildRequests: function (validBidRequests) { + return validBidRequests.flatMap((bidRequest) => buildBannerRequests(bidRequest, ENDPOINT_URL)); }, - /** - * Unpack the response from the server into a list of bids. - * - * @param {ServerResponse} serverResponse A successful response from the server. - * @param {BidRequest} bidderRequest A matched bid request for this response. - * @return Array An array of bids which were nested inside the server. - */ - interpretResponse: function(serverResponse, {bidderRequest}) { - const response = serverResponse.body; - const bidResponses = []; - - if (response && response.template && response.template.html) { - const {bidId} = bidderRequest; - const [width, height] = bidderRequest.params.requestedSizes; - - const bidResponse = { - requestId: bidId, - cpm: response.hb.cpm, - creativeId: response.banner.hash, - currency: 'USD', - netRevenue: response.hb.netRevenue, - ttl: 600, - ad: response.template.html, - mediaType: 'banner', - meta: { - advertiserDomains: response.hb.adomains || [], - }, - width, - height, - }; - - bidResponses.push(bidResponse); - } - - return bidResponses; + interpretResponse: function (serverResponse, { bidderRequest }) { + return interpretBannerResponse(serverResponse, bidderRequest); }, -} +}; registerBidder(spec);