@stonecrop/stonecrop 0.11.0 → 0.11.1

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 (49) hide show
  1. package/package.json +4 -4
  2. package/dist/composable.js +0 -1
  3. package/dist/composables/use-lazy-link-state.js +0 -125
  4. package/dist/composables/use-stonecrop.js +0 -476
  5. package/dist/operation-log-DB-dGNT9.js +0 -593
  6. package/dist/operation-log-DB-dGNT9.js.map +0 -1
  7. package/dist/src/composable.d.ts +0 -11
  8. package/dist/src/composable.d.ts.map +0 -1
  9. package/dist/src/composable.js +0 -477
  10. package/dist/src/composables/operation-log.js +0 -224
  11. package/dist/src/composables/stonecrop.js +0 -574
  12. package/dist/src/composables/use-lazy-link-state.d.ts +0 -25
  13. package/dist/src/composables/use-lazy-link-state.d.ts.map +0 -1
  14. package/dist/src/composables/use-stonecrop.d.ts +0 -93
  15. package/dist/src/composables/use-stonecrop.d.ts.map +0 -1
  16. package/dist/src/composables/useNestedSchema.d.ts +0 -110
  17. package/dist/src/composables/useNestedSchema.d.ts.map +0 -1
  18. package/dist/src/composables/useNestedSchema.js +0 -155
  19. package/dist/src/doctype.js +0 -234
  20. package/dist/src/exceptions.js +0 -16
  21. package/dist/src/field-triggers.js +0 -567
  22. package/dist/src/index.js +0 -23
  23. package/dist/src/plugins/index.js +0 -96
  24. package/dist/src/registry.js +0 -246
  25. package/dist/src/schema-validator.js +0 -315
  26. package/dist/src/stonecrop.js +0 -339
  27. package/dist/src/stores/data.d.ts +0 -11
  28. package/dist/src/stores/data.d.ts.map +0 -1
  29. package/dist/src/stores/hst.js +0 -495
  30. package/dist/src/stores/index.js +0 -12
  31. package/dist/src/stores/operation-log.js +0 -568
  32. package/dist/src/stores/xstate.d.ts +0 -31
  33. package/dist/src/stores/xstate.d.ts.map +0 -1
  34. package/dist/src/tsdoc-metadata.json +0 -11
  35. package/dist/src/types/field-triggers.js +0 -4
  36. package/dist/src/types/index.js +0 -4
  37. package/dist/src/types/operation-log.js +0 -0
  38. package/dist/src/types/registry.js +0 -0
  39. package/dist/src/utils.d.ts +0 -24
  40. package/dist/src/utils.d.ts.map +0 -1
  41. package/dist/stonecrop.css +0 -1
  42. package/dist/stonecrop.umd.cjs +0 -6
  43. package/dist/stonecrop.umd.cjs.map +0 -1
  44. package/dist/stores/data.js +0 -7
  45. package/dist/stores/xstate.js +0 -29
  46. package/dist/tests/setup.d.ts +0 -5
  47. package/dist/tests/setup.d.ts.map +0 -1
  48. package/dist/tests/setup.js +0 -15
  49. package/dist/utils.js +0 -46
@@ -1,339 +0,0 @@
1
- import { reactive } from 'vue';
2
- import { createHST } from './stores/hst';
3
- import { useOperationLogStore } from './stores/operation-log';
4
- /**
5
- * Main Stonecrop class with HST integration and built-in Operation Log
6
- * @public
7
- */
8
- export class Stonecrop {
9
- _hstStore;
10
- _operationLogStore;
11
- _operationLogConfig;
12
- _client;
13
- /** The registry instance containing all doctype definitions */
14
- registry;
15
- get hstStore() {
16
- if (!this._hstStore) {
17
- const initialStoreStructure = {};
18
- Object.keys(this.registry.registry).forEach(doctypeSlug => {
19
- initialStoreStructure[doctypeSlug] = {};
20
- });
21
- this._hstStore = createHST(reactive(initialStoreStructure), 'StonecropStore');
22
- }
23
- return this._hstStore;
24
- }
25
- /**
26
- * Creates a new Stonecrop instance with HST integration
27
- * @param registry - The Registry instance containing doctype definitions
28
- * @param operationLogConfig - Optional configuration for the operation log
29
- * @param options - Options including the data client (can be set later via setClient)
30
- */
31
- constructor(registry, operationLogConfig, options) {
32
- this.registry = registry;
33
- // Store config for lazy initialization
34
- this._operationLogConfig = operationLogConfig;
35
- // Store data client (can be set later via setClient)
36
- this._client = options?.client;
37
- // Setup automatic sync with Registry when doctypes are added
38
- this.setupRegistrySync();
39
- }
40
- /**
41
- * Set the data client for fetching doctype metadata and records.
42
- * Use this for deferred configuration in Nuxt/Vue plugin setups.
43
- *
44
- * @param client - DataClient implementation (e.g., StonecropClient from \@stonecrop/graphql-client)
45
- *
46
- * @example
47
- * ```ts
48
- * const { setClient } = useStonecropRegistry()
49
- * const client = new StonecropClient({ endpoint: '/graphql' })
50
- * setClient(client)
51
- * ```
52
- */
53
- setClient(client) {
54
- this._client = client;
55
- }
56
- /**
57
- * Get the current data client
58
- * @returns The DataClient instance or undefined if not set
59
- */
60
- getClient() {
61
- return this._client;
62
- }
63
- /**
64
- * Get the operation log store (lazy initialization)
65
- * @internal
66
- */
67
- getOperationLogStore() {
68
- if (!this._operationLogStore) {
69
- this._operationLogStore = useOperationLogStore();
70
- if (this._operationLogConfig) {
71
- this._operationLogStore.configure(this._operationLogConfig);
72
- }
73
- }
74
- return this._operationLogStore;
75
- }
76
- /**
77
- * Setup automatic sync with Registry when doctypes are added
78
- */
79
- setupRegistrySync() {
80
- // Extend Registry.addDoctype to auto-create HST store sections
81
- const originalAddDoctype = this.registry.addDoctype.bind(this.registry);
82
- this.registry.addDoctype = (doctype) => {
83
- // Call original method
84
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
85
- originalAddDoctype(doctype);
86
- // Auto-create HST store section for new doctype
87
- if (!this.hstStore.has(doctype.slug)) {
88
- this.hstStore.set(doctype.slug, {});
89
- }
90
- };
91
- }
92
- /**
93
- * Get records hash for a doctype
94
- * @param doctype - The doctype to get records for
95
- * @returns HST node containing records hash
96
- */
97
- records(doctype) {
98
- const slug = typeof doctype === 'string' ? doctype : doctype.slug;
99
- this.ensureDoctypeExists(slug);
100
- return this.hstStore.getNode(slug);
101
- }
102
- /**
103
- * Add a record to the store
104
- * @param doctype - The doctype
105
- * @param recordId - The record ID
106
- * @param recordData - The record data
107
- */
108
- addRecord(doctype, recordId, recordData) {
109
- const slug = typeof doctype === 'string' ? doctype : doctype.slug;
110
- this.ensureDoctypeExists(slug);
111
- // Store raw record data - let HST handle wrapping with proper hierarchy
112
- this.hstStore.set(`${slug}.${recordId}`, recordData);
113
- }
114
- /**
115
- * Get a specific record
116
- * @param doctype - The doctype
117
- * @param recordId - The record ID
118
- * @returns HST node for the record or undefined
119
- */
120
- getRecordById(doctype, recordId) {
121
- const slug = typeof doctype === 'string' ? doctype : doctype.slug;
122
- this.ensureDoctypeExists(slug);
123
- // First check if the record exists
124
- const recordExists = this.hstStore.has(`${slug}.${recordId}`);
125
- if (!recordExists) {
126
- return undefined;
127
- }
128
- // Check if the actual value is undefined (i.e., record was removed)
129
- const recordValue = this.hstStore.get(`${slug}.${recordId}`);
130
- if (recordValue === undefined) {
131
- return undefined;
132
- }
133
- // Use getNode to get the properly wrapped HST node with correct parent relationships
134
- return this.hstStore.getNode(`${slug}.${recordId}`);
135
- }
136
- /**
137
- * Remove a record from the store
138
- * @param doctype - The doctype
139
- * @param recordId - The record ID
140
- */
141
- removeRecord(doctype, recordId) {
142
- const slug = typeof doctype === 'string' ? doctype : doctype.slug;
143
- this.ensureDoctypeExists(slug);
144
- // Remove the specific record directly by setting to undefined
145
- if (this.hstStore.has(`${slug}.${recordId}`)) {
146
- this.hstStore.set(`${slug}.${recordId}`, undefined);
147
- }
148
- }
149
- /**
150
- * Get all record IDs for a doctype
151
- * @param doctype - The doctype
152
- * @returns Array of record IDs
153
- */
154
- getRecordIds(doctype) {
155
- const slug = typeof doctype === 'string' ? doctype : doctype.slug;
156
- this.ensureDoctypeExists(slug);
157
- const doctypeNode = this.hstStore.get(slug);
158
- if (!doctypeNode || typeof doctypeNode !== 'object') {
159
- return [];
160
- }
161
- return Object.keys(doctypeNode).filter(key => doctypeNode[key] !== undefined);
162
- }
163
- /**
164
- * Clear all records for a doctype
165
- * @param doctype - The doctype
166
- */
167
- clearRecords(doctype) {
168
- const slug = typeof doctype === 'string' ? doctype : doctype.slug;
169
- this.ensureDoctypeExists(slug);
170
- // Get all record IDs and remove them
171
- const recordIds = this.getRecordIds(slug);
172
- recordIds.forEach(recordId => {
173
- this.hstStore.set(`${slug}.${recordId}`, undefined);
174
- });
175
- }
176
- /**
177
- * Setup method for doctype initialization
178
- * @param doctype - The doctype to setup
179
- */
180
- setup(doctype) {
181
- // Ensure doctype exists in store
182
- this.ensureDoctypeExists(doctype.slug);
183
- }
184
- /**
185
- * Run action on doctype
186
- * Executes the action and logs it to the operation log for audit tracking
187
- * @param doctype - The doctype
188
- * @param action - The action to run
189
- * @param args - Action arguments (typically record IDs)
190
- */
191
- runAction(doctype, action, args) {
192
- const registry = this.registry.registry[doctype.slug];
193
- const actions = registry?.actions?.get(action);
194
- const recordIds = Array.isArray(args) ? args.filter((arg) => typeof arg === 'string') : undefined;
195
- // Log action execution start
196
- const opLogStore = this.getOperationLogStore();
197
- let actionResult = 'success';
198
- let actionError;
199
- try {
200
- // Execute action functions
201
- if (actions && actions.length > 0) {
202
- actions.forEach(actionStr => {
203
- try {
204
- // eslint-disable-next-line @typescript-eslint/no-implied-eval
205
- const actionFn = new Function('args', actionStr);
206
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
207
- actionFn(args);
208
- }
209
- catch (error) {
210
- actionResult = 'failure';
211
- actionError = error instanceof Error ? error.message : 'Unknown error';
212
- throw error;
213
- }
214
- });
215
- }
216
- }
217
- catch {
218
- // Error already set in inner catch
219
- }
220
- finally {
221
- // Log the action execution to operation log
222
- opLogStore.logAction(doctype.doctype, action, recordIds, actionResult, actionError);
223
- }
224
- }
225
- /**
226
- * Get records from server using the configured data client.
227
- * @param doctype - The doctype
228
- * @throws Error if no data client has been configured
229
- */
230
- async getRecords(doctype) {
231
- if (!this._client) {
232
- throw new Error('No data client configured. Call setClient() with a DataClient implementation ' +
233
- '(e.g., StonecropClient from @stonecrop/graphql-client) before fetching records.');
234
- }
235
- const records = await this._client.getRecords(doctype);
236
- // Store each record in HST
237
- records.forEach(record => {
238
- if (record.id) {
239
- this.addRecord(doctype, record.id, record);
240
- }
241
- });
242
- }
243
- /**
244
- * Get single record from server using the configured data client.
245
- * @param doctype - The doctype
246
- * @param recordId - The record ID
247
- * @throws Error if no data client has been configured
248
- */
249
- async getRecord(doctype, recordId) {
250
- if (!this._client) {
251
- throw new Error('No data client configured. Call setClient() with a DataClient implementation ' +
252
- '(e.g., StonecropClient from @stonecrop/graphql-client) before fetching records.');
253
- }
254
- const record = await this._client.getRecord(doctype, recordId);
255
- if (record) {
256
- this.addRecord(doctype, recordId, record);
257
- }
258
- }
259
- /**
260
- * Dispatch an action to the server via the configured data client.
261
- * All state changes flow through this single mutation endpoint.
262
- *
263
- * @param doctype - The doctype
264
- * @param action - Action name to execute (e.g., 'SUBMIT', 'APPROVE', 'save')
265
- * @param args - Action arguments (typically record ID and/or form data)
266
- * @returns Action result with success status, response data, and any error
267
- * @throws Error if no data client has been configured
268
- */
269
- async dispatchAction(doctype, action, args) {
270
- if (!this._client) {
271
- throw new Error('No data client configured. Call setClient() with a DataClient implementation ' +
272
- '(e.g., StonecropClient from @stonecrop/graphql-client) before dispatching actions.');
273
- }
274
- return this._client.runAction(doctype, action, args);
275
- }
276
- /**
277
- * Ensure doctype section exists in HST store
278
- * @param slug - The doctype slug
279
- */
280
- ensureDoctypeExists(slug) {
281
- if (!this.hstStore.has(slug)) {
282
- this.hstStore.set(slug, {});
283
- }
284
- }
285
- /**
286
- * Get doctype metadata from the registry
287
- * @param context - The route context
288
- * @returns The doctype metadata
289
- */
290
- async getMeta(context) {
291
- if (!this.registry.getMeta) {
292
- throw new Error('No getMeta function provided to Registry');
293
- }
294
- return await this.registry.getMeta(context);
295
- }
296
- /**
297
- * Get the root HST store node for advanced usage
298
- * @returns Root HST node
299
- */
300
- getStore() {
301
- return this.hstStore;
302
- }
303
- /**
304
- * Determine the current workflow state for a record.
305
- *
306
- * Reads the record's `status` field from the HST store. If the field is absent or
307
- * empty the doctype's declared `workflow.initial` state is used as the fallback,
308
- * giving callers a reliable state name without having to duplicate that logic.
309
- *
310
- * @param doctype - The doctype slug or Doctype instance
311
- * @param recordId - The record identifier
312
- * @returns The current state name, or an empty string if the doctype has no workflow
313
- *
314
- * @public
315
- */
316
- getRecordState(doctype, recordId) {
317
- const slug = typeof doctype === 'string' ? doctype : doctype.slug;
318
- const meta = this.registry.getDoctype(slug);
319
- if (!meta?.workflow)
320
- return '';
321
- const record = this.getRecordById(slug, recordId);
322
- const status = record?.get('status');
323
- // Handle both XState format and WorkflowMeta format
324
- const workflow = meta.workflow;
325
- let initialState;
326
- if (Array.isArray(workflow.states)) {
327
- // WorkflowMeta format: states is a string array
328
- initialState = workflow.states[0] ?? '';
329
- }
330
- else {
331
- // XState format: states is an object, use initial or first key
332
- initialState =
333
- typeof workflow.initial === 'string'
334
- ? workflow.initial
335
- : Object.keys(workflow.states ?? {})[0] ?? '';
336
- }
337
- return status || initialState;
338
- }
339
- }
@@ -1,11 +0,0 @@
1
- export declare const useDataStore: import("pinia").StoreDefinition<"data", Pick<{
2
- records: import("vue").Ref<Record<string, any>[], Record<string, any>[]>;
3
- record: import("vue").Ref<Record<string, any>, Record<string, any>>;
4
- }, "records" | "record">, Pick<{
5
- records: import("vue").Ref<Record<string, any>[], Record<string, any>[]>;
6
- record: import("vue").Ref<Record<string, any>, Record<string, any>>;
7
- }, never>, Pick<{
8
- records: import("vue").Ref<Record<string, any>[], Record<string, any>[]>;
9
- record: import("vue").Ref<Record<string, any>, Record<string, any>>;
10
- }, never>>;
11
- //# sourceMappingURL=data.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../../src/stores/data.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY;;;;;;;;;UAIvB,CAAA"}