@stonecrop/stonecrop 0.10.6 → 0.10.7

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
@@ -84,12 +84,19 @@ export default {
84
84
  // Base mode — operation log only, no HST record loading
85
85
  const { stonecrop, operationLog } = useStonecrop()
86
86
 
87
- // HST mode — pass doctype and optional recordId for full integration
87
+ // HST mode — pass Doctype instance and optional recordId
88
88
  const { stonecrop, formData, provideHSTPath, handleHSTChange } = useStonecrop({
89
89
  doctype: myDoctype,
90
90
  recordId: 'record-123', // omit or pass undefined for new records
91
91
  })
92
92
 
93
+ // HST mode with lazy-loading — pass string doctype slug
94
+ // Automatically loads doctype via registry.getMeta if not in registry
95
+ const { isLoading, error, resolvedDoctype, formData } = useStonecrop({
96
+ doctype: 'plan',
97
+ recordId: 'record-123',
98
+ })
99
+
93
100
  // Access HST store
94
101
  const store = stonecrop.value?.getStore()
95
102
 
@@ -101,6 +108,28 @@ export default {
101
108
  }
102
109
  ```
103
110
 
111
+ ### String Doctype Lazy-Loading
112
+
113
+ When you pass a string doctype slug instead of a `Doctype` instance, `useStonecrop` will:
114
+
115
+ 1. Check if the doctype is already in the Registry
116
+ 2. If not, call `registry.getMeta` to lazy-load it
117
+ 3. Return `isLoading`, `error`, and `resolvedDoctype` refs for handling the async state
118
+
119
+ ```typescript
120
+ const { isLoading, error, resolvedDoctype, formData } = useStonecrop({
121
+ doctype: 'plan', // string slug - triggers lazy-loading
122
+ recordId: '123',
123
+ })
124
+
125
+ // In your template:
126
+ // <div v-if="isLoading">Loading doctype...</div>
127
+ // <div v-else-if="error">Error: {{ error.message }}</div>
128
+ // <AForm v-else :schema="resolvedDoctype.schema" v-model:data="formData" />
129
+ ```
130
+
131
+ This pattern eliminates the timing mismatch when loading doctypes asynchronously in Nuxt plugins.
132
+
104
133
  ## Design
105
134
  A Doctype defines schema, workflow, and actions.
106
135
  - **Schema** describes the data model and field layout — used by AForm for rendering.
@@ -17,14 +17,13 @@ export function useStonecrop(options) {
17
17
  const routerRecordId = ref();
18
18
  // Resolved schema with nested Doctype fields expanded
19
19
  const resolvedSchema = ref([]);
20
- // Auto-resolve schema when doctype is available
21
- if (options.doctype && registry) {
22
- const schemaArray = options.doctype.schema
23
- ? Array.isArray(options.doctype.schema)
24
- ? options.doctype.schema
25
- : Array.from(options.doctype.schema)
26
- : [];
27
- resolvedSchema.value = registry.resolveSchema(schemaArray);
20
+ // Loading state for lazy-loaded doctypes
21
+ const isLoading = ref(false);
22
+ const error = ref(null);
23
+ const resolvedDoctype = ref();
24
+ // If doctype is a Doctype instance (not string), set resolved immediately
25
+ if (options?.doctype && typeof options.doctype !== 'string') {
26
+ resolvedDoctype.value = options.doctype;
28
27
  }
29
28
  // Operation log state and methods - will be populated after stonecrop instance is created
30
29
  const operations = ref([]);
@@ -166,8 +165,56 @@ export function useStonecrop(options) {
166
165
  // Handle HST integration if doctype is provided explicitly
167
166
  if (options.doctype) {
168
167
  hstStore.value = stonecrop.value.getStore();
169
- const doctype = options.doctype;
170
168
  const recordId = options.recordId;
169
+ // Resolve doctype - handle string (lazy-load) or Doctype instance
170
+ let doctype;
171
+ if (typeof options.doctype === 'string') {
172
+ // String doctype - check registry first, then lazy-load
173
+ const doctypeSlug = options.doctype;
174
+ isLoading.value = true;
175
+ error.value = null;
176
+ try {
177
+ // Check if already in registry
178
+ doctype = registry.getDoctype(doctypeSlug);
179
+ if (!doctype && registry.getMeta) {
180
+ // Lazy-load via getMeta
181
+ const routeContext = {
182
+ path: `/${doctypeSlug}`,
183
+ segments: [doctypeSlug],
184
+ };
185
+ doctype = await registry.getMeta(routeContext);
186
+ if (doctype) {
187
+ registry.addDoctype(doctype);
188
+ }
189
+ }
190
+ if (!doctype) {
191
+ error.value = new Error(`Doctype '${doctypeSlug}' not found in registry and getMeta returned no result`);
192
+ }
193
+ }
194
+ catch (e) {
195
+ error.value = e instanceof Error ? e : new Error(String(e));
196
+ }
197
+ finally {
198
+ isLoading.value = false;
199
+ }
200
+ }
201
+ else {
202
+ // Doctype instance provided directly
203
+ doctype = options.doctype;
204
+ }
205
+ // Set resolved doctype for consumers
206
+ resolvedDoctype.value = doctype;
207
+ if (!doctype) {
208
+ // Error already set above, just return
209
+ return;
210
+ }
211
+ // Resolve schema for the doctype
212
+ const schemaArray = doctype.schema
213
+ ? Array.isArray(doctype.schema)
214
+ ? doctype.schema
215
+ : Array.from(doctype.schema)
216
+ : [];
217
+ resolvedSchema.value = registry.resolveSchema(schemaArray);
171
218
  if (recordId && recordId !== 'new') {
172
219
  const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
173
220
  if (existingRecord) {
@@ -196,14 +243,14 @@ export function useStonecrop(options) {
196
243
  });
197
244
  // HST integration functions - always created but only populated when HST is available
198
245
  const provideHSTPath = (fieldname, customRecordId) => {
199
- const doctype = options.doctype || routerDoctype.value;
246
+ const doctype = resolvedDoctype.value || routerDoctype.value;
200
247
  if (!doctype)
201
248
  return '';
202
249
  const actualRecordId = customRecordId || options.recordId || routerRecordId.value || 'new';
203
250
  return `${doctype.slug}.${actualRecordId}.${fieldname}`;
204
251
  };
205
252
  const handleHSTChange = (changeData) => {
206
- const doctype = options.doctype || routerDoctype.value;
253
+ const doctype = resolvedDoctype.value || routerDoctype.value;
207
254
  if (!hstStore.value || !stonecrop.value || !doctype) {
208
255
  return;
209
256
  }
@@ -294,7 +341,11 @@ export function useStonecrop(options) {
294
341
  // Build the save payload using resolved schema
295
342
  const payload = { ...recordData };
296
343
  // Use resolveSchema to get the full resolved tree, then walk Doctype fields
297
- const schemaArray = (doctype.schema ? (Array.isArray(doctype.schema) ? doctype.schema : Array.from(doctype.schema)) : []);
344
+ const schemaArray = doctype.schema
345
+ ? Array.isArray(doctype.schema)
346
+ ? doctype.schema
347
+ : Array.from(doctype.schema)
348
+ : [];
298
349
  const resolved = registry ? registry.resolveSchema(schemaArray) : schemaArray;
299
350
  const doctypeFields = resolved.filter(field => 'fieldtype' in field && field.fieldtype === 'Doctype' && 'schema' in field && Array.isArray(field.schema));
300
351
  // Recursively collect nested data from HST using resolved schemas
@@ -364,6 +415,9 @@ export function useStonecrop(options) {
364
415
  loadNestedData,
365
416
  saveRecursive,
366
417
  createNestedContext,
418
+ isLoading,
419
+ error,
420
+ resolvedDoctype,
367
421
  };
368
422
  }
369
423
  else if (!options.doctype && registry?.router) {
@@ -379,6 +433,9 @@ export function useStonecrop(options) {
379
433
  loadNestedData,
380
434
  saveRecursive,
381
435
  createNestedContext,
436
+ isLoading,
437
+ error,
438
+ resolvedDoctype,
382
439
  };
383
440
  }
384
441
  // No doctype and no router - basic mode
@@ -59,6 +59,9 @@ export type HSTStonecropReturn = BaseStonecropReturn & {
59
59
  provideHSTPath: (fieldname: string) => string;
60
60
  handleHSTChange: (changeData: HSTChangeData) => void;
61
61
  };
62
+ isLoading: Ref<boolean>;
63
+ error: Ref<Error | null>;
64
+ resolvedDoctype: Ref<Doctype | undefined>;
62
65
  };
63
66
  /**
64
67
  * HST Change data structure
@@ -81,13 +84,13 @@ export declare function useStonecrop(): BaseStonecropReturn | HSTStonecropReturn
81
84
  /**
82
85
  * Unified Stonecrop composable with HST integration for a specific doctype and record
83
86
  *
84
- * @param options - Configuration with doctype and optional recordId
87
+ * @param options - Configuration with doctype (string slug or Doctype instance) and optional recordId
85
88
  * @returns Stonecrop instance with full HST integration utilities
86
89
  * @public
87
90
  */
88
91
  export declare function useStonecrop(options: {
89
92
  registry?: Registry;
90
- doctype: Doctype;
93
+ doctype: Doctype | string;
91
94
  recordId?: string;
92
95
  }): HSTStonecropReturn;
93
96
  //# sourceMappingURL=stonecrop.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"stonecrop.d.ts","sourceRoot":"","sources":["../../../src/composables/stonecrop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,GAAG,EAAiC,WAAW,EAAE,MAAM,KAAK,CAAA;AAExF,OAAO,QAAQ,MAAM,aAAa,CAAA;AAClC,OAAO,EAAE,SAA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AAA;CACzC,CAAA;AAED;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,GAAG,CAAA;IACV,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,IAAI,mBAAmB,GAAG,kBAAkB,CAAA;AACxE;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE;IACrC,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,OAAO,EAAE,OAAO,GAAG,MAAM,CAAA;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAA;CACjB,GAAG,kBAAkB,CAAA"}
@@ -722,6 +722,9 @@ export declare type HSTStonecropReturn = BaseStonecropReturn & {
722
722
  provideHSTPath: (fieldname: string) => string;
723
723
  handleHSTChange: (changeData: HSTChangeData) => void;
724
724
  };
725
+ isLoading: Ref<boolean>;
726
+ error: Ref<Error | null>;
727
+ resolvedDoctype: Ref<Doctype | undefined>;
725
728
  };
726
729
 
727
730
  /**
@@ -1991,13 +1994,13 @@ export declare function useStonecrop(): BaseStonecropReturn | HSTStonecropReturn
1991
1994
  /**
1992
1995
  * Unified Stonecrop composable with HST integration for a specific doctype and record
1993
1996
  *
1994
- * @param options - Configuration with doctype and optional recordId
1997
+ * @param options - Configuration with doctype (string slug or Doctype instance) and optional recordId
1995
1998
  * @returns Stonecrop instance with full HST integration utilities
1996
1999
  * @public
1997
2000
  */
1998
2001
  export declare function useStonecrop(options: {
1999
2002
  registry?: Registry;
2000
- doctype: Doctype;
2003
+ doctype: Doctype | string;
2001
2004
  recordId?: string;
2002
2005
  }): HSTStonecropReturn;
2003
2006