Skip to content

Commit

Permalink
import utils as needed; dont import all
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-harshad-mane committed Sep 24, 2021
1 parent ed83320 commit 9c7d944
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 113 deletions.
110 changes: 56 additions & 54 deletions modules/adbookpspBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import find from 'core-js-pure/features/array/find';
import { config } from '../src/config.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { getStorageManager } from '../src/storageManager.js';
import * as utils from '../src/utils.js';
import {
isPlainObject, deepSetValue, deepAccess, logWarn, inIframe, isNumber, logError, isArray, uniques,
flatten, triggerPixel, isStr, isEmptyStr, generateUUID
} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';

/**
Expand Down Expand Up @@ -150,7 +153,7 @@ function buildDevice() {

const deviceConfig = common.getConfig('device');

if (utils.isPlainObject(deviceConfig)) {
if (isPlainObject(deviceConfig)) {
return { ...device, ...deviceConfig };
}

Expand All @@ -163,20 +166,20 @@ function buildRegs(bidderRequest) {
};

if (bidderRequest.gdprConsent) {
utils.deepSetValue(
deepSetValue(
regs,
'ext.gdpr',
bidderRequest.gdprConsent.gdprApplies ? 1 : 0
);
utils.deepSetValue(
deepSetValue(
regs,
'ext.gdprConsentString',
bidderRequest.gdprConsent.consentString || ''
);
}

if (bidderRequest.uspConsent) {
utils.deepSetValue(regs, 'ext.us_privacy', bidderRequest.uspConsent);
deepSetValue(regs, 'ext.us_privacy', bidderRequest.uspConsent);
}

return regs;
Expand All @@ -187,10 +190,10 @@ function buildSource(bidRequests, bidderRequest) {
fd: 1,
tid: bidderRequest.auctionId,
};
const schain = utils.deepAccess(bidRequests, '0.schain');
const schain = deepAccess(bidRequests, '0.schain');

if (schain) {
utils.deepSetValue(source, 'ext.schain', schain);
deepSetValue(source, 'ext.schain', schain);
}

return source;
Expand Down Expand Up @@ -232,7 +235,7 @@ function buildMediaTypeObject(mediaType, bidRequest) {
case VIDEO:
return buildVideoObject(bidRequest);
default:
utils.logWarn(`${BIDDER_CODE}: Unsupported media type ${mediaType}!`);
logWarn(`${BIDDER_CODE}: Unsupported media type ${mediaType}!`);
}
}

Expand All @@ -246,7 +249,7 @@ function buildBannerObject(bidRequest) {

return {
pos: 0,
topframe: utils.inIframe() ? 0 : 1,
topframe: inIframe() ? 0 : 1,
format,
w,
h,
Expand All @@ -261,8 +264,8 @@ function buildVideoObject(bidRequest) {
};

for (const param of VIDEO_PARAMS) {
const paramsValue = utils.deepAccess(bidRequest, `params.video.${param}`);
const mediaTypeValue = utils.deepAccess(
const paramsValue = deepAccess(bidRequest, `params.video.${param}`);
const mediaTypeValue = deepAccess(
bidRequest,
`mediaTypes.video.${param}`
);
Expand All @@ -276,10 +279,10 @@ function buildVideoObject(bidRequest) {
}

function getVideoSize(bidRequest) {
const playerSize = utils.deepAccess(bidRequest, 'mediaTypes.video.playerSize', [[]]);
const { w, h } = utils.deepAccess(bidRequest, 'mediaTypes.video', {});
const playerSize = deepAccess(bidRequest, 'mediaTypes.video.playerSize', [[]]);
const { w, h } = deepAccess(bidRequest, 'mediaTypes.video', {});

if (utils.isNumber(w) && utils.isNumber(h)) {
if (isNumber(w) && isNumber(h)) {
return { w, h };
}

Expand All @@ -296,11 +299,11 @@ function buildImpExt(validBidRequest) {
const ext = {};

if (placementId) {
utils.deepSetValue(ext, 'adbook.placementId', placementId);
deepSetValue(ext, 'adbook.placementId', placementId);
}

if (effectiverOrgId) {
utils.deepSetValue(ext, 'adbook.orgId', effectiverOrgId);
deepSetValue(ext, 'adbook.orgId', effectiverOrgId);
}

return ext;
Expand All @@ -314,20 +317,19 @@ function interpretResponse(bidResponse, bidderRequest) {
const bidderRequestBody = safeJSONparse(bidderRequest.data);

if (
utils.deepAccess(bidderRequestBody, 'id') !=
utils.deepAccess(bidResponse, 'body.id')
deepAccess(bidderRequestBody, 'id') !=
deepAccess(bidResponse, 'body.id')
) {
utils.logError(
logError(
`${BIDDER_CODE}: Bid response id does not match bidder request id`
);

return [];
}

const referrer = utils.deepAccess(bidderRequestBody, 'site.ref', '');
const incomingBids = utils
.deepAccess(bidResponse, 'body.seatbid', [])
.filter((seat) => utils.isArray(seat.bid))
const referrer = deepAccess(bidderRequestBody, 'site.ref', '');
const incomingBids = deepAccess(bidResponse, 'body.seatbid', [])
.filter((seat) => isArray(seat.bid))
.reduce((bids, seat) => bids.concat(seat.bid), [])
.filter(validateBid(bidderRequestBody));
const targetingMap = buildTargetingMap(incomingBids);
Expand Down Expand Up @@ -366,11 +368,11 @@ const impToPrebidBid =
const bidRequest = findBidRequest(bidderRequestBody, bid);

if (!bidRequest) {
utils.logError(`${BIDDER_CODE}: Could not match bid to bid request`);
logError(`${BIDDER_CODE}: Could not match bid to bid request`);

return null;
}
const categories = utils.deepAccess(bid, 'cat', []);
const categories = deepAccess(bid, 'cat', []);
const mediaType = getMediaType(bid.adm);
let prebidBid = {
ad: bid.adm,
Expand All @@ -383,7 +385,7 @@ const impToPrebidBid =
creativeId: bid.crid || bid.id,
currency: bidResponseCurrency || getBidderConfig('defaultCurrency'),
height: bid.h,
lineItemId: utils.deepAccess(bid, 'ext.liid'),
lineItemId: deepAccess(bid, 'ext.liid'),
mediaType,
meta: {
advertiserDomains: bid.adomain,
Expand All @@ -408,7 +410,7 @@ const impToPrebidBid =

return prebidBid;
} catch (error) {
utils.logError(`${BIDDER_CODE}: Error while building bid`, error);
logError(`${BIDDER_CODE}: Error while building bid`, error);

return null;
}
Expand All @@ -423,7 +425,7 @@ function getVideoSpecificParams(bidRequest, bid) {
}

function buildTargetingMap(bids) {
const impIds = bids.map(({ impid }) => impid).filter(utils.uniques);
const impIds = bids.map(({ impid }) => impid).filter(uniques);
const values = impIds.reduce((result, id) => {
result[id] = {
lineItemIds: [],
Expand Down Expand Up @@ -459,49 +461,49 @@ function buildTargetingMap(bids) {

function hasRequiredParams(bidRequest) {
const value =
utils.deepAccess(bidRequest, 'params.placementId') != null ||
utils.deepAccess(bidRequest, 'params.orgId') != null ||
deepAccess(bidRequest, 'params.placementId') != null ||
deepAccess(bidRequest, 'params.orgId') != null ||
getBidderConfig('orgId') != null;

if (!value) {
utils.logError(`${BIDDER_CODE}: missing orgId and placementId parameter`);
logError(`${BIDDER_CODE}: missing orgId and placementId parameter`);
}

return value;
}

function isValidBannerRequest(bidRequest) {
const value = validateSizes(
utils.deepAccess(bidRequest, 'mediaTypes.banner.sizes', [])
deepAccess(bidRequest, 'mediaTypes.banner.sizes', [])
);

return value;
}

function isValidVideoRequest(bidRequest) {
const value =
utils.isArray(utils.deepAccess(bidRequest, 'mediaTypes.video.mimes')) &&
isArray(deepAccess(bidRequest, 'mediaTypes.video.mimes')) &&
validateVideoSizes(bidRequest);

return value;
}

function validateSize(size) {
return utils.isArray(size) && size.length === 2 && size.every(utils.isNumber);
return isArray(size) && size.length === 2 && size.every(isNumber);
}

function validateSizes(sizes) {
return utils.isArray(sizes) && sizes.length > 0 && sizes.every(validateSize);
return isArray(sizes) && sizes.length > 0 && sizes.every(validateSize);
}

function validateVideoSizes(bidRequest) {
const { w, h } = utils.deepAccess(bidRequest, 'mediaTypes.video', {});
const { w, h } = deepAccess(bidRequest, 'mediaTypes.video', {});

return (
validateSizes(
utils.deepAccess(bidRequest, 'mediaTypes.video.playerSize')
deepAccess(bidRequest, 'mediaTypes.video.playerSize')
) ||
(utils.isNumber(w) && utils.isNumber(h))
(isNumber(w) && isNumber(h))
);
}

Expand All @@ -518,21 +520,21 @@ function validateBid(bidderRequestBody) {
const value = validators.every((validator) => validator(bid, bidRequest));

if (!value) {
utils.logWarn(`${BIDDER_CODE}: Invalid bid`, bid);
logWarn(`${BIDDER_CODE}: Invalid bid`, bid);
}

return value;
};
}

const commonBidValidators = [
(bid) => utils.isPlainObject(bid),
(bid) => isPlainObject(bid),
(bid) => isNonEmptyStr(bid.adid),
(bid) => isNonEmptyStr(bid.adm),
(bid) => isNonEmptyStr(bid.id),
(bid) => isNonEmptyStr(bid.impid),
(bid) => isNonEmptyStr(utils.deepAccess(bid, 'ext.liid')),
(bid) => utils.isNumber(bid.price),
(bid) => isNonEmptyStr(deepAccess(bid, 'ext.liid')),
(bid) => isNumber(bid.price),
];

const bannerBidValidators = [
Expand All @@ -546,12 +548,12 @@ function validateBannerDimension(dimension) {
return bannerHasSingleSize(bidRequest);
}

return utils.isNumber(bid[dimension]);
return isNumber(bid[dimension]);
};
}

function bannerHasSingleSize(bidRequest) {
return utils.deepAccess(bidRequest, 'banner.format', []).length === 1;
return deepAccess(bidRequest, 'banner.format', []).length === 1;
}

/**
Expand All @@ -562,9 +564,9 @@ export const storage = getStorageManager();

function getUserSyncs(syncOptions, responses, gdprConsent, uspConsent) {
return responses
.map((response) => utils.deepAccess(response, 'body.ext.sync'))
.filter(utils.isArray)
.reduce(utils.flatten, [])
.map((response) => deepAccess(response, 'body.ext.sync'))
.filter(isArray)
.reduce(flatten, [])
.filter(validateSync(syncOptions))
.map(applyConsents(gdprConsent, uspConsent));
}
Expand Down Expand Up @@ -644,11 +646,11 @@ function onBidWon(bid) {
const wurl = buildWinUrl(bid);

if (wurl !== null) {
utils.triggerPixel(wurl);
triggerPixel(wurl);
}

if (utils.isStr(bid.nurl)) {
utils.triggerPixel(bid.nurl);
if (isStr(bid.nurl)) {
triggerPixel(bid.nurl);
}
}

Expand All @@ -662,7 +664,7 @@ function buildWinUrl(bid) {

return url.toString();
} catch (_) {
utils.logError(
logError(
`${BIDDER_CODE}: Could not build win tracking URL with %s`,
getBidderConfig('winTrackingUrl')
);
Expand All @@ -686,7 +688,7 @@ function getMediaType(adm) {

const markup = safeJSONparse(adm.replace(/\\/g, ''));

if (markup && utils.isPlainObject(markup.native)) {
if (markup && isPlainObject(markup.native)) {
return NATIVE;
}

Expand All @@ -702,7 +704,7 @@ function safeJSONparse(...args) {
}

function isNonEmptyStr(value) {
return utils.isStr(value) && !utils.isEmptyStr(value);
return isStr(value) && !isEmptyStr(value);
}

function findBidRequest(bidderRequest, bid) {
Expand Down Expand Up @@ -782,7 +784,7 @@ const getUrlBuilder = function (url) {

export const common = {
generateUUID: function () {
return utils.generateUUID();
return generateUUID();
},
getConfig: function (property) {
return config.getConfig(property);
Expand Down
Loading

0 comments on commit 9c7d944

Please sign in to comment.