@proveanything/smartlinks 2.1.22 → 2.1.24

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.
@@ -0,0 +1,121 @@
1
+ /** A value a field can be compared with. Dates are ISO 8601 strings. */
2
+ export type QueryValue = string | number | boolean;
3
+ /** How one field is tested. Several tests on one field must all hold. */
4
+ export interface QueryFieldTest {
5
+ eq?: QueryValue;
6
+ ne?: QueryValue;
7
+ gt?: QueryValue;
8
+ gte?: QueryValue;
9
+ lt?: QueryValue;
10
+ lte?: QueryValue;
11
+ /** One of these values (1 to 100). */
12
+ in?: QueryValue[];
13
+ /** None of these values (1 to 100). */
14
+ nin?: QueryValue[];
15
+ /** Text containing this, in any case. */
16
+ contains?: string;
17
+ /** Text starting with this, in any case. */
18
+ startsWith?: string;
19
+ /** true: the field is empty. false: it has a value. */
20
+ isNull?: boolean;
21
+ /** A list field (e.g. a contact's tags) holds this value. */
22
+ has?: string;
23
+ /** A list field holds at least one of these. */
24
+ hasAny?: string[];
25
+ /** A list field holds all of these. */
26
+ hasAll?: string[];
27
+ }
28
+ /**
29
+ * A keyed field (e.g. a product's `facets`) is filtered key by key: `{ brand: "Breville" }` or
30
+ * `{ category: { in: ["Toasters", "Kettles"] } }`. Every key must match.
31
+ */
32
+ export type QueryKeyedTest = Record<string, QueryValue | {
33
+ eq?: QueryValue;
34
+ in?: QueryValue[];
35
+ }>;
36
+ /**
37
+ * What to match. Each entry is `field: value` (equal to) or `field: { op: value }`; all entries must
38
+ * hold. `and`, `or` and `not` combine further conditions (nested up to 4 levels).
39
+ *
40
+ * A document field (an app record's `data`) is reached by path, as a field name: `"data.status"`.
41
+ */
42
+ export interface QueryWhere {
43
+ and?: QueryWhere[];
44
+ or?: QueryWhere[];
45
+ not?: QueryWhere;
46
+ [field: string]: QueryValue | QueryFieldTest | QueryKeyedTest | QueryWhere | QueryWhere[] | undefined;
47
+ }
48
+ /** One total. `count` counts rows; the others take a field (or a path into a document). */
49
+ export type QueryAggregate = {
50
+ count: true;
51
+ } | {
52
+ countDistinct: string;
53
+ } | {
54
+ sum: string;
55
+ } | {
56
+ avg: string;
57
+ } | {
58
+ min: string;
59
+ } | {
60
+ max: string;
61
+ };
62
+ /** The standard query body. Everything is optional: an empty query returns the first page. */
63
+ export interface StandardQuery {
64
+ where?: QueryWhere;
65
+ /** e.g. `[{ createdAt: "desc" }]`. Only fields that can be sorted on (never an empty-able one). */
66
+ orderBy?: Array<Record<string, 'asc' | 'desc'>>;
67
+ /** Rows per page (1 to 200, default 50), or groups (1 to 500) with groupBy / aggregates. */
68
+ limit?: number;
69
+ /** `nextCursor` from the previous page, with the same `where` and `orderBy`. */
70
+ cursor?: string | null;
71
+ /** Which fields to return. A path into a document can be selected: `"data.status"`. */
72
+ select?: string[];
73
+ /** Also return how many rows match `where` in all (not just this page). */
74
+ count?: boolean;
75
+ /** Group rows by up to 3 fields (or document paths). Returns groups, not rows. */
76
+ groupBy?: string[];
77
+ /** Named totals, e.g. `{ owners: { countDistinct: "userId" } }`. Returns groups, not rows. */
78
+ aggregates?: Record<string, QueryAggregate>;
79
+ /**
80
+ * Follow a link and return the linked thing inside each row: `["product", "owner"]`, or with the
81
+ * fields wanted `{ owner: ["firstName", "email"] }`. Rows only. Null where there is nothing linked.
82
+ */
83
+ expand?: string[] | Record<string, string[] | true>;
84
+ }
85
+ /** A page of rows. */
86
+ export interface QueryRows<T = Record<string, unknown>> {
87
+ items: T[];
88
+ /** Pass as `cursor` for the next page; null on the last page. */
89
+ nextCursor: string | null;
90
+ /** Present when the query asked for `count: true`. */
91
+ count?: number;
92
+ }
93
+ /** The answer to a query with `groupBy` and/or `aggregates`. */
94
+ export interface QueryGroups<G = Record<string, unknown>> {
95
+ groups: G[];
96
+ /** True when there were more groups than `limit`. */
97
+ more: boolean;
98
+ }
99
+ export type QueryResult<T = Record<string, unknown>, G = Record<string, unknown>> = QueryRows<T> | QueryGroups<G>;
100
+ /** What a resource's query can use (from `GET …/query`). */
101
+ export interface QueryDescription {
102
+ name: string;
103
+ fields: Record<string, {
104
+ type: 'string' | 'number' | 'boolean' | 'date' | 'list' | 'keyed' | 'json';
105
+ about?: string;
106
+ can: string[];
107
+ }>;
108
+ count: boolean;
109
+ aggregates: boolean;
110
+ links: Record<string, {
111
+ to: string;
112
+ by: string;
113
+ about?: string;
114
+ }>;
115
+ limits: {
116
+ limit: number;
117
+ groups: number;
118
+ };
119
+ /** The query shape in a few lines of plain words. */
120
+ shape: string;
121
+ }
@@ -0,0 +1,5 @@
1
+ // src/types/query.ts
2
+ //
3
+ // The STANDARD QUERY: one request body for filtering, sorting, paging, counting and totalling, the same
4
+ // on every resource that takes it (proofs, contacts, products, app records). See docs/standard-query.md.
5
+ export {};
@@ -296,7 +296,7 @@ export interface ConditionParams {
296
296
  itemContext?: ItemContext;
297
297
  /** Statistics/tracking information */
298
298
  stats?: StatsInfo;
299
- /** Function to fetch conditions by ID (optional) */
299
+ /** Load a saved condition by id (optional; by default the collection's saved conditions are read with `conditions.get`) */
300
300
  fetchCondition?: (collectionId: string, conditionId: string) => Promise<ConditionSet | null>;
301
301
  /** Function to get user's current location (optional) */
302
302
  getLocation?: () => Promise<{
@@ -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.', {