@stonecrop/stonecrop 0.16.6 → 0.18.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 +16 -8
- package/dist/src/client-action.d.ts +31 -0
- package/dist/src/client-action.d.ts.map +1 -0
- package/dist/src/client-action.js +169 -0
- package/dist/src/composables/stonecrop.d.ts +9 -6
- package/dist/src/composables/stonecrop.d.ts.map +1 -1
- package/dist/src/composables/stonecrop.js +19 -90
- package/dist/src/doctype.d.ts +15 -12
- package/dist/src/doctype.d.ts.map +1 -1
- package/dist/src/doctype.js +19 -20
- package/dist/src/draft.d.ts +35 -0
- package/dist/src/draft.d.ts.map +1 -0
- package/dist/src/draft.js +36 -0
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +10 -4
- package/dist/src/registry.d.ts.map +1 -1
- package/dist/src/registry.js +6 -8
- package/dist/src/stonecrop.d.ts +67 -27
- package/dist/src/stonecrop.d.ts.map +1 -1
- package/dist/src/stonecrop.js +98 -84
- package/dist/src/stores/operation-log.d.ts +1 -1
- package/dist/src/types/client-action.d.ts +121 -0
- package/dist/src/types/client-action.d.ts.map +1 -0
- package/dist/src/types/client-action.js +0 -0
- package/dist/src/types/doctype.d.ts +1 -4
- package/dist/src/types/doctype.d.ts.map +1 -1
- package/dist/src/types/index.d.ts +1 -0
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/index.js +1 -0
- package/dist/stonecrop.d.ts +274 -49
- package/dist/stonecrop.js +662 -617
- package/dist/stonecrop.js.map +1 -1
- package/package.json +4 -4
- package/src/client-action.ts +207 -0
- package/src/composables/stonecrop.ts +28 -102
- package/src/doctype.ts +18 -20
- package/src/draft.ts +38 -0
- package/src/index.ts +8 -2
- package/src/registry.ts +6 -8
- package/src/stonecrop.ts +107 -93
- package/src/types/client-action.ts +121 -0
- package/src/types/doctype.ts +1 -4
- package/src/types/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stonecrop/stonecrop",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "Schema-driven framework with XState workflows and HST state management",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/agritheory/stonecrop/issues"
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"immutable": "^5.1.4",
|
|
35
35
|
"pinia-shared-state": "^1.0.1",
|
|
36
36
|
"xstate": "^5.25.0",
|
|
37
|
-
"@stonecrop/
|
|
38
|
-
"@stonecrop/
|
|
39
|
-
"@stonecrop/
|
|
37
|
+
"@stonecrop/atable": "0.18.0",
|
|
38
|
+
"@stonecrop/schema": "0.18.0",
|
|
39
|
+
"@stonecrop/aform": "0.18.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@microsoft/api-documenter": "^7.30.5",
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { WorkflowMeta } from '@stonecrop/schema'
|
|
2
|
+
import { useRouter } from 'vue-router'
|
|
3
|
+
|
|
4
|
+
import { executeClientHandler } from './client-handler'
|
|
5
|
+
import { useStonecrop } from './composables/stonecrop'
|
|
6
|
+
import Doctype from './doctype'
|
|
7
|
+
import { isDraftRecordId } from './draft'
|
|
8
|
+
import type {
|
|
9
|
+
ActionDispatchResult,
|
|
10
|
+
ActionEventPayload,
|
|
11
|
+
ActionFailure,
|
|
12
|
+
FollowRecordContext,
|
|
13
|
+
UseClientActionOptions,
|
|
14
|
+
} from './types/client-action'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The identity a server response settled on, or `undefined` when it did not state one.
|
|
18
|
+
*
|
|
19
|
+
* Deliberately stricter than `getRecordId`: that falls back to `id` when the declared key is
|
|
20
|
+
* absent, which here would let a handler returning a partial record (say `{ id, total }` for a
|
|
21
|
+
* natural-keyed doctype) look like a rename and relocate the record to a key the adapter cannot
|
|
22
|
+
* look up. An action whose result carries no identity at all — a `{ state }` outcome — leaves the
|
|
23
|
+
* record exactly where it is.
|
|
24
|
+
*/
|
|
25
|
+
function settledRecordId(doctype: Doctype, record: Record<string, unknown>): string | undefined {
|
|
26
|
+
if (record[doctype.recordIdField] === undefined) return undefined
|
|
27
|
+
return doctype.getRecordId(record)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Default failure surface: a blocking alert beats a silent `console.error` for a failed action
|
|
32
|
+
* (e.g. a Save that the server refused), and there is no notification system in the framework to
|
|
33
|
+
* route it into. A host with one passes `onError` and this is never called.
|
|
34
|
+
*/
|
|
35
|
+
function notifyActionError(failure: ActionFailure): void {
|
|
36
|
+
console.error('Action failed:', failure.message)
|
|
37
|
+
if (typeof window !== 'undefined') window.alert(failure.message)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Shared executor for doctype action clicks. A host's Desktop `@action` handler delegates
|
|
42
|
+
* here so every host runs the same logic from one definition:
|
|
43
|
+
*
|
|
44
|
+
* - If the clicked action carries a `clientHandler`, run it. The handler **owns
|
|
45
|
+
* orchestration** — it calls `runAction` itself when it needs the server, navigates via
|
|
46
|
+
* `router`, reads `record`, or queries `graphql`. It supersedes the default dispatch.
|
|
47
|
+
* - Otherwise dispatch the action to its server `handler` (the pre-existing behavior),
|
|
48
|
+
* so actions without a `clientHandler` are unchanged.
|
|
49
|
+
*
|
|
50
|
+
* `runAction` is the only blessed write: it dispatches **and** leaves the store consistent,
|
|
51
|
+
* filing the returned record under the identity the *server* settled on and following the route
|
|
52
|
+
* there when that differs from the one dispatched. For a created record those are never the same,
|
|
53
|
+
* which is what makes hand-rolling this reliably wrong.
|
|
54
|
+
*
|
|
55
|
+
* The store write itself lives one layer down, in {@link Stonecrop.dispatchAction}, so a host that
|
|
56
|
+
* never adopts this composable still cannot file a record under the wrong key. What stays here is
|
|
57
|
+
* only what needs the *dispatched* id: dropping the stale key, and moving the route.
|
|
58
|
+
*
|
|
59
|
+
* Pass `buildArgs` to change the argument envelope your backend receives, `followRecord` to change
|
|
60
|
+
* where a created record sends the user, and `onError` to route failures into your own
|
|
61
|
+
* notification system. Identity resolution and HST keying are deliberately not configurable — see
|
|
62
|
+
* {@link UseClientActionOptions}.
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
export function useClientAction(options: UseClientActionOptions = {}) {
|
|
67
|
+
const { stonecrop } = useStonecrop()
|
|
68
|
+
// Called during setup so the injection is available; the registry fallback below covers a
|
|
69
|
+
// caller outside an injection context, and a host that installed the plugin with `{ router }`.
|
|
70
|
+
const injectedRouter = useRouter()
|
|
71
|
+
const onError = options.onError ?? notifyActionError
|
|
72
|
+
|
|
73
|
+
const resolveRouter = () => injectedRouter ?? stonecrop.value?.registry.router
|
|
74
|
+
|
|
75
|
+
const buildArgs =
|
|
76
|
+
options.buildArgs ??
|
|
77
|
+
// Spreading `undefined` in an object literal adds nothing, so `extra` needs no fallback.
|
|
78
|
+
(({ recordId, isDraft, data, extra }) => [{ ...(isDraft ? {} : { id: recordId }), data, ...extra }])
|
|
79
|
+
|
|
80
|
+
const followRecord =
|
|
81
|
+
options.followRecord ??
|
|
82
|
+
(async ({ doctype, recordId }: FollowRecordContext) => {
|
|
83
|
+
await resolveRouter()?.replace(`/${doctype}/${recordId}`)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Dispatch `action` for the record and reconcile the store and the route with whichever
|
|
88
|
+
* identity the result carries.
|
|
89
|
+
*/
|
|
90
|
+
async function dispatchAndWriteback(
|
|
91
|
+
doctypeSlug: string,
|
|
92
|
+
recordId: string,
|
|
93
|
+
data: Record<string, unknown>,
|
|
94
|
+
action: string,
|
|
95
|
+
extra?: Record<string, unknown>
|
|
96
|
+
): Promise<ActionDispatchResult> {
|
|
97
|
+
const sc = stonecrop.value
|
|
98
|
+
if (!sc) return { success: false, data: null, error: 'Stonecrop is not initialized' }
|
|
99
|
+
|
|
100
|
+
const doctype = sc.registry.getDoctype(doctypeSlug)
|
|
101
|
+
if (!doctype) return { success: false, data: null, error: `Unknown doctype: ${doctypeSlug}` }
|
|
102
|
+
|
|
103
|
+
// A draft omits the id, which the write path reads as "create". Sending the route segment
|
|
104
|
+
// instead reaches the same branch by accident — via a lookup for a record named `new`.
|
|
105
|
+
const isDraft = isDraftRecordId(recordId)
|
|
106
|
+
const result = await sc.dispatchAction(
|
|
107
|
+
doctype,
|
|
108
|
+
action,
|
|
109
|
+
buildArgs({ doctype: doctypeSlug, action, recordId, isDraft, data, extra })
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
if (result.success && result.data) {
|
|
113
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the action result payload is an opaque JSON scalar; every adapter returns the record as an object
|
|
114
|
+
const record = result.data as Record<string, unknown>
|
|
115
|
+
const settledId = settledRecordId(doctype, record)
|
|
116
|
+
|
|
117
|
+
if (settledId === undefined) {
|
|
118
|
+
// The result states no identity of its own — a `{ state }` outcome. `dispatchAction`
|
|
119
|
+
// could not file it, because only this layer knows which record was dispatched. A
|
|
120
|
+
// draft has nowhere to put it: filing it under the route segment would leave a
|
|
121
|
+
// record named `new` in the list view.
|
|
122
|
+
if (!isDraft) sc.addRecord(doctypeSlug, recordId, record)
|
|
123
|
+
return result
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// `dispatchAction` has already filed the record under `settledId`. Only the two steps
|
|
127
|
+
// that need the dispatched id are left.
|
|
128
|
+
if (recordId !== settledId) {
|
|
129
|
+
// No-op for a draft, which was never in the store — `removeRecord` checks first.
|
|
130
|
+
sc.removeRecord(doctypeSlug, recordId)
|
|
131
|
+
await followRecord({ doctype: doctypeSlug, recordId: settledId, previousRecordId: recordId })
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return result
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Handle a Desktop `@action` event: run the action's `clientHandler` if present,
|
|
139
|
+
* else dispatch to the server handler.
|
|
140
|
+
*/
|
|
141
|
+
async function run(payload: ActionEventPayload): Promise<void> {
|
|
142
|
+
const sc = stonecrop.value
|
|
143
|
+
if (!sc) return
|
|
144
|
+
|
|
145
|
+
const { name, doctype: doctypeSlug, recordId, data } = payload
|
|
146
|
+
|
|
147
|
+
const doctype = sc.registry.getDoctype(doctypeSlug)
|
|
148
|
+
// `workflow` is a union (XState | WorkflowMeta); only WorkflowMeta carries an
|
|
149
|
+
// `actions` map with `clientHandler`. An XState workflow has no `actions`, so the
|
|
150
|
+
// lookup is undefined and we fall through to the server dispatch.
|
|
151
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- narrowing the workflow union to read the optional actions map
|
|
152
|
+
const workflow = doctype?.workflow as WorkflowMeta | undefined
|
|
153
|
+
const clientHandler = workflow?.actions?.[name]?.clientHandler
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
if (!clientHandler) {
|
|
157
|
+
// No client handler — preserve the existing server-dispatch behavior.
|
|
158
|
+
const result = await dispatchAndWriteback(doctypeSlug, recordId, data, name)
|
|
159
|
+
if (!result.success) {
|
|
160
|
+
onError({
|
|
161
|
+
message: result.error ?? `Action "${name}" failed`,
|
|
162
|
+
action: name,
|
|
163
|
+
doctype: doctypeSlug,
|
|
164
|
+
recordId,
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
return
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Client handler present — it owns orchestration. Assemble the capability map.
|
|
171
|
+
// Prefer the live HST record; fall back to the form snapshot in the payload.
|
|
172
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- HSTNode.get returns any; a record node is always an object of field values
|
|
173
|
+
const record = (sc.getRecordById(doctypeSlug, recordId)?.get('') as Record<string, unknown> | undefined) ?? data
|
|
174
|
+
|
|
175
|
+
const runAction = (action: string, extra?: Record<string, unknown>) =>
|
|
176
|
+
dispatchAndWriteback(doctypeSlug, recordId, data, action, extra)
|
|
177
|
+
|
|
178
|
+
// Read-only GraphQL escape hatch — no mutation is injected (a raw mutation would
|
|
179
|
+
// bypass the dispatch and leave HST stale). `query` is the GraphQL transport's
|
|
180
|
+
// method, not part of the abstract DataClient interface (a non-GraphQL client may
|
|
181
|
+
// not support it), so probe for it structurally and reject if unsupported.
|
|
182
|
+
const graphql = {
|
|
183
|
+
query<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T> {
|
|
184
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- probing DataClient for the GraphQL transport's `query`, which the abstract interface deliberately does not declare
|
|
185
|
+
const client = sc.getClient() as unknown as
|
|
186
|
+
{ query?: <R>(q: string, v?: Record<string, unknown>) => Promise<R> } | undefined
|
|
187
|
+
if (!client?.query) {
|
|
188
|
+
return Promise.reject(new Error('The configured data client does not support graphql.query'))
|
|
189
|
+
}
|
|
190
|
+
return client.query<T>(query, variables)
|
|
191
|
+
},
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
await executeClientHandler(clientHandler, { router: resolveRouter(), record, runAction, graphql })
|
|
195
|
+
} catch (error) {
|
|
196
|
+
onError({
|
|
197
|
+
message: error instanceof Error ? error.message : String(error),
|
|
198
|
+
action: name,
|
|
199
|
+
doctype: doctypeSlug,
|
|
200
|
+
recordId,
|
|
201
|
+
cause: error,
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { run }
|
|
207
|
+
}
|
|
@@ -3,6 +3,7 @@ import { storeToRefs } from 'pinia'
|
|
|
3
3
|
import { inject, onMounted, Ref, ref, watch, provide, computed } from 'vue'
|
|
4
4
|
|
|
5
5
|
import Doctype from '../doctype'
|
|
6
|
+
import { isDraftRecordId } from '../draft'
|
|
6
7
|
import Registry from '../registry'
|
|
7
8
|
import { Stonecrop } from '../stonecrop'
|
|
8
9
|
import type { HSTNode } from '../types/hst'
|
|
@@ -11,13 +12,15 @@ import type { HSTOperation, OperationLogConfig } from '../types/operation-log'
|
|
|
11
12
|
import type { RouteContext } from '../types/registry'
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
|
-
* Unified Stonecrop composable - handles both general operations and HST reactive integration
|
|
15
|
+
* Unified Stonecrop composable - handles both general operations and HST reactive integration.
|
|
15
16
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
17
|
+
* Called with no doctype, it returns the Stonecrop instance and the operation log, and touches
|
|
18
|
+
* the network not at all. Name a doctype to get the HST surface.
|
|
19
|
+
*
|
|
20
|
+
* @returns The Stonecrop instance and the operation log
|
|
18
21
|
* @public
|
|
19
22
|
*/
|
|
20
|
-
export function useStonecrop(): BaseStonecropReturn
|
|
23
|
+
export function useStonecrop(): BaseStonecropReturn
|
|
21
24
|
/**
|
|
22
25
|
* Unified Stonecrop composable with HST integration for a specific doctype and record.
|
|
23
26
|
*
|
|
@@ -28,8 +31,9 @@ export function useStonecrop(): BaseStonecropReturn | HSTStonecropReturn
|
|
|
28
31
|
* immediately after calling this composable; no `nextTick`, `flushPromises`, or
|
|
29
32
|
* `setTimeout` is required.
|
|
30
33
|
*
|
|
31
|
-
* The only
|
|
32
|
-
*
|
|
34
|
+
* The only async work in `onMounted` is fetching an existing record from the server when
|
|
35
|
+
* `recordId` is not a draft, and lazy-loading a doctype by slug string. Both need a doctype
|
|
36
|
+
* named here — nothing is inferred from the route.
|
|
33
37
|
*
|
|
34
38
|
* @param options - Configuration with doctype (string slug or Doctype instance) and optional recordId
|
|
35
39
|
* @returns Stonecrop instance with full HST integration utilities
|
|
@@ -56,10 +60,6 @@ export function useStonecrop(options?: {
|
|
|
56
60
|
const hstStore = ref<HSTNode>()
|
|
57
61
|
const formData = ref<Record<string, any>>({})
|
|
58
62
|
|
|
59
|
-
// Use refs for router-loaded doctype to maintain reactivity
|
|
60
|
-
const routerDoctype = ref<Doctype | undefined>()
|
|
61
|
-
const routerRecordId = ref<string | undefined>()
|
|
62
|
-
|
|
63
63
|
// Resolved schema with nested Doctype fields expanded
|
|
64
64
|
const resolvedSchema = ref<ResolvedField[]>([])
|
|
65
65
|
|
|
@@ -70,7 +70,7 @@ export function useStonecrop(options?: {
|
|
|
70
70
|
|
|
71
71
|
// Workflow readiness computed properties
|
|
72
72
|
const isWorkflowReady = computed(() => {
|
|
73
|
-
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || options.recordId
|
|
73
|
+
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
|
|
74
74
|
return true
|
|
75
75
|
}
|
|
76
76
|
const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId)
|
|
@@ -78,7 +78,7 @@ export function useStonecrop(options?: {
|
|
|
78
78
|
})
|
|
79
79
|
|
|
80
80
|
const blockedLinks = computed(() => {
|
|
81
|
-
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || options.recordId
|
|
81
|
+
if (!stonecrop.value || !resolvedDoctype.value || !options.recordId || isDraftRecordId(options.recordId)) {
|
|
82
82
|
return []
|
|
83
83
|
}
|
|
84
84
|
const status = stonecrop.value.isWorkflowReady(resolvedDoctype.value, options.recordId)
|
|
@@ -209,7 +209,7 @@ export function useStonecrop(options?: {
|
|
|
209
209
|
if (options.doctype && typeof options.doctype !== 'string' && registry && stonecrop.value) {
|
|
210
210
|
hstStore.value = stonecrop.value.getStore()
|
|
211
211
|
resolvedSchema.value = registry.resolveSchema(options.doctype)
|
|
212
|
-
if (!options.recordId || options.recordId
|
|
212
|
+
if (!options.recordId || isDraftRecordId(options.recordId)) {
|
|
213
213
|
formData.value = registry.initializeRecord(resolvedSchema.value)
|
|
214
214
|
}
|
|
215
215
|
if (hstStore.value) {
|
|
@@ -217,73 +217,19 @@ export function useStonecrop(options?: {
|
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
-
// onMounted handles only work that is genuinely async: lazy-loading a doctype
|
|
221
|
-
//
|
|
220
|
+
// onMounted handles only work that is genuinely async: lazy-loading a doctype by slug and
|
|
221
|
+
// fetching an existing record from the server. Both require the caller to name a doctype.
|
|
222
|
+
//
|
|
223
|
+
// Rejected: inferring the doctype from the route when the caller names none. Every output of
|
|
224
|
+
// that inference landed in refs the no-argument callers never read — all of them wanted the
|
|
225
|
+
// instance, which is resolved synchronously above — so each paid a getMeta plus a getRecord to
|
|
226
|
+
// read a value already in hand. It also lowercased the route's record id, which silently missed
|
|
227
|
+
// any natural key carrying uppercase.
|
|
222
228
|
onMounted(async () => {
|
|
223
229
|
if (!registry || !stonecrop.value) {
|
|
224
230
|
return
|
|
225
231
|
}
|
|
226
232
|
|
|
227
|
-
// Handle router-based setup if no specific doctype provided
|
|
228
|
-
if (!options.doctype && registry.router) {
|
|
229
|
-
const route = registry.router.currentRoute.value
|
|
230
|
-
|
|
231
|
-
// Parse route path - let the application determine the doctype from the route
|
|
232
|
-
if (!route.path) return // Early return if no path available
|
|
233
|
-
|
|
234
|
-
const pathSegments = route.path.split('/').filter(segment => segment.length > 0)
|
|
235
|
-
const recordId = pathSegments[1]?.toLowerCase()
|
|
236
|
-
|
|
237
|
-
if (pathSegments.length > 0) {
|
|
238
|
-
// Create route context for getMeta function
|
|
239
|
-
const routeContext: RouteContext = {
|
|
240
|
-
path: route.path,
|
|
241
|
-
segments: pathSegments,
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const doctype = await registry.getMeta?.(routeContext)
|
|
245
|
-
if (doctype) {
|
|
246
|
-
registry.addDoctype(doctype)
|
|
247
|
-
stonecrop.value.setup(doctype)
|
|
248
|
-
|
|
249
|
-
// Set reactive refs for router-based doctype
|
|
250
|
-
routerDoctype.value = doctype
|
|
251
|
-
routerRecordId.value = recordId
|
|
252
|
-
hstStore.value = stonecrop.value.getStore()
|
|
253
|
-
|
|
254
|
-
// Resolve schema for router-loaded doctype
|
|
255
|
-
if (registry) {
|
|
256
|
-
resolvedSchema.value = registry.resolveSchema(doctype)
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (recordId && recordId !== 'new') {
|
|
260
|
-
const existingRecord = stonecrop.value.getRecordById(doctype, recordId)
|
|
261
|
-
if (existingRecord) {
|
|
262
|
-
formData.value = existingRecord.get('') || {}
|
|
263
|
-
} else {
|
|
264
|
-
try {
|
|
265
|
-
await stonecrop.value.getRecord(doctype, recordId)
|
|
266
|
-
const loadedRecord = stonecrop.value.getRecordById(doctype, recordId)
|
|
267
|
-
if (loadedRecord) {
|
|
268
|
-
formData.value = loadedRecord.get('') || {}
|
|
269
|
-
}
|
|
270
|
-
} catch {
|
|
271
|
-
formData.value = registry.initializeRecord(resolvedSchema.value)
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
} else {
|
|
275
|
-
formData.value = registry.initializeRecord(resolvedSchema.value)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (hstStore.value) {
|
|
279
|
-
setupDeepReactivity(doctype, recordId || 'new', formData, hstStore.value)
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
stonecrop.value.runAction(doctype, 'load', recordId ? [recordId] : undefined)
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
233
|
// Handle HST integration if doctype is provided explicitly
|
|
288
234
|
if (options.doctype) {
|
|
289
235
|
const recordId = options.recordId
|
|
@@ -326,7 +272,7 @@ export function useStonecrop(options?: {
|
|
|
326
272
|
|
|
327
273
|
resolvedSchema.value = registry.resolveSchema(doctype)
|
|
328
274
|
|
|
329
|
-
if (recordId && recordId
|
|
275
|
+
if (recordId && !isDraftRecordId(recordId)) {
|
|
330
276
|
const existingRecord = stonecrop.value.getRecordById(doctype, recordId)
|
|
331
277
|
if (existingRecord) {
|
|
332
278
|
formData.value = existingRecord.get('') || {}
|
|
@@ -351,7 +297,7 @@ export function useStonecrop(options?: {
|
|
|
351
297
|
} else {
|
|
352
298
|
// Doctype instance — sync init was done during setup().
|
|
353
299
|
// Only handle the async path: fetching an existing record from the server.
|
|
354
|
-
if (recordId && recordId
|
|
300
|
+
if (recordId && !isDraftRecordId(recordId)) {
|
|
355
301
|
const doctype = options.doctype
|
|
356
302
|
const existingRecord = stonecrop.value.getRecordById(doctype, recordId)
|
|
357
303
|
if (existingRecord) {
|
|
@@ -374,15 +320,15 @@ export function useStonecrop(options?: {
|
|
|
374
320
|
|
|
375
321
|
// HST integration functions - always created but only populated when HST is available
|
|
376
322
|
const provideHSTPath = (fieldname: string, customRecordId?: string): string => {
|
|
377
|
-
const doctype = resolvedDoctype.value
|
|
323
|
+
const doctype = resolvedDoctype.value
|
|
378
324
|
if (!doctype) return ''
|
|
379
325
|
|
|
380
|
-
const actualRecordId = customRecordId || options.recordId ||
|
|
326
|
+
const actualRecordId = customRecordId || options.recordId || 'new'
|
|
381
327
|
return `${doctype.slug}.${actualRecordId}.${fieldname}`
|
|
382
328
|
}
|
|
383
329
|
|
|
384
330
|
const handleHSTChange = (changeData: HSTChangeData): void => {
|
|
385
|
-
const doctype = resolvedDoctype.value
|
|
331
|
+
const doctype = resolvedDoctype.value
|
|
386
332
|
if (!hstStore.value || !stonecrop.value || !doctype) {
|
|
387
333
|
return
|
|
388
334
|
}
|
|
@@ -432,7 +378,7 @@ export function useStonecrop(options?: {
|
|
|
432
378
|
}
|
|
433
379
|
|
|
434
380
|
// Provide injection tokens if HST will be available
|
|
435
|
-
if (options.doctype
|
|
381
|
+
if (options.doctype) {
|
|
436
382
|
provide('hstPathProvider', provideHSTPath)
|
|
437
383
|
provide('hstChangeHandler', handleHSTChange)
|
|
438
384
|
}
|
|
@@ -553,29 +499,9 @@ export function useStonecrop(options?: {
|
|
|
553
499
|
isWorkflowReady,
|
|
554
500
|
blockedLinks,
|
|
555
501
|
} satisfies HSTStonecropReturn
|
|
556
|
-
} else if (!options.doctype && registry?.router) {
|
|
557
|
-
// Router-based - return HST (will be populated after mount)
|
|
558
|
-
return {
|
|
559
|
-
stonecrop,
|
|
560
|
-
operationLog,
|
|
561
|
-
provideHSTPath,
|
|
562
|
-
handleHSTChange,
|
|
563
|
-
hstStore,
|
|
564
|
-
formData,
|
|
565
|
-
resolvedSchema,
|
|
566
|
-
initializeNestedData,
|
|
567
|
-
fetchNestedData,
|
|
568
|
-
collectRecordPayload,
|
|
569
|
-
createNestedContext,
|
|
570
|
-
isLoading,
|
|
571
|
-
error,
|
|
572
|
-
resolvedDoctype,
|
|
573
|
-
isWorkflowReady,
|
|
574
|
-
blockedLinks,
|
|
575
|
-
} satisfies HSTStonecropReturn
|
|
576
502
|
}
|
|
577
503
|
|
|
578
|
-
// No doctype and
|
|
504
|
+
// No doctype named: the instance and the operation log are everything this can offer.
|
|
579
505
|
return {
|
|
580
506
|
stonecrop,
|
|
581
507
|
operationLog,
|
package/src/doctype.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { DoctypeField, LinkDeclaration, TriggerDefinition, WorkflowMeta } from '@stonecrop/schema'
|
|
2
|
-
import { getRecordIdentity, isActionAllowedInState, normalizeFieldKind } from '@stonecrop/schema'
|
|
3
|
-
import { List
|
|
2
|
+
import { getRecordIdentity, getRecordIdField, isActionAllowedInState, normalizeFieldKind } from '@stonecrop/schema'
|
|
3
|
+
import { List } from 'immutable'
|
|
4
4
|
import { Component } from 'vue'
|
|
5
5
|
|
|
6
6
|
import type { DoctypeConfig, ImmutableDoctype } from './types/doctype'
|
|
@@ -40,13 +40,6 @@ export default class Doctype {
|
|
|
40
40
|
*/
|
|
41
41
|
readonly workflow: ImmutableDoctype['workflow']
|
|
42
42
|
|
|
43
|
-
/**
|
|
44
|
-
* The doctype actions and field triggers
|
|
45
|
-
* @public
|
|
46
|
-
* @readonly
|
|
47
|
-
*/
|
|
48
|
-
readonly actions: ImmutableDoctype['actions']
|
|
49
|
-
|
|
50
43
|
/**
|
|
51
44
|
* The doctype component
|
|
52
45
|
* @public
|
|
@@ -66,7 +59,6 @@ export default class Doctype {
|
|
|
66
59
|
* @param doctype - The doctype name
|
|
67
60
|
* @param schema - The doctype schema definition
|
|
68
61
|
* @param workflow - The doctype workflow configuration (XState machine)
|
|
69
|
-
* @param actions - The doctype actions and field triggers
|
|
70
62
|
* @param component - Optional Vue component for rendering the doctype
|
|
71
63
|
* @param links - Optional relationship links to other doctypes
|
|
72
64
|
*/
|
|
@@ -74,14 +66,12 @@ export default class Doctype {
|
|
|
74
66
|
doctype: string,
|
|
75
67
|
schema: ImmutableDoctype['schema'],
|
|
76
68
|
workflow: ImmutableDoctype['workflow'],
|
|
77
|
-
actions: ImmutableDoctype['actions'],
|
|
78
69
|
component?: Component,
|
|
79
70
|
links?: Record<string, LinkDeclaration>
|
|
80
71
|
) {
|
|
81
72
|
this.doctype = doctype
|
|
82
73
|
this.schema = schema
|
|
83
74
|
this.workflow = workflow
|
|
84
|
-
this.actions = actions
|
|
85
75
|
this.component = component
|
|
86
76
|
this.links = links
|
|
87
77
|
}
|
|
@@ -132,9 +122,8 @@ export default class Doctype {
|
|
|
132
122
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- normalizeFieldKind only injects the `kind` discriminant; the field shape is otherwise preserved
|
|
133
123
|
const fields = config.fields?.map(normalizeFieldKind) as DoctypeField[] | undefined
|
|
134
124
|
const schema = fields ? List(fields) : List<DoctypeField>()
|
|
135
|
-
const actions = config.actions ? Map(config.actions) : Map<string, string[]>()
|
|
136
125
|
|
|
137
|
-
return new Doctype(config.name, schema, config.workflow,
|
|
126
|
+
return new Doctype(config.name, schema, config.workflow, undefined, config.links)
|
|
138
127
|
}
|
|
139
128
|
|
|
140
129
|
/**
|
|
@@ -174,16 +163,25 @@ export default class Doctype {
|
|
|
174
163
|
}
|
|
175
164
|
|
|
176
165
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
166
|
+
* The field a record of this doctype is identified by: the declared `primaryKey`, or `id`
|
|
167
|
+
* when nothing is declared.
|
|
168
|
+
*
|
|
169
|
+
* The client-side twin of the adapters' `recordLookupField`. Both are the same call to
|
|
170
|
+
* `@stonecrop/schema`'s `getRecordIdField`, so the field a caller reads an identity out of is
|
|
171
|
+
* the same field the adapter builds its lookup predicate on.
|
|
172
|
+
*
|
|
173
|
+
* Use this to ask whether a record *states* its own identity. `getRecordId` deliberately
|
|
174
|
+
* falls back to `id` when the declared key is missing, which is right for resolving a link
|
|
175
|
+
* from a record already in hand and wrong for deciding whether a server response settled on
|
|
176
|
+
* a new identity: a response that omits a natural key would resolve through that fallback to
|
|
177
|
+
* a surrogate the adapter cannot look up.
|
|
179
178
|
*
|
|
180
|
-
* @returns
|
|
179
|
+
* @returns The identifying fieldname
|
|
181
180
|
*
|
|
182
181
|
* @public
|
|
183
182
|
*/
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return this.actions.toObject()
|
|
183
|
+
get recordIdField(): string {
|
|
184
|
+
return getRecordIdField(this.getSchemaArray())
|
|
187
185
|
}
|
|
188
186
|
|
|
189
187
|
/**
|
package/src/draft.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The route identity of a record the user is composing that does not exist on the server yet.
|
|
3
|
+
*
|
|
4
|
+
* Rejected: minting `new-<timestamp>` so a draft had a store key too. Nothing ever created that
|
|
5
|
+
* HST node, so every write to `doctype.<draftId>.<field>` threw on the missing ancestor, and the
|
|
6
|
+
* form's data survived only because Vue cached the empty object the getter returned and the form
|
|
7
|
+
* mutated it in place — any invalidation discarded the edits silently. Seeding the node instead
|
|
8
|
+
* makes the draft a phantom row, because `getRecords` lists every key under the doctype.
|
|
9
|
+
*
|
|
10
|
+
* So a draft is held off-HST by whoever renders the form, and reaches the server as an action
|
|
11
|
+
* carrying no record id, which the write path already reads as "create".
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The record-id segment a draft route carries: `/{doctype}/new`.
|
|
16
|
+
*
|
|
17
|
+
* Route only — never a store key, and never sent: an action dispatched for a draft omits the id
|
|
18
|
+
* rather than sending this.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export const DRAFT_RECORD_ID = 'new'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether a record id refers to a record that has not been saved yet.
|
|
26
|
+
*
|
|
27
|
+
* Guard anything that assumes the record exists — fetching it, resolving its links, judging
|
|
28
|
+
* workflow readiness — with this rather than the literal. The shell and this package once spelled
|
|
29
|
+
* the rule differently, which left every guard here dead.
|
|
30
|
+
*
|
|
31
|
+
* @param recordId - The record id to test
|
|
32
|
+
* @returns `true` when the id is the draft segment, `false` for a real identity or no id at all
|
|
33
|
+
*
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export function isDraftRecordId(recordId: string | null | undefined): boolean {
|
|
37
|
+
return recordId === DRAFT_RECORD_ID
|
|
38
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export type * from '@stonecrop/aform/types'
|
|
2
2
|
export type * from '@stonecrop/atable/types'
|
|
3
3
|
|
|
4
|
+
import { useClientAction } from './client-action'
|
|
4
5
|
import { executeClientHandler } from './client-handler'
|
|
5
6
|
import { useLazyLink } from './composables/lazy-link'
|
|
6
7
|
import { useStonecrop } from './composables/stonecrop'
|
|
7
8
|
import { useOperationLog, useUndoRedoShortcuts, withBatch } from './composables/operation-log'
|
|
8
9
|
import Doctype from './doctype'
|
|
10
|
+
import { DRAFT_RECORD_ID, isDraftRecordId } from './draft'
|
|
9
11
|
import {
|
|
10
12
|
FieldTriggerEngine,
|
|
11
13
|
getGlobalTriggerEngine,
|
|
@@ -17,7 +19,7 @@ import {
|
|
|
17
19
|
} from './field-triggers'
|
|
18
20
|
import plugin from './plugins'
|
|
19
21
|
import Registry from './registry'
|
|
20
|
-
import { Stonecrop
|
|
22
|
+
import { Stonecrop } from './stonecrop'
|
|
21
23
|
import { HST, createHST, type HSTNode } from './stores/hst'
|
|
22
24
|
import { useOperationLogStore } from './stores/operation-log'
|
|
23
25
|
import { useValidationStore } from './stores/validation'
|
|
@@ -40,10 +42,15 @@ export {
|
|
|
40
42
|
Doctype,
|
|
41
43
|
Registry,
|
|
42
44
|
Stonecrop,
|
|
45
|
+
// Unsaved-record identity, shared with the desktop shell
|
|
46
|
+
DRAFT_RECORD_ID,
|
|
47
|
+
isDraftRecordId,
|
|
43
48
|
useLazyLink,
|
|
44
49
|
useStonecrop,
|
|
45
50
|
// clientHandler executor
|
|
46
51
|
executeClientHandler,
|
|
52
|
+
// The one blessed write path, shared by every Vue host
|
|
53
|
+
useClientAction,
|
|
47
54
|
// HST exports for advanced usage
|
|
48
55
|
HST,
|
|
49
56
|
createHST,
|
|
@@ -68,7 +75,6 @@ export {
|
|
|
68
75
|
// Field-validation store
|
|
69
76
|
useValidationStore,
|
|
70
77
|
// Utility functions
|
|
71
|
-
getStonecrop,
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
// Default export is the Vue plugin
|
package/src/registry.ts
CHANGED
|
@@ -4,7 +4,6 @@ import { componentCategory, componentLinkExpansion, resolveLinkRenderMode } from
|
|
|
4
4
|
import { Router } from 'vue-router'
|
|
5
5
|
|
|
6
6
|
import Doctype from './doctype'
|
|
7
|
-
import { getGlobalTriggerEngine } from './field-triggers'
|
|
8
7
|
import { RouteContext } from './types/registry'
|
|
9
8
|
|
|
10
9
|
/**
|
|
@@ -89,13 +88,12 @@ export default class Registry {
|
|
|
89
88
|
this._ancestorIndexDirty = true
|
|
90
89
|
}
|
|
91
90
|
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
91
|
+
// Field triggers are registered imperatively, by the app, via the FieldTriggerEngine's own
|
|
92
|
+
// `registerDoctypeActions`. A doctype used to be able to declare them too, but that path
|
|
93
|
+
// could not survive the wire — neither the Zod gate nor the SDL carries such a map — so it
|
|
94
|
+
// only ever worked for a host bypassing both, and it left two public `runAction` methods
|
|
95
|
+
// meaning opposite things. Registering here would also overwrite an app's imperative
|
|
96
|
+
// registration, since `registerDoctypeActions` sets the maps even when handed nothing.
|
|
99
97
|
|
|
100
98
|
if (doctype.component && this.router && !this.router.hasRoute(doctype.doctype)) {
|
|
101
99
|
this.router.addRoute({
|