@adhese/sdk 0.8.0 → 0.9.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
@@ -121,6 +121,56 @@ function createSafeFrame({
121
121
  dispose
122
122
  };
123
123
  }
124
+ const name = "@adhese/sdk";
125
+ const type = "module";
126
+ const version = "0.9.0";
127
+ const description = "Adhese SDK";
128
+ const license = "GPL-3.0";
129
+ const repository = {
130
+ type: "git",
131
+ url: "git+https://github.com/adhese/sdk_typescript.git"
132
+ };
133
+ const main = "./dist/index.cjs";
134
+ const module = "./dist/index.js";
135
+ const types = "./dist/index.d.ts";
136
+ const files = [
137
+ "LICENSE",
138
+ "README.md",
139
+ "dist"
140
+ ];
141
+ const scripts = {
142
+ dev: "vitest --config vite.config.npm.ts --silent",
143
+ "dev:verbose": "vitest --config vite.config.npm.ts",
144
+ test: "vitest --config vite.config.npm.ts --silent --run --coverage",
145
+ "test:verbose": "vitest --config vite.config.npm.ts --run --coverage",
146
+ build: "npm run build:npm && npm run build:standalone",
147
+ "build:npm": "vite build --config vite.config.npm.ts && tsup src/index.ts --dts-only --minify --format esm",
148
+ "build:standalone": "vite build --config vite.config.standalone.ts",
149
+ lint: "eslint . --ignore-pattern **/*.md/**/*",
150
+ "lint:fix": "eslint --fix . --ignore-pattern **/*.md/**/*",
151
+ clean: "rm -rf dist",
152
+ typecheck: "tsc --noEmit",
153
+ prepareRelease: "npm run build"
154
+ };
155
+ const dependencies = {
156
+ "@vue/runtime-core": "^3.4.21",
157
+ remeda: "^1.61.0",
158
+ zod: "^3.23.0"
159
+ };
160
+ const packageJson = {
161
+ name,
162
+ type,
163
+ version,
164
+ description,
165
+ license,
166
+ repository,
167
+ main,
168
+ module,
169
+ types,
170
+ files,
171
+ scripts,
172
+ dependencies
173
+ };
124
174
  function renderIframe(ad, element) {
125
175
  const iframe = document.createElement("iframe");
126
176
  iframe.srcdoc = `
@@ -199,6 +249,79 @@ function createQueryDetector({
199
249
  dispose
200
250
  };
201
251
  }
252
+ const hookMap = /* @__PURE__ */ new Map();
253
+ function clearAllHooks() {
254
+ hookMap.clear();
255
+ }
256
+ function createAsyncHook(name2, {
257
+ onRun,
258
+ onAdd
259
+ } = {}) {
260
+ hookMap.set(name2, /* @__PURE__ */ new Set());
261
+ const run = async (arg) => {
262
+ let latestResult = arg;
263
+ for (const callback of hookMap.get(name2) ?? [])
264
+ latestResult = await callback(latestResult) ?? latestResult;
265
+ onRun == null ? void 0 : onRun(hookMap.get(name2));
266
+ return latestResult;
267
+ };
268
+ return [run, (callback) => add(callback, { name: name2, onAdd })];
269
+ }
270
+ function createSyncHook(name2, {
271
+ onRun,
272
+ onAdd
273
+ } = {}) {
274
+ hookMap.set(name2, /* @__PURE__ */ new Set());
275
+ const run = (arg) => {
276
+ let latestResult = arg;
277
+ const promisedCallbacks = [];
278
+ for (const callback of hookMap.get(name2) ?? []) {
279
+ if (isCallbackAsync(callback))
280
+ promisedCallbacks.push(callback);
281
+ else
282
+ latestResult = callback(latestResult) ?? latestResult;
283
+ }
284
+ Promise.allSettled(promisedCallbacks.map((callback) => callback(latestResult))).catch(console.trace);
285
+ onRun == null ? void 0 : onRun(hookMap.get(name2));
286
+ return latestResult;
287
+ };
288
+ return [run, (callback) => add(callback, { name: name2, onAdd })];
289
+ }
290
+ function add(callback, {
291
+ name: name2,
292
+ onAdd
293
+ }) {
294
+ const hookSet = hookMap.get(name2);
295
+ if (hookSet)
296
+ hookSet.add(callback);
297
+ else
298
+ hookMap.set(name2, /* @__PURE__ */ new Set([callback]));
299
+ onAdd == null ? void 0 : onAdd(hookSet);
300
+ return () => {
301
+ var _a;
302
+ (_a = hookMap.get(name2)) == null ? void 0 : _a.delete(callback);
303
+ };
304
+ }
305
+ function isCallbackAsync(callback) {
306
+ return callback.constructor.name === "AsyncFunction";
307
+ }
308
+ let resolveOnInitPromise = () => {
309
+ };
310
+ let isInit = false;
311
+ const waitOnInit = new Promise((resolve) => {
312
+ resolveOnInitPromise = resolve;
313
+ });
314
+ const [runOnInit, onInit] = createSyncHook("onInit", {
315
+ onRun(callbacks) {
316
+ isInit = true;
317
+ resolveOnInitPromise();
318
+ callbacks == null ? void 0 : callbacks.clear();
319
+ },
320
+ onAdd() {
321
+ if (isInit)
322
+ runOnInit();
323
+ }
324
+ });
202
325
  const defaultLogLevels = ["trace", "debug", "info", "warn", "error"];
203
326
  function createLogger({
204
327
  scope,
@@ -272,56 +395,8 @@ function createLogger({
272
395
  const logger = createLogger({
273
396
  scope: "Adhese SDK"
274
397
  });
275
- const hookMap = /* @__PURE__ */ new Map();
276
- function clearAllHooks() {
277
- hookMap.clear();
278
- }
279
- function createHook(name, {
280
- onRun,
281
- onAdd
282
- } = {}) {
283
- hookMap.set(name, /* @__PURE__ */ new Set());
284
- const run = async (arg) => {
285
- let latestResult = arg;
286
- for (const callback of hookMap.get(name) ?? [])
287
- latestResult = await callback(latestResult) ?? latestResult;
288
- onRun == null ? void 0 : onRun(hookMap.get(name));
289
- return latestResult;
290
- };
291
- function add(callback) {
292
- const hookSet = hookMap.get(name);
293
- if (hookSet)
294
- hookSet.add(callback);
295
- else
296
- hookMap.set(name, /* @__PURE__ */ new Set([callback]));
297
- onAdd == null ? void 0 : onAdd(hookSet);
298
- return () => {
299
- var _a;
300
- (_a = hookMap.get(name)) == null ? void 0 : _a.delete(callback);
301
- };
302
- }
303
- return [run, add];
304
- }
305
- let resolveOnInitPromise = () => {
306
- };
307
- let isInit = false;
308
- const waitOnInit = new Promise((resolve) => {
309
- resolveOnInitPromise = resolve;
310
- });
311
- const [runOnInit, onInit] = createHook("onInit", {
312
- onRun(callbacks) {
313
- isInit = true;
314
- resolveOnInitPromise();
315
- logger.debug("Initialization completed");
316
- callbacks == null ? void 0 : callbacks.clear();
317
- },
318
- onAdd() {
319
- if (isInit)
320
- runOnInit().catch(logger.error);
321
- }
322
- });
323
- const [runOnRequest, onRequest] = createHook("onRequest");
324
- const [runOnResponse, onResponse] = createHook("onResponse");
398
+ const [runOnRequest, onRequest] = createAsyncHook("onRequest");
399
+ const [runOnResponse, onResponse] = createAsyncHook("onResponse");
325
400
  const numberLike = union([coerce.string().regex(/^\d+$/), literal("")]).transform((value) => value === "" ? void 0 : Number(value));
326
401
  const booleanLike = union([coerce.boolean(), literal("")]);
327
402
  const urlLike = union([coerce.string(), literal("")]).transform((value) => {
@@ -614,8 +689,9 @@ async function requestAd({
614
689
  });
615
690
  return ad;
616
691
  }
617
- const [runOnRender, onRender] = createHook("onRender");
618
- function useViewabilityObserver({ context, ad, name, element }) {
692
+ const [runOnRender, onRender] = createAsyncHook("onRender");
693
+ const [runOnSlotCreate, onSlotCreate] = createSyncHook("onSlotCreate");
694
+ function useViewabilityObserver({ context, ad, name: name2, element }) {
619
695
  let timeoutId = null;
620
696
  const {
621
697
  threshold,
@@ -638,7 +714,7 @@ function useViewabilityObserver({ context, ad, name, element }) {
638
714
  timeoutId = null;
639
715
  if ((_a = ad.value) == null ? void 0 : _a.viewableImpressionCounter) {
640
716
  trackingPixel.value = addTrackingPixel(ad.value.viewableImpressionCounter);
641
- logger.debug(`Viewability tracking pixel fired for ${name.value}`);
717
+ logger.debug(`Viewability tracking pixel fired for ${name2.value}`);
642
718
  (_c = context.events) == null ? void 0 : _c.changeSlots.dispatch(Array.from(((_b = context.getAll) == null ? void 0 : _b.call(context)) ?? []));
643
719
  }
644
720
  }, duration);
@@ -705,9 +781,10 @@ const renderFunctions = {
705
781
  iframe: renderIframe,
706
782
  inline: renderInline
707
783
  };
708
- function createSlot(options) {
784
+ function createSlot(slotOptions) {
709
785
  const scope = effectScope();
710
786
  return scope.run(() => {
787
+ const options = runOnSlotCreate(slotOptions);
711
788
  const {
712
789
  containingElement,
713
790
  slot,
@@ -728,8 +805,8 @@ function createSlot(options) {
728
805
  }
729
806
  const ad = ref(null);
730
807
  const originalAd = ref(ad.value);
731
- const name = computed(() => generateName(context.location, format.value, slot));
732
- watch(name, async (newName, oldName) => {
808
+ const name2 = computed(() => generateName(context.location, format.value, slot));
809
+ watch(name2, async (newName, oldName) => {
733
810
  var _a;
734
811
  if (newName === oldName)
735
812
  return;
@@ -775,7 +852,7 @@ function createSlot(options) {
775
852
  ] = useViewabilityObserver({
776
853
  context,
777
854
  ad,
778
- name,
855
+ name: name2,
779
856
  element
780
857
  });
781
858
  const impressionTrackingPixelElement = ref(null);
@@ -783,7 +860,7 @@ function createSlot(options) {
783
860
  async function requestAd$1() {
784
861
  const response = await requestAd({
785
862
  slot: {
786
- name: name.value,
863
+ name: name2.value,
787
864
  parameters
788
865
  },
789
866
  context
@@ -814,7 +891,7 @@ function createSlot(options) {
814
891
  }
815
892
  if (renderAd.impressionCounter && !impressionTrackingPixelElement.value) {
816
893
  impressionTrackingPixelElement.value = addTrackingPixel(renderAd.impressionCounter);
817
- logger.debug(`Impression tracking pixel fired for ${name.value}`);
894
+ logger.debug(`Impression tracking pixel fired for ${name2.value}`);
818
895
  }
819
896
  logger.debug("Slot rendered", {
820
897
  renderedElement: element,
@@ -823,6 +900,7 @@ function createSlot(options) {
823
900
  containingElement
824
901
  });
825
902
  (_b = options.onRender) == null ? void 0 : _b.call(options, element.value);
903
+ ad.value = renderAd;
826
904
  disposeRenderIntersectionObserver();
827
905
  return element.value;
828
906
  }
@@ -851,7 +929,7 @@ function createSlot(options) {
851
929
  slot,
852
930
  parameters,
853
931
  format,
854
- name,
932
+ name: name2,
855
933
  ad,
856
934
  isViewabilityTracked,
857
935
  isImpressionTracked,
@@ -875,12 +953,12 @@ async function findDomSlots(context) {
875
953
  var _a;
876
954
  if (!element.dataset.format)
877
955
  return false;
878
- const name = generateName(
956
+ const name2 = generateName(
879
957
  context.location,
880
958
  element.dataset.format,
881
959
  element.dataset.slot
882
960
  );
883
- return !((_a = context.getAll) == null ? void 0 : _a.call(context).some((activeSlot) => activeSlot.name.value === name));
961
+ return !((_a = context.getAll) == null ? void 0 : _a.call(context).some((activeSlot) => activeSlot.name.value === name2));
884
962
  }).map((element) => createSlot({
885
963
  format: element.dataset.format,
886
964
  containingElement: element,
@@ -905,7 +983,7 @@ function createSlotManager({
905
983
  function getAll() {
906
984
  return Array.from(slots).map(([, slot]) => slot);
907
985
  }
908
- function add(options) {
986
+ function add2(options) {
909
987
  var _a;
910
988
  const slot = createSlot({
911
989
  ...options,
@@ -941,8 +1019,8 @@ function createSlotManager({
941
1019
  slots.set(slot.name.value, slot);
942
1020
  return domSlots;
943
1021
  }
944
- function get(name) {
945
- return slots.get(name);
1022
+ function get(name2) {
1023
+ return slots.get(name2);
946
1024
  }
947
1025
  function dispose() {
948
1026
  for (const slot of slots.values())
@@ -951,14 +1029,14 @@ function createSlotManager({
951
1029
  scope.stop();
952
1030
  }
953
1031
  for (const options of initialSlots) {
954
- add({
1032
+ add2({
955
1033
  ...options,
956
1034
  lazyLoading: false
957
1035
  });
958
1036
  }
959
1037
  return {
960
1038
  getAll,
961
- add,
1039
+ add: add2,
962
1040
  findDomSlots: findDomSlots$1,
963
1041
  get,
964
1042
  dispose
@@ -1002,15 +1080,14 @@ function isPreviewMode() {
1002
1080
  return window.location.search.includes("adhesePreviewCreativeId");
1003
1081
  }
1004
1082
  let isDisposed = false;
1005
- const [runOnDispose, onDispose] = createHook("onDispose", {
1083
+ const [runOnDispose, onDispose] = createSyncHook("onDispose", {
1006
1084
  onRun(callbacks) {
1007
1085
  isDisposed = true;
1008
- logger.debug("Disposal completed");
1009
1086
  callbacks == null ? void 0 : callbacks.clear();
1010
1087
  },
1011
1088
  onAdd() {
1012
1089
  if (isDisposed)
1013
- runOnDispose().catch(logger.error);
1090
+ runOnDispose();
1014
1091
  }
1015
1092
  });
1016
1093
  function createAdhese(options) {
@@ -1046,7 +1123,14 @@ function createAdhese(options) {
1046
1123
  });
1047
1124
  for (const [index, plugin] of mergedOptions.plugins.entries()) {
1048
1125
  plugin(context, {
1049
- index
1126
+ index,
1127
+ version: packageJson.version,
1128
+ onInit,
1129
+ onDispose,
1130
+ onRender,
1131
+ onRequest,
1132
+ onResponse,
1133
+ onSlotCreate
1050
1134
  });
1051
1135
  }
1052
1136
  context.events = createEventManager();
@@ -1103,8 +1187,8 @@ function createAdhese(options) {
1103
1187
  function getAll() {
1104
1188
  return slotManager.getAll() ?? [];
1105
1189
  }
1106
- function get(name) {
1107
- return slotManager.get(name);
1190
+ function get(name2) {
1191
+ return slotManager.get(name2);
1108
1192
  }
1109
1193
  function addSlot(slotOptions) {
1110
1194
  if (!slotManager)
@@ -1175,7 +1259,7 @@ function createAdhese(options) {
1175
1259
  logger.resetLogs();
1176
1260
  (_b = context.events) == null ? void 0 : _b.dispose();
1177
1261
  logger.info("Adhese instance disposed");
1178
- runOnDispose().catch(logger.error);
1262
+ runOnDispose();
1179
1263
  clearAllHooks();
1180
1264
  scope.stop();
1181
1265
  }
@@ -1190,7 +1274,7 @@ function createAdhese(options) {
1190
1274
  if (!scope.active)
1191
1275
  dispose();
1192
1276
  });
1193
- runOnInit().catch(logger.error);
1277
+ runOnInit();
1194
1278
  return {
1195
1279
  parameters: context.parameters,
1196
1280
  events: context.events,
@@ -1212,11 +1296,6 @@ function createAdhese(options) {
1212
1296
  export {
1213
1297
  createAdhese,
1214
1298
  logger,
1215
- onDispose,
1216
- onInit,
1217
- onRender,
1218
- onRequest,
1219
- onResponse,
1220
1299
  requestAd,
1221
1300
  requestAds
1222
1301
  };