Files
mizan/backends/mizan-django/generate/generator/lib/adapters/vue.mjs
Ryth Azhur 9d2781b52c Catch missed content edits from tree restructure
The fe39fcb commit captured the file moves (git mv stages those automatically)
but didn't catch the content edits I made afterward — npm package rename
(@mizan/runtime → @mizan/base), path updates in Makefile/Dockerfile/examples,
and doc updates were all left unstaged at commit time.

This commit lands those:
- npm rename: 3 frontend package.jsons (base/vue/svelte) + mizan-base/src/index.ts + 4 codegen templates
- path updates: Makefile, Dockerfile.test, two Gitea workflows, four example/harness configs
- doc updates: CLAUDE.md, ROADMAP.md, ISSUES.md, docs/AFI_ARCHITECTURE.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 01:26:04 -04:00

105 lines
4.5 KiB
JavaScript

/**
* Vue Stage 2 — Generates composables from Stage 1 output.
*
* Subscribes to the kernel for state. Vue reactivity wraps kernel notifications.
*/
function pascalCase(str) {
return str.split(/[.\-_]/).map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('')
}
export function generateVueAdapter(schema) {
const functions = schema['x-mizan-functions'] || []
const contextGroups = schema['x-mizan-contexts'] || {}
const mutations = functions.filter(fn => !fn.isContext && !fn.isForm && fn.affects)
const plainFns = functions.filter(fn => !fn.isContext && !fn.isForm && !fn.affects)
const lines = [
'// AUTO-GENERATED by mizan — do not edit',
'',
"import { ref, computed, onMounted, onUnmounted, onServerPrefetch, type ComputedRef } from 'vue'",
"import { registerContext, type ContextState } from '@mizan/base'",
'',
]
const stage1Imports = []
for (const [ctxName] of Object.entries(contextGroups)) {
const p = pascalCase(ctxName)
stage1Imports.push(`fetch${p}Context`, `type ${p}ContextData`, `type ${p}ContextParams`)
}
for (const fn of [...mutations, ...plainFns]) {
stage1Imports.push(`call${pascalCase(fn.camelName)}`)
}
if (stage1Imports.length > 0) {
lines.push(`import { ${stage1Imports.join(', ')} } from '../index'`)
lines.push('')
}
for (const [ctxName, ctxMeta] of Object.entries(contextGroups)) {
const p = pascalCase(ctxName)
const ctxFunctions = functions.filter(fn => fn.isContext === ctxName)
const paramEntries = Object.entries(ctxMeta.params || {})
const paramsArg = paramEntries.length > 0 ? 'params' : '{} as any'
if (paramEntries.length > 0) {
lines.push(`export function use${p}Context(params: ${p}ContextParams) {`)
} else {
lines.push(`export function use${p}Context() {`)
}
lines.push(` const state = ref<ContextState<${p}ContextData>>({ data: null, status: 'idle', error: null })`)
lines.push(` let handle: ReturnType<typeof registerContext> | null = null`)
lines.push('')
lines.push(` onMounted(() => {`)
lines.push(` handle = registerContext('${ctxName}', ${paramsArg}, () => fetch${p}Context(${paramsArg}))`)
lines.push(` handle.subscribe(() => { state.value = handle!.getState() })`)
lines.push(` handle.refetch()`)
lines.push(` })`)
lines.push('')
lines.push(` onServerPrefetch(async () => {`)
lines.push(` handle = registerContext('${ctxName}', ${paramsArg}, () => fetch${p}Context(${paramsArg}))`)
lines.push(` await handle.refetch()`)
lines.push(` state.value = handle.getState()`)
lines.push(` })`)
lines.push('')
lines.push(` onUnmounted(() => { handle?.unregister() })`)
lines.push('')
lines.push(` return {`)
lines.push(` state,`)
for (const fn of ctxFunctions) {
lines.push(` ${fn.camelName}: computed(() => state.value.data?.${fn.name} ?? null) as ComputedRef<${fn.outputType} | null>,`)
}
lines.push(` loading: computed(() => state.value.status === 'loading'),`)
lines.push(` error: computed(() => state.value.error),`)
lines.push(` }`)
lines.push('}')
lines.push('')
}
for (const fn of [...mutations, ...plainFns]) {
const p = pascalCase(fn.camelName)
lines.push(`export function use${p}() {`)
lines.push(` const isPending = ref(false)`)
lines.push(` const error = ref<Error | null>(null)`)
if (fn.hasInput) {
lines.push(` async function mutate(args: Parameters<typeof call${p}>[0]) {`)
} else {
lines.push(` async function mutate() {`)
}
lines.push(` isPending.value = true; error.value = null`)
lines.push(` try { return await call${p}(${fn.hasInput ? 'args' : ''}) }`)
lines.push(` catch (e) { error.value = e as Error; throw e }`)
lines.push(` finally { isPending.value = false }`)
lines.push(` }`)
lines.push(` return { mutate, isPending, error }`)
lines.push('}')
lines.push('')
}
lines.push("export type { ContextState } from '@mizan/base'")
lines.push("export { configure, initSession, MizanError } from '@mizan/base'")
lines.push('')
return lines.join('\n')
}