@stonecrop/stonecrop 0.38.1 → 0.40.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.
@@ -1,3 +1,4 @@
1
+ import { ActionDispatchResult } from '@stonecrop/schema';
1
2
  import type { AnyStateNodeConfig } from 'xstate';
2
3
  import { Component } from 'vue';
3
4
  import { ComputedRef } from 'vue';
@@ -38,17 +39,7 @@ export declare interface ActionArgsContext {
38
39
  extra?: Record<string, unknown>;
39
40
  }
40
41
 
41
- /**
42
- * Result of dispatching an action to its server handler.
43
- * @public
44
- */
45
- export declare type ActionDispatchResult = {
46
- success: boolean;
47
- data: unknown;
48
- error: string | null;
49
- /** The record as a read returns it after the action; null when it failed or targets no record. */
50
- record: Record<string, unknown> | null;
51
- };
42
+ export { ActionDispatchResult }
52
43
 
53
44
  /**
54
45
  * Payload emitted with the 'action' event when the user triggers a declared action —
@@ -167,6 +158,37 @@ export declare interface BatchOperation {
167
158
  */
168
159
  export declare type ClientHandlerApi = Record<string, unknown>;
169
160
 
161
+ /**
162
+ * Build a new record, once, from every starting value: the schema's empty values, the doctype's `defaults`, then
163
+ * the source the app registered for it. Layers apply in that order, so the registered source wins however long
164
+ * the doctype's took.
165
+ *
166
+ * Nothing is returned until every value is in, so nothing is written to the record after a user can see it.
167
+ * A value that throws, rejects, or has not arrived within `timeoutMs` (default {@link DEFAULTS_TIMEOUT_MS}) is
168
+ * skipped and reported with `console.warn`; the record still opens with everything else.
169
+ * @public
170
+ */
171
+ export declare function composeNewRecord(registry: Registry, doctype: Doctype, options?: ComposeNewRecordOptions): Promise<ComposeNewRecordResult>;
172
+
173
+ /**
174
+ * Options for composing a new record.
175
+ * @public
176
+ */
177
+ export declare type ComposeNewRecordOptions = {
178
+ /** The moment `"now"` resolves to. Defaults to the time of the call. */
179
+ now?: Date;
180
+ /** How long to wait for starting values that have not arrived, in milliseconds. */
181
+ timeoutMs?: number;
182
+ };
183
+
184
+ /**
185
+ * A composed new record, complete: every starting value that arrived in time is in it.
186
+ * @public
187
+ */
188
+ export declare type ComposeNewRecordResult = {
189
+ record: Record<string, unknown>;
190
+ };
191
+
170
192
  /**
171
193
  * Factory function for HST creation
172
194
  * Creates a new HSTNode proxy for hierarchical state tree navigation.
@@ -211,6 +233,51 @@ export declare interface CrossTabMessage {
211
233
  */
212
234
  export declare type CrossTabMessageType = 'operation' | 'undo' | 'redo' | 'sync-request' | 'sync-response';
213
235
 
236
+ /**
237
+ * How long a new record waits for a starting value that has not arrived. A value still missing by then is
238
+ * skipped and reported, and the record opens without it.
239
+ * @public
240
+ */
241
+ export declare const DEFAULTS_TIMEOUT_MS = 5000;
242
+
243
+ /**
244
+ * Context passed to defaults functions at document or field level.
245
+ * @public
246
+ */
247
+ export declare type DefaultsContext = {
248
+ doctype: Doctype;
249
+ record: Record<string, unknown>;
250
+ fieldname?: string;
251
+ };
252
+
253
+ /**
254
+ * A fixed starting value, as a doctype file can hold it: plain data, nested like the record.
255
+ * @public
256
+ */
257
+ export declare type DefaultsData = string | number | boolean | null | DefaultsData[] | {
258
+ [field: string]: DefaultsData;
259
+ };
260
+
261
+ /**
262
+ * Nested object in the same shape as a composed record (HST / formData).
263
+ * @public
264
+ */
265
+ export declare type DefaultsDocument = {
266
+ [field: string]: DefaultsValue;
267
+ };
268
+
269
+ /**
270
+ * Static document or a function that returns one (sync blocks, promise loads).
271
+ * @public
272
+ */
273
+ export declare type DefaultsSource = DefaultsDocument | ((ctx: DefaultsContext) => DefaultsDocument | Promise<DefaultsDocument>);
274
+
275
+ /**
276
+ * A value inside a registered defaults document.
277
+ * @public
278
+ */
279
+ export declare type DefaultsValue = string | number | boolean | null | DefaultsDocument | DefaultsValue[] | ((ctx: DefaultsContext) => DefaultsValue | Promise<DefaultsValue>);
280
+
214
281
  /**
215
282
  * Doctype runtime class with Immutable.js collections for HST change tracking.
216
283
  * @public
@@ -258,6 +325,12 @@ export declare class Doctype {
258
325
  * @readonly
259
326
  */
260
327
  readonly displayField?: string;
328
+ /**
329
+ * Starting values for new records: fixed data in the shape of the record.
330
+ * @public
331
+ * @readonly
332
+ */
333
+ readonly defaults?: DoctypeDefaults;
261
334
  /**
262
335
  * Creates a new Doctype instance
263
336
  * @param doctype - The doctype name
@@ -267,7 +340,7 @@ export declare class Doctype {
267
340
  * @param links - Optional relationship links to other doctypes
268
341
  * @param displayField - Optional field used when displaying references to this doctype
269
342
  */
270
- constructor(doctype: string, schema: ImmutableDoctype['schema'], workflow: ImmutableDoctype['workflow'], component?: Component, links?: Record<string, LinkDeclaration>, displayField?: string);
343
+ constructor(doctype: string, schema: ImmutableDoctype['schema'], workflow: ImmutableDoctype['workflow'], component?: Component, links?: Record<string, LinkDeclaration>, displayField?: string, defaults?: DoctypeDefaults);
271
344
  /**
272
345
  * Creates a Doctype instance from a plain configuration object.
273
346
  * Handles conversion of arrays to Immutable.js collections internally.
@@ -471,6 +544,17 @@ export declare type DoctypeConfig = {
471
544
  workflow?: UnknownMachineConfig | WorkflowMeta;
472
545
  /** Ancestor doctype for inheritance */
473
546
  inherits?: string;
547
+ /** Starting values for new records: fixed data in the shape of the record */
548
+ defaults?: DoctypeDefaults;
549
+ };
550
+
551
+ /**
552
+ * A doctype's own starting values for a new record: fixed data in the shape of the record. Anything worked out when
553
+ * a record is made (today's date, a value looked up for the user's company) is registered on the registry instead.
554
+ * @public
555
+ */
556
+ export declare type DoctypeDefaults = {
557
+ [field: string]: DefaultsData;
474
558
  };
475
559
 
476
560
  /**
@@ -988,7 +1072,7 @@ export declare type HSTStonecropReturn = BaseStonecropReturn & {
988
1072
  * @param path - The HST path where initialized data should be stored
989
1073
  * @param doctype - The doctype to initialize
990
1074
  */
991
- initializeNestedData: (path: string, doctype: Doctype) => void;
1075
+ initializeNestedData: (path: string, doctype: Doctype) => Promise<void>;
992
1076
  /**
993
1077
  * Fetch a record and its nested data from the server.
994
1078
  * Stores each field at its own HST path per the field-level convention.
@@ -1406,6 +1490,11 @@ export declare class Registry {
1406
1490
  * @internal
1407
1491
  */
1408
1492
  private _ancestorIndexDirty;
1493
+ /**
1494
+ * The app's starting values for each doctype, by slug: one registration per doctype (functions allowed).
1495
+ * @internal
1496
+ */
1497
+ private registeredDefaults;
1409
1498
  /**
1410
1499
  * The Vue router instance
1411
1500
  * @see {@link https://router.vuejs.org/}
@@ -1467,8 +1556,8 @@ export declare class Registry {
1467
1556
  * Narrows by `kind` discriminator for precise branch selection.
1468
1557
  *
1469
1558
  * - `kind: 'table'` or `kind: 'link'` → `[]` or `{}`
1470
- * - `kind: 'fieldset'` → recursively initializes children as `{}`
1471
- * - `kind: 'field'` → derives the default from the component's category; falls back to `null`
1559
+ * - `kind: 'fieldset'` → its children, at the top level of the record: a fieldset is layout
1560
+ * - `kind: 'field'` → an empty value for the component's category; falls back to `null`
1472
1561
  *
1473
1562
  * @param schema - The resolved schema array to derive defaults from
1474
1563
  * @returns A plain object with default values for each field
@@ -1522,6 +1611,26 @@ export declare class Registry {
1522
1611
  * @internal
1523
1612
  */
1524
1613
  private _ensureAncestorIndex;
1614
+ /**
1615
+ * Register the app's starting values for a doctype, by its name or its slug (typically from app bootstrap).
1616
+ * Either one is keyed the way {@link Doctype.slug} is, so `'OrderItem'` and `'order-item'` register for the
1617
+ * same doctype, whether or not it has been added yet.
1618
+ *
1619
+ * They apply over the doctype's own `defaults` for every new record, and a function here runs for each one, so
1620
+ * it can work out what a doctype file cannot (today's date, a value looked up for the user's company). A doctype
1621
+ * takes one registration: a second replaces the first, with a warning.
1622
+ * @public
1623
+ */
1624
+ registerDefaults(doctype: string, source: DefaultsSource): void;
1625
+ /**
1626
+ * @internal
1627
+ */
1628
+ getRegisteredDefaults(slug: string): DefaultsSource | undefined;
1629
+ /**
1630
+ * Compose a new record, once, from every starting value. See {@link composeNewRecord}.
1631
+ * @public
1632
+ */
1633
+ composeNewRecord(doctype: Doctype, options?: ComposeNewRecordOptions): Promise<ComposeNewRecordResult>;
1525
1634
  }
1526
1635
 
1527
1636
  /**
@@ -2041,7 +2150,7 @@ export declare class Stonecrop {
2041
2150
  * @param doctype - The doctype
2042
2151
  * @param action - Action name to execute (e.g., 'SUBMIT', 'APPROVE', 'save')
2043
2152
  * @param args - Action arguments (typically record ID and/or form data)
2044
- * @returns Action result with success status, the handler's data, any error, and the record
2153
+ * @returns Action result with success status, the handler's data, any error, the record, and the keys the write discarded
2045
2154
  * @throws Error if no data client has been configured
2046
2155
  */
2047
2156
  dispatchAction(doctype: Doctype, action: string, args?: unknown[]): Promise<ActionDispatchResult>;
@@ -2093,7 +2202,7 @@ export declare class Stonecrop {
2093
2202
  * @param doctype - The doctype to initialize
2094
2203
  * @public
2095
2204
  */
2096
- initializeNestedData(path: string, doctype: Doctype): void;
2205
+ initializeNestedData(path: string, doctype: Doctype): Promise<void>;
2097
2206
  /**
2098
2207
  * Fetch a record and its nested data from the server.
2099
2208
  *