@adhese/sdk 0.11.1 → 0.12.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.js CHANGED
@@ -1,4 +1,4 @@
1
- import { uniqueId, createEventManager, waitForDomLoad } from "@adhese/sdk-shared";
1
+ import { uniqueId, createLogger, waitForDomLoad, createEventManager } from "@adhese/sdk-shared";
2
2
  import { toValue, ref, computed, watch, effectScope, reactive, shallowReactive, watchEffect } from "@vue/runtime-core";
3
3
  import { debounce, round, isDeepEqual } from "remeda";
4
4
  import { union, coerce, literal, number, string, ZodIssueCode, NEVER, object, unknown, lazy } from "zod";
@@ -56,7 +56,7 @@ function createSafeFrame({
56
56
  }
57
57
  const name = "@adhese/sdk";
58
58
  const type = "module";
59
- const version = "0.11.1";
59
+ const version = "0.12.0";
60
60
  const description = "Adhese SDK";
61
61
  const license = "GPL-3.0";
62
62
  const repository = {
@@ -86,10 +86,10 @@ const scripts = {
86
86
  prepareRelease: "npm run build"
87
87
  };
88
88
  const dependencies = {
89
- "@adhese/sdk-shared": "^0.1.0",
89
+ "@adhese/sdk-shared": "^0.2.0",
90
90
  "@vue/runtime-core": "^3.4.21",
91
91
  remeda: "^1.61.0",
92
- zod: "^3.23.4"
92
+ zod: "^3.23.5"
93
93
  };
94
94
  const packageJson = {
95
95
  name,
@@ -221,17 +221,6 @@ function createSyncHook(name2, {
221
221
  };
222
222
  return [run, (callback) => add(callback, { name: name2, onAdd })];
223
223
  }
224
- function createPassiveHook(name2, {
225
- onRun,
226
- onAdd
227
- } = {}) {
228
- hookMap.set(name2, /* @__PURE__ */ new Set());
229
- function run(arg) {
230
- Promise.allSettled(Array.from(hookMap.get(name2) ?? []).map((callback) => callback(arg))).catch(console.trace);
231
- onRun == null ? void 0 : onRun(hookMap.get(name2));
232
- }
233
- return [run, (callback) => add(callback, { name: name2, onAdd })];
234
- }
235
224
  function isCallbackAsync(callback) {
236
225
  return callback.constructor.name === "AsyncFunction";
237
226
  }
@@ -267,76 +256,6 @@ const [runOnInit, onInit] = createSyncHook("onInit", {
267
256
  runOnInit();
268
257
  }
269
258
  });
270
- const defaultLogLevels = ["trace", "debug", "info", "warn", "error"];
271
- function createLogger({
272
- scope,
273
- logLevels = defaultLogLevels,
274
- minLogLevelThreshold = logLevels[2]
275
- }) {
276
- const logs = /* @__PURE__ */ new Set();
277
- let currentMinLogLevelThreshold = minLogLevelThreshold;
278
- const events = createEventManager();
279
- const logFunctions = Object.fromEntries(logLevels.map((level, index) => {
280
- const logFunction = (message, attributes) => {
281
- logs.add({
282
- scope,
283
- level,
284
- message,
285
- attributes,
286
- timestamp: Date.now(),
287
- id: uniqueId()
288
- });
289
- events.log.dispatch({
290
- scope,
291
- level,
292
- message,
293
- attributes,
294
- timestamp: Date.now(),
295
- id: uniqueId()
296
- });
297
- if (index >= logLevels.indexOf(currentMinLogLevelThreshold)) {
298
- if (["warn", "error", "trace"].includes(level)) {
299
- console[level](...[
300
- `%c${scope}`,
301
- "color: red; font-weight: bold;",
302
- message,
303
- attributes
304
- ].filter(Boolean));
305
- } else {
306
- console.log(...[
307
- `%c${scope} %c${level.toUpperCase()}`,
308
- "color: red; font-weight: bold;",
309
- "font-weight: bold;",
310
- message,
311
- attributes
312
- ].filter(Boolean));
313
- }
314
- }
315
- };
316
- return [level, logFunction];
317
- }));
318
- return {
319
- ...logFunctions,
320
- scope,
321
- events,
322
- setMinLogLevelThreshold(level) {
323
- currentMinLogLevelThreshold = level;
324
- },
325
- resetMinLogLevelThreshold() {
326
- currentMinLogLevelThreshold = minLogLevelThreshold;
327
- },
328
- getMinLogLevelThreshold() {
329
- return currentMinLogLevelThreshold;
330
- },
331
- getLogs() {
332
- return Array.from(logs);
333
- },
334
- resetLogs() {
335
- events.reset.dispatch();
336
- logs.clear();
337
- }
338
- };
339
- }
340
259
  const logger = createLogger({
341
260
  scope: "Adhese SDK"
342
261
  });
@@ -539,7 +458,7 @@ function getPreviewObjects() {
539
458
  return previewObjects;
540
459
  }
541
460
  function requestWithPost({
542
- context: { options: { host }, parameters },
461
+ context,
543
462
  ...options
544
463
  }) {
545
464
  const payload = {
@@ -548,9 +467,9 @@ function requestWithPost({
548
467
  slotname: toValue(slot.name),
549
468
  parameters: parseParameters(slot.parameters)
550
469
  })),
551
- parameters: parameters && parseParameters(parameters)
470
+ parameters: context.parameters && parseParameters(context.parameters)
552
471
  };
553
- return fetch(`${new URL(host).href}json`, {
472
+ return fetch(`${new URL(context.options.host).href}json`, {
554
473
  method: "POST",
555
474
  body: JSON.stringify(payload),
556
475
  headers: {
@@ -619,7 +538,7 @@ async function requestAds(requestOptions) {
619
538
  const options = await runOnRequest(requestOptions);
620
539
  const { context } = options;
621
540
  try {
622
- (_a = context.events) == null ? void 0 : _a.requestAd.dispatch({
541
+ (_a = context == null ? void 0 : context.events) == null ? void 0 : _a.requestAd.dispatch({
623
542
  ...options,
624
543
  context
625
544
  });
@@ -650,13 +569,12 @@ async function requestAds(requestOptions) {
650
569
  return mergedResult;
651
570
  } catch (error) {
652
571
  logger.error(String(error));
653
- (_e = context.events) == null ? void 0 : _e.requestError.dispatch(error);
572
+ (_e = context == null ? void 0 : context.events) == null ? void 0 : _e.requestError.dispatch(error);
654
573
  throw error;
655
574
  }
656
575
  }
657
576
  const [runOnRender, onRender] = createAsyncHook("onRender");
658
577
  const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
659
- const [runOnViewabilityChanged, onViewabilityChanged] = createPassiveHook("onViewabilityChanged");
660
578
  function useViewabilityObserver({ context, ad, name: name2, element }) {
661
579
  let timeoutId = null;
662
580
  const {
@@ -805,11 +723,9 @@ function createSlot(slotOptions) {
805
723
  (_b = context.events) == null ? void 0 : _b.changeSlots.dispatch(Array.from(((_a = context.getAll) == null ? void 0 : _a.call(context)) ?? []));
806
724
  });
807
725
  watch(isInViewport, (value) => {
808
- runOnViewabilityChanged({
809
- name: name2.value,
810
- isInViewport: value
811
- });
812
- }, { immediate: true });
726
+ var _a;
727
+ (_a = options.onViewabilityChanged) == null ? void 0 : _a.call(options, value);
728
+ });
813
729
  const [
814
730
  isViewabilityTracked,
815
731
  disposeViewabilityObserver
@@ -888,7 +804,7 @@ function createSlot(slotOptions) {
888
804
  scope.stop();
889
805
  }
890
806
  return {
891
- location: context.location,
807
+ location: context.location ?? "",
892
808
  lazyLoading: options.lazyLoading ?? false,
893
809
  slot,
894
810
  parameters,
@@ -958,6 +874,10 @@ function createSlotManager({
958
874
  slot.dispose();
959
875
  throw new Error(`Slot with the name: ${slot.name.value} already exists. Create a new slot with a different format, slot, or the location.`);
960
876
  }
877
+ const disposeSlotWatch = watch(slot.name, (newName, previousName) => {
878
+ slots.set(newName, slot);
879
+ slots.delete(previousName);
880
+ });
961
881
  function onDispose2() {
962
882
  var _a2;
963
883
  slots.delete(slot.name.value);
@@ -966,12 +886,9 @@ function createSlotManager({
966
886
  slots: Array.from(slots)
967
887
  });
968
888
  (_a2 = context.events) == null ? void 0 : _a2.removeSlot.dispatch(slot);
889
+ disposeSlotWatch();
969
890
  }
970
891
  slots.set(slot.name.value, slot);
971
- watch(slot.name, (newName, previousName) => {
972
- slots.set(newName, slot);
973
- slots.delete(previousName);
974
- });
975
892
  logger.debug("Slot added", {
976
893
  slot,
977
894
  slots: Array.from(slots.values())
@@ -1085,7 +1002,14 @@ function createAdhese(options) {
1085
1002
  debug: mergedOptions.debug,
1086
1003
  options: mergedOptions,
1087
1004
  logger,
1088
- isDisposed: false
1005
+ isDisposed: false,
1006
+ parameters: /* @__PURE__ */ new Map(),
1007
+ events: createEventManager(),
1008
+ dispose,
1009
+ findDomSlots: findDomSlots2,
1010
+ getAll,
1011
+ get,
1012
+ addSlot
1089
1013
  });
1090
1014
  for (const [index, plugin] of mergedOptions.plugins.entries()) {
1091
1015
  plugin(context, {
@@ -1096,8 +1020,7 @@ function createAdhese(options) {
1096
1020
  onRender,
1097
1021
  onRequest,
1098
1022
  onResponse,
1099
- onSlotCreate,
1100
- onViewabilityChanged
1023
+ onSlotCreate
1101
1024
  });
1102
1025
  }
1103
1026
  context.events = createEventManager();
@@ -1105,32 +1028,25 @@ function createAdhese(options) {
1105
1028
  renderFile: `${mergedOptions.poolHost}/sf/r.html`,
1106
1029
  context
1107
1030
  }) : void 0;
1108
- function getLocation() {
1109
- return context.location;
1110
- }
1111
- function setLocation(newLocation) {
1031
+ watch(() => context.location, (newLocation) => {
1112
1032
  var _a;
1113
- context.location = newLocation;
1114
1033
  (_a = context.events) == null ? void 0 : _a.locationChange.dispatch(newLocation);
1115
- }
1034
+ });
1116
1035
  const queryDetector = createQueryDetector({
1117
1036
  onChange: onQueryChange,
1118
1037
  queries: mergedOptions.queries
1119
1038
  });
1120
1039
  context.parameters = createParameters(mergedOptions, queryDetector);
1121
1040
  watch(
1122
- context.parameters,
1123
- onParametersChange,
1041
+ () => context.parameters,
1042
+ (newParameters) => {
1043
+ var _a;
1044
+ (_a = context.events) == null ? void 0 : _a.parametersChange.dispatch(newParameters);
1045
+ },
1124
1046
  {
1125
- deep: true,
1126
- immediate: true
1047
+ deep: true
1127
1048
  }
1128
1049
  );
1129
- function onParametersChange() {
1130
- var _a;
1131
- if (context.parameters)
1132
- (_a = context.events) == null ? void 0 : _a.parametersChange.dispatch(context.parameters);
1133
- }
1134
1050
  const debouncedFetchAllUnrenderedSlots = debounce(fetchAllUnrenderedSlots, {
1135
1051
  waitMs: 100,
1136
1052
  timing: "both"
@@ -1142,15 +1058,13 @@ function createAdhese(options) {
1142
1058
  (_b = context.parameters) == null ? void 0 : _b.set("br", query);
1143
1059
  await debouncedFetchAllUnrenderedSlots.call();
1144
1060
  }
1145
- function getConsent() {
1146
- return context.consent;
1147
- }
1148
- function setConsent(newConsent) {
1061
+ watch(() => context.consent, (newConsent) => {
1149
1062
  var _a, _b;
1150
1063
  (_a = context.parameters) == null ? void 0 : _a.set("tl", newConsent ? "all" : "none");
1151
- context.consent = newConsent;
1152
1064
  (_b = context.events) == null ? void 0 : _b.consentChange.dispatch(newConsent);
1153
- }
1065
+ }, {
1066
+ immediate: true
1067
+ });
1154
1068
  const slotManager = createSlotManager({
1155
1069
  initialSlots: mergedOptions.initialSlots,
1156
1070
  context
@@ -1164,9 +1078,7 @@ function createAdhese(options) {
1164
1078
  }
1165
1079
  context.get = get;
1166
1080
  function addSlot(slotOptions) {
1167
- const newSlot = slotManager.add(slotOptions);
1168
- debouncedFetchAllUnrenderedSlots.call().catch(logger.error);
1169
- return newSlot;
1081
+ return slotManager.add(slotOptions);
1170
1082
  }
1171
1083
  context.addSlot = addSlot;
1172
1084
  async function findDomSlots2() {
@@ -1184,10 +1096,10 @@ function createAdhese(options) {
1184
1096
  }
1185
1097
  return domSlots;
1186
1098
  }
1187
- async function toggleDebug() {
1099
+ context.findDomSlots = findDomSlots2;
1100
+ watch(() => context.debug, async (newDebug) => {
1188
1101
  var _a, _b;
1189
- context.debug = !context.debug;
1190
- if (context.debug) {
1102
+ if (newDebug) {
1191
1103
  logger.setMinLogLevelThreshold("debug");
1192
1104
  logger.debug("Debug mode enabled");
1193
1105
  (_a = context.events) == null ? void 0 : _a.debugChange.dispatch(true);
@@ -1196,8 +1108,9 @@ function createAdhese(options) {
1196
1108
  logger.setMinLogLevelThreshold("info");
1197
1109
  (_b = context.events) == null ? void 0 : _b.debugChange.dispatch(false);
1198
1110
  }
1199
- return context.debug;
1200
- }
1111
+ }, {
1112
+ immediate: true
1113
+ });
1201
1114
  async function fetchAllUnrenderedSlots() {
1202
1115
  const slots = (slotManager.getAll() ?? []).filter((slot) => !slot.lazyLoading && !slot.ad.value);
1203
1116
  if (slots.length === 0)
@@ -1238,34 +1151,20 @@ function createAdhese(options) {
1238
1151
  clearAllHooks();
1239
1152
  scope.stop();
1240
1153
  }
1154
+ context.dispose = dispose;
1241
1155
  onInit(async () => {
1242
1156
  var _a;
1243
1157
  if ((slotManager.getAll().length ?? 0) > 0)
1244
1158
  await fetchAllUnrenderedSlots().catch(logger.error);
1245
1159
  if (mergedOptions.findDomSlotsOnLoad)
1246
- await findDomSlots2();
1160
+ await (context == null ? void 0 : context.findDomSlots());
1247
1161
  if (mergedOptions.debug || window.location.search.includes("adhese_debug=true") || isPreviewMode())
1248
1162
  (_a = context.events) == null ? void 0 : _a.debugChange.dispatch(true);
1249
1163
  if (!scope.active)
1250
1164
  dispose();
1251
1165
  });
1252
1166
  runOnInit();
1253
- return {
1254
- parameters: context.parameters,
1255
- events: context.events,
1256
- getLocation,
1257
- setLocation,
1258
- getConsent,
1259
- setConsent,
1260
- addSlot,
1261
- findDomSlots: findDomSlots2,
1262
- dispose,
1263
- toggleDebug,
1264
- get: slotManager.get,
1265
- getAll: slotManager.getAll,
1266
- context,
1267
- options: mergedOptions
1268
- };
1167
+ return context;
1269
1168
  });
1270
1169
  }
1271
1170
  export {