import type {AdUnitCode, BidderCode, ContextIdentifiers, Size} from "./types/common.d.ts"; import type {ORTBImp} from "./types/ortb/request.d.ts"; import type {Bid} from "./bidfactory.ts"; import type {MediaTypes} from "./mediaTypes.ts"; import type {DeepPartial} from "./types/objects.d.ts"; export interface RendererConfig { /** * URL to the renderer script that will be loaded before invoking `render`. */ url?: string /** * Function that tells Prebid.js how to invoke the renderer script to render a bid. */ render(bid: Bid): void; /** * if set to true, this renderer config will be used only when the bid adapter doesn't provide its own renderer. */ backupOnly?: boolean; } // eslint-disable-next-line @typescript-eslint/no-empty-object-type export interface BidderParams { /** * Adapter-specific parameters - to be extended in the adapters */ } export interface BaseAdUnitBidDefinition { /** * Used for conditional ads (sizeMapping or sizeMappingV2 modules). */ labelAny?: string[]; /** * Used for conditional ads (sizeMapping or sizeMappingV2 modules). */ labelAll?: string[]; /** * Custom renderer. Takes precedence over adUnit.renderer, but applies only to this bidder or module. */ renderer?: RendererConfig; /** * OpenRTB first-party data specific to this bidder or module. This is merged with, and takes precedence over, adUnit.ortb2Imp. */ ortb2Imp?: DeepPartial; } export interface AdUnitBidderBid extends BaseAdUnitBidDefinition { /** * Unique code identifying the bidder. */ bidder: BIDDER; module?: undefined | null; /** * Bid request parameters for a given bidder. */ params: BIDDER extends keyof BidderParams ? BidderParams[BIDDER] : Record; /** * One or more s2sConfig.name. If provided, this bid will be requested only from the given S2S instance(s). */ s2sConfigName?: string | string[] } export type AdUnitModuleBidder = 'pbsBidAdapter'; export interface AdUnitModuleBid extends BaseAdUnitBidDefinition { /** * Module code - for requesting bids from modules that are not bid adapters */ module: AdUnitModuleBidder; bidder?: never; params?: { /** * Name given to a PBS configuration. Used to identify specific PBS instances when multiple are in use. */ configName?: string; } } export type AdUnitBidDefinition = AdUnitModuleBid | AdUnitBidderBid | AdUnitBidderBid; export interface AdUnitDefinition { /** * An identifier you create and assign to this ad unit. * Generally this is set to the ad slot name or the div element ID. * Used by setTargetingForGPTAsync() to match which auction is for which ad slot. */ code: AdUnitCode; /** * Bid requests representing demand partners and associated parameters. */ bids?: AdUnitBidDefinition[]; mediaTypes?: MediaTypes; /** * TTL buffer override for this adUnit. */ ttlBuffer?: number; /** * Used to signal OpenRTB Imp objects at the adUnit grain. * Similar to the global ortb2 field used for global first party data configuration, but specific to this adunit. */ ortb2Imp?: DeepPartial; /** * Custom renderer, typically used for outstream video */ renderer?: RendererConfig; /** * Used to flag adUnits as being separately billable. This allows for a publisher to trigger billing manually for winning bids. See pbjs.triggerBilling and onBidBillable for more info. */ deferBilling?: boolean; /** * @deprecated - use mediaType specific size parameters instead. */ sizes?: Size | Size[]; } /** * Ad unit objects generated by Prebid (as opposed to the definitions provided by the publisher) * can have a placeholder "null" bidder to represent s2s-only stored requests. */ interface NullBid extends BaseAdUnitBidDefinition { bidder: null module?: undefined | null; params?: undefined | null; } export type AdUnitBid = AdUnitBidDefinition | NullBid; export interface AdUnit extends ContextIdentifiers, Omit { bids: AdUnitBid[] } const REQUESTS = 'requests'; const WINS = 'wins'; const AUCTIONS = 'auctions'; let adUnits = {}; export function reset() { adUnits = {} } function ensureAdUnit(adunit, bidderCode?) { const adUnit = adUnits[adunit] = adUnits[adunit] || { bidders: {} }; if (bidderCode) { adUnit.bidders[bidderCode] = adUnit.bidders[bidderCode] || {} return adUnit.bidders[bidderCode] } return adUnit; } type AdUnitCounter = (adUnit: AdUnitCode) => number; type BidderCounter = (adUnit: AdUnitCode, bidderCode: BidderCode) => number; type Counter = BY_BIDDER extends true ? BidderCounter : AdUnitCounter; function incrementer(counter, byBidder: BY_BIDDER): Counter { return function (adUnit, bidder?) { const counters = ensureAdUnit(adUnit, byBidder && bidder); counters[counter] = (counters[counter] ?? 0) + 1; return counters[counter]; } } function getter(counter, byBidder: BY_BIDDER): Counter { return function (adUnit, bidder?) { return ensureAdUnit(adUnit, byBidder && bidder)[counter] ?? 0; } } /** * Increments and returns current Adunit counter */ export const incrementRequestsCounter = incrementer(REQUESTS, false); /** * Increments and returns current Adunit requests counter for a bidder */ export const incrementBidderRequestsCounter = incrementer(REQUESTS, true); /** * Increments and returns current Adunit wins counter for a bidder */ export const incrementBidderWinsCounter = incrementer(WINS, true); /** * Increments and returns current Adunit auctions counter */ export const incrementAuctionsCounter = incrementer(AUCTIONS, false); /** * Returns current Adunit counter */ export const getRequestsCounter = getter(REQUESTS, false); /** * Returns current Adunit requests counter for a specific bidder code */ export const getBidderRequestsCounter = getter(REQUESTS, true) /** * Returns current Adunit requests counter for a specific bidder code */ export const getBidderWinsCounter = getter(WINS, true); /** * Returns current Adunit auctions counter */ export const getAuctionsCounter = getter(AUCTIONS, false);