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:
@@ -0,0 +1,15 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanFetch } from '@mizan/runtime'
|
||||
|
||||
import type { currentUserOutput } from '../types'
|
||||
|
||||
export interface GlobalContextData {
|
||||
current_user: currentUserOutput
|
||||
}
|
||||
|
||||
export type GlobalContextParams = Record<string, never>
|
||||
|
||||
export function fetchGlobalContext(params: GlobalContextParams): Promise<GlobalContextData> {
|
||||
return mizanFetch('global', params)
|
||||
}
|
||||
17
examples/django-react-site/harness/src/api/contexts/local.ts
Normal file
17
examples/django-react-site/harness/src/api/contexts/local.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanFetch } from '@mizan/runtime'
|
||||
|
||||
import type { greetOutput } from '../types'
|
||||
|
||||
export interface LocalContextData {
|
||||
greet: greetOutput
|
||||
}
|
||||
|
||||
export interface LocalContextParams {
|
||||
name: string
|
||||
}
|
||||
|
||||
export function fetchLocalContext(params: LocalContextParams): Promise<LocalContextData> {
|
||||
return mizanFetch('local', params)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { addInput, addOutput } from '../types'
|
||||
|
||||
export function callAdd(args: addInput): Promise<addOutput> {
|
||||
return mizanCall('add', args)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { buggyFnOutput } from '../types'
|
||||
|
||||
export function callBuggyFn(): Promise<buggyFnOutput> {
|
||||
return mizanCall('buggy_fn', {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { echoInput, echoOutput } from '../types'
|
||||
|
||||
export function callEcho(args: echoInput): Promise<echoOutput> {
|
||||
return mizanCall('echo', args)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { httpOnlyEchoInput, httpOnlyEchoOutput } from '../types'
|
||||
|
||||
export function callHttpOnlyEcho(args: httpOnlyEchoInput): Promise<httpOnlyEchoOutput> {
|
||||
return mizanCall('http_only_echo', args)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { jwtObtainOutput } from '../types'
|
||||
|
||||
export function callJwtObtain(): Promise<jwtObtainOutput> {
|
||||
return mizanCall('jwt_obtain', {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { jwtRefreshInput, jwtRefreshOutput } from '../types'
|
||||
|
||||
export function callJwtRefresh(args: jwtRefreshInput): Promise<jwtRefreshOutput> {
|
||||
return mizanCall('jwt_refresh', args)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { multiplyInput, multiplyOutput } from '../types'
|
||||
|
||||
export function callMultiply(args: multiplyInput): Promise<multiplyOutput> {
|
||||
return mizanCall('multiply', args)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { notImplementedFnOutput } from '../types'
|
||||
|
||||
export function callNotImplementedFn(): Promise<notImplementedFnOutput> {
|
||||
return mizanCall('not_implemented_fn', {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { permissionCheckFnInput, permissionCheckFnOutput } from '../types'
|
||||
|
||||
export function callPermissionCheckFn(args: permissionCheckFnInput): Promise<permissionCheckFnOutput> {
|
||||
return mizanCall('permission_check_fn', args)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { staffOnlyOutput } from '../types'
|
||||
|
||||
export function callStaffOnly(): Promise<staffOnlyOutput> {
|
||||
return mizanCall('staff_only', {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { superuserOnlyOutput } from '../types'
|
||||
|
||||
export function callSuperuserOnly(): Promise<superuserOnlyOutput> {
|
||||
return mizanCall('superuser_only', {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { verifiedOnlyOutput } from '../types'
|
||||
|
||||
export function callVerifiedOnly(): Promise<verifiedOnlyOutput> {
|
||||
return mizanCall('verified_only', {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { whoamiOutput } from '../types'
|
||||
|
||||
export function callWhoami(): Promise<whoamiOutput> {
|
||||
return mizanCall('whoami', {})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/runtime'
|
||||
|
||||
import type { wsWhoamiOutput } from '../types'
|
||||
|
||||
export function callWsWhoami(): Promise<wsWhoamiOutput> {
|
||||
return mizanCall('ws_whoami', {})
|
||||
}
|
||||
@@ -1,268 +0,0 @@
|
||||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {
|
||||
"title": "mizan Channels",
|
||||
"version": "1.0.0",
|
||||
"description": "Auto-generated schema for mizan channels"
|
||||
},
|
||||
"paths": {
|
||||
"/channels/chat/params": {
|
||||
"post": {
|
||||
"operationId": "chatParams",
|
||||
"summary": "Chat channel params",
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/BaseModel"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ChatParams"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"/channels/chat/react": {
|
||||
"post": {
|
||||
"operationId": "chatReactMessage",
|
||||
"summary": "Chat React→Django message",
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/BaseModel"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ChatReactMessage"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"/channels/chat/django": {
|
||||
"post": {
|
||||
"operationId": "chatDjangoMessage",
|
||||
"summary": "Chat Django→React message",
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ChatDjangoMessage"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/channels/notifications/django": {
|
||||
"post": {
|
||||
"operationId": "notificationsDjangoMessage",
|
||||
"summary": "Notifications Django→React message",
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/NotificationsDjangoMessage"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/channels/presence/django": {
|
||||
"post": {
|
||||
"operationId": "presenceDjangoMessage",
|
||||
"summary": "Presence Django→React message",
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/PresenceDjangoMessage"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/channels/private/django": {
|
||||
"post": {
|
||||
"operationId": "privateDjangoMessage",
|
||||
"summary": "Private Django→React message",
|
||||
"parameters": [],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/PrivateDjangoMessage"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"BaseModel": {
|
||||
"properties": {},
|
||||
"title": "BaseModel",
|
||||
"type": "object"
|
||||
},
|
||||
"ChatParams": {
|
||||
"properties": {
|
||||
"room": {
|
||||
"title": "Room",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"room"
|
||||
],
|
||||
"title": "ChatParams",
|
||||
"type": "object"
|
||||
},
|
||||
"ChatReactMessage": {
|
||||
"properties": {
|
||||
"text": {
|
||||
"title": "Text",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"text"
|
||||
],
|
||||
"title": "ChatReactMessage",
|
||||
"type": "object"
|
||||
},
|
||||
"ChatDjangoMessage": {
|
||||
"properties": {
|
||||
"text": {
|
||||
"title": "Text",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"text"
|
||||
],
|
||||
"title": "ChatDjangoMessage",
|
||||
"type": "object"
|
||||
},
|
||||
"NotificationsDjangoMessage": {
|
||||
"properties": {
|
||||
"text": {
|
||||
"title": "Text",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"text"
|
||||
],
|
||||
"title": "NotificationsDjangoMessage",
|
||||
"type": "object"
|
||||
},
|
||||
"PresenceDjangoMessage": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"title": "Value",
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"value"
|
||||
],
|
||||
"title": "PresenceDjangoMessage",
|
||||
"type": "object"
|
||||
},
|
||||
"PrivateDjangoMessage": {
|
||||
"properties": {
|
||||
"text": {
|
||||
"title": "Text",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"text"
|
||||
],
|
||||
"title": "PrivateDjangoMessage",
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"servers": [],
|
||||
"x-mizan-channels": [
|
||||
{
|
||||
"name": "chat",
|
||||
"pascalName": "Chat",
|
||||
"hasParams": true,
|
||||
"hasReactMessage": true,
|
||||
"hasDjangoMessage": true,
|
||||
"paramsType": "ChatParams",
|
||||
"reactMessageType": "ChatReactMessage",
|
||||
"djangoMessageType": "ChatDjangoMessage"
|
||||
},
|
||||
{
|
||||
"name": "notifications",
|
||||
"pascalName": "Notifications",
|
||||
"hasParams": false,
|
||||
"hasReactMessage": false,
|
||||
"hasDjangoMessage": true,
|
||||
"djangoMessageType": "NotificationsDjangoMessage"
|
||||
},
|
||||
{
|
||||
"name": "presence",
|
||||
"pascalName": "Presence",
|
||||
"hasParams": false,
|
||||
"hasReactMessage": false,
|
||||
"hasDjangoMessage": true,
|
||||
"djangoMessageType": "PresenceDjangoMessage"
|
||||
},
|
||||
{
|
||||
"name": "private",
|
||||
"pascalName": "Private",
|
||||
"hasParams": false,
|
||||
"hasReactMessage": false,
|
||||
"hasDjangoMessage": true,
|
||||
"djangoMessageType": "PrivateDjangoMessage"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,226 +0,0 @@
|
||||
'use client'
|
||||
|
||||
// AUTO-GENERATED by mizan - do not edit manually
|
||||
// Regenerate with: npm run schemas
|
||||
|
||||
// Typed form hooks with Zod validation.
|
||||
// Zod schemas are generated from Django form field definitions.
|
||||
// Client-side validation matches Django constraints (required, max_length, email, etc.)
|
||||
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
useDjangoFormCore,
|
||||
useDjangoFormsetCore,
|
||||
type DjangoFormState,
|
||||
type DjangoFormsetState,
|
||||
type FormOptions,
|
||||
} from 'mizan'
|
||||
|
||||
// ============================================================================
|
||||
// Zod Schemas
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Zod schema for login form
|
||||
* Generated from Django form field definitions
|
||||
*/
|
||||
export const LoginSchema = z.object({
|
||||
})
|
||||
|
||||
/**
|
||||
* Zod schema for signup form
|
||||
* Generated from Django form field definitions
|
||||
*/
|
||||
export const SignupSchema = z.object({
|
||||
})
|
||||
|
||||
/**
|
||||
* Zod schema for add_email form
|
||||
* Generated from Django form field definitions
|
||||
*/
|
||||
export const AddEmailSchema = z.object({
|
||||
})
|
||||
|
||||
/**
|
||||
* Zod schema for contact form
|
||||
* Generated from Django form field definitions
|
||||
*/
|
||||
export const ContactSchema = z.object({
|
||||
name: z.string().max(100),
|
||||
email: z.string().email('Invalid email address').max(320),
|
||||
message: z.string(),
|
||||
})
|
||||
|
||||
/**
|
||||
* Zod schema for item form
|
||||
* Generated from Django form field definitions
|
||||
*/
|
||||
export const ItemSchema = z.object({
|
||||
label: z.string().max(50),
|
||||
quantity: z.number().int().min(1),
|
||||
})
|
||||
|
||||
// ============================================================================
|
||||
// Form Data Types (inferred from Zod schemas)
|
||||
// ============================================================================
|
||||
|
||||
/** Form data type for login, inferred from Zod schema */
|
||||
export type LoginFormData = z.infer<typeof LoginSchema>
|
||||
|
||||
/** Form data type for signup, inferred from Zod schema */
|
||||
export type SignupFormData = z.infer<typeof SignupSchema>
|
||||
|
||||
/** Form data type for add_email, inferred from Zod schema */
|
||||
export type AddEmailFormData = z.infer<typeof AddEmailSchema>
|
||||
|
||||
/** Form data type for contact, inferred from Zod schema */
|
||||
export type ContactFormData = z.infer<typeof ContactSchema>
|
||||
|
||||
/** Form data type for item, inferred from Zod schema */
|
||||
export type ItemFormData = z.infer<typeof ItemSchema>
|
||||
|
||||
// ============================================================================
|
||||
// Form Hooks
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Typed form hook for login
|
||||
*
|
||||
* Features:
|
||||
* - Full TypeScript inference for form fields
|
||||
* - Client-side Zod validation (instant feedback)
|
||||
* - Server-side Django validation (authoritative)
|
||||
*/
|
||||
export function useLoginForm(
|
||||
options?: FormOptions
|
||||
): DjangoFormState<LoginFormData> {
|
||||
return useDjangoFormCore<LoginFormData>({
|
||||
name: 'login',
|
||||
zodSchema: LoginSchema,
|
||||
options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Typed form hook for signup
|
||||
*
|
||||
* Features:
|
||||
* - Full TypeScript inference for form fields
|
||||
* - Client-side Zod validation (instant feedback)
|
||||
* - Server-side Django validation (authoritative)
|
||||
*/
|
||||
export function useSignupForm(
|
||||
options?: FormOptions
|
||||
): DjangoFormState<SignupFormData> {
|
||||
return useDjangoFormCore<SignupFormData>({
|
||||
name: 'signup',
|
||||
zodSchema: SignupSchema,
|
||||
options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Typed form hook for add_email
|
||||
*
|
||||
* Features:
|
||||
* - Full TypeScript inference for form fields
|
||||
* - Client-side Zod validation (instant feedback)
|
||||
* - Server-side Django validation (authoritative)
|
||||
*/
|
||||
export function useAddEmailForm(
|
||||
options?: FormOptions
|
||||
): DjangoFormState<AddEmailFormData> {
|
||||
return useDjangoFormCore<AddEmailFormData>({
|
||||
name: 'add_email',
|
||||
zodSchema: AddEmailSchema,
|
||||
options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Typed form hook for contact
|
||||
*
|
||||
* Features:
|
||||
* - Full TypeScript inference for form fields
|
||||
* - Client-side Zod validation (instant feedback)
|
||||
* - Server-side Django validation (authoritative)
|
||||
*/
|
||||
export function useContactForm(
|
||||
options?: FormOptions
|
||||
): DjangoFormState<ContactFormData> {
|
||||
return useDjangoFormCore<ContactFormData>({
|
||||
name: 'contact',
|
||||
zodSchema: ContactSchema,
|
||||
options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Typed form hook for item
|
||||
*
|
||||
* Features:
|
||||
* - Full TypeScript inference for form fields
|
||||
* - Client-side Zod validation (instant feedback)
|
||||
* - Server-side Django validation (authoritative)
|
||||
*/
|
||||
export function useItemForm(
|
||||
options?: FormOptions
|
||||
): DjangoFormState<ItemFormData> {
|
||||
return useDjangoFormCore<ItemFormData>({
|
||||
name: 'item',
|
||||
zodSchema: ItemSchema,
|
||||
options,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Typed formset hook for item
|
||||
*/
|
||||
export function useItemFormset(
|
||||
initialCount?: number,
|
||||
liveValidation?: boolean
|
||||
): DjangoFormsetState<ItemFormData> {
|
||||
return useDjangoFormsetCore<ItemFormData>({
|
||||
name: 'item',
|
||||
zodSchema: ItemSchema,
|
||||
initialCount,
|
||||
liveValidation,
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Form Registry
|
||||
// ============================================================================
|
||||
|
||||
export const MIZAN_FORMS = {
|
||||
login: {
|
||||
name: 'login',
|
||||
schema: LoginSchema,
|
||||
hook: 'useLoginForm',
|
||||
hasFormset: false,
|
||||
},
|
||||
signup: {
|
||||
name: 'signup',
|
||||
schema: SignupSchema,
|
||||
hook: 'useSignupForm',
|
||||
hasFormset: false,
|
||||
},
|
||||
addEmail: {
|
||||
name: 'add_email',
|
||||
schema: AddEmailSchema,
|
||||
hook: 'useAddEmailForm',
|
||||
hasFormset: false,
|
||||
},
|
||||
contact: {
|
||||
name: 'contact',
|
||||
schema: ContactSchema,
|
||||
hook: 'useContactForm',
|
||||
hasFormset: false,
|
||||
},
|
||||
item: {
|
||||
name: 'item',
|
||||
schema: ItemSchema,
|
||||
hook: 'useItemForm',
|
||||
hasFormset: true,
|
||||
},
|
||||
} as const
|
||||
@@ -1,97 +1,21 @@
|
||||
/**
|
||||
* mizan API - Consolidated Exports
|
||||
*
|
||||
* Import everything from here:
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* import {
|
||||
* MizanContext,
|
||||
* useCurrentUser,
|
||||
* useEcho,
|
||||
* useChatChannel,
|
||||
* } from '@/api'
|
||||
* ```
|
||||
*/
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
// AUTO-GENERATED by mizan - do not edit manually
|
||||
// Regenerate with: npm run schemas
|
||||
export * from './types'
|
||||
|
||||
// =============================================================================
|
||||
// mizan Provider & Hooks
|
||||
// =============================================================================
|
||||
export { fetchGlobalContext, type GlobalContextData, type GlobalContextParams } from './contexts/global'
|
||||
export { fetchLocalContext, type LocalContextData, type LocalContextParams } from './contexts/local'
|
||||
|
||||
export {
|
||||
getMizanHydration,
|
||||
getDjangoHydration,
|
||||
type MizanHydrationData,
|
||||
type DjangoHydration,
|
||||
} from './generated.server'
|
||||
|
||||
export {
|
||||
// Provider
|
||||
MizanContext,
|
||||
type MizanContextProps,
|
||||
DjangoContext,
|
||||
type DjangoContextProps,
|
||||
|
||||
// Global context hooks
|
||||
useCurrentUser,
|
||||
|
||||
// Refresh hooks
|
||||
useMizanRefresh,
|
||||
useDjangoRefresh,
|
||||
|
||||
// Named context providers
|
||||
LocalContext,
|
||||
useGreet,
|
||||
|
||||
// Function hooks
|
||||
useEcho,
|
||||
useAdd,
|
||||
useWhoami,
|
||||
useHttpOnlyEcho,
|
||||
useStaffOnly,
|
||||
useSuperuserOnly,
|
||||
useVerifiedOnly,
|
||||
useMultiply,
|
||||
useNotImplementedFn,
|
||||
useBuggyFn,
|
||||
usePermissionCheckFn,
|
||||
useWsWhoami,
|
||||
useJwtObtain,
|
||||
useJwtRefresh,
|
||||
|
||||
// Re-exports from mizan library
|
||||
useMizan,
|
||||
useMizanStatus,
|
||||
usePush,
|
||||
DjangoError,
|
||||
type ConnectionStatus,
|
||||
type PushMessage,
|
||||
type PushListener,
|
||||
} from './generated.provider'
|
||||
|
||||
// =============================================================================
|
||||
// Channel Hooks
|
||||
// =============================================================================
|
||||
|
||||
export {
|
||||
useChatChannel,
|
||||
useNotificationsChannel,
|
||||
usePresenceChannel,
|
||||
usePrivateChannel,
|
||||
} from './generated.channels.hooks'
|
||||
|
||||
// =============================================================================
|
||||
// Channel Types
|
||||
// =============================================================================
|
||||
|
||||
export type {
|
||||
ChatParams,
|
||||
ChatReactMessage,
|
||||
ChatDjangoMessage,
|
||||
NotificationsDjangoMessage,
|
||||
PresenceDjangoMessage,
|
||||
PrivateDjangoMessage,
|
||||
} from './generated.channels'
|
||||
export { callEcho } from './functions/echo'
|
||||
export { callAdd } from './functions/add'
|
||||
export { callWhoami } from './functions/whoami'
|
||||
export { callHttpOnlyEcho } from './functions/httpOnlyEcho'
|
||||
export { callStaffOnly } from './functions/staffOnly'
|
||||
export { callSuperuserOnly } from './functions/superuserOnly'
|
||||
export { callVerifiedOnly } from './functions/verifiedOnly'
|
||||
export { callMultiply } from './functions/multiply'
|
||||
export { callNotImplementedFn } from './functions/notImplementedFn'
|
||||
export { callBuggyFn } from './functions/buggyFn'
|
||||
export { callPermissionCheckFn } from './functions/permissionCheckFn'
|
||||
export { callWsWhoami } from './functions/wsWhoami'
|
||||
export { callJwtObtain } from './functions/jwtObtain'
|
||||
export { callJwtRefresh } from './functions/jwtRefresh'
|
||||
|
||||
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), [])
|
||||
}
|
||||
2678
examples/django-react-site/harness/src/api/schema.json
Normal file
2678
examples/django-react-site/harness/src/api/schema.json
Normal file
File diff suppressed because it is too large
Load Diff
67
examples/django-react-site/harness/src/api/svelte.ts
Normal file
67
examples/django-react-site/harness/src/api/svelte.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { writable, derived, type Readable } from 'svelte/store'
|
||||
import { registerContext } 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
|
||||
export function createGlobalContext() {
|
||||
const data = writable<GlobalContextData | null>(null)
|
||||
const loading = writable(true)
|
||||
|
||||
const refetch = async () => {
|
||||
loading.set(true)
|
||||
const result = await fetchGlobalContext({} as any)
|
||||
data.set(result)
|
||||
loading.set(false)
|
||||
}
|
||||
|
||||
refetch()
|
||||
const unregister = registerContext('global', {}, refetch)
|
||||
|
||||
return {
|
||||
data,
|
||||
loading,
|
||||
currentUser: derived(data, $d => $d?.current_user ?? null) as Readable<currentUserOutput | null>,
|
||||
destroy: unregister,
|
||||
}
|
||||
}
|
||||
|
||||
// Local context
|
||||
export function createLocalContext(params: LocalContextParams) {
|
||||
const data = writable<LocalContextData | null>(null)
|
||||
const loading = writable(true)
|
||||
|
||||
const refetch = async () => {
|
||||
loading.set(true)
|
||||
const result = await fetchLocalContext(params)
|
||||
data.set(result)
|
||||
loading.set(false)
|
||||
}
|
||||
|
||||
refetch()
|
||||
const unregister = registerContext('local', params, refetch)
|
||||
|
||||
return {
|
||||
data,
|
||||
loading,
|
||||
greet: derived(data, $d => $d?.greet ?? null) as Readable<greetOutput | null>,
|
||||
destroy: unregister,
|
||||
}
|
||||
}
|
||||
|
||||
export { callEcho } from '../functions/echo'
|
||||
export { callAdd } from '../functions/add'
|
||||
export { callWhoami } from '../functions/whoami'
|
||||
export { callHttpOnlyEcho } from '../functions/httpOnlyEcho'
|
||||
export { callStaffOnly } from '../functions/staffOnly'
|
||||
export { callSuperuserOnly } from '../functions/superuserOnly'
|
||||
export { callVerifiedOnly } from '../functions/verifiedOnly'
|
||||
export { callMultiply } from '../functions/multiply'
|
||||
export { callNotImplementedFn } from '../functions/notImplementedFn'
|
||||
export { callBuggyFn } from '../functions/buggyFn'
|
||||
export { callPermissionCheckFn } from '../functions/permissionCheckFn'
|
||||
export { callWsWhoami } from '../functions/wsWhoami'
|
||||
export { callJwtObtain } from '../functions/jwtObtain'
|
||||
export { callJwtRefresh } from '../functions/jwtRefresh'
|
||||
1908
examples/django-react-site/harness/src/api/types.ts
Normal file
1908
examples/django-react-site/harness/src/api/types.ts
Normal file
File diff suppressed because it is too large
Load Diff
96
examples/django-react-site/harness/src/api/vue.ts
Normal file
96
examples/django-react-site/harness/src/api/vue.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { ref, computed, watch, onMounted, onUnmounted, provide, inject, type Ref, type ComputedRef, type InjectionKey } from 'vue'
|
||||
import { registerContext } 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
|
||||
const GlobalKey: InjectionKey<{ data: Ref<GlobalContextData | null>, loading: Ref<boolean> }> = Symbol('global')
|
||||
|
||||
export function provideGlobalContext() {
|
||||
const data = ref<GlobalContextData | null>(null)
|
||||
const loading = ref(true)
|
||||
|
||||
const refetch = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
data.value = await fetchGlobalContext({} as any)
|
||||
} catch (e) { console.error('[mizan] global fetch failed:', e) }
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
let unregister: (() => void) | null = null
|
||||
onMounted(() => {
|
||||
refetch()
|
||||
unregister = registerContext('global', {}, refetch)
|
||||
})
|
||||
onUnmounted(() => { unregister?.() })
|
||||
|
||||
provide(GlobalKey, { data, loading })
|
||||
}
|
||||
|
||||
export function useCurrentUser(): ComputedRef<currentUserOutput | null> {
|
||||
const ctx = inject(GlobalKey)
|
||||
if (!ctx) throw new Error('useCurrentUser requires provideGlobalContext in a parent')
|
||||
return computed(() => ctx.data.value?.current_user ?? null)
|
||||
}
|
||||
|
||||
// Local context
|
||||
const LocalKey: InjectionKey<{ data: Ref<LocalContextData | null>, loading: Ref<boolean> }> = Symbol('local')
|
||||
|
||||
export function provideLocalContext(params: { name: string }) {
|
||||
const data = ref<LocalContextData | null>(null)
|
||||
const loading = ref(true)
|
||||
|
||||
const refetch = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
data.value = await fetchLocalContext(params as any)
|
||||
} catch (e) { console.error('[mizan] local fetch failed:', e) }
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
let unregister: (() => void) | null = null
|
||||
onMounted(() => {
|
||||
refetch()
|
||||
unregister = registerContext('local', params, refetch)
|
||||
})
|
||||
onUnmounted(() => { unregister?.() })
|
||||
|
||||
provide(LocalKey, { data, loading })
|
||||
}
|
||||
|
||||
export function useGreet(): ComputedRef<greetOutput | null> {
|
||||
const ctx = inject(LocalKey)
|
||||
if (!ctx) throw new Error('useGreet requires provideLocalContext in a parent')
|
||||
return computed(() => ctx.data.value?.greet ?? null)
|
||||
}
|
||||
|
||||
export const useEcho = callEcho
|
||||
|
||||
export const useAdd = callAdd
|
||||
|
||||
export const useWhoami = callWhoami
|
||||
|
||||
export const useHttpOnlyEcho = callHttpOnlyEcho
|
||||
|
||||
export const useStaffOnly = callStaffOnly
|
||||
|
||||
export const useSuperuserOnly = callSuperuserOnly
|
||||
|
||||
export const useVerifiedOnly = callVerifiedOnly
|
||||
|
||||
export const useMultiply = callMultiply
|
||||
|
||||
export const useNotImplementedFn = callNotImplementedFn
|
||||
|
||||
export const useBuggyFn = callBuggyFn
|
||||
|
||||
export const usePermissionCheckFn = callPermissionCheckFn
|
||||
|
||||
export const useWsWhoami = callWsWhoami
|
||||
|
||||
export const useJwtObtain = callJwtObtain
|
||||
|
||||
export const useJwtRefresh = callJwtRefresh
|
||||
Reference in New Issue
Block a user