@nextad/auction 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Auction.ts +24 -8
- package/src/BidComparator.ts +19 -7
- package/src/types/index.ts +5 -26
- package/src/BidFactory.ts +0 -32
package/package.json
CHANGED
package/src/Auction.ts
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
import { BidComparator } from "./BidComparator";
|
2
|
-
import { BidFactory } from "./BidFactory";
|
3
2
|
import {
|
4
3
|
AlreadyEndedAuctionException,
|
5
4
|
BidNotFoundException,
|
6
5
|
} from "./exceptions";
|
7
|
-
import type {
|
6
|
+
import type {
|
7
|
+
BidResponse as BaseBidResponseV26,
|
8
|
+
Bid as BidV26,
|
9
|
+
} from "iab-openrtb/v26";
|
10
|
+
import type { BidInformation, CurrencyConversionData } from "./types";
|
8
11
|
|
9
12
|
type AuctionType = "open" | "closed";
|
10
13
|
|
@@ -22,9 +25,9 @@ export class Auction {
|
|
22
25
|
};
|
23
26
|
|
24
27
|
private itemIds: string[] = [];
|
25
|
-
private currencyConversionData?: CurrencyConversionData;
|
26
28
|
private status: AuctionType;
|
27
29
|
private options: AuctionOptions;
|
30
|
+
private bidInformation: WeakMap<BidV26, BidInformation>;
|
28
31
|
|
29
32
|
public constructor(
|
30
33
|
itemOrImpressionIds: string | string[],
|
@@ -41,8 +44,8 @@ export class Auction {
|
|
41
44
|
this.losingBids = {
|
42
45
|
v26: [],
|
43
46
|
};
|
44
|
-
this.currencyConversionData = options.currencyConversionData;
|
45
47
|
this.status = "open";
|
48
|
+
this.bidInformation = new WeakMap();
|
46
49
|
|
47
50
|
if (typeof itemOrImpressionIds === "string") {
|
48
51
|
this.itemIds.push(itemOrImpressionIds);
|
@@ -61,9 +64,18 @@ export class Auction {
|
|
61
64
|
|
62
65
|
public placeBidResponseV26(bidResponse: BaseBidResponseV26): this {
|
63
66
|
if (bidResponse.seatbid) {
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
const bids: BidV26[] = bidResponse.seatbid.flatMap((seatbid) => {
|
68
|
+
return seatbid.bid.map((bid) => {
|
69
|
+
this.bidInformation.set(bid, {
|
70
|
+
currency: bidResponse.cur,
|
71
|
+
version: "2.6",
|
72
|
+
seat: seatbid.seat,
|
73
|
+
});
|
74
|
+
return bid;
|
75
|
+
});
|
76
|
+
});
|
77
|
+
|
78
|
+
this.bids.v26.push(...bids);
|
67
79
|
}
|
68
80
|
|
69
81
|
return this;
|
@@ -78,7 +90,11 @@ export class Auction {
|
|
78
90
|
throw new BidNotFoundException();
|
79
91
|
}
|
80
92
|
|
81
|
-
const highestBid = BidComparator.getHighestBidV26(
|
93
|
+
const highestBid = BidComparator.getHighestBidV26(
|
94
|
+
this.bids.v26,
|
95
|
+
this.bidInformation,
|
96
|
+
this.options.currencyConversionData
|
97
|
+
);
|
82
98
|
|
83
99
|
this.setLosingBids(highestBid);
|
84
100
|
this.handleLossBids();
|
package/src/BidComparator.ts
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
import type {
|
1
|
+
import type { BidInformation, CurrencyConversionData } from "./types";
|
2
|
+
import type { Bid as BidV26 } from "iab-openrtb/v26";
|
2
3
|
|
3
4
|
export class BidComparator {
|
4
5
|
public static getHighestBidV26(
|
5
6
|
bids: BidV26[],
|
7
|
+
bidInformation: WeakMap<BidV26, BidInformation>,
|
6
8
|
currencyConversionData?: CurrencyConversionData
|
7
9
|
): BidV26 {
|
8
10
|
if (!currencyConversionData) {
|
@@ -14,19 +16,29 @@ export class BidComparator {
|
|
14
16
|
}
|
15
17
|
|
16
18
|
return bids.reduce((highest, current) => {
|
17
|
-
const highestPrice = this.convertPrice(
|
18
|
-
|
19
|
+
const highestPrice = this.convertPrice(
|
20
|
+
highest,
|
21
|
+
bidInformation,
|
22
|
+
currencyConversionData
|
23
|
+
);
|
24
|
+
const currentPrice = this.convertPrice(
|
25
|
+
current,
|
26
|
+
bidInformation,
|
27
|
+
currencyConversionData
|
28
|
+
);
|
19
29
|
return currentPrice > highestPrice ? current : highest;
|
20
30
|
});
|
21
31
|
}
|
22
32
|
|
23
33
|
private static convertPrice(
|
24
|
-
bid: BidV26,
|
34
|
+
bid: BidV26,
|
35
|
+
bidInformation: WeakMap<BidV26, BidInformation>,
|
25
36
|
conversionData: CurrencyConversionData
|
26
37
|
): number {
|
27
38
|
const price = bid.price;
|
28
|
-
const rates =
|
29
|
-
|
39
|
+
const rates =
|
40
|
+
conversionData.conversions[bidInformation.get(bid)?.currency || "USD"];
|
41
|
+
|
30
42
|
if (!rates) {
|
31
43
|
return price * 100;
|
32
44
|
}
|
@@ -35,4 +47,4 @@ export class BidComparator {
|
|
35
47
|
const rate = rates[targetCurrency];
|
36
48
|
return price * (rate || 1) * 100;
|
37
49
|
}
|
38
|
-
}
|
50
|
+
}
|
package/src/types/index.ts
CHANGED
@@ -1,29 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
}
|
6
|
-
export type {
|
7
|
-
BidResponse as BaseBidResponseV26,
|
8
|
-
Bid as BaseBidV26,
|
9
|
-
} from "iab-openrtb/v26";
|
10
|
-
|
11
|
-
export interface BidResponseV26 extends BaseBidResponseV26 {
|
12
|
-
seatbid?: SeatBidV26[];
|
13
|
-
}
|
14
|
-
export interface SeatBidV26 extends BaseSeatBidV26 {
|
15
|
-
bid: BidV26[];
|
16
|
-
}
|
17
|
-
|
18
|
-
export interface BidV26 extends BaseBidV26 {
|
19
|
-
ext: {
|
20
|
-
nextadjs: {
|
21
|
-
openrtbVersion: "2.6";
|
22
|
-
seat?: string;
|
23
|
-
currency: string;
|
24
|
-
};
|
25
|
-
};
|
26
|
-
}
|
1
|
+
export type BidInformation = {
|
2
|
+
currency?: string;
|
3
|
+
seat?: string;
|
4
|
+
version: string;
|
5
|
+
};
|
27
6
|
|
28
7
|
|
29
8
|
export interface CurrencyRates {
|
package/src/BidFactory.ts
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
import type { BaseBidResponseV26, BidV26 } from "./types";
|
2
|
-
|
3
|
-
export class BidFactory {
|
4
|
-
public static createV26Bids(
|
5
|
-
bidResponse: BaseBidResponseV26,
|
6
|
-
itemIds: string[]
|
7
|
-
): BidV26[] {
|
8
|
-
let bids: BidV26[] = [];
|
9
|
-
|
10
|
-
if (bidResponse.seatbid) {
|
11
|
-
bidResponse.seatbid.forEach((seatBid) => {
|
12
|
-
seatBid.bid.forEach((bid) => {
|
13
|
-
if (itemIds.some((itemId) => bid.impid === itemId)) {
|
14
|
-
bids.push({
|
15
|
-
...bid,
|
16
|
-
ext: {
|
17
|
-
...bid.ext,
|
18
|
-
nextadjs: {
|
19
|
-
openrtbVersion: "2.6",
|
20
|
-
currency: bidResponse.cur || "USD",
|
21
|
-
seat: seatBid.seat,
|
22
|
-
},
|
23
|
-
},
|
24
|
-
});
|
25
|
-
}
|
26
|
-
});
|
27
|
-
});
|
28
|
-
}
|
29
|
-
|
30
|
-
return bids;
|
31
|
-
}
|
32
|
-
}
|