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