Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion crates/js/lib/src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Global configuration storage for the tsjs runtime (logging, debug, etc.).
// Global configuration storage for the tsjs runtime (mode, logging, etc.).
import { log, LogLevel } from './log';
import type { Config, GamConfig } from './types';
import { RequestMode } from './types';

export interface Config {
debug?: boolean;
Expand All @@ -9,13 +11,41 @@ export interface Config {

let CONFIG: Config = {};


// Lazy import to avoid circular dependencies - GAM integration may not be present
let setGamConfigFn: ((cfg: GamConfig) => void) | null | undefined = undefined;

function getSetGamConfig(): ((cfg: GamConfig) => void) | null {
if (setGamConfigFn === undefined) {
try {
// Dynamic import path - bundler will include if gam integration is present
// eslint-disable-next-line @typescript-eslint/no-require-imports
const gam = require('../integrations/gam/index');
setGamConfigFn = gam.setGamConfig || null;
} catch {
// GAM integration not available
setGamConfigFn = null;
}
}
return setGamConfigFn ?? null;
}

// Merge publisher-provided config and adjust the log level accordingly.
export function setConfig(cfg: Config): void {
CONFIG = { ...CONFIG, ...cfg };
const debugFlag = cfg.debug;
const l = cfg.logLevel as LogLevel | undefined;
if (typeof l === 'string') log.setLevel(l);
else if (debugFlag === true) log.setLevel('debug');

// Forward GAM config to the GAM integration if present
if (cfg.gam) {
const setGam = getSetGamConfig();
if (setGam) {
setGam(cfg.gam);
}
}

log.info('setConfig:', cfg);
}

Expand Down
49 changes: 47 additions & 2 deletions crates/js/lib/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export interface TsjsApi {
addAdUnits(units: AdUnit | AdUnit[]): void;
renderAdUnit(codeOrUnit: string | AdUnit): void;
renderAllAdUnits(): void;
setConfig?(cfg: Record<string, unknown>): void;
getConfig?(): Record<string, unknown>;
setConfig?(cfg: Config): void;
getConfig?(): Config;
requestAds?(opts?: { bidsBackHandler?: () => void; timeout?: number }): void;
requestAds?(
callback: () => void,
Expand All @@ -42,3 +42,48 @@ export interface TsjsApi {
debug(...args: unknown[]): void;
};
}

/** GAM interceptor configuration. */
export interface GamConfig {
/** Enable the GAM interceptor. Defaults to false. */
enabled?: boolean;
/** Only intercept bids from these bidders. Empty array = all bidders. */
bidders?: string[];
/** Force render Prebid creative even if GAM returned a line item. Defaults to false. */
forceRender?: boolean;
}

export interface Config {
debug?: boolean;
logLevel?: 'silent' | 'error' | 'warn' | 'info' | 'debug';
/** Select ad serving mode. Default is RequestMode.FirstParty. */
mode?: RequestMode;
/** GAM interceptor configuration. */
gam?: GamConfig;
// Extendable for future fields
[key: string]: unknown;
}

// Core-neutral request types
export type RequestAdsCallback = () => void;
export interface RequestAdsOptions {
bidsBackHandler?: RequestAdsCallback;
timeout?: number;
}

// Back-compat aliases for Prebid-style naming (used by the extension shim)
export type RequestBidsCallback = RequestAdsCallback;

export interface HighestCpmBid {
adUnitCode: string;
width: number;
height: number;
cpm: number;
currency: string;
bidderCode: string;
creativeId: string;
adserverTargeting: Record<string, string>;
}

// Minimal OpenRTB response typing
// OpenRTB response typing is specific to the Prebid extension and lives in src/ext/types.ts
Loading