@stonecrop/stonecrop 0.30.0 → 0.32.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.
Files changed (121) hide show
  1. package/dist/stonecrop.js +3548 -2740
  2. package/dist/stonecrop.js.map +1 -1
  3. package/dist/tsdoc-metadata.json +1 -1
  4. package/package.json +33 -27
  5. package/dist/src/client-action.d.ts +0 -31
  6. package/dist/src/client-action.d.ts.map +0 -1
  7. package/dist/src/client-action.js +0 -169
  8. package/dist/src/client-handler.d.ts +0 -43
  9. package/dist/src/client-handler.d.ts.map +0 -1
  10. package/dist/src/client-handler.js +0 -42
  11. package/dist/src/composables/lazy-link.d.ts +0 -25
  12. package/dist/src/composables/lazy-link.d.ts.map +0 -1
  13. package/dist/src/composables/lazy-link.js +0 -124
  14. package/dist/src/composables/operation-log.d.ts +0 -136
  15. package/dist/src/composables/operation-log.d.ts.map +0 -1
  16. package/dist/src/composables/operation-log.js +0 -224
  17. package/dist/src/composables/stonecrop.d.ts +0 -37
  18. package/dist/src/composables/stonecrop.d.ts.map +0 -1
  19. package/dist/src/composables/stonecrop.js +0 -433
  20. package/dist/src/doctype.d.ts +0 -243
  21. package/dist/src/doctype.d.ts.map +0 -1
  22. package/dist/src/doctype.js +0 -325
  23. package/dist/src/draft.d.ts +0 -35
  24. package/dist/src/draft.d.ts.map +0 -1
  25. package/dist/src/draft.js +0 -36
  26. package/dist/src/exceptions.d.ts +0 -14
  27. package/dist/src/exceptions.d.ts.map +0 -1
  28. package/dist/src/exceptions.js +0 -16
  29. package/dist/src/field-triggers.d.ts +0 -184
  30. package/dist/src/field-triggers.d.ts.map +0 -1
  31. package/dist/src/field-triggers.js +0 -577
  32. package/dist/src/index.d.ts +0 -25
  33. package/dist/src/index.d.ts.map +0 -1
  34. package/dist/src/index.js +0 -39
  35. package/dist/src/plugins/index.d.ts +0 -35
  36. package/dist/src/plugins/index.d.ts.map +0 -1
  37. package/dist/src/plugins/index.js +0 -98
  38. package/dist/src/registry.d.ts +0 -161
  39. package/dist/src/registry.d.ts.map +0 -1
  40. package/dist/src/registry.js +0 -384
  41. package/dist/src/schema-validator.d.ts +0 -78
  42. package/dist/src/schema-validator.d.ts.map +0 -1
  43. package/dist/src/schema-validator.js +0 -398
  44. package/dist/src/stonecrop.d.ts +0 -539
  45. package/dist/src/stonecrop.d.ts.map +0 -1
  46. package/dist/src/stonecrop.js +0 -610
  47. package/dist/src/stores/hst.d.ts +0 -92
  48. package/dist/src/stores/hst.d.ts.map +0 -1
  49. package/dist/src/stores/hst.js +0 -487
  50. package/dist/src/stores/index.d.ts +0 -9
  51. package/dist/src/stores/index.d.ts.map +0 -1
  52. package/dist/src/stores/index.js +0 -13
  53. package/dist/src/stores/operation-log.d.ts +0 -262
  54. package/dist/src/stores/operation-log.d.ts.map +0 -1
  55. package/dist/src/stores/operation-log.js +0 -571
  56. package/dist/src/stores/validation.d.ts +0 -83
  57. package/dist/src/stores/validation.d.ts.map +0 -1
  58. package/dist/src/stores/validation.js +0 -104
  59. package/dist/src/types/client-action.d.ts +0 -121
  60. package/dist/src/types/client-action.d.ts.map +0 -1
  61. package/dist/src/types/client-action.js +0 -0
  62. package/dist/src/types/composable.d.ts +0 -268
  63. package/dist/src/types/composable.d.ts.map +0 -1
  64. package/dist/src/types/composable.js +0 -0
  65. package/dist/src/types/doctype.d.ts +0 -34
  66. package/dist/src/types/doctype.d.ts.map +0 -1
  67. package/dist/src/types/doctype.js +0 -0
  68. package/dist/src/types/field-triggers.d.ts +0 -186
  69. package/dist/src/types/field-triggers.d.ts.map +0 -1
  70. package/dist/src/types/field-triggers.js +0 -4
  71. package/dist/src/types/hst.d.ts +0 -70
  72. package/dist/src/types/hst.d.ts.map +0 -1
  73. package/dist/src/types/hst.js +0 -0
  74. package/dist/src/types/index.d.ts +0 -11
  75. package/dist/src/types/index.d.ts.map +0 -1
  76. package/dist/src/types/index.js +0 -11
  77. package/dist/src/types/operation-log.d.ts +0 -163
  78. package/dist/src/types/operation-log.d.ts.map +0 -1
  79. package/dist/src/types/operation-log.js +0 -0
  80. package/dist/src/types/plugin.d.ts +0 -37
  81. package/dist/src/types/plugin.d.ts.map +0 -1
  82. package/dist/src/types/plugin.js +0 -0
  83. package/dist/src/types/registry.d.ts +0 -11
  84. package/dist/src/types/registry.d.ts.map +0 -1
  85. package/dist/src/types/registry.js +0 -0
  86. package/dist/src/types/schema-validator.d.ts +0 -66
  87. package/dist/src/types/schema-validator.d.ts.map +0 -1
  88. package/dist/src/types/schema-validator.js +0 -13
  89. package/dist/src/types/stonecrop.d.ts +0 -32
  90. package/dist/src/types/stonecrop.d.ts.map +0 -1
  91. package/dist/src/types/stonecrop.js +0 -0
  92. package/dist/stonecrop.tsbuildinfo +0 -1
  93. package/src/client-action.ts +0 -207
  94. package/src/client-handler.ts +0 -55
  95. package/src/composables/lazy-link.ts +0 -145
  96. package/src/composables/operation-log.ts +0 -257
  97. package/src/composables/stonecrop.ts +0 -556
  98. package/src/doctype.ts +0 -360
  99. package/src/draft.ts +0 -38
  100. package/src/exceptions.ts +0 -16
  101. package/src/field-triggers.ts +0 -686
  102. package/src/index.ts +0 -81
  103. package/src/plugins/index.ts +0 -113
  104. package/src/registry.ts +0 -433
  105. package/src/schema-validator.ts +0 -476
  106. package/src/stonecrop.ts +0 -721
  107. package/src/stores/hst.ts +0 -626
  108. package/src/stores/index.ts +0 -21
  109. package/src/stores/operation-log.ts +0 -675
  110. package/src/stores/validation.ts +0 -138
  111. package/src/types/client-action.ts +0 -121
  112. package/src/types/composable.ts +0 -288
  113. package/src/types/doctype.ts +0 -35
  114. package/src/types/field-triggers.ts +0 -201
  115. package/src/types/hst.ts +0 -77
  116. package/src/types/index.ts +0 -11
  117. package/src/types/operation-log.ts +0 -202
  118. package/src/types/plugin.ts +0 -38
  119. package/src/types/registry.ts +0 -10
  120. package/src/types/schema-validator.ts +0 -69
  121. package/src/types/stonecrop.ts +0 -33
@@ -1,433 +0,0 @@
1
- import { storeToRefs } from 'pinia';
2
- import { inject, onMounted, ref, watch, provide, computed } from 'vue';
3
- import { isDraftRecordId } from '../draft';
4
- import { Stonecrop } from '../stonecrop';
5
- /**
6
- * @public
7
- */
8
- export function useStonecrop(options) {
9
- if (!options)
10
- options = {};
11
- const registry = options.registry || inject('$registry');
12
- const providedStonecrop = inject('$stonecrop');
13
- const stonecrop = ref();
14
- const hstStore = ref();
15
- const formData = ref({});
16
- // Resolved schema with nested Doctype fields expanded
17
- const resolvedSchema = ref([]);
18
- // Loading state for lazy-loaded doctypes
19
- const isLoading = ref(false);
20
- const error = ref(null);
21
- const resolvedDoctype = ref();
22
- // Workflow readiness computed properties
23
- const isWorkflowReady = computed(() => {
24
- if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
25
- return true;
26
- }
27
- const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId);
28
- return status.ready;
29
- });
30
- const blockedLinks = computed(() => {
31
- if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
32
- return [];
33
- }
34
- const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId);
35
- return status.blockedLinks ?? [];
36
- });
37
- // Initialize stonecrop instance synchronously using singleton pattern
38
- // Use injected instance if available, otherwise fall back to the singleton root
39
- const stonecropInstance = providedStonecrop || Stonecrop._root;
40
- if (stonecropInstance) {
41
- stonecrop.value = stonecropInstance;
42
- }
43
- // If doctype is a Doctype instance (not string), set resolved immediately
44
- if (options?.doctype && typeof options.doctype !== 'string') {
45
- resolvedDoctype.value = options.doctype;
46
- }
47
- // Operation log state and methods
48
- const operations = ref([]);
49
- const currentIndex = ref(-1);
50
- const canUndo = computed(() => stonecrop.value?.getOperationLogStore().canUndo ?? false);
51
- const canRedo = computed(() => stonecrop.value?.getOperationLogStore().canRedo ?? false);
52
- const undoCount = computed(() => stonecrop.value?.getOperationLogStore().undoCount ?? 0);
53
- const redoCount = computed(() => stonecrop.value?.getOperationLogStore().redoCount ?? 0);
54
- const undoRedoState = computed(() => stonecrop.value?.getOperationLogStore().undoRedoState ?? {
55
- canUndo: false,
56
- canRedo: false,
57
- undoCount: 0,
58
- redoCount: 0,
59
- currentIndex: -1,
60
- });
61
- // Operation log methods
62
- const undo = (hstNode) => {
63
- return stonecrop.value?.getOperationLogStore().undo(hstNode) ?? false;
64
- };
65
- const redo = (hstNode) => {
66
- return stonecrop.value?.getOperationLogStore().redo(hstNode) ?? false;
67
- };
68
- const startBatch = () => {
69
- stonecrop.value?.getOperationLogStore().startBatch();
70
- };
71
- const commitBatch = (description) => {
72
- return stonecrop.value?.getOperationLogStore().commitBatch(description) ?? null;
73
- };
74
- const cancelBatch = () => {
75
- stonecrop.value?.getOperationLogStore().cancelBatch();
76
- };
77
- const clear = () => {
78
- stonecrop.value?.getOperationLogStore().clear();
79
- };
80
- const getOperationsFor = (doctype, recordId) => {
81
- return stonecrop.value?.getOperationLogStore().getOperationsFor(doctype, recordId) ?? [];
82
- };
83
- const getSnapshot = () => {
84
- return (stonecrop.value?.getOperationLogStore().getSnapshot() ?? {
85
- operations: [],
86
- currentIndex: -1,
87
- totalOperations: 0,
88
- reversibleOperations: 0,
89
- irreversibleOperations: 0,
90
- });
91
- };
92
- const markIrreversible = (operationId, reason) => {
93
- stonecrop.value?.getOperationLogStore().markIrreversible(operationId, reason);
94
- };
95
- const logAction = (doctype, actionName, recordIds, result = 'success', errorMessage) => {
96
- return stonecrop.value?.getOperationLogStore().logAction(doctype, actionName, recordIds, result, errorMessage) ?? '';
97
- };
98
- const configure = (config) => {
99
- stonecrop.value?.getOperationLogStore().configure(config);
100
- };
101
- // Wire operation log reactive state synchronously — no lifecycle hook needed.
102
- // storeToRefs and watch are both safe to call in setup() body.
103
- if (registry && stonecrop.value) {
104
- try {
105
- const opLogStore = stonecrop.value.getOperationLogStore();
106
- const opLogRefs = storeToRefs(opLogStore);
107
- operations.value = opLogRefs.operations.value;
108
- currentIndex.value = opLogRefs.currentIndex.value;
109
- // Watch for changes in operation log state
110
- watch(() => opLogRefs.operations.value, newOps => {
111
- operations.value = newOps;
112
- });
113
- watch(() => opLogRefs.currentIndex.value, newIndex => {
114
- currentIndex.value = newIndex;
115
- });
116
- }
117
- catch {
118
- // Pinia not available — operation log is optional, silently skip
119
- }
120
- }
121
- // Synchronous HST initialisation for an explicit Doctype instance.
122
- // When the caller passes a Doctype object (not a slug string), every piece of
123
- // setup that doesn't require network I/O runs here during setup() so that
124
- // hstStore, resolvedSchema, and formData are populated before the first render
125
- // and are immediately available to callers without any await.
126
- if (options.doctype && typeof options.doctype !== 'string' && registry && stonecrop.value) {
127
- hstStore.value = stonecrop.value.getStore();
128
- resolvedSchema.value = registry.resolveSchema(options.doctype);
129
- if (!options.recordId || isDraftRecordId(options.recordId)) {
130
- formData.value = registry.initializeRecord(resolvedSchema.value);
131
- }
132
- if (hstStore.value) {
133
- setupDeepReactivity(options.doctype, options.recordId || 'new', formData, hstStore.value);
134
- }
135
- }
136
- // onMounted handles only work that is genuinely async: lazy-loading a doctype by slug and
137
- // fetching an existing record from the server. Both require the caller to name a doctype.
138
- //
139
- // Rejected: inferring the doctype from the route when the caller names none. Every output of
140
- // that inference landed in refs the no-argument callers never read — all of them wanted the
141
- // instance, which is resolved synchronously above — so each paid a getMeta plus a getRecord to
142
- // read a value already in hand. It also lowercased the route's record id, which silently missed
143
- // any natural key carrying uppercase.
144
- onMounted(async () => {
145
- if (!registry || !stonecrop.value) {
146
- return;
147
- }
148
- // Handle HST integration if doctype is provided explicitly
149
- if (options.doctype) {
150
- const recordId = options.recordId;
151
- if (typeof options.doctype === 'string') {
152
- // String doctype — resolve lazily, then do full sync-equivalent setup here.
153
- const doctypeSlug = options.doctype;
154
- hstStore.value = stonecrop.value.getStore();
155
- isLoading.value = true;
156
- error.value = null;
157
- let doctype;
158
- try {
159
- // Check if already in registry
160
- doctype = registry.getDoctype(doctypeSlug);
161
- if (!doctype && registry.getMeta) {
162
- // Lazy-load via getMeta
163
- const routeContext = {
164
- path: `/${doctypeSlug}`,
165
- segments: [doctypeSlug],
166
- };
167
- doctype = await registry.getMeta(routeContext);
168
- if (doctype) {
169
- registry.addDoctype(doctype);
170
- }
171
- }
172
- if (!doctype) {
173
- error.value = new Error(`Doctype '${doctypeSlug}' not found in registry and getMeta returned no result`);
174
- }
175
- }
176
- catch (e) {
177
- error.value = e instanceof Error ? e : new Error(String(e));
178
- }
179
- finally {
180
- isLoading.value = false;
181
- }
182
- resolvedDoctype.value = doctype;
183
- if (!doctype)
184
- return;
185
- resolvedSchema.value = registry.resolveSchema(doctype);
186
- if (recordId && !isDraftRecordId(recordId)) {
187
- const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
188
- if (existingRecord) {
189
- formData.value = existingRecord.get('') || {};
190
- }
191
- else {
192
- try {
193
- await stonecrop.value.getRecord(doctype, recordId);
194
- const loadedRecord = stonecrop.value.getRecordById(doctype, recordId);
195
- if (loadedRecord) {
196
- formData.value = loadedRecord.get('') || {};
197
- }
198
- }
199
- catch {
200
- formData.value = registry.initializeRecord(resolvedSchema.value);
201
- }
202
- }
203
- }
204
- else {
205
- formData.value = registry.initializeRecord(resolvedSchema.value);
206
- }
207
- if (hstStore.value) {
208
- setupDeepReactivity(doctype, recordId || 'new', formData, hstStore.value);
209
- }
210
- }
211
- else {
212
- // Doctype instance — sync init was done during setup().
213
- // Only handle the async path: fetching an existing record from the server.
214
- if (recordId && !isDraftRecordId(recordId)) {
215
- const doctype = options.doctype;
216
- const existingRecord = stonecrop.value.getRecordById(doctype, recordId);
217
- if (existingRecord) {
218
- formData.value = existingRecord.get('') || {};
219
- }
220
- else {
221
- try {
222
- await stonecrop.value.getRecord(doctype, recordId);
223
- const loadedRecord = stonecrop.value.getRecordById(doctype, recordId);
224
- if (loadedRecord) {
225
- formData.value = loadedRecord.get('') || {};
226
- }
227
- }
228
- catch {
229
- formData.value = registry.initializeRecord(resolvedSchema.value);
230
- }
231
- }
232
- }
233
- }
234
- }
235
- });
236
- // HST integration functions - always created but only populated when HST is available
237
- const provideHSTPath = (fieldname, customRecordId) => {
238
- const doctype = resolvedDoctype.value;
239
- if (!doctype)
240
- return '';
241
- const actualRecordId = customRecordId || options.recordId || 'new';
242
- return `${doctype.slug}.${actualRecordId}.${fieldname}`;
243
- };
244
- const handleHSTChange = (changeData) => {
245
- const doctype = resolvedDoctype.value;
246
- if (!hstStore.value || !stonecrop.value || !doctype) {
247
- return;
248
- }
249
- try {
250
- const pathParts = changeData.path.split('.');
251
- if (pathParts.length >= 2) {
252
- const doctypeSlug = pathParts[0];
253
- const recordId = pathParts[1];
254
- if (!hstStore.value.has(`${doctypeSlug}.${recordId}`)) {
255
- stonecrop.value.addRecord(doctype, recordId, { ...formData.value });
256
- }
257
- if (pathParts.length > 3) {
258
- const recordPath = `${doctypeSlug}.${recordId}`;
259
- const nestedParts = pathParts.slice(2);
260
- let currentPath = recordPath;
261
- for (let i = 0; i < nestedParts.length - 1; i++) {
262
- currentPath += `.${nestedParts[i]}`;
263
- if (!hstStore.value.has(currentPath)) {
264
- const nextPart = nestedParts[i + 1];
265
- const isArray = !isNaN(Number(nextPart));
266
- hstStore.value.set(currentPath, isArray ? [] : {});
267
- }
268
- }
269
- }
270
- }
271
- hstStore.value.set(changeData.path, changeData.value);
272
- const fieldParts = changeData.fieldname.split('.');
273
- const newFormData = { ...formData.value };
274
- if (fieldParts.length === 1) {
275
- newFormData[fieldParts[0]] = changeData.value;
276
- }
277
- else {
278
- updateNestedObject(newFormData, fieldParts, changeData.value);
279
- }
280
- formData.value = newFormData;
281
- }
282
- catch {
283
- // Silently handle errors
284
- }
285
- };
286
- // Provide injection tokens if HST will be available
287
- if (options.doctype) {
288
- provide('hstPathProvider', provideHSTPath);
289
- provide('hstChangeHandler', handleHSTChange);
290
- }
291
- /**
292
- * Scaffold empty descendant records from defaults for all descendant links.
293
- * Delegates to Stonecrop.initializeNestedData method.
294
- * @param path - The HST path where initialized data should be stored
295
- * @param doctype - The doctype to initialize
296
- */
297
- const initializeNestedData = (path, doctype) => {
298
- if (!stonecrop.value) {
299
- throw new Error('Stonecrop instance not available');
300
- }
301
- return stonecrop.value.initializeNestedData(path, doctype);
302
- };
303
- /**
304
- * Fetch a record and its nested data from the server.
305
- * Delegates to Stonecrop.fetchNestedData method.
306
- * @param path - The HST path (e.g., "recipe.r1")
307
- * @param doctype - The doctype to fetch
308
- * @param recordId - Record ID to fetch
309
- * @param options - Query options (includeNested to control which links are fetched)
310
- */
311
- const fetchNestedData = async (path, doctype, recordId, fetchOptions) => {
312
- if (!stonecrop.value) {
313
- throw new Error('Stonecrop instance not available');
314
- }
315
- return stonecrop.value.fetchNestedData(path, doctype, recordId, fetchOptions);
316
- };
317
- /**
318
- * Collect a record payload with all nested doctype fields from HST
319
- * Delegates to Stonecrop.collectRecordPayload method
320
- * @param doctype - The doctype metadata
321
- * @param recordId - The record ID to collect
322
- * @returns The complete record payload ready for API submission
323
- */
324
- const collectRecordPayload = (doctype, recordId) => {
325
- if (!stonecrop.value) {
326
- throw new Error('Stonecrop instance not available');
327
- }
328
- return stonecrop.value.collectRecordPayload(doctype, recordId);
329
- };
330
- /**
331
- * Create a nested context for descendant forms
332
- * @param basePath - The base path for the nested context (e.g., "customer.123.address")
333
- * @param _descendantDoctype - The descendant doctype metadata (unused but kept for API consistency)
334
- * @returns Object with scoped provideHSTPath and handleHSTChange
335
- */
336
- const createNestedContext = (basePath, _descendantDoctype) => {
337
- const nestedProvideHSTPath = (fieldname) => {
338
- return `${basePath}.${fieldname}`;
339
- };
340
- const nestedHandleHSTChange = (changeData) => {
341
- // Update the path to be relative to the nested base path
342
- const nestedPath = changeData.path.startsWith(basePath) ? changeData.path : `${basePath}.${changeData.fieldname}`;
343
- handleHSTChange({
344
- ...changeData,
345
- path: nestedPath,
346
- });
347
- };
348
- return {
349
- provideHSTPath: nestedProvideHSTPath,
350
- handleHSTChange: nestedHandleHSTChange,
351
- };
352
- };
353
- // Create operation log API object
354
- const operationLog = {
355
- operations,
356
- currentIndex,
357
- undoRedoState,
358
- canUndo,
359
- canRedo,
360
- undoCount,
361
- redoCount,
362
- undo,
363
- redo,
364
- startBatch,
365
- commitBatch,
366
- cancelBatch,
367
- clear,
368
- getOperationsFor,
369
- getSnapshot,
370
- markIrreversible,
371
- logAction,
372
- configure,
373
- };
374
- // Always return HST functions if doctype is provided or will be loaded from router
375
- if (options.doctype) {
376
- // Explicit doctype - return HST immediately
377
- return {
378
- stonecrop,
379
- operationLog,
380
- provideHSTPath,
381
- handleHSTChange,
382
- hstStore,
383
- formData,
384
- resolvedSchema,
385
- initializeNestedData,
386
- fetchNestedData,
387
- collectRecordPayload,
388
- createNestedContext,
389
- isLoading,
390
- error,
391
- resolvedDoctype,
392
- isWorkflowReady,
393
- blockedLinks,
394
- };
395
- }
396
- // No doctype named: the instance and the operation log are everything this can offer.
397
- return {
398
- stonecrop,
399
- operationLog,
400
- };
401
- }
402
- /**
403
- * Setup deep reactivity between form data and HST store
404
- */
405
- function setupDeepReactivity(doctype, recordId, formData, hstStore) {
406
- watch(formData, newData => {
407
- const recordPath = `${doctype.slug}.${recordId}`;
408
- Object.keys(newData).forEach(fieldname => {
409
- const path = `${recordPath}.${fieldname}`;
410
- try {
411
- hstStore.set(path, newData[fieldname]);
412
- }
413
- catch {
414
- // Silently handle errors
415
- }
416
- });
417
- }, { deep: true });
418
- }
419
- /**
420
- * Update nested object with dot-notation path
421
- */
422
- function updateNestedObject(obj, path, value) {
423
- let current = obj;
424
- for (let i = 0; i < path.length - 1; i++) {
425
- const key = path[i];
426
- if (!(key in current) || typeof current[key] !== 'object') {
427
- current[key] = isNaN(Number(path[i + 1])) ? {} : [];
428
- }
429
- current = current[key];
430
- }
431
- const finalKey = path[path.length - 1];
432
- current[finalKey] = value;
433
- }
@@ -1,243 +0,0 @@
1
- import type { DoctypeField, LinkDeclaration, TriggerDefinition } from '@stonecrop/schema';
2
- import { Component } from 'vue';
3
- import type { DoctypeConfig, ImmutableDoctype } from './types/doctype';
4
- /**
5
- * Doctype runtime class with Immutable.js collections for HST change tracking.
6
- * @public
7
- */
8
- export default class Doctype {
9
- /**
10
- * The doctype name
11
- * @public
12
- * @readonly
13
- */
14
- readonly doctype: string;
15
- /**
16
- * Alias for doctype (for DoctypeLike interface compatibility)
17
- * @public
18
- * @readonly
19
- */
20
- get name(): string;
21
- /**
22
- * The doctype schema
23
- * @public
24
- * @readonly
25
- */
26
- readonly schema: ImmutableDoctype['schema'];
27
- /**
28
- * The doctype workflow
29
- * @public
30
- * @readonly
31
- */
32
- readonly workflow: ImmutableDoctype['workflow'];
33
- /**
34
- * The doctype component
35
- * @public
36
- * @readonly
37
- */
38
- readonly component?: Component;
39
- /**
40
- * Relationship links to other doctypes
41
- * @public
42
- * @readonly
43
- */
44
- readonly links?: Record<string, LinkDeclaration>;
45
- /**
46
- * Field on this doctype used when displaying a reference to one of its records.
47
- * @public
48
- * @readonly
49
- */
50
- readonly displayField?: string;
51
- /**
52
- * Creates a new Doctype instance
53
- * @param doctype - The doctype name
54
- * @param schema - The doctype schema definition
55
- * @param workflow - The doctype workflow configuration (XState machine)
56
- * @param component - Optional Vue component for rendering the doctype
57
- * @param links - Optional relationship links to other doctypes
58
- * @param displayField - Optional field used when displaying references to this doctype
59
- */
60
- constructor(doctype: string, schema: ImmutableDoctype['schema'], workflow: ImmutableDoctype['workflow'], component?: Component, links?: Record<string, LinkDeclaration>, displayField?: string);
61
- /**
62
- * Creates a Doctype instance from a plain configuration object.
63
- * Handles conversion of arrays to Immutable.js collections internally.
64
- *
65
- * This is the recommended way to create a Doctype from API responses
66
- * or configuration files, as it encapsulates the Immutable.js construction
67
- * that the framework uses internally.
68
- *
69
- * @param config - Plain object with doctype configuration (typically from API response)
70
- * @returns A new Doctype instance with Immutable.js collections
71
- *
72
- * @example
73
- * ```ts
74
- * // From an API response
75
- * const response = await client.getMeta({ doctype: 'plan' })
76
- * const doctype = Doctype.fromObject(response)
77
- * registry.addDoctype(doctype)
78
- * ```
79
- *
80
- * @example
81
- * ```ts
82
- * // From a configuration object
83
- * const planDoctype = Doctype.fromObject({
84
- * name: 'Plan',
85
- * fields: [
86
- * { fieldname: 'title', label: 'Title', component: 'ATextInput' },
87
- * { fieldname: 'status', label: 'Status', component: 'ATextInput' },
88
- * ],
89
- * workflow: {
90
- * id: 'plan',
91
- * initial: 'draft',
92
- * states: { draft: {}, submitted: {} }
93
- * }
94
- * })
95
- * ```
96
- *
97
- * @public
98
- */
99
- static fromObject(config: DoctypeConfig): Doctype;
100
- /**
101
- * Returns the raw authoring schema as a plain array.
102
- * For the resolved schema suitable for AForm, use `registry.resolveSchema(doctype)`.
103
- *
104
- * @returns Array of raw DoctypeField authoring definitions
105
- *
106
- * @example
107
- * ```ts
108
- * const fields = doctype.getSchemaArray()
109
- * // Pass to resolveSchema for AForm-ready output:
110
- * const resolved = registry.resolveSchema(doctype)
111
- * ```
112
- *
113
- * @public
114
- */
115
- getSchemaArray(): DoctypeField[];
116
- /**
117
- * Resolve a record's identity using this doctype's declared `primaryKey`, falling back to `id`.
118
- *
119
- * Lives here rather than at the call site because the doctype owns its schema — and because the
120
- * rule must match the server's, which builds its SQL identity predicate from the same declared
121
- * field via `@stonecrop/schema`'s `getPrimaryKeyField`.
122
- *
123
- * @param record - the record to read the identity from
124
- * @returns the identity as a string, or `undefined` when neither the declared key nor `id` yields one
125
- *
126
- * @public
127
- */
128
- getRecordId(record: Record<string, unknown>): string | undefined;
129
- /**
130
- * The field a record of this doctype is identified by: the declared `primaryKey`, or `id`
131
- * when nothing is declared.
132
- *
133
- * The client-side twin of the adapters' `recordLookupField`. Both are the same call to
134
- * `@stonecrop/schema`'s `getRecordIdField`, so the field a caller reads an identity out of is
135
- * the same field the adapter builds its lookup predicate on.
136
- *
137
- * Use this to ask whether a record *states* its own identity. `getRecordId` deliberately
138
- * falls back to `id` when the declared key is missing, which is right for resolving a link
139
- * from a record already in hand and wrong for deciding whether a server response settled on
140
- * a new identity: a response that omits a natural key would resolve through that fallback to
141
- * a surrogate the adapter cannot look up.
142
- *
143
- * @returns The identifying fieldname
144
- *
145
- * @public
146
- */
147
- get recordIdField(): string;
148
- /**
149
- * Returns the transitions available from a given workflow state, derived from the
150
- * doctype's workflow configuration. Supports both XState format and WorkflowMeta format.
151
- *
152
- * @param currentState - The state name to read transitions from
153
- * @returns Array of transition descriptors with `name` and `targetState`
154
- *
155
- * @example
156
- * ```ts
157
- * const transitions = doctype.getAvailableTransitions('draft')
158
- * // [{ name: 'SUBMIT', targetState: 'submitted' }]
159
- * ```
160
- *
161
- * @public
162
- */
163
- getAvailableTransitions(currentState: string): Array<{
164
- name: string;
165
- targetState: string;
166
- }>;
167
- /**
168
- * Returns the stateless **Commands** available in a given workflow state — side-effect
169
- * actions (save/print/email…) that do not change workflow state. Unlike transitions,
170
- * Commands may exist on a workflow that declares no `states` (a commands-only doctype),
171
- * and a Command with no `allowedStates` is available in every state.
172
- *
173
- * Only meaningful for WorkflowMeta format; XState workflows have no Commands.
174
- *
175
- * @param currentState - The record's current state, used to honor a Command's `allowedStates`
176
- * @returns Array of command descriptors with `name`
177
- *
178
- * @example
179
- * ```ts
180
- * const commands = doctype.getAvailableCommands('Draft')
181
- * // [{ name: 'Print' }]
182
- * ```
183
- *
184
- * @public
185
- */
186
- getAvailableCommands(currentState?: string): Array<{
187
- name: string;
188
- }>;
189
- /**
190
- * Returns the field-validation **triggers** declared on this doctype's workflow (advisory,
191
- * client-side). Keyed by trigger name. Returns undefined when the workflow is absent, is an
192
- * XState machine (no triggers), or simply declares none.
193
- *
194
- * @returns The `workflow.triggers` map, or undefined
195
- *
196
- * @example
197
- * ```ts
198
- * const triggers = doctype.getTriggers()
199
- * // { dateOrder: { on: ['start_date', 'end_date'], clientHandler: '…' } }
200
- * ```
201
- *
202
- * @public
203
- */
204
- getTriggers(): Record<string, TriggerDefinition> | undefined;
205
- /**
206
- * Returns metadata for a specific action, if available.
207
- * Only works with WorkflowMeta format; returns undefined for XState format.
208
- *
209
- * @param actionName - The action name to get metadata for
210
- * @returns Action metadata or undefined
211
- *
212
- * @example
213
- * ```ts
214
- * const actionMeta = doctype.getActionMeta('submit')
215
- * // { label: 'Submit', allowedStates: ['draft'] }
216
- * ```
217
- *
218
- * @public
219
- */
220
- getActionMeta(actionName: string): {
221
- label: string;
222
- requiredFields?: string[];
223
- allowedStates?: string[];
224
- } | undefined;
225
- /**
226
- * Converts the registered doctype string to a slug (kebab-case). The following conversions are made:
227
- * - It replaces camelCase and PascalCase with kebab-case strings
228
- * - It replaces spaces and underscores with hyphens
229
- * - It converts the string to lowercase
230
- *
231
- * @returns The slugified doctype string
232
- *
233
- * @example
234
- * ```ts
235
- * const doctype = new Doctype('TaskItem', schema, workflow, actions)
236
- * console.log(doctype.slug) // 'task-item'
237
- * ```
238
- *
239
- * @public
240
- */
241
- get slug(): string;
242
- }
243
- //# sourceMappingURL=doctype.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"doctype.d.ts","sourceRoot":"","sources":["../../src/doctype.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAgB,MAAM,mBAAmB,CAAA;AAGvG,OAAO,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AAE/B,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAEtE;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,OAAO;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IAExB;;;;OAIG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAA;IAE3C;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAA;IAE/C;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAA;IAE9B;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAEhD;;;;OAIG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;IAE9B;;;;;;;;OAQG;gBAEF,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,EAClC,QAAQ,EAAE,gBAAgB,CAAC,UAAU,CAAC,EACtC,SAAS,CAAC,EAAE,SAAS,EACrB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,EACvC,YAAY,CAAC,EAAE,MAAM;IAUtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO;IAYjD;;;;;;;;;;;;;;OAcG;IACH,cAAc,IAAI,YAAY,EAAE;IAKhC;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,SAAS;IAIhE;;;;;;;;;;;;;;;;;OAiBG;IACH,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED;;;;;;;;;;;;;;OAcG;IACH,uBAAuB,CAAC,YAAY,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC;IAqC3F;;;;;;;;;;;;;;;;;;OAkBG;IACH,oBAAoB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAcpE;;;;;;;;;;;;;;OAcG;IACH,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,SAAS;IAS5D;;;;;;;;;;;;;;OAcG;IACH,aAAa,CAAC,UAAU,EAAE,MAAM,GAC7B;QACA,KAAK,EAAE,MAAM,CAAA;QACb,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;QACzB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;KACvB,GACD,SAAS;IAWZ;;;;;;;;;;;;;;;OAeG;IACH,IAAI,IAAI,WAKP;CACD"}