@stonecrop/stonecrop 0.41.0 → 0.42.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/README.md CHANGED
@@ -163,6 +163,8 @@ When you pass a string doctype slug instead of a `Doctype` instance, `useStonecr
163
163
  2. If not, call `registry.getMeta` to lazy-load it
164
164
  3. Return `isLoading`, `error`, and `resolvedDoctype` refs for handling the async state
165
165
 
166
+ The same refs cover reading the record: `isLoading` is true while it is fetched, and a failed read sets `error` and leaves `formData` empty, never a blank record in its place. A record the server does not have fails with the code `RECORD_NOT_FOUND`.
167
+
166
168
  ```typescript
167
169
  const { isLoading, error, resolvedDoctype, resolvedSchema, formData } = useStonecrop({
168
170
  doctype: 'plan', // string slug - triggers lazy-loading
@@ -170,7 +172,7 @@ const { isLoading, error, resolvedDoctype, resolvedSchema, formData } = useStone
170
172
  })
171
173
 
172
174
  // In your template:
173
- // <div v-if="isLoading">Loading doctype...</div>
175
+ // <div v-if="isLoading">Loading...</div>
174
176
  // <div v-else-if="error">Error: {{ error.message }}</div>
175
177
  // <AForm v-else :schema="resolvedSchema" v-model:data="formData" />
176
178
  ```
@@ -1106,13 +1106,14 @@ export declare type HSTStonecropReturn = BaseStonecropReturn & {
1106
1106
  handleHSTChange: (changeData: HSTChangeData) => void;
1107
1107
  };
1108
1108
  /**
1109
- * Loading state for async doctype resolution.
1110
- * True while fetching doctype by slug string from registry.
1109
+ * Loading state: true while a doctype named by slug is resolved, a new record's starting values
1110
+ * are composed, or an existing record is read from the server.
1111
1111
  */
1112
1112
  isLoading: Ref<boolean>;
1113
1113
  /**
1114
- * Error state for doctype resolution failures.
1115
- * Set when doctype slug lookup fails.
1114
+ * Why the form cannot show: the doctype slug did not resolve, or the record read failed. A
1115
+ * record the server does not find carries the code `"RECORD_NOT_FOUND"`. `formData` stays
1116
+ * empty when the read fails.
1116
1117
  */
1117
1118
  error: Ref<Error | null>;
1118
1119
  /**
@@ -2122,9 +2123,13 @@ export declare class Stonecrop {
2122
2123
  *
2123
2124
  * @param doctype - The doctype slug string or Doctype object
2124
2125
  * @param recordId - The record ID
2126
+ * A record the server does not find is an error, not an empty answer: a caller drawing what is in
2127
+ * HST would otherwise show it as a blank record.
2128
+ *
2125
2129
  * @param options - Query options (includeNested, maxDepth), forwarded to the client
2126
2130
  * @throws Error if no data client has been configured
2127
2131
  * @throws Error if a slug string is given and no matching doctype is found in the registry
2132
+ * @throws Error with code `"RECORD_NOT_FOUND"` if the server returns no record
2128
2133
  * @public
2129
2134
  */
2130
2135
  getRecord(doctype: string | Doctype, recordId: string, options?: GetRecordOptions): Promise<void>;
package/dist/stonecrop.js CHANGED
@@ -1544,9 +1544,13 @@ var Stonecrop = class Stonecrop {
1544
1544
  *
1545
1545
  * @param doctype - The doctype slug string or Doctype object
1546
1546
  * @param recordId - The record ID
1547
+ * A record the server does not find is an error, not an empty answer: a caller drawing what is in
1548
+ * HST would otherwise show it as a blank record.
1549
+ *
1547
1550
  * @param options - Query options (includeNested, maxDepth), forwarded to the client
1548
1551
  * @throws Error if no data client has been configured
1549
1552
  * @throws Error if a slug string is given and no matching doctype is found in the registry
1553
+ * @throws Error with code `"RECORD_NOT_FOUND"` if the server returns no record
1550
1554
  * @public
1551
1555
  */
1552
1556
  async getRecord(doctype, recordId, options) {
@@ -1556,7 +1560,8 @@ var Stonecrop = class Stonecrop {
1556
1560
  if (isDraftRecordId(recordId)) return;
1557
1561
  if (this.getRecordById(resolved.slug, recordId)) return;
1558
1562
  const result = await this._client.getRecord(resolved, recordId, options);
1559
- if (result?.record) this.addRecord(resolved, recordId, result.record);
1563
+ if (!result?.record) throw recordNotFound(resolved, recordId);
1564
+ this.addRecord(resolved, recordId, result.record);
1560
1565
  }
1561
1566
  /**
1562
1567
  * Dispatch an action to the server via the configured data client, and file the record it
@@ -1699,7 +1704,7 @@ var Stonecrop = class Stonecrop {
1699
1704
  async fetchNestedData(path, doctype, recordId, options) {
1700
1705
  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");
1701
1706
  const result = await this._client.getRecord({ name: doctype.doctype }, recordId, { includeNested: options?.includeNested ?? true });
1702
- if (!result?.record) throw createCodedError(`Record not found: ${doctype.doctype} ${recordId}`, "RECORD_NOT_FOUND");
1707
+ if (!result?.record) throw recordNotFound(doctype, recordId);
1703
1708
  const slug = doctype.slug;
1704
1709
  this.ensureDoctypeExists(slug);
1705
1710
  if (!this.hstStore.get(`${slug}.${recordId}`)) this.hstStore.set(`${slug}.${recordId}`, {}, "system");
@@ -1733,6 +1738,10 @@ function createCodedError(message, code) {
1733
1738
  error.code = code;
1734
1739
  return error;
1735
1740
  }
1741
+ /** The one error both record reads throw when the server returns no record. */
1742
+ function recordNotFound(doctype, recordId) {
1743
+ return createCodedError(`Record not found: ${doctype.doctype} ${recordId}`, "RECORD_NOT_FOUND");
1744
+ }
1736
1745
  //#endregion
1737
1746
  //#region src/composables/stonecrop.ts
1738
1747
  /**
@@ -1837,6 +1846,25 @@ function useStonecrop(options) {
1837
1846
  }
1838
1847
  if (hstStore.value) setupDeepReactivity(options.doctype, options.recordId || "new", formData, hstStore.value);
1839
1848
  }
1849
+ const loadExistingRecord = async (doctype, recordId) => {
1850
+ if (!stonecrop.value) return;
1851
+ const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
1852
+ if (existingRecord) {
1853
+ formData.value = existingRecord.get("") || {};
1854
+ return;
1855
+ }
1856
+ if (!stonecrop.value.getClient()) return;
1857
+ isLoading.value = true;
1858
+ error.value = null;
1859
+ try {
1860
+ await stonecrop.value.getRecord(doctype, recordId);
1861
+ formData.value = stonecrop.value.getRecordById(doctype, recordId)?.get("") || {};
1862
+ } catch (e) {
1863
+ error.value = e instanceof Error ? e : new Error(String(e));
1864
+ } finally {
1865
+ isLoading.value = false;
1866
+ }
1867
+ };
1840
1868
  onMounted(async () => {
1841
1869
  if (!registry || !stonecrop.value) return;
1842
1870
  if (options.doctype) {
@@ -1866,17 +1894,8 @@ function useStonecrop(options) {
1866
1894
  resolvedDoctype.value = doctype;
1867
1895
  if (!doctype) return;
1868
1896
  resolvedSchema.value = registry.resolveSchema(doctype);
1869
- if (recordId && !isDraftRecordId(recordId)) {
1870
- const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
1871
- if (existingRecord) formData.value = existingRecord.get("") || {};
1872
- else try {
1873
- await stonecrop.value.getRecord(doctype, recordId);
1874
- const loadedRecord = stonecrop.value.getRecordById(doctype, recordId);
1875
- if (loadedRecord) formData.value = loadedRecord.get("") || {};
1876
- } catch {
1877
- formData.value = registry.initializeRecord(resolvedSchema.value);
1878
- }
1879
- } else {
1897
+ if (recordId && !isDraftRecordId(recordId)) await loadExistingRecord(doctype, recordId);
1898
+ else {
1880
1899
  isLoading.value = true;
1881
1900
  try {
1882
1901
  formData.value = (await registry.composeNewRecord(doctype)).record;
@@ -1885,18 +1904,7 @@ function useStonecrop(options) {
1885
1904
  }
1886
1905
  }
1887
1906
  if (hstStore.value) setupDeepReactivity(doctype, recordId || "new", formData, hstStore.value);
1888
- } else if (recordId && !isDraftRecordId(recordId)) {
1889
- const doctype = options.doctype;
1890
- const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
1891
- if (existingRecord) formData.value = existingRecord.get("") || {};
1892
- else try {
1893
- await stonecrop.value.getRecord(doctype, recordId);
1894
- const loadedRecord = stonecrop.value.getRecordById(doctype, recordId);
1895
- if (loadedRecord) formData.value = loadedRecord.get("") || {};
1896
- } catch {
1897
- formData.value = registry.initializeRecord(resolvedSchema.value);
1898
- }
1899
- }
1907
+ } else if (recordId && !isDraftRecordId(recordId)) await loadExistingRecord(options.doctype, recordId);
1900
1908
  }
1901
1909
  });
1902
1910
  const provideHSTPath = (fieldname, customRecordId) => {