weifuwu 0.33.3 → 0.33.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -39,25 +39,24 @@ serve(app, { port: 3000 })
|
|
|
39
39
|
### Frontend
|
|
40
40
|
|
|
41
41
|
```tsx
|
|
42
|
-
import { signal,
|
|
42
|
+
import { signal, createApp, api, auth, ws, router, RouteView, LoginForm } from 'weifuwu/client'
|
|
43
43
|
import type { WfuiContext } from 'weifuwu/client'
|
|
44
44
|
|
|
45
|
-
function
|
|
45
|
+
function AppShell(_props: {}, ctx: WfuiContext) {
|
|
46
|
+
if (!ctx.isAuthenticated) return <LoginForm />
|
|
46
47
|
return (
|
|
47
48
|
<div>
|
|
48
|
-
<
|
|
49
|
-
<
|
|
49
|
+
<nav><a onClick={() => ctx.app.navigate('/chat')}>聊天</a></nav>
|
|
50
|
+
<main><RouteView /></main>
|
|
50
51
|
</div>
|
|
51
52
|
)
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
const app = createApp()
|
|
55
|
-
app.use(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
],
|
|
60
|
-
}))
|
|
56
|
+
app.use(api()) // ← ctx.api.get/post
|
|
57
|
+
app.use(auth()) // ← ctx.user / ctx.login / ctx.logout
|
|
58
|
+
app.use(ws()) // ← ctx.ws.send / onMessage
|
|
59
|
+
app.use(router({ routes })) // ← ctx.route / 路由
|
|
61
60
|
app.mount('#root', AppShell)
|
|
62
61
|
```
|
|
63
62
|
|
|
@@ -127,6 +126,7 @@ esbuild.build({
|
|
|
127
126
|
| `redis()` | Redis client (`ctx.redis`) |
|
|
128
127
|
| `queue()` | Job queue + cron |
|
|
129
128
|
| `createHub()` | WebSocket pub/sub |
|
|
129
|
+
| `ui()` | SPA HTML shell (`ctx.ui.html()`) |
|
|
130
130
|
|
|
131
131
|
### Frontend (weifuwu/client)
|
|
132
132
|
|
|
@@ -140,6 +140,11 @@ esbuild.build({
|
|
|
140
140
|
| `<RouteView>` | Route outlet |
|
|
141
141
|
| `createApp()` | App instance with middleware chain |
|
|
142
142
|
| `router()` | Hash/history router with params + query |
|
|
143
|
+
| `api()` | HTTP client (`ctx.api.get/post`) |
|
|
144
|
+
| `auth()` | Auth state (`ctx.user/login/logout`) |
|
|
145
|
+
| `ws()` | WebSocket (`ctx.ws.send/onMessage`) |
|
|
146
|
+
| `LoginForm` | Login/register form component |
|
|
147
|
+
| `Chat` | Real-time messaging component |
|
|
143
148
|
| `domMount()` | Direct DOM mounting |
|
|
144
149
|
|
|
145
150
|
### Utilities
|
|
@@ -218,6 +223,51 @@ function AppShell(_, ctx) {
|
|
|
218
223
|
ctx.route.path // "/chat/123"
|
|
219
224
|
ctx.route.params // { id: "123" }
|
|
220
225
|
ctx.route.query // { tab: "settings" }
|
|
226
|
+
|
|
227
|
+
### Middleware: api / auth / ws
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import { api, auth, ws } from 'weifuwu/client'
|
|
231
|
+
|
|
232
|
+
app.use(api()) // ctx.api.get/post/put/patch/delete
|
|
233
|
+
app.use(auth()) // ctx.user / ctx.login / ctx.logout / ctx.register
|
|
234
|
+
app.use(ws()) // ctx.ws.send / onMessage / join / leave
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
`api()` creates a fetch client with automatic token injection.
|
|
238
|
+
`auth()` persists sessions to localStorage, validates tokens on startup.
|
|
239
|
+
`ws()` manages WebSocket connections with auto-reconnect.
|
|
240
|
+
|
|
241
|
+
### Pre-built Components
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
import { LoginForm, Chat } from 'weifuwu/client'
|
|
245
|
+
|
|
246
|
+
// Login / Register form
|
|
247
|
+
function LoginPage(_, ctx) {
|
|
248
|
+
if (ctx.isAuthenticated) return ctx.app.navigate('/')
|
|
249
|
+
return <LoginForm />
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Real-time chat
|
|
253
|
+
function ChatPage(_, ctx) {
|
|
254
|
+
return <Chat conversationId="123" />
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Backend: ctx.ui.html()
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
import { ui, serveStatic } from 'weifuwu'
|
|
262
|
+
|
|
263
|
+
app.use(ui({ title: 'My App', script: '/static/app.js' }))
|
|
264
|
+
app.get('/static/*', serveStatic('./dist/client'))
|
|
265
|
+
|
|
266
|
+
// SPA route
|
|
267
|
+
app.get('/', async (req, ctx) => ctx.ui.html())
|
|
268
|
+
|
|
269
|
+
// With initial props (accessible via window.__WFUI_PROPS__):
|
|
270
|
+
app.get('/', async (req, ctx) => ctx.ui.html({ title: 'Custom', props: { user: userData } }))
|
|
221
271
|
```
|
|
222
272
|
|
|
223
273
|
### Show / For
|
|
@@ -746,17 +796,26 @@ src/
|
|
|
746
796
|
├── queue/ ← Job queue + cron
|
|
747
797
|
├── graphql.ts ← GraphQL
|
|
748
798
|
├── hub.ts ← WebSocket hub
|
|
749
|
-
├──
|
|
750
|
-
|
|
751
|
-
│ ├──
|
|
752
|
-
│ ├──
|
|
753
|
-
│ ├──
|
|
754
|
-
│ ├──
|
|
755
|
-
│
|
|
799
|
+
├── ui/ ← SPA HTML shell (`ctx.ui.html()`)
|
|
800
|
+
├── client/ ← Frontend framework (~600 lines)
|
|
801
|
+
│ ├── index.ts ← Entry
|
|
802
|
+
│ ├── signal.ts ← Signal / effect / computed
|
|
803
|
+
│ ├── jsx-runtime.ts ← JSX → DOM / Show / For
|
|
804
|
+
│ ├── app.ts ← createApp / middleware chain
|
|
805
|
+
│ ├── router.ts ← Route matching / RouteView
|
|
806
|
+
│ ├── types.ts ← WfuiContext / RouteDef
|
|
807
|
+
│ ├── middleware/
|
|
808
|
+
│ │ ├── api.ts ← HTTP client
|
|
809
|
+
│ │ ├── auth.ts ← Login / logout / token
|
|
810
|
+
│ │ └── ws.ts ← WebSocket
|
|
811
|
+
│ └── components/
|
|
812
|
+
│ ├── LoginForm.ts ← Login / register form
|
|
813
|
+
│ └── Chat.ts ← Real-time messaging
|
|
756
814
|
└── test/ ← 281 tests
|
|
757
815
|
|
|
758
816
|
apps/demo/ ← Full-stack demo
|
|
759
|
-
├── src/main.tsx
|
|
817
|
+
├── src/main.tsx ← SPA demo pages
|
|
818
|
+
├── server.ts ← weifuwu server
|
|
760
819
|
├── public/index.html
|
|
761
820
|
├── tsconfig.json
|
|
762
821
|
└── scripts/build.mjs
|