Stage 1 (framework-agnostic):
types.ts — OpenAPI interfaces
contexts/<name>.ts — fetchXxxContext(params) using mizanFetch
mutations/<name>.ts — callXxx(args) using mizanCall
functions/<name>.ts — callXxx(args) using mizanCall
index.ts — re-exports
Stage 2 (per framework):
react.tsx — hooks + context providers + SSR hydration
vue.ts — composables with provide/inject + ref/computed
svelte.ts — writable/derived store factories
New packages:
mizan-runtime — the kernel (~200 lines, zero framework deps)
configure(), initSession(), registerContext(), invalidate(),
mizanFetch(), mizanCall(), MizanError
mizan-vue — Vue adapter (package.json, codegen template)
mizan-svelte — Svelte adapter (package.json, codegen template)
CLI: mizan-generate --target react,vue,svelte
Config: target: 'react' (default) in django.config.mjs
Verified: codegen produces 33 functions across 2 contexts,
14 plain functions, 0 mutations, generating all three Stage 2
outputs from one schema fetch.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
98 lines
3.8 KiB
JavaScript
98 lines
3.8 KiB
JavaScript
/**
|
|
* Svelte Stage 2 — Generates stores from Stage 1 output.
|
|
*/
|
|
|
|
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 { writable, derived, type Readable } from 'svelte/store'",
|
|
"import { registerContext } from '@mizan/runtime'",
|
|
'',
|
|
]
|
|
|
|
// Stage 1 imports
|
|
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('')
|
|
}
|
|
|
|
// ── Context stores ──────────────────────────────────────────────────
|
|
|
|
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 || {})
|
|
|
|
lines.push(`// ${p} context`)
|
|
|
|
if (paramEntries.length > 0) {
|
|
lines.push(`export function create${p}Context(params: ${p}ContextParams) {`)
|
|
} else {
|
|
lines.push(`export function create${p}Context() {`)
|
|
}
|
|
|
|
lines.push(` const data = writable<${p}ContextData | null>(null)`)
|
|
lines.push(` const loading = writable(true)`)
|
|
lines.push('')
|
|
lines.push(` const refetch = async () => {`)
|
|
lines.push(` loading.set(true)`)
|
|
if (paramEntries.length > 0) {
|
|
lines.push(` const result = await fetch${p}Context(params)`)
|
|
} else {
|
|
lines.push(` const result = await fetch${p}Context({} as any)`)
|
|
}
|
|
lines.push(` data.set(result)`)
|
|
lines.push(` loading.set(false)`)
|
|
lines.push(` }`)
|
|
lines.push('')
|
|
lines.push(` refetch()`)
|
|
if (paramEntries.length > 0) {
|
|
lines.push(` const unregister = registerContext('${ctxName}', params, refetch)`)
|
|
} else {
|
|
lines.push(` const unregister = registerContext('${ctxName}', {}, refetch)`)
|
|
}
|
|
lines.push('')
|
|
|
|
// Derived stores for each function
|
|
lines.push(` return {`)
|
|
lines.push(` data,`)
|
|
lines.push(` loading,`)
|
|
for (const fn of ctxFunctions) {
|
|
const camel = fn.camelName
|
|
lines.push(` ${camel}: derived(data, $d => $d?.${fn.name} ?? null) as Readable<${fn.outputType} | null>,`)
|
|
}
|
|
lines.push(` destroy: unregister,`)
|
|
lines.push(` }`)
|
|
lines.push('}')
|
|
lines.push('')
|
|
}
|
|
|
|
// ── Mutation + function exports ─────────────────────────────────────
|
|
|
|
for (const fn of [...mutations, ...plainFns]) {
|
|
const p = pascalCase(fn.camelName)
|
|
lines.push(`export { call${p} } from '../${fn.affects ? 'mutations' : 'functions'}/${fn.camelName}'`)
|
|
}
|
|
lines.push('')
|
|
|
|
return lines.join('\n')
|
|
}
|