Two-stage codegen: React + Vue + Svelte from one schema
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>
This commit is contained in:
118
examples/django-react-site/harness/src/api/react.tsx
Normal file
118
examples/django-react-site/harness/src/api/react.tsx
Normal file
@@ -0,0 +1,118 @@
|
||||
'use client'
|
||||
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { createContext, useContext, useState, useEffect, useCallback, type ReactNode } from 'react'
|
||||
import { registerContext, mizanCall, mizanFetch } from '@mizan/runtime'
|
||||
|
||||
import { fetchGlobalContext, type GlobalContextData, type GlobalContextParams, fetchLocalContext, type LocalContextData, type LocalContextParams, callEcho, callAdd, callWhoami, callHttpOnlyEcho, callStaffOnly, callSuperuserOnly, callVerifiedOnly, callMultiply, callNotImplementedFn, callBuggyFn, callPermissionCheckFn, callWsWhoami, callJwtObtain, callJwtRefresh } from '../index'
|
||||
|
||||
// Global context — fetched once at app init
|
||||
const GlobalCtx = createContext<GlobalContextData | null>(null)
|
||||
|
||||
export function GlobalContextProvider({ children }: { children: ReactNode }) {
|
||||
const [data, setData] = useState<GlobalContextData | null>(() => {
|
||||
if (typeof window === 'undefined') return null
|
||||
const ssr = (window as any).__MIZAN_SSR_DATA__
|
||||
return ssr ?? null
|
||||
})
|
||||
|
||||
const refetch = useCallback(async () => {
|
||||
const result = await fetchGlobalContext({} as any)
|
||||
setData(result)
|
||||
}, [])
|
||||
|
||||
useEffect(() => { if (!data) refetch() }, [data, refetch])
|
||||
useEffect(() => registerContext('global', {}, refetch), [refetch])
|
||||
|
||||
return <GlobalCtx.Provider value={data}>{children}</GlobalCtx.Provider>
|
||||
}
|
||||
|
||||
export function useCurrentUser(): currentUserOutput {
|
||||
const ctx = useContext(GlobalCtx)
|
||||
if (!ctx) throw new Error('useCurrentUser requires GlobalContextProvider')
|
||||
return ctx.current_user
|
||||
}
|
||||
|
||||
// Local context
|
||||
const LocalCtx = createContext<LocalContextData | null>(null)
|
||||
|
||||
export function LocalContext({ children, ...params }: LocalContextParams & { children: ReactNode }) {
|
||||
const [data, setData] = useState<LocalContextData | null>(() => {
|
||||
if (typeof window === 'undefined') return null
|
||||
const ssr = (window as any).__MIZAN_SSR_DATA__
|
||||
if (ssr?.greet !== undefined) return ssr
|
||||
return null
|
||||
})
|
||||
|
||||
const refetch = useCallback(async () => {
|
||||
const result = await fetchLocalContext(params)
|
||||
setData(result)
|
||||
}, [params.name])
|
||||
|
||||
useEffect(() => { refetch() }, [refetch])
|
||||
useEffect(() => registerContext('local', params, refetch), [params.name, refetch])
|
||||
|
||||
return <LocalCtx.Provider value={data}>{children}</LocalCtx.Provider>
|
||||
}
|
||||
|
||||
export function useGreet(): greetOutput | null {
|
||||
const ctx = useContext(LocalCtx)
|
||||
return ctx?.greet ?? null
|
||||
}
|
||||
|
||||
export function useEcho() {
|
||||
return useCallback((args: Parameters<typeof callEcho>[0]) => callEcho(args), [])
|
||||
}
|
||||
|
||||
export function useAdd() {
|
||||
return useCallback((args: Parameters<typeof callAdd>[0]) => callAdd(args), [])
|
||||
}
|
||||
|
||||
export function useWhoami() {
|
||||
return useCallback(() => callWhoami(), [])
|
||||
}
|
||||
|
||||
export function useHttpOnlyEcho() {
|
||||
return useCallback((args: Parameters<typeof callHttpOnlyEcho>[0]) => callHttpOnlyEcho(args), [])
|
||||
}
|
||||
|
||||
export function useStaffOnly() {
|
||||
return useCallback(() => callStaffOnly(), [])
|
||||
}
|
||||
|
||||
export function useSuperuserOnly() {
|
||||
return useCallback(() => callSuperuserOnly(), [])
|
||||
}
|
||||
|
||||
export function useVerifiedOnly() {
|
||||
return useCallback(() => callVerifiedOnly(), [])
|
||||
}
|
||||
|
||||
export function useMultiply() {
|
||||
return useCallback((args: Parameters<typeof callMultiply>[0]) => callMultiply(args), [])
|
||||
}
|
||||
|
||||
export function useNotImplementedFn() {
|
||||
return useCallback(() => callNotImplementedFn(), [])
|
||||
}
|
||||
|
||||
export function useBuggyFn() {
|
||||
return useCallback(() => callBuggyFn(), [])
|
||||
}
|
||||
|
||||
export function usePermissionCheckFn() {
|
||||
return useCallback((args: Parameters<typeof callPermissionCheckFn>[0]) => callPermissionCheckFn(args), [])
|
||||
}
|
||||
|
||||
export function useWsWhoami() {
|
||||
return useCallback(() => callWsWhoami(), [])
|
||||
}
|
||||
|
||||
export function useJwtObtain() {
|
||||
return useCallback(() => callJwtObtain(), [])
|
||||
}
|
||||
|
||||
export function useJwtRefresh() {
|
||||
return useCallback((args: Parameters<typeof callJwtRefresh>[0]) => callJwtRefresh(args), [])
|
||||
}
|
||||
Reference in New Issue
Block a user