diff --git a/lib/config.ts b/lib/config.ts index 7a40b45..9df1b31 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -1,7 +1,7 @@ import { getConsent, inferRegulation } from "./core/regs/consent"; import type { CMPApiConfig, Consent } from "./core/regs/consent"; -type Experiment = "tokenize-v2"; +type Experiment = "tokenize-v2" | "targeting-cascade"; type InitConsent = { // A "cmpapi" configuration indicating that consent should be gathered from CMP apis. diff --git a/lib/edge/targeting.ts b/lib/edge/targeting.ts index 6c1505f..827459b 100644 --- a/lib/edge/targeting.ts +++ b/lib/edge/targeting.ts @@ -27,7 +27,7 @@ type TargetingResponse = { ortb2: { user: ortb2.User }; }; -async function Targeting(config: ResolvedConfig, ...ids: readonly string[]): Promise { +async function Targeting(config: ResolvedConfig, ids: readonly string[]): Promise { const searchParams = new URLSearchParams(ids.map((id) => ["id", id])); const path = "/v2/targeting?" + searchParams.toString(); diff --git a/lib/sdk.test.ts b/lib/sdk.test.ts index cc75e82..0310a74 100644 --- a/lib/sdk.test.ts +++ b/lib/sdk.test.ts @@ -395,8 +395,10 @@ describe("behavior testing of", () => { }) ); - const targetingWithParam = await sdk.targeting("someId", "someOtherId"); - expect(targetingWithParam).toBeDefined(); + await expect(sdk.targeting({ ids: ["someId", "someOtherId"] })).rejects.toMatch(/targeting-cascade/); + + sdk.dcn.experiments = ["targeting-cascade"]; + const targetingWithParam = await sdk.targeting({ ids: ["someId", "someOtherId"] }); expect(fetchSpy).toHaveBeenLastCalledWith( expect.objectContaining({ diff --git a/lib/sdk.ts b/lib/sdk.ts index eb080b9..541607e 100644 --- a/lib/sdk.ts +++ b/lib/sdk.ts @@ -20,6 +20,12 @@ import { Profile } from "./edge/profile"; import { sha256 } from "js-sha256"; import { Tokenize, TokenizeResponse } from "./edge/tokenize"; +type TargetingRequest = + | string + | { + ids?: string[]; + }; + class OptableSDK { public static version = buildInfo.version; @@ -54,13 +60,15 @@ class OptableSDK { return Uid2Token(this.dcn, id); } - async targeting(...ids: string[]): Promise { - if (!ids.length) { - ids = ["__passport__"]; + async targeting(request: TargetingRequest = "__passport__"): Promise { + const ids = typeof request === "string" ? [request] : request?.ids || []; + + if (ids.length > 1 && !this.dcn.experiments.includes("targeting-cascade")) { + throw "Targeting multiple IDs is only available with the 'targeting-cascade' experiment enabled."; } await this.init; - return Targeting(this.dcn, ...ids); + return Targeting(this.dcn, ids); } targetingFromCache(): TargetingResponse | null {