@adhese/sdk 0.10.0 → 0.11.0

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/dist/index.d.ts CHANGED
@@ -448,24 +448,21 @@ type SlotManagerOptions = {
448
448
 
449
449
  type AdRequestOptions = {
450
450
  /**
451
- * List of slots you want to fetch the ad for
451
+ * Slot you want to fetch the ad for
452
452
  */
453
- slots: ReadonlyArray<{
453
+ slot: {
454
454
  name: MaybeRef<string>;
455
455
  parameters: Map<string, ReadonlyArray<string> | string>;
456
- }>;
456
+ };
457
457
  context: AdheseContext;
458
458
  };
459
- /**
460
- * Request multiple ads at once from the API
461
- */
462
- declare function requestAds(requestOptions: AdRequestOptions): Promise<ReadonlyArray<Ad>>;
459
+ type AdMultiRequestOptions = Omit<AdRequestOptions, 'slot'> & {
460
+ slots: ReadonlyArray<AdRequestOptions['slot']>;
461
+ };
463
462
  /**
464
463
  * Request a single ad from the API. If you need to fetch multiple ads at once use the `requestAds` function.
465
464
  */
466
- declare function requestAd({ slot, ...options }: Omit<AdRequestOptions, 'slots'> & {
467
- slot: AdRequestOptions['slots'][number];
468
- }): Promise<Ad>;
465
+ declare function requestAd(options: AdRequestOptions): Promise<Ad>;
469
466
 
470
467
  /**
471
468
  * A log entry saved by the logger
@@ -538,7 +535,7 @@ declare const onDispose: (callback: (arg: void) => void | Promise<void>) => () =
538
535
 
539
536
  declare const onRender: (callback: (arg: Ad) => void | Ad | Promise<void | Ad>) => () => void;
540
537
 
541
- declare const onRequest: (callback: (arg: AdRequestOptions) => void | AdRequestOptions | Promise<void | AdRequestOptions>) => () => void;
538
+ declare const onRequest: (callback: (arg: AdMultiRequestOptions) => void | AdMultiRequestOptions | Promise<void | AdMultiRequestOptions>) => () => void;
542
539
 
543
540
  declare const onResponse: (callback: (arg: readonly Ad[]) => void | readonly Ad[] | Promise<void | readonly Ad[]>) => () => void;
544
541
 
@@ -688,7 +685,7 @@ type AdheseEvents = {
688
685
  removeSlot: AdheseSlot;
689
686
  changeSlots: ReadonlyArray<AdheseSlot>;
690
687
  responseReceived: ReadonlyArray<Ad>;
691
- requestAd: AdRequestOptions;
688
+ requestAd: AdMultiRequestOptions;
692
689
  requestError: Error;
693
690
  previewReceived: ReadonlyArray<Ad>;
694
691
  parametersChange: Map<string, ReadonlyArray<string> | string>;
@@ -762,6 +759,7 @@ type AdheseContext = Partial<Pick<Adhese, 'events' | 'getAll' | 'get' | 'paramet
762
759
  logger: typeof logger;
763
760
  debug: boolean;
764
761
  safeFrame?: SafeFrame;
762
+ isDisposed: boolean;
765
763
  };
766
764
 
767
765
  /**
@@ -788,4 +786,4 @@ type AdheseContext = Partial<Pick<Adhese, 'events' | 'getAll' | 'get' | 'paramet
788
786
  */
789
787
  declare function createAdhese(options: AdheseOptions): Readonly<Adhese>;
790
788
 
791
- export { type Ad, type AdRequestOptions, type Adhese, type AdheseContext, type AdheseOptions, type AdhesePlugin, type AdhesePluginInformation, type AdheseSlot, type AdheseSlotOptions, type SlotManager, createAdhese, logger, requestAd, requestAds };
789
+ export { type Ad, type AdRequestOptions, type Adhese, type AdheseContext, type AdheseOptions, type AdhesePlugin, type AdhesePluginInformation, type AdheseSlot, type AdheseSlotOptions, type SlotManager, createAdhese, logger, requestAd };
package/dist/index.js CHANGED
@@ -123,7 +123,7 @@ function createSafeFrame({
123
123
  }
124
124
  const name = "@adhese/sdk";
125
125
  const type = "module";
126
- const version = "0.10.0";
126
+ const version = "0.11.0";
127
127
  const description = "Adhese SDK";
128
128
  const license = "GPL-3.0";
129
129
  const repository = {
@@ -155,7 +155,7 @@ const scripts = {
155
155
  const dependencies = {
156
156
  "@vue/runtime-core": "^3.4.21",
157
157
  remeda: "^1.61.0",
158
- zod: "^3.23.0"
158
+ zod: "^3.23.4"
159
159
  };
160
160
  const packageJson = {
161
161
  name,
@@ -650,6 +650,36 @@ function filterSpecialChars(value) {
650
650
  const specialRegex = /[^\p{L}\p{N}_]/gu;
651
651
  return value.replaceAll(specialRegex, "_");
652
652
  }
653
+ const batch = /* @__PURE__ */ new Map();
654
+ const debouncedRequestAds = debounce(async (context) => {
655
+ if (batch.size === 0)
656
+ return [];
657
+ const ads = await requestAds({
658
+ slots: Array.from(batch.values()).map(({ options }) => options.slot),
659
+ context
660
+ });
661
+ for (const { options, resolve, reject } of batch.values()) {
662
+ const ad = ads.find(({ slotName }) => toValue(slotName) === toValue(options.slot.name));
663
+ if (ad)
664
+ resolve(ad);
665
+ else
666
+ reject(new Error(`Ad: ${toValue(options.slot.name)} not found`));
667
+ }
668
+ batch.clear();
669
+ return ads;
670
+ }, {
671
+ waitMs: 20,
672
+ timing: "trailing"
673
+ });
674
+ async function requestAd(options) {
675
+ const promise = new Promise(
676
+ (resolve, reject) => {
677
+ batch.set(toValue(options.slot.name), { options, resolve, reject });
678
+ }
679
+ );
680
+ await debouncedRequestAds.call(options.context);
681
+ return promise;
682
+ }
653
683
  async function requestAds(requestOptions) {
654
684
  var _a, _b, _c, _d, _e;
655
685
  const options = await runOnRequest(requestOptions);
@@ -690,16 +720,6 @@ async function requestAds(requestOptions) {
690
720
  throw error;
691
721
  }
692
722
  }
693
- async function requestAd({
694
- slot,
695
- ...options
696
- }) {
697
- const [ad] = await requestAds({
698
- slots: [slot],
699
- ...options
700
- });
701
- return ad;
702
- }
703
723
  const [runOnRender, onRender] = createAsyncHook("onRender");
704
724
  const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
705
725
  const [runOnViewabilityChanged, onViewabilityChanged] = createPassiveHook("onViewabilityChanged");
@@ -1000,6 +1020,10 @@ function createSlotManager({
1000
1020
  onDispose: onDispose2,
1001
1021
  context
1002
1022
  });
1023
+ if (slots.has(slot.name.value)) {
1024
+ slot.dispose();
1025
+ throw new Error(`Slot with the name: ${slot.name.value} already exists. Create a new slot with a different format, slot, or the location.`);
1026
+ }
1003
1027
  function onDispose2() {
1004
1028
  var _a2;
1005
1029
  slots.delete(slot.name.value);
@@ -1125,11 +1149,9 @@ function createAdhese(options) {
1125
1149
  location: mergedOptions.location,
1126
1150
  consent: mergedOptions.consent,
1127
1151
  debug: mergedOptions.debug,
1128
- getAll,
1129
- get,
1130
1152
  options: mergedOptions,
1131
1153
  logger,
1132
- addSlot
1154
+ isDisposed: false
1133
1155
  });
1134
1156
  for (const [index, plugin] of mergedOptions.plugins.entries()) {
1135
1157
  plugin(context, {
@@ -1202,22 +1224,25 @@ function createAdhese(options) {
1202
1224
  function getAll() {
1203
1225
  return slotManager.getAll() ?? [];
1204
1226
  }
1227
+ context.getAll = getAll;
1205
1228
  function get(name2) {
1206
1229
  return slotManager.get(name2);
1207
1230
  }
1231
+ context.get = get;
1208
1232
  function addSlot(slotOptions) {
1209
1233
  const newSlot = slotManager.add(slotOptions);
1210
1234
  debouncedFetchAllUnrenderedSlots.call().catch(logger.error);
1211
1235
  return newSlot;
1212
1236
  }
1237
+ context.addSlot = addSlot;
1213
1238
  async function findDomSlots2() {
1214
1239
  const domSlots = (await slotManager.findDomSlots() ?? []).filter((slot) => !slot.lazyLoading);
1215
1240
  if (domSlots.length <= 0)
1216
1241
  return [];
1217
- const ads = await requestAds({
1218
- slots: domSlots,
1242
+ const ads = await Promise.all(domSlots.map((slot) => requestAd({
1243
+ slot,
1219
1244
  context
1220
- });
1245
+ })));
1221
1246
  for (const ad of ads) {
1222
1247
  const slot = slotManager.get(ad.slotName);
1223
1248
  if (slot)
@@ -1243,10 +1268,10 @@ function createAdhese(options) {
1243
1268
  const slots = (slotManager.getAll() ?? []).filter((slot) => !slot.lazyLoading && !slot.ad.value);
1244
1269
  if (slots.length === 0)
1245
1270
  return;
1246
- const ads = await requestAds({
1247
- slots,
1271
+ const ads = await Promise.all(slots.map((slot) => requestAd({
1272
+ slot,
1248
1273
  context
1249
- });
1274
+ })));
1250
1275
  for (const ad of ads) {
1251
1276
  const slot = slotManager.get(ad.slotName);
1252
1277
  if (slot)
@@ -1266,6 +1291,7 @@ function createAdhese(options) {
1266
1291
  });
1267
1292
  function dispose() {
1268
1293
  var _a, _b;
1294
+ context.isDisposed = true;
1269
1295
  queryDetector.dispose();
1270
1296
  slotManager.dispose();
1271
1297
  queryDetector.dispose();
@@ -1311,7 +1337,6 @@ function createAdhese(options) {
1311
1337
  export {
1312
1338
  createAdhese,
1313
1339
  logger,
1314
- requestAd,
1315
- requestAds
1340
+ requestAd
1316
1341
  };
1317
1342
  //# sourceMappingURL=index.js.map