@proveanything/smartlinks 2.1.22 → 2.1.23

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.
@@ -1,4 +1,5 @@
1
1
  // src/utils/conditions.ts
2
+ import { request } from '../http.js';
2
3
  /**
3
4
  * Geographic region definitions for country-based conditions
4
5
  */
@@ -16,6 +17,15 @@ export const REGION_COUNTRIES = {
16
17
  };
17
18
  // Condition cache
18
19
  const conditionCache = {};
20
+ /** The default loader: a collection's saved condition, from the public API; null when there isn't one. */
21
+ async function fetchSavedCondition(collectionId, conditionId) {
22
+ try {
23
+ return await request(`/public/collection/${encodeURIComponent(collectionId)}/condition/${encodeURIComponent(conditionId)}`);
24
+ }
25
+ catch (_a) {
26
+ return null;
27
+ }
28
+ }
19
29
  const CONDITION_DEBUG_GLOBAL_KEY = 'SMARTLINKS_CONDITION_DEBUG';
20
30
  function defaultConditionDebugLogger(...args) {
21
31
  if (typeof console === 'undefined')
@@ -227,7 +237,7 @@ async function evaluateConditionEntry(condition, params) {
227
237
  * ```
228
238
  */
229
239
  export async function validateCondition(params) {
230
- var _a, _b, _c;
240
+ var _a, _b, _c, _d;
231
241
  const internalParams = params;
232
242
  (_a = internalParams.__conditionDebugState) !== null && _a !== void 0 ? _a : (internalParams.__conditionDebugState = resolveConditionDebugState(params));
233
243
  const debugState = internalParams.__conditionDebugState;
@@ -242,9 +252,9 @@ export async function validateCondition(params) {
242
252
  // Check cache first
243
253
  if (!conditionCache[params.conditionId]) {
244
254
  logConditionDebug(debugState, 'Condition cache miss.', { conditionId: params.conditionId });
245
- // Try to fetch if function provided
246
- if (params.fetchCondition && params.collection) {
247
- const fetchedCond = await params.fetchCondition(params.collection.id, params.conditionId);
255
+ // Fetch it: the caller's own loader, else the collection's saved conditions
256
+ if (params.collection) {
257
+ const fetchedCond = await ((_b = params.fetchCondition) !== null && _b !== void 0 ? _b : fetchSavedCondition)(params.collection.id, params.conditionId);
248
258
  if (fetchedCond) {
249
259
  conditionCache[params.conditionId] = fetchedCond;
250
260
  logConditionDebug(debugState, 'Fetched condition into cache.', {
@@ -259,7 +269,7 @@ export async function validateCondition(params) {
259
269
  return true;
260
270
  }
261
271
  // Prevent infinite recursion
262
- (_b = params.conditionStack) !== null && _b !== void 0 ? _b : (params.conditionStack = []);
272
+ (_c = params.conditionStack) !== null && _c !== void 0 ? _c : (params.conditionStack = []);
263
273
  params.conditionStack.push(params.conditionId);
264
274
  cond = conditionCache[params.conditionId];
265
275
  }
@@ -268,12 +278,17 @@ export async function validateCondition(params) {
268
278
  conditionSet: cond ? summarizeConditionSet(cond) : 'none',
269
279
  });
270
280
  }
281
+ // A switched-off condition passes for everyone
282
+ if (cond === null || cond === void 0 ? void 0 : cond.disabled) {
283
+ logConditionDebug(debugState, 'Condition is switched off; passing.');
284
+ return true;
285
+ }
271
286
  if (!cond) {
272
287
  logConditionDebug(debugState, 'Resolved condition set is empty; passing by default.');
273
288
  return true;
274
289
  }
275
290
  // Default to AND logic
276
- const conditionType = (_c = cond.type) !== null && _c !== void 0 ? _c : 'and';
291
+ const conditionType = (_d = cond.type) !== null && _d !== void 0 ? _d : 'and';
277
292
  // Empty condition set passes
278
293
  if (!cond.conditions || cond.conditions.length === 0) {
279
294
  logConditionDebug(debugState, 'Condition set has no entries; passing by default.', {