The codegen consumes a schema from any backend and emits typed client code for any frontend — it doesn't belong inside a backend adapter. That placement was historical sediment from when there was only a Django backend; it predates the AFI generalization. New top-level slot: `protocol/` for protocol-level tooling. Tree is now: backends/ server protocol adapters frontends/ client kernel + per-framework adapters cores/ shared language-level primitives protocol/ protocol-level tooling workers/ runtime workers / bridges Codegen moves to `protocol/mizan-generate/`. Same file layout under `generator/` (cli.mjs, lib/), preserved via git mv. Package metadata cleaned up: - name: "generate" (placeholder) → "mizan-generate" - description filled in - type: module (cli.mjs is .mjs ESM, was previously declared "commonjs") - bin entry added so `npx mizan-generate --config <config.mjs>` works once the package is published, instead of `node path/to/cli.mjs`. Path-reference fixups: - backends/mizan-django/README.md: `node path/to/...` → `npx mizan-generate` - backends/mizan-fastapi/README.md: same - ISSUES.md: file paths in three issue entries - CLAUDE.md: codegen description + Package Layout section refreshed (added protocol/, mizan-fastapi entry, mizan-python entry) - docs/AFI_ARCHITECTURE.md: Package Layout refreshed identically Verified codegen runs from new location: regenerated the FastAPI example harness's api/ output, identical to pre-move. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
79 lines
3.0 KiB
JavaScript
79 lines
3.0 KiB
JavaScript
/**
|
|
* Svelte Stage 2 — Generates stores from Stage 1 output.
|
|
*
|
|
* Subscribes to the kernel for state. Returns readable stores.
|
|
*/
|
|
|
|
function pascalCase(str) {
|
|
return str.split(/[.\-_]/).map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('')
|
|
}
|
|
|
|
export function generateSvelteAdapter(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 { readable, type Readable } from 'svelte/store'",
|
|
"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 create${p}Context(params: ${p}ContextParams) {`)
|
|
} else {
|
|
lines.push(`export function create${p}Context() {`)
|
|
}
|
|
|
|
// Use readable store backed by kernel subscription
|
|
lines.push(` const store = readable<ContextState<${p}ContextData>>(`)
|
|
lines.push(` { data: null, status: 'idle', error: null },`)
|
|
lines.push(` (set) => {`)
|
|
lines.push(` const handle = registerContext('${ctxName}', ${paramsArg}, () => fetch${p}Context(${paramsArg}))`)
|
|
lines.push(` const unsub = handle.subscribe(() => set(handle.getState()))`)
|
|
lines.push(` handle.refetch()`)
|
|
lines.push(` return () => { unsub(); handle.unregister() }`)
|
|
lines.push(` },`)
|
|
lines.push(` )`)
|
|
lines.push('')
|
|
lines.push(` return store`)
|
|
lines.push('}')
|
|
lines.push('')
|
|
}
|
|
|
|
// Re-export mutations as-is from Stage 1
|
|
for (const fn of [...mutations, ...plainFns]) {
|
|
const p = pascalCase(fn.camelName)
|
|
lines.push(`export { call${p} } from '../index'`)
|
|
}
|
|
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')
|
|
}
|