@stonecrop/stonecrop 0.55.0 → 0.55.2

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.
@@ -2084,6 +2084,7 @@ export declare class Stonecrop {
2084
2084
  * @param doctype - The doctype
2085
2085
  * @param recordId - The record ID
2086
2086
  * @param recordData - The record data
2087
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2087
2088
  */
2088
2089
  addRecord(doctype: string | Doctype, recordId: string, recordData: any): void;
2089
2090
  /**
@@ -2096,6 +2097,7 @@ export declare class Stonecrop {
2096
2097
  * @param startingValues - A record not saved yet's values before anything is set, such as
2097
2098
  * {@link Registry.composeNewRecord} gives; they run no rules. Only a new record takes them.
2098
2099
  * @returns The record to read and set
2100
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2099
2101
  */
2100
2102
  editRecord(doctype: string | Doctype, recordId: string, startingValues?: Record<string, unknown>): EditableRecord;
2101
2103
  /**
@@ -2103,12 +2105,14 @@ export declare class Stonecrop {
2103
2105
  * @param doctype - The doctype
2104
2106
  * @param recordId - The record ID
2105
2107
  * @returns HST node for the record or undefined
2108
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2106
2109
  */
2107
2110
  getRecordById(doctype: string | Doctype, recordId: string): HSTNode | undefined;
2108
2111
  /**
2109
2112
  * Remove a record from the store
2110
2113
  * @param doctype - The doctype
2111
2114
  * @param recordId - The record ID
2115
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2112
2116
  */
2113
2117
  removeRecord(doctype: string | Doctype, recordId: string): void;
2114
2118
  /**
@@ -2140,6 +2144,7 @@ export declare class Stonecrop {
2140
2144
  * @param doctype - The doctype to check
2141
2145
  * @param recordId - The record ID
2142
2146
  * @returns Object with `ready: true` if all blocked links are loaded, or `ready: false` with `blockedLinks` array
2147
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2143
2148
  */
2144
2149
  isWorkflowReady(doctype: Doctype, recordId: string): {
2145
2150
  ready: boolean;
@@ -2198,6 +2203,7 @@ export declare class Stonecrop {
2198
2203
  * @throws Error if no data client has been configured
2199
2204
  * @throws Error if a slug string is given and no matching doctype is found in the registry
2200
2205
  * @throws Error with code `"RECORD_NOT_FOUND"` if the server returns no record
2206
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2201
2207
  * @public
2202
2208
  */
2203
2209
  getRecord(doctype: string | Doctype, recordId: string, options?: GetRecordOptions): Promise<void>;
@@ -2258,6 +2264,7 @@ export declare class Stonecrop {
2258
2264
  * @param recordId - The record identifier
2259
2265
  * @returns The current state name, `null` for a record not created yet, or an empty string if
2260
2266
  * the doctype has no workflow
2267
+ * @throws Error if the doctype has a workflow and `recordId` is empty, or holds a dot or a bracket
2261
2268
  *
2262
2269
  * @public
2263
2270
  */
@@ -2267,6 +2274,7 @@ export declare class Stonecrop {
2267
2274
  * @param doctype - The doctype metadata
2268
2275
  * @param recordId - The record ID to collect
2269
2276
  * @returns The complete record payload ready for API submission
2277
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2270
2278
  * @public
2271
2279
  */
2272
2280
  collectRecordPayload(doctype: Doctype, recordId: string): Record<string, any>;
@@ -2292,6 +2300,7 @@ export declare class Stonecrop {
2292
2300
  * @param options - Query options (includeNested to control which links are fetched)
2293
2301
  * @throws Error with code `"CLIENT_REQUIRED"` if no data client is configured
2294
2302
  * @throws Error with code `"RECORD_NOT_FOUND"` if the server returns null
2303
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2295
2304
  * @public
2296
2305
  */
2297
2306
  fetchNestedData(path: string, doctype: Doctype, recordId: string, options?: {
@@ -2500,6 +2509,7 @@ export declare interface UseClientActionOptions {
2500
2509
  * @param recordId - The record ID
2501
2510
  * @param linkFieldname - The link fieldname to load
2502
2511
  * @returns LazyLink with loading, loaded, error, reload, and data
2512
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2503
2513
  * @public
2504
2514
  */
2505
2515
  export declare function useLazyLink(doctype: Doctype, recordId: string, linkFieldname: string): LazyLink;
package/dist/stonecrop.js CHANGED
@@ -91,19 +91,33 @@ function parsePath(path) {
91
91
  //#endregion
92
92
  //#region src/record-access.ts
93
93
  /**
94
+ * A record's path in its store, `doctype.recordId`. An ID the store would not keep as one part of a
95
+ * path is refused: an empty one would address every record of the doctype, and one with a dot or a
96
+ * bracket would address part of another record.
97
+ *
98
+ * @param slug - the record's doctype
99
+ * @param recordId - the record's ID
100
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
101
+ */
102
+ function recordPath(slug, recordId) {
103
+ if (recordId === "") throw new Error(`No record ID given for ${slug}`);
104
+ if (parsePath(recordId)[0] !== recordId) throw new Error(`Record ID ${recordId} for ${slug} can't contain a dot`);
105
+ return `${slug}.${recordId}`;
106
+ }
107
+ /**
94
108
  * A saved record's fields, read and set by name through the store that holds it. A set is a store
95
109
  * write, so it runs the rules of the field it sets. A set with no field name throws, rather than
96
110
  * reaching the store as a write to the whole record.
97
111
  *
98
112
  * @param store - the store holding the record
99
- * @param recordPath - the record's path in that store, `doctype.recordId`
113
+ * @param path - the record's path in that store, from {@link recordPath}
100
114
  */
101
- function recordInStore(store, recordPath) {
115
+ function recordInStore(store, path) {
102
116
  return {
103
- get: (fieldname) => store.get(fieldname ? `${recordPath}.${fieldname}` : recordPath),
117
+ get: (fieldname) => store.get(fieldname ? `${path}.${fieldname}` : path),
104
118
  set: (fieldname, value) => {
105
119
  if (parsePath(fieldname).length === 0) throw new Error("Cannot set value on empty path");
106
- store.set(`${recordPath}.${fieldname}`, value);
120
+ store.set(`${path}.${fieldname}`, value);
107
121
  }
108
122
  };
109
123
  }
@@ -1477,11 +1491,13 @@ var Stonecrop = class Stonecrop {
1477
1491
  * @param doctype - The doctype
1478
1492
  * @param recordId - The record ID
1479
1493
  * @param recordData - The record data
1494
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1480
1495
  */
1481
1496
  addRecord(doctype, recordId, recordData) {
1482
1497
  const slug = typeof doctype === "string" ? doctype : doctype.slug;
1498
+ const path = recordPath(slug, recordId);
1483
1499
  this.ensureDoctypeExists(slug);
1484
- this.hstStore.set(`${slug}.${recordId}`, recordData);
1500
+ this.hstStore.set(path, recordData);
1485
1501
  }
1486
1502
  /**
1487
1503
  * The record a form is editing, read and set by field name; setting a field runs its rules. A
@@ -1493,36 +1509,41 @@ var Stonecrop = class Stonecrop {
1493
1509
  * @param startingValues - A record not saved yet's values before anything is set, such as
1494
1510
  * {@link Registry.composeNewRecord} gives; they run no rules. Only a new record takes them.
1495
1511
  * @returns The record to read and set
1512
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1496
1513
  */
1497
1514
  editRecord(doctype, recordId, startingValues) {
1498
1515
  const slug = typeof doctype === "string" ? doctype : doctype.slug;
1499
1516
  if (isDraftRecordId(recordId)) return newRecord(slug, this.hstStore, startingValues);
1500
1517
  if (startingValues) throw new Error(`Starting values are only for a new record, not ${slug} ${recordId}`);
1501
1518
  this.ensureDoctypeExists(slug);
1502
- return recordInStore(this.hstStore, `${slug}.${recordId}`);
1519
+ return recordInStore(this.hstStore, recordPath(slug, recordId));
1503
1520
  }
1504
1521
  /**
1505
1522
  * Get a specific record
1506
1523
  * @param doctype - The doctype
1507
1524
  * @param recordId - The record ID
1508
1525
  * @returns HST node for the record or undefined
1526
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1509
1527
  */
1510
1528
  getRecordById(doctype, recordId) {
1511
1529
  const slug = typeof doctype === "string" ? doctype : doctype.slug;
1530
+ const path = recordPath(slug, recordId);
1512
1531
  this.ensureDoctypeExists(slug);
1513
- if (!this.hstStore.has(`${slug}.${recordId}`)) return;
1514
- if (this.hstStore.get(`${slug}.${recordId}`) === void 0) return;
1515
- return this.hstStore.getNode(`${slug}.${recordId}`);
1532
+ if (!this.hstStore.has(path)) return;
1533
+ if (this.hstStore.get(path) === void 0) return;
1534
+ return this.hstStore.getNode(path);
1516
1535
  }
1517
1536
  /**
1518
1537
  * Remove a record from the store
1519
1538
  * @param doctype - The doctype
1520
1539
  * @param recordId - The record ID
1540
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1521
1541
  */
1522
1542
  removeRecord(doctype, recordId) {
1523
1543
  const slug = typeof doctype === "string" ? doctype : doctype.slug;
1544
+ const path = recordPath(slug, recordId);
1524
1545
  this.ensureDoctypeExists(slug);
1525
- if (this.hstStore.has(`${slug}.${recordId}`)) this.hstStore.set(`${slug}.${recordId}`, void 0);
1546
+ if (this.hstStore.has(path)) this.hstStore.set(path, void 0);
1526
1547
  }
1527
1548
  /**
1528
1549
  * Get all record IDs for a doctype
@@ -1570,13 +1591,15 @@ var Stonecrop = class Stonecrop {
1570
1591
  * @param doctype - The doctype to check
1571
1592
  * @param recordId - The record ID
1572
1593
  * @returns Object with `ready: true` if all blocked links are loaded, or `ready: false` with `blockedLinks` array
1594
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1573
1595
  */
1574
1596
  isWorkflowReady(doctype, recordId) {
1575
1597
  if (isDraftRecordId(recordId)) return { ready: true };
1598
+ const path = recordPath(doctype.slug, recordId);
1576
1599
  const links = this.registry.getDescendantLinks(doctype.slug);
1577
1600
  const blockedLinks = [];
1578
1601
  for (const link of links) if (this.getEffectiveBlockWorkflows(link)) {
1579
- const linkPath = `${doctype.slug}.${recordId}.${link.fieldname}`;
1602
+ const linkPath = `${path}.${link.fieldname}`;
1580
1603
  if (!this.hstStore.has(linkPath)) blockedLinks.push(link.fieldname);
1581
1604
  }
1582
1605
  if (blockedLinks.length > 0) return {
@@ -1659,6 +1682,7 @@ var Stonecrop = class Stonecrop {
1659
1682
  * @throws Error if no data client has been configured
1660
1683
  * @throws Error if a slug string is given and no matching doctype is found in the registry
1661
1684
  * @throws Error with code `"RECORD_NOT_FOUND"` if the server returns no record
1685
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1662
1686
  * @public
1663
1687
  */
1664
1688
  async getRecord(doctype, recordId, options) {
@@ -1746,6 +1770,7 @@ var Stonecrop = class Stonecrop {
1746
1770
  * @param recordId - The record identifier
1747
1771
  * @returns The current state name, `null` for a record not created yet, or an empty string if
1748
1772
  * the doctype has no workflow
1773
+ * @throws Error if the doctype has a workflow and `recordId` is empty, or holds a dot or a bracket
1749
1774
  *
1750
1775
  * @public
1751
1776
  */
@@ -1766,13 +1791,14 @@ var Stonecrop = class Stonecrop {
1766
1791
  * @param doctype - The doctype metadata
1767
1792
  * @param recordId - The record ID to collect
1768
1793
  * @returns The complete record payload ready for API submission
1794
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1769
1795
  * @public
1770
1796
  */
1771
1797
  collectRecordPayload(doctype, recordId) {
1772
- const recordPath = `${doctype.slug}.${recordId}`;
1773
- const payload = { ...this.hstStore.get(recordPath) || {} };
1798
+ const path = recordPath(doctype.slug, recordId);
1799
+ const payload = { ...this.hstStore.get(path) || {} };
1774
1800
  if (doctype.links) for (const [fieldname, link] of Object.entries(doctype.links)) {
1775
- const fieldPath = `${recordPath}.${fieldname}`;
1801
+ const fieldPath = `${path}.${fieldname}`;
1776
1802
  if (link.cardinality === "noneOrMany" || link.cardinality === "atLeastOne") {
1777
1803
  const arrayData = this.hstStore.get(fieldPath);
1778
1804
  if (Array.isArray(arrayData)) payload[fieldname] = arrayData;
@@ -1812,16 +1838,17 @@ var Stonecrop = class Stonecrop {
1812
1838
  * @param options - Query options (includeNested to control which links are fetched)
1813
1839
  * @throws Error with code `"CLIENT_REQUIRED"` if no data client is configured
1814
1840
  * @throws Error with code `"RECORD_NOT_FOUND"` if the server returns null
1841
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
1815
1842
  * @public
1816
1843
  */
1817
1844
  async fetchNestedData(path, doctype, recordId, options) {
1818
1845
  if (!this._client) throw createCodedError("No data client configured. Call setClient() with a DataClient implementation (e.g., StonecropClient from @stonecrop/graphql-client) before fetching records.", "CLIENT_REQUIRED");
1846
+ const storePath = recordPath(doctype.slug, recordId);
1819
1847
  const result = await this._client.getRecord({ name: doctype.doctype }, recordId, { includeNested: options?.includeNested ?? true });
1820
1848
  if (!result?.record) throw recordNotFound(doctype, recordId);
1821
- const slug = doctype.slug;
1822
- this.ensureDoctypeExists(slug);
1823
- if (!this.hstStore.get(`${slug}.${recordId}`)) this.hstStore.set(`${slug}.${recordId}`, {}, "system");
1824
- for (const [key, value] of Object.entries(result.record)) this.hstStore.set(`${slug}.${recordId}.${key}`, value, "system");
1849
+ this.ensureDoctypeExists(doctype.slug);
1850
+ if (!this.hstStore.get(storePath)) this.hstStore.set(storePath, {}, "system");
1851
+ for (const [key, value] of Object.entries(result.record)) this.hstStore.set(`${storePath}.${key}`, value, "system");
1825
1852
  }
1826
1853
  /**
1827
1854
  * Recursively collect nested data from HST
@@ -2362,11 +2389,13 @@ function useClientAction(options = {}) {
2362
2389
  * @param recordId - The record ID
2363
2390
  * @param linkFieldname - The link fieldname to load
2364
2391
  * @returns LazyLink with loading, loaded, error, reload, and data
2392
+ * @throws Error if `recordId` is empty, or holds a dot or a bracket
2365
2393
  * @public
2366
2394
  */
2367
2395
  function useLazyLink(doctype, recordId, linkFieldname) {
2368
2396
  const stonecropInstance = inject("$stonecrop") || Stonecrop._root;
2369
2397
  if (!stonecropInstance) throw new Error("Stonecrop instance not available. Ensure useStonecrop() has been called first.");
2398
+ const storePath = recordPath(doctype.slug || doctype.doctype, recordId);
2370
2399
  const loading = ref(false);
2371
2400
  const loaded = ref(false);
2372
2401
  const error = ref(null);
@@ -2374,9 +2403,7 @@ function useLazyLink(doctype, recordId, linkFieldname) {
2374
2403
  /**
2375
2404
  * Build the HST path for a lazy link field
2376
2405
  */
2377
- const getLinkPath = () => {
2378
- return `${doctype.slug || doctype.doctype}.${recordId}.${linkFieldname}`;
2379
- };
2406
+ const getLinkPath = () => `${storePath}.${linkFieldname}`;
2380
2407
  /**
2381
2408
  * Get the link declaration from the doctype schema
2382
2409
  */
@@ -2401,12 +2428,11 @@ function useLazyLink(doctype, recordId, linkFieldname) {
2401
2428
  */
2402
2429
  const fetchLinkData = async () => {
2403
2430
  const linkFetch = getLinkDeclaration();
2404
- const ancestorPath = `${doctype.slug || doctype.doctype}.${recordId}`;
2405
2431
  if (linkFetch?.method === "custom") {
2406
- if (!hstStore.has(ancestorPath)) hstStore.set(ancestorPath, {}, "system");
2432
+ if (!hstStore.has(storePath)) hstStore.set(storePath, {}, "system");
2407
2433
  const result = await invokeCustomHandler(linkFetch.handler);
2408
2434
  hstStore.set(getLinkPath(), result, "system");
2409
- } else await stonecropInstance.fetchNestedData(ancestorPath, doctype, recordId, { includeNested: [linkFieldname] });
2435
+ } else await stonecropInstance.fetchNestedData(storePath, doctype, recordId, { includeNested: [linkFieldname] });
2410
2436
  };
2411
2437
  /**
2412
2438
  * Explicitly reload the lazy link data