create-fullstack-scaffold 0.1.0
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 +154 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +439 -0
- package/dist/cli/index.js.map +1 -0
- package/package.json +171 -0
- package/template/.agents/skills/writing-hookify-rules/SKILL.md +408 -0
- package/template/.ai-context.md +368 -0
- package/template/.claude/hookify.block-dangerous-commands.local.md +41 -0
- package/template/.claude/hookify.check-api-types.local.md +111 -0
- package/template/.claude/hookify.check-duplicate-types.local.md +287 -0
- package/template/.claude/hookify.check-test-assertions.local.md +93 -0
- package/template/.claude/hookify.remind-add-tests.local.md +209 -0
- package/template/.claude/hookify.remind-db-migration.local.md +58 -0
- package/template/.claude/hookify.remind-run-tests.local.md +52 -0
- package/template/.claude/hookify.warn-any-type.local.md +203 -0
- package/template/.claude/hookify.warn-console-log.local.md +73 -0
- package/template/.claude/rules/00-project-config.md +147 -0
- package/template/.claude/rules/01-file-types.md +127 -0
- package/template/.claude/rules/02-git-workflow.md +167 -0
- package/template/.claude/rules/10-api-type-inference.md +555 -0
- package/template/.claude/rules/20-server-api.md +349 -0
- package/template/.claude/rules/21-server-entrypoint.md +257 -0
- package/template/.claude/rules/30-client-components.md +314 -0
- package/template/.claude/rules/31-client-services.md +768 -0
- package/template/.claude/rules/32-client-state-zustand.md +324 -0
- package/template/.claude/rules/33-client-app-entry.md +276 -0
- package/template/.claude/rules/34-client-admin-module.md +445 -0
- package/template/.claude/rules/35-cli-module.md +259 -0
- package/template/.claude/rules/40-admin-module.md +495 -0
- package/template/.claude/rules/40-shared-types.md +450 -0
- package/template/.claude/rules/50-websocket.md +245 -0
- package/template/.claude/rules/51-sse.md +372 -0
- package/template/.claude/rules/52-cloudflare-realtime.md +384 -0
- package/template/.claude/rules/60-testing-standards.md +245 -0
- package/template/.claude/rules/61-hono-testing.md +200 -0
- package/template/.claude/scripts/README.md +187 -0
- package/template/.claude/scripts/check-duplicate-types.ts +188 -0
- package/template/.claude/scripts/post-edit-check-incremental.sh +61 -0
- package/template/.claude/scripts/post-edit-check.sh +22 -0
- package/template/.claude/scripts/pre-tool-use-check.sh +86 -0
- package/template/.claude/scripts/test-pre-tool-use.sh +73 -0
- package/template/.claude/scripts/validate-git-changes.sh +38 -0
- package/template/.claude/settings.json +32 -0
- package/template/.claude/skills/writing-hookify-rules/SKILL.md +408 -0
- package/template/.dev-context-compact.md +54 -0
- package/template/.dev-context.md +621 -0
- package/template/.dev-guide.md +243 -0
- package/template/.env.example +60 -0
- package/template/.eslintrc.routes.json +39 -0
- package/template/.husky/_/husky.sh +27 -0
- package/template/.husky/commit-msg +4 -0
- package/template/.husky/post-commit +4 -0
- package/template/.husky/pre-commit +19 -0
- package/template/.husky/pre-push +12 -0
- package/template/.prettierignore +22 -0
- package/template/.prettierrc +8 -0
- package/template/.sessions/3/2026-03-26T05-41-09-154Z_03bb597f-3898-4a2e-8c08-5746bbe8d95e.jsonl +18 -0
- package/template/.sessions/3/2026-03-26T05-48-30-052Z_12de4774-9cb3-494c-9852-bd1e6b0003c3.jsonl +6 -0
- package/template/.vscode/extensions.json +8 -0
- package/template/.vscode/settings.json +4 -0
- package/template/.workspaces/3/hello.py +1 -0
- package/template/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/803c2e37182b913f8857edf6d8776515cef4af6d48fcb25913e457f4b14ffa97.sqlite +0 -0
- package/template/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/803c2e37182b913f8857edf6d8776515cef4af6d48fcb25913e457f4b14ffa97.sqlite-shm +0 -0
- package/template/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/803c2e37182b913f8857edf6d8776515cef4af6d48fcb25913e457f4b14ffa97.sqlite-wal +0 -0
- package/template/CLAUDE.md +296 -0
- package/template/DESIGN.md +260 -0
- package/template/DEV_CONTEXT_GUIDE.md +366 -0
- package/template/DEV_TOOLS_README.md +396 -0
- package/template/FRAMEWORK_HISTORY.md +34 -0
- package/template/PROJECT_SUMMARY.md +325 -0
- package/template/QUICKSTART.md +305 -0
- package/template/README.md +154 -0
- package/template/STRUCTURE.txt +81 -0
- package/template/admin.html +13 -0
- package/template/auth-inject.html +33 -0
- package/template/commitlint.config.js +12 -0
- package/template/docs/PERMISSION_ARCHITECTURE.md +279 -0
- package/template/docs/PERMISSION_EXAMPLES.md +230 -0
- package/template/docs/PERMISSION_SYSTEM.md +301 -0
- package/template/docs/SMART_TEST.md +171 -0
- package/template/docs/runtime-abstract.md +253 -0
- package/template/drizzle/0000_rainy_boomer.sql +101 -0
- package/template/drizzle/0001_add_todo_attachments.sql +12 -0
- package/template/drizzle/0002_chilly_magneto.sql +89 -0
- package/template/drizzle/meta/0000_snapshot.json +652 -0
- package/template/drizzle/meta/0001_snapshot.json +735 -0
- package/template/drizzle/meta/0002_snapshot.json +1198 -0
- package/template/drizzle/meta/_journal.json +27 -0
- package/template/drizzle.config.ts +13 -0
- package/template/eslint-rules/README-no-middleware-in-routes.md +154 -0
- package/template/eslint-rules/__tests__/enforce-valid-method.test.ts +251 -0
- package/template/eslint-rules/__tests__/layer-boundary.test.ts +102 -0
- package/template/eslint-rules/__tests__/module-boundary.test.ts +77 -0
- package/template/eslint-rules/__tests__/no-direct-zod-import-in-file-routes.test.ts +45 -0
- package/template/eslint-rules/__tests__/no-new-old-service-naming.test.ts +76 -0
- package/template/eslint-rules/e2e-test-location.js +153 -0
- package/template/eslint-rules/enforce-valid-method.js +217 -0
- package/template/eslint-rules/flat-routes-services.js +69 -0
- package/template/eslint-rules/framework-protect.js +186 -0
- package/template/eslint-rules/layer-boundary.js +164 -0
- package/template/eslint-rules/limit-type-complexity.js +130 -0
- package/template/eslint-rules/middleware-location.js +162 -0
- package/template/eslint-rules/module-boundary.js +125 -0
- package/template/eslint-rules/no-ambiguous-file-paths.js +64 -0
- package/template/eslint-rules/no-any-on-apiclient.js +173 -0
- package/template/eslint-rules/no-boolean-success.js +43 -0
- package/template/eslint-rules/no-deep-relative-imports.js +69 -0
- package/template/eslint-rules/no-direct-fetch.js +122 -0
- package/template/eslint-rules/no-direct-ws-sse.js +57 -0
- package/template/eslint-rules/no-direct-zod-import-in-file-routes.js +54 -0
- package/template/eslint-rules/no-disable-direct-fetch.js +57 -0
- package/template/eslint-rules/no-disable-type-safe-client.js +58 -0
- package/template/eslint-rules/no-inline-schema.js +220 -0
- package/template/eslint-rules/no-middleware-in-routes.js +81 -0
- package/template/eslint-rules/no-new-old-service-naming.js +123 -0
- package/template/eslint-rules/no-type-assertion-in-rpc.js +99 -0
- package/template/eslint-rules/no-type-assertion-on-shared-types.js +109 -0
- package/template/eslint-rules/no-util-functions-in-service.js +128 -0
- package/template/eslint-rules/prefer-shared-types.js +352 -0
- package/template/eslint-rules/protect-ws-sse-interface.js +66 -0
- package/template/eslint-rules/require-antd-generic-types.js +73 -0
- package/template/eslint-rules/require-file-openapi-props.js +114 -0
- package/template/eslint-rules/require-hono-chain-syntax.js +129 -0
- package/template/eslint-rules/require-nullable-for-optional.js +99 -0
- package/template/eslint-rules/require-response-helpers.js +140 -0
- package/template/eslint-rules/require-type-safe-test-client.js +106 -0
- package/template/eslint-rules/route-location.js +72 -0
- package/template/eslint.config.js +231 -0
- package/template/index.html +17 -0
- package/template/lint-scripts/DIRECTORY_RULES.md +335 -0
- package/template/lint-scripts/README.md +299 -0
- package/template/lint-scripts/TESTING.md +273 -0
- package/template/lint-scripts/ai-context-generator.ts +159 -0
- package/template/lint-scripts/check-framework-modify.sh +8 -0
- package/template/lint-scripts/check-menu-permissions.js +242 -0
- package/template/lint-scripts/check-refs.ts +165 -0
- package/template/lint-scripts/check-route-auth-complete.js +260 -0
- package/template/lint-scripts/check-route-auth.js +212 -0
- package/template/lint-scripts/check-route-permissions.js +210 -0
- package/template/lint-scripts/config/project.config.ts +358 -0
- package/template/lint-scripts/dev-context-analyzer.ts +707 -0
- package/template/lint-scripts/dev-guide-generator.ts +830 -0
- package/template/lint-scripts/dev-server.ts +58 -0
- package/template/lint-scripts/eslint-route-auth.js +158 -0
- package/template/lint-scripts/eslint-routes-permission.js +147 -0
- package/template/lint-scripts/fix-route-auth.js +245 -0
- package/template/lint-scripts/framework/check-modify.ts +152 -0
- package/template/lint-scripts/framework/hash-utils.ts +180 -0
- package/template/lint-scripts/framework/init-baseline.ts +84 -0
- package/template/lint-scripts/framework/update-baseline.ts +148 -0
- package/template/lint-scripts/post-commit-track.ts +66 -0
- package/template/lint-scripts/quick-test.sh +27 -0
- package/template/lint-scripts/smart-test.ts +268 -0
- package/template/lint-scripts/test-history.ts +360 -0
- package/template/lint-scripts/test-tracker.ts +350 -0
- package/template/lint-scripts/validate-all.ts +250 -0
- package/template/lint-scripts/validators/api-coverage.validator.ts +355 -0
- package/template/lint-scripts/validators/client-rpc.validator.ts +190 -0
- package/template/lint-scripts/validators/client-tests.validator.ts +155 -0
- package/template/lint-scripts/validators/console-log.validator.ts +122 -0
- package/template/lint-scripts/validators/directory-structure.validator.ts +181 -0
- package/template/lint-scripts/validators/imports.validator.ts +180 -0
- package/template/lint-scripts/validators/index.ts +271 -0
- package/template/lint-scripts/validators/md-refs.validator.ts +165 -0
- package/template/lint-scripts/validators/module-tests.validator.ts +112 -0
- package/template/lint-scripts/validators/sensitive.validator.ts +185 -0
- package/template/lint-scripts/validators/server-rpc.validator.ts +141 -0
- package/template/lint-scripts/validators/test-quality.validator.ts +257 -0
- package/template/lint-scripts/validators/todos.validator.ts +126 -0
- package/template/lint-scripts/watch-validator.ts +131 -0
- package/template/modules.config.ts +71 -0
- package/template/package-lock.json +14554 -0
- package/template/package.json +158 -0
- package/template/patches/@hono+zod-openapi+1.2.2.patch +67 -0
- package/template/patches/@hono+zod-validator+0.7.6.patch +13 -0
- package/template/patches/hono+4.12.16.patch +246 -0
- package/template/patches/typescript+5.8.3.patch +26 -0
- package/template/playwright.config.ts +60 -0
- package/template/pnpm-lock.yaml +7137 -0
- package/template/postcss.config.js +6 -0
- package/template/scripts/create-template.ts +1724 -0
- package/template/src/admin/App.tsx +59 -0
- package/template/src/admin/components/AccountSwitcher.tsx +104 -0
- package/template/src/admin/components/CaptchaModal.tsx +150 -0
- package/template/src/admin/components/NotificationDrawer.tsx +213 -0
- package/template/src/admin/components/PageHeader.tsx +45 -0
- package/template/src/admin/components/PermissionConfigEditor.tsx +237 -0
- package/template/src/admin/components/PermissionGuard.tsx +108 -0
- package/template/src/admin/components/PermissionTree.tsx +193 -0
- package/template/src/admin/components/ProtectedRoute.tsx +33 -0
- package/template/src/admin/components/StatsCard.tsx +44 -0
- package/template/src/admin/components/UserFormModal.tsx +67 -0
- package/template/src/admin/components/UserTable.tsx +72 -0
- package/template/src/admin/components/__tests__/PageHeader.test.tsx +48 -0
- package/template/src/admin/components/__tests__/PermissionConfigEditor.test.tsx +147 -0
- package/template/src/admin/components/__tests__/PermissionGuard.test.tsx +221 -0
- package/template/src/admin/components/__tests__/PermissionTree.test.tsx +187 -0
- package/template/src/admin/components/__tests__/ProtectedRoute.test.tsx +129 -0
- package/template/src/admin/components/__tests__/StatsCard.test.tsx +44 -0
- package/template/src/admin/components/__tests__/UserFormModal.test.tsx +125 -0
- package/template/src/admin/components/__tests__/UserTable.test.tsx +89 -0
- package/template/src/admin/components/index.ts +7 -0
- package/template/src/admin/hooks/__tests__/useAuditLogs.test.ts +109 -0
- package/template/src/admin/hooks/__tests__/usePermissions.test.tsx +327 -0
- package/template/src/admin/hooks/__tests__/usePermissionsBranches.test.ts +198 -0
- package/template/src/admin/hooks/__tests__/useRoles.test.ts +261 -0
- package/template/src/admin/hooks/useAdminNotifications.ts +156 -0
- package/template/src/admin/hooks/useAuditLogs.ts +44 -0
- package/template/src/admin/hooks/useConfig.ts +168 -0
- package/template/src/admin/hooks/usePermissions.ts +156 -0
- package/template/src/admin/hooks/useRoles.ts +120 -0
- package/template/src/admin/layouts/Header.tsx +93 -0
- package/template/src/admin/layouts/Layout.tsx +23 -0
- package/template/src/admin/layouts/Sidebar.tsx +120 -0
- package/template/src/admin/main.tsx +18 -0
- package/template/src/admin/pages/ContentPage.tsx +252 -0
- package/template/src/admin/pages/DashboardPage.tsx +141 -0
- package/template/src/admin/pages/DisputesPage.tsx +261 -0
- package/template/src/admin/pages/LoginPage.tsx +121 -0
- package/template/src/admin/pages/MediaTestPage.tsx +524 -0
- package/template/src/admin/pages/OrdersPage.tsx +302 -0
- package/template/src/admin/pages/PermissionsPage.tsx +95 -0
- package/template/src/admin/pages/RegisterPage.tsx +103 -0
- package/template/src/admin/pages/RolesPage.tsx +330 -0
- package/template/src/admin/pages/SettingsPage.tsx +73 -0
- package/template/src/admin/pages/SystemLogsPage.tsx +218 -0
- package/template/src/admin/pages/TestCaptchaPage.tsx +120 -0
- package/template/src/admin/pages/TicketsPage.tsx +292 -0
- package/template/src/admin/pages/UsersPage.tsx +282 -0
- package/template/src/admin/pages/__tests__/ContentPage.test.tsx +213 -0
- package/template/src/admin/pages/__tests__/DashboardPage.test.tsx +153 -0
- package/template/src/admin/pages/__tests__/DisputesPage.test.tsx +196 -0
- package/template/src/admin/pages/__tests__/LoginPage.test.tsx +112 -0
- package/template/src/admin/pages/__tests__/OrdersPage.test.tsx +217 -0
- package/template/src/admin/pages/__tests__/PermissionsPage.test.tsx +119 -0
- package/template/src/admin/pages/__tests__/RegisterPage.test.tsx +179 -0
- package/template/src/admin/pages/__tests__/RolesPage.test.tsx +256 -0
- package/template/src/admin/pages/__tests__/SettingsPage.test.tsx +66 -0
- package/template/src/admin/pages/__tests__/SystemLogsPage.test.tsx +125 -0
- package/template/src/admin/pages/__tests__/TicketsPage.test.tsx +202 -0
- package/template/src/admin/pages/__tests__/UsersPage.test.tsx +229 -0
- package/template/src/admin/services/README.md +263 -0
- package/template/src/admin/services/TESTING.md +323 -0
- package/template/src/admin/services/__tests__/skipLoading.test.ts +146 -0
- package/template/src/admin/services/apiClient.ts +66 -0
- package/template/src/admin/services/requestInterceptor.ts +127 -0
- package/template/src/admin/services/types.ts +5 -0
- package/template/src/admin/stores/__tests__/adminStore.test.ts +184 -0
- package/template/src/admin/stores/__tests__/adminStoreBranches.test.ts +148 -0
- package/template/src/admin/stores/__tests__/captchaStore.test.ts +75 -0
- package/template/src/admin/stores/__tests__/captchaStoreBranches.test.ts +37 -0
- package/template/src/admin/stores/__tests__/loadingStore.test.ts +68 -0
- package/template/src/admin/stores/adminStore.ts +88 -0
- package/template/src/admin/stores/captchaStore.ts +47 -0
- package/template/src/admin/stores/loadingStore.ts +27 -0
- package/template/src/cli/index.ts +22 -0
- package/template/src/cli/modules/config/index.ts +132 -0
- package/template/src/cli/modules/index.ts +12 -0
- package/template/src/cli/modules/notification/index.ts +86 -0
- package/template/src/cli/modules/todo/index.ts +60 -0
- package/template/src/cli/rpc/client.ts +23 -0
- package/template/src/cli/rpc/index.ts +1 -0
- package/template/src/cli/utils/api.ts +17 -0
- package/template/src/cli/utils/auto-command.ts +229 -0
- package/template/src/cli/utils/index.ts +8 -0
- package/template/src/cli/utils/logger.ts +79 -0
- package/template/src/client/App.tsx +24 -0
- package/template/src/client/Layout.tsx +27 -0
- package/template/src/client/components/AuthButton.tsx +41 -0
- package/template/src/client/components/ConnectionStatus.tsx +75 -0
- package/template/src/client/components/EmptyState.tsx +26 -0
- package/template/src/client/components/Footer.tsx +17 -0
- package/template/src/client/components/LoadingSpinner.tsx +26 -0
- package/template/src/client/components/MessageCard.tsx +61 -0
- package/template/src/client/components/Navigation.tsx +65 -0
- package/template/src/client/components/StatusBadge.tsx +50 -0
- package/template/src/client/components/__tests__/App.test.tsx +79 -0
- package/template/src/client/components/__tests__/AuthButton.test.tsx +119 -0
- package/template/src/client/components/__tests__/ConnectionStatus.test.tsx +188 -0
- package/template/src/client/components/__tests__/EmptyState.test.tsx +70 -0
- package/template/src/client/components/__tests__/Footer.test.tsx +22 -0
- package/template/src/client/components/__tests__/LoadingSpinner.test.tsx +75 -0
- package/template/src/client/components/__tests__/MessageCard.test.tsx +142 -0
- package/template/src/client/components/__tests__/Navigation.test.tsx +40 -0
- package/template/src/client/components/__tests__/StatusBadge.test.tsx +107 -0
- package/template/src/client/components/index.ts +6 -0
- package/template/src/client/index.css +27 -0
- package/template/src/client/main.tsx +25 -0
- package/template/src/client/pages/ContentDetailPage.tsx +142 -0
- package/template/src/client/pages/ContentListPage.tsx +169 -0
- package/template/src/client/pages/NotificationPage.tsx +288 -0
- package/template/src/client/pages/TodoPage.tsx +389 -0
- package/template/src/client/pages/WebSocketPage.tsx +239 -0
- package/template/src/client/pages/__tests__/ContentDetailPage.test.tsx +176 -0
- package/template/src/client/pages/__tests__/ContentListPage.test.tsx +202 -0
- package/template/src/client/pages/__tests__/NotificationPage.test.tsx +246 -0
- package/template/src/client/pages/__tests__/TodoPage.test.tsx +433 -0
- package/template/src/client/pages/__tests__/WebSocketPage.test.tsx +378 -0
- package/template/src/client/services/apiClient.ts +66 -0
- package/template/src/client/stores/__tests__/authStore.test.ts +139 -0
- package/template/src/client/stores/__tests__/chatWSStore.test.ts +416 -0
- package/template/src/client/stores/__tests__/notificationStore.test.ts +453 -0
- package/template/src/client/stores/__tests__/todoStore.test.ts +509 -0
- package/template/src/client/stores/authStore.ts +51 -0
- package/template/src/client/stores/chatWSStore.ts +122 -0
- package/template/src/client/stores/notificationStore.ts +199 -0
- package/template/src/client/stores/todoStore.ts +207 -0
- package/template/src/server/__tests__/integration/todos-api.test.ts +138 -0
- package/template/src/server/app.ts +105 -0
- package/template/src/server/config.ts +86 -0
- package/template/src/server/core/__tests__/realtime-core.test.ts +329 -0
- package/template/src/server/core/__tests__/realtime-scanner.test.ts +210 -0
- package/template/src/server/core/__tests__/runtime-node.test.ts +330 -0
- package/template/src/server/core/__tests__/runtime.test.ts +219 -0
- package/template/src/server/core/__tests__/typed-runtime.test.ts +178 -0
- package/template/src/server/core/durable-objects/RealtimeDO.ts +206 -0
- package/template/src/server/core/index.ts +83 -0
- package/template/src/server/core/module-loader.ts +281 -0
- package/template/src/server/core/realtime-core.ts +152 -0
- package/template/src/server/core/realtime-scanner.ts +99 -0
- package/template/src/server/core/runtime-cloudflare.ts +207 -0
- package/template/src/server/core/runtime-node.ts +170 -0
- package/template/src/server/core/runtime.ts +129 -0
- package/template/src/server/core/typed-runtime.ts +80 -0
- package/template/src/server/db/__tests__/driver.test.ts +203 -0
- package/template/src/server/db/config.ts +1 -0
- package/template/src/server/db/driver-cloudflare.ts +50 -0
- package/template/src/server/db/driver.ts +116 -0
- package/template/src/server/db/index.ts +3 -0
- package/template/src/server/db/init.ts +412 -0
- package/template/src/server/db/schema/api-endpoints.ts +23 -0
- package/template/src/server/db/schema/contents.ts +31 -0
- package/template/src/server/db/schema/disputes.ts +39 -0
- package/template/src/server/db/schema/index.ts +14 -0
- package/template/src/server/db/schema/notifications.ts +16 -0
- package/template/src/server/db/schema/orders.ts +30 -0
- package/template/src/server/db/schema/permission-audit-logs.ts +17 -0
- package/template/src/server/db/schema/permission-route-mappings.ts +22 -0
- package/template/src/server/db/schema/permissions.ts +17 -0
- package/template/src/server/db/schema/role-permissions.ts +22 -0
- package/template/src/server/db/schema/roles.ts +17 -0
- package/template/src/server/db/schema/tickets.ts +62 -0
- package/template/src/server/db/schema/todo-attachments.ts +22 -0
- package/template/src/server/db/schema/todos.ts +21 -0
- package/template/src/server/db/schema/user-roles.ts +17 -0
- package/template/src/server/db/seeds/index.ts +1 -0
- package/template/src/server/db/seeds/permission-data.ts +354 -0
- package/template/src/server/db/test-setup.ts +647 -0
- package/template/src/server/entries/cloudflare.ts +71 -0
- package/template/src/server/entries/node.ts +190 -0
- package/template/src/server/index.ts +62 -0
- package/template/src/server/middleware/__tests__/auth-simple.test.ts +147 -0
- package/template/src/server/middleware/__tests__/auth.test.ts +276 -0
- package/template/src/server/middleware/__tests__/captcha.test.ts +258 -0
- package/template/src/server/middleware/__tests__/error-handler.test.ts +364 -0
- package/template/src/server/middleware/__tests__/error-response-format.test.ts +80 -0
- package/template/src/server/middleware/__tests__/permission.test.ts +312 -0
- package/template/src/server/middleware/audit-log.ts +101 -0
- package/template/src/server/middleware/auth.ts +235 -0
- package/template/src/server/middleware/captcha.ts +166 -0
- package/template/src/server/middleware/cors.ts +30 -0
- package/template/src/server/middleware/error-handler.ts +135 -0
- package/template/src/server/middleware/index.ts +24 -0
- package/template/src/server/middleware/logger.ts +73 -0
- package/template/src/server/middleware/permission.ts +127 -0
- package/template/src/server/middleware/rate-limit.ts +69 -0
- package/template/src/server/middleware/realtime-env.ts +12 -0
- package/template/src/server/module-admin/__tests__/admin-routes.test.ts +360 -0
- package/template/src/server/module-admin/__tests__/admin-service.test.ts +222 -0
- package/template/src/server/module-admin/__tests__/audit-log-routes.test.ts +261 -0
- package/template/src/server/module-admin/__tests__/export-routes.test.ts +160 -0
- package/template/src/server/module-admin/__tests__/media-routes.test.ts +122 -0
- package/template/src/server/module-admin/module.ts +40 -0
- package/template/src/server/module-admin/routes/admin-notification-routes.ts +197 -0
- package/template/src/server/module-admin/routes/admin-routes.ts +20 -0
- package/template/src/server/module-admin/routes/auth-routes.ts +86 -0
- package/template/src/server/module-admin/routes/export-routes.ts +207 -0
- package/template/src/server/module-admin/routes/media-routes.ts +78 -0
- package/template/src/server/module-admin/routes/system-routes.ts +89 -0
- package/template/src/server/module-admin/routes/user-management-routes.ts +143 -0
- package/template/src/server/module-admin/services/admin-service.ts +268 -0
- package/template/src/server/module-captcha/__tests__/captcha-route.test.ts +35 -0
- package/template/src/server/module-captcha/__tests__/captcha-service.test.ts +53 -0
- package/template/src/server/module-captcha/module.ts +35 -0
- package/template/src/server/module-captcha/routes/captcha-routes.ts +68 -0
- package/template/src/server/module-captcha/services/captcha-service.ts +3 -0
- package/template/src/server/module-chat/__tests__/chat-routes.test.ts +39 -0
- package/template/src/server/module-chat/__tests__/chat-rpc.test.ts +185 -0
- package/template/src/server/module-chat/__tests__/chat-service.test.ts +64 -0
- package/template/src/server/module-chat/module.ts +27 -0
- package/template/src/server/module-chat/routes/chat-routes.ts +46 -0
- package/template/src/server/module-chat/services/chat-service.ts +38 -0
- package/template/src/server/module-content/__tests__/content-route.test.ts +40 -0
- package/template/src/server/module-content/__tests__/content-service.test.ts +107 -0
- package/template/src/server/module-content/module.ts +39 -0
- package/template/src/server/module-content/routes/content-routes.ts +182 -0
- package/template/src/server/module-content/routes/public-content-routes.ts +58 -0
- package/template/src/server/module-content/services/content-service.ts +224 -0
- package/template/src/server/module-dispute/__tests__/dispute-route.test.ts +43 -0
- package/template/src/server/module-dispute/__tests__/dispute-service.test.ts +135 -0
- package/template/src/server/module-dispute/module.ts +30 -0
- package/template/src/server/module-dispute/routes/dispute-routes.ts +153 -0
- package/template/src/server/module-dispute/services/dispute-service.ts +255 -0
- package/template/src/server/module-file/__tests__/file-routes.test.ts +284 -0
- package/template/src/server/module-file/__tests__/file-storage-service.test.ts +323 -0
- package/template/src/server/module-file/module.ts +28 -0
- package/template/src/server/module-file/routes/file-routes.ts +288 -0
- package/template/src/server/module-notifications/__tests__/notification-route-rpc.test.ts +375 -0
- package/template/src/server/module-notifications/__tests__/notification-service.test.ts +241 -0
- package/template/src/server/module-notifications/__tests__/sse-rpc.test.ts +289 -0
- package/template/src/server/module-notifications/module.ts +32 -0
- package/template/src/server/module-notifications/routes/notification-routes.ts +190 -0
- package/template/src/server/module-notifications/services/notification-service.ts +104 -0
- package/template/src/server/module-order/__tests__/order-route.test.ts +258 -0
- package/template/src/server/module-order/__tests__/order-service.test.ts +222 -0
- package/template/src/server/module-order/module.ts +30 -0
- package/template/src/server/module-order/routes/order-routes.ts +202 -0
- package/template/src/server/module-order/services/order-service.ts +268 -0
- package/template/src/server/module-permission/__tests__/permission-middleware.test.ts +90 -0
- package/template/src/server/module-permission/__tests__/permission-routes.test.ts +330 -0
- package/template/src/server/module-permission/__tests__/permission-service-impl.test.ts +388 -0
- package/template/src/server/module-permission/__tests__/permission-service.test.ts +190 -0
- package/template/src/server/module-permission/__tests__/role-routes.test.ts +215 -0
- package/template/src/server/module-permission/module.ts +68 -0
- package/template/src/server/module-permission/routes/audit-log-routes.ts +76 -0
- package/template/src/server/module-permission/routes/permission-routes.ts +184 -0
- package/template/src/server/module-permission/routes/role-routes.ts +263 -0
- package/template/src/server/module-permission/services/audit-log-service.ts +108 -0
- package/template/src/server/module-permission/services/permission-service-impl.ts +233 -0
- package/template/src/server/module-permission/services/permission-service.ts +256 -0
- package/template/src/server/module-permission/services/role-service.ts +122 -0
- package/template/src/server/module-ticket/__tests__/ticket-route.test.ts +173 -0
- package/template/src/server/module-ticket/__tests__/ticket-service.test.ts +201 -0
- package/template/src/server/module-ticket/module.ts +30 -0
- package/template/src/server/module-ticket/routes/ticket-routes.ts +210 -0
- package/template/src/server/module-ticket/services/ticket-service.ts +297 -0
- package/template/src/server/module-todos/__tests__/todo-service.test.ts +196 -0
- package/template/src/server/module-todos/__tests__/todos-file-upload.test.ts +439 -0
- package/template/src/server/module-todos/__tests__/todos-route-rpc.test.ts +575 -0
- package/template/src/server/module-todos/module.ts +30 -0
- package/template/src/server/module-todos/routes/todos-routes.ts +260 -0
- package/template/src/server/module-todos/services/todo-service.ts +202 -0
- package/template/src/server/route-registry.ts +47 -0
- package/template/src/server/test-utils/test-client.ts +66 -0
- package/template/src/server/test-utils/test-server.ts +131 -0
- package/template/src/server/types/bindings.ts +10 -0
- package/template/src/server/utils/__tests__/app-error.test.ts +374 -0
- package/template/src/server/utils/__tests__/auth.test.ts +76 -0
- package/template/src/server/utils/__tests__/captcha.test.ts +80 -0
- package/template/src/server/utils/__tests__/file-storage.test.ts +472 -0
- package/template/src/server/utils/__tests__/logger.test.ts +221 -0
- package/template/src/server/utils/__tests__/route-helpers.test.ts +264 -0
- package/template/src/server/utils/app-error.ts +355 -0
- package/template/src/server/utils/auth.ts +65 -0
- package/template/src/server/utils/captcha.ts +119 -0
- package/template/src/server/utils/date.ts +50 -0
- package/template/src/server/utils/env.ts +4 -0
- package/template/src/server/utils/file-storage.ts +462 -0
- package/template/src/server/utils/generate.ts +26 -0
- package/template/src/server/utils/id-helpers.ts +5 -0
- package/template/src/server/utils/logger.ts +139 -0
- package/template/src/server/utils/permission-utils.ts +6 -0
- package/template/src/server/utils/response.ts +40 -0
- package/template/src/server/utils/route-helpers.ts +200 -0
- package/template/src/server/utils/uuid.ts +8 -0
- package/template/src/server/utils/ws-helper.ts +30 -0
- package/template/src/shared/constants/index.ts +1 -0
- package/template/src/shared/constants/resource-types.ts +72 -0
- package/template/src/shared/core/__tests__/sse-client.test.ts +376 -0
- package/template/src/shared/core/__tests__/ws-client.test.ts +468 -0
- package/template/src/shared/core/api-request.ts +192 -0
- package/template/src/shared/core/api-schemas.ts +41 -0
- package/template/src/shared/core/index.ts +37 -0
- package/template/src/shared/core/module-manifest.ts +120 -0
- package/template/src/shared/core/protocol-types.ts +26 -0
- package/template/src/shared/core/sse-client.ts +185 -0
- package/template/src/shared/core/ws-client.ts +236 -0
- package/template/src/shared/hooks/index.ts +2 -0
- package/template/src/shared/hooks/useSSE.ts +57 -0
- package/template/src/shared/hooks/useWebSocket.ts +66 -0
- package/template/src/shared/index.ts +5 -0
- package/template/src/shared/modules/admin/index.ts +1 -0
- package/template/src/shared/modules/admin/schemas.ts +100 -0
- package/template/src/shared/modules/audit/index.ts +1 -0
- package/template/src/shared/modules/audit/schemas.ts +24 -0
- package/template/src/shared/modules/captcha/index.ts +1 -0
- package/template/src/shared/modules/captcha/schemas.ts +19 -0
- package/template/src/shared/modules/chat/index.ts +38 -0
- package/template/src/shared/modules/content/index.ts +1 -0
- package/template/src/shared/modules/content/schemas.ts +53 -0
- package/template/src/shared/modules/dispute/index.ts +1 -0
- package/template/src/shared/modules/dispute/schemas.ts +62 -0
- package/template/src/shared/modules/files/index.ts +11 -0
- package/template/src/shared/modules/files/schemas.ts +44 -0
- package/template/src/shared/modules/index.ts +70 -0
- package/template/src/shared/modules/notifications/index.ts +26 -0
- package/template/src/shared/modules/notifications/schemas.ts +73 -0
- package/template/src/shared/modules/order/index.ts +1 -0
- package/template/src/shared/modules/order/schemas.ts +63 -0
- package/template/src/shared/modules/permission/index.ts +42 -0
- package/template/src/shared/modules/permission/permission-dependencies.ts +50 -0
- package/template/src/shared/modules/permission/permissions.ts +275 -0
- package/template/src/shared/modules/permission/schemas.ts +92 -0
- package/template/src/shared/modules/permission/types.ts +23 -0
- package/template/src/shared/modules/role/index.ts +1 -0
- package/template/src/shared/modules/role/schemas.ts +46 -0
- package/template/src/shared/modules/ticket/index.ts +1 -0
- package/template/src/shared/modules/ticket/schemas.ts +77 -0
- package/template/src/shared/modules/todos/index.ts +20 -0
- package/template/src/shared/modules/todos/schemas.ts +65 -0
- package/template/src/shared/schemas/index.ts +79 -0
- package/template/src/types/global.d.ts +14 -0
- package/template/src/vite-env.d.ts +21 -0
- package/template/tailwind.config.js +11 -0
- package/template/tests/e2e/admin-crud.spec.ts +81 -0
- package/template/tests/e2e/case-o1.spec.ts +108 -0
- package/template/tests/e2e/global-setup.ts +73 -0
- package/template/tests/e2e/global-teardown.ts +28 -0
- package/template/tests/e2e/notification.spec.ts +458 -0
- package/template/tests/e2e/todo.spec.ts +623 -0
- package/template/tests/e2e/websocket.spec.ts +254 -0
- package/template/tsconfig.json +35 -0
- package/template/tsup.config.ts +95 -0
- package/template/vite-plugins.ts +113 -0
- package/template/vite.config.ts +96 -0
- package/template/vitest.config.ts +53 -0
- package/template/vitest.integration.config.ts +24 -0
- package/template/vitest.integration.setup.ts +22 -0
- package/template/vitest.setup.ts +74 -0
- package/template/wrangler.toml +33 -0
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
---
|
|
2
|
+
paths: src/client/services/**/*.ts, src/admin/services/**/*.ts, src/cli/rpc/**/*.ts
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Client Service 开发规范
|
|
6
|
+
|
|
7
|
+
## 🎯 核心职责
|
|
8
|
+
|
|
9
|
+
`src/*/services/` 目录包含 API 客户端和实时通信客户端的配置。
|
|
10
|
+
|
|
11
|
+
## 📁 文件结构
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
src/
|
|
15
|
+
├── client/services/
|
|
16
|
+
│ └── apiClient.ts # 前端 API 客户端(基础版)
|
|
17
|
+
│
|
|
18
|
+
├── admin/services/
|
|
19
|
+
│ ├── apiClient.ts # 管理后台 API 客户端(完整功能)
|
|
20
|
+
│ ├── requestInterceptor.ts # 自定义 fetch 实现
|
|
21
|
+
│ └── types.ts # 扩展参数类型定义
|
|
22
|
+
│
|
|
23
|
+
└── cli/rpc/
|
|
24
|
+
└── client.ts # CLI RPC 客户端
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 🔧 API 客户端配置
|
|
28
|
+
|
|
29
|
+
### 创建类型安全的客户端
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// src/client/services/apiClient.ts
|
|
33
|
+
import { hc } from 'hono/client'
|
|
34
|
+
import type { AppType } from '@server/index'
|
|
35
|
+
import { WSClientImpl } from '@shared/core/ws-client'
|
|
36
|
+
import { SSEClientImpl } from '@shared/core/sse-client'
|
|
37
|
+
|
|
38
|
+
const baseUrl = import.meta.env.API_BASE_URL || window.location.origin
|
|
39
|
+
|
|
40
|
+
export const apiClient = hc<AppType>(baseUrl, {
|
|
41
|
+
webSocket: url => new WSClientImpl(url),
|
|
42
|
+
sse: url => new SSEClientImpl(url),
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**关键点**:
|
|
47
|
+
|
|
48
|
+
- 使用 `hc<AppType>()` 创建类型安全的客户端
|
|
49
|
+
- 导入 `AppType` 从 `@server/index`
|
|
50
|
+
- 配置 WebSocket 和 SSE 客户端实现
|
|
51
|
+
|
|
52
|
+
**ESLint 规则**: `no-ambiguous-file-paths`
|
|
53
|
+
|
|
54
|
+
**路径**: `src/client/services/apiClient.ts`
|
|
55
|
+
|
|
56
|
+
## 🚀 扩展参数机制
|
|
57
|
+
|
|
58
|
+
### 概述
|
|
59
|
+
|
|
60
|
+
Hono 客户端支持通过 `extend` 参数传递自定义选项,用于控制请求行为。
|
|
61
|
+
|
|
62
|
+
### 数据流
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
用户调用
|
|
66
|
+
↓
|
|
67
|
+
$get(undefined, { extend: { loading: '加载中...' } })
|
|
68
|
+
↓
|
|
69
|
+
Hono 内部透传
|
|
70
|
+
↓
|
|
71
|
+
fetch(url, { extend: { loading: '加载中...' } })
|
|
72
|
+
↓
|
|
73
|
+
自定义 fetch 处理
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 定义扩展参数类型
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// src/admin/services/types.ts
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 管理后台请求扩展参数
|
|
83
|
+
*/
|
|
84
|
+
export interface AdminFetchExtendOptions {
|
|
85
|
+
/** Loading 控制:true 显示默认 loading,string 显示自定义文字 */
|
|
86
|
+
loading?: boolean | string
|
|
87
|
+
/** 重试次数 */
|
|
88
|
+
retry?: number
|
|
89
|
+
/** 重试延迟 */
|
|
90
|
+
retryDelay?: number
|
|
91
|
+
/** 静默错误,不显示错误提示 */
|
|
92
|
+
silentError?: boolean
|
|
93
|
+
/** 超时时间 */
|
|
94
|
+
timeout?: number
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 使用扩展参数
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
// 管理后台 - 完整功能
|
|
102
|
+
apiClient.api.users.$get(undefined, {
|
|
103
|
+
extend: {
|
|
104
|
+
loading: '加载中...', // 自定义 loading 文字
|
|
105
|
+
retry: 3, // 重试次数
|
|
106
|
+
silentError: true, // 静默错误
|
|
107
|
+
timeout: 5000, // 超时时间
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
// CLI RPC - 简单功能
|
|
112
|
+
rpcClient.api.users.$get(undefined, {
|
|
113
|
+
extend: {
|
|
114
|
+
verbose: true, // 详细日志
|
|
115
|
+
timeout: 10000, // 超时时间
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## 🔌 自定义 Fetch 实现
|
|
121
|
+
|
|
122
|
+
### 创建自定义 Fetch
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
// src/admin/services/requestInterceptor.ts
|
|
126
|
+
import type { AdminFetchExtendOptions, InterceptorCallbacks } from './types'
|
|
127
|
+
|
|
128
|
+
export class RequestInterceptor {
|
|
129
|
+
constructor(private callbacks: InterceptorCallbacks) {}
|
|
130
|
+
|
|
131
|
+
async intercept(
|
|
132
|
+
url: string,
|
|
133
|
+
init: RequestInit & { extend?: AdminFetchExtendOptions }
|
|
134
|
+
): Promise<Response> {
|
|
135
|
+
const extend = init.extend
|
|
136
|
+
|
|
137
|
+
// 1. 触发请求开始回调
|
|
138
|
+
this.callbacks.onRequest?.(extend)
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
// 2. 添加认证 token
|
|
142
|
+
const headers = new Headers(init.headers)
|
|
143
|
+
const token = this.getStoredToken()
|
|
144
|
+
if (token) {
|
|
145
|
+
headers.set('Authorization', `Bearer ${token}`)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 3. 移除 extend 属性,避免传递给原生 fetch
|
|
149
|
+
const { extend: _extend, ...restInit } = init
|
|
150
|
+
|
|
151
|
+
// 4. 发送请求
|
|
152
|
+
const response = await window.fetch(url, {
|
|
153
|
+
...restInit,
|
|
154
|
+
headers,
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
// 5. 触发响应回调
|
|
158
|
+
this.callbacks.onResponse?.(extend)
|
|
159
|
+
return response
|
|
160
|
+
} catch (error) {
|
|
161
|
+
// 6. 触发错误回调
|
|
162
|
+
this.callbacks.onError?.(extend)
|
|
163
|
+
throw error
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private getStoredToken(): string | null {
|
|
168
|
+
// 从 localStorage 获取 token
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function createRequestInterceptor(callbacks: InterceptorCallbacks) {
|
|
173
|
+
const interceptor = new RequestInterceptor(callbacks)
|
|
174
|
+
return (url: string, init: RequestInit & { extend?: AdminFetchExtendOptions }) =>
|
|
175
|
+
interceptor.intercept(url, init)
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 配置自定义 Fetch
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
// src/admin/services/apiClient.ts
|
|
183
|
+
import { createRequestInterceptor } from './requestInterceptor'
|
|
184
|
+
import type { AdminFetchExtendOptions } from './types'
|
|
185
|
+
import { useLoadingStore } from '../stores/loadingStore'
|
|
186
|
+
|
|
187
|
+
function createCustomFetch() {
|
|
188
|
+
const { startLoading, stopLoading } = useLoadingStore.getState()
|
|
189
|
+
|
|
190
|
+
return createRequestInterceptor({
|
|
191
|
+
onShowLogin: () => {
|
|
192
|
+
localStorage.removeItem('admin-storage')
|
|
193
|
+
window.location.href = '/admin/login'
|
|
194
|
+
},
|
|
195
|
+
onShowCaptcha: async config => {
|
|
196
|
+
return showCaptcha({ type: config.type })
|
|
197
|
+
},
|
|
198
|
+
onRequest: (extend?: AdminFetchExtendOptions) => {
|
|
199
|
+
if (extend?.loading !== false) {
|
|
200
|
+
const text = typeof extend?.loading === 'string' ? extend.loading : undefined
|
|
201
|
+
startLoading(text)
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
onResponse: (extend?: AdminFetchExtendOptions) => {
|
|
205
|
+
if (extend?.loading !== false) {
|
|
206
|
+
stopLoading()
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
onError: (extend?: AdminFetchExtendOptions) => {
|
|
210
|
+
if (extend?.loading !== false) {
|
|
211
|
+
stopLoading()
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export const apiClient = hc<AppType>(baseUrl, {
|
|
218
|
+
fetch: createCustomFetch() as typeof fetch,
|
|
219
|
+
webSocket: url => new WSClientImpl(url),
|
|
220
|
+
sse: url => new SSEClientImpl(url, headers),
|
|
221
|
+
})
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## 📋 不同客户端的扩展参数
|
|
225
|
+
|
|
226
|
+
| 客户端 | 扩展参数 | 说明 |
|
|
227
|
+
| ---------- | -------------------------------------------- | -------------------- |
|
|
228
|
+
| 前端客户端 | 无 | 基础功能,不需要扩展 |
|
|
229
|
+
| 管理后台 | `loading`, `retry`, `silentError`, `timeout` | 完整功能 |
|
|
230
|
+
| CLI RPC | `verbose`, `timeout` | 简单功能 |
|
|
231
|
+
|
|
232
|
+
## 🚫 禁止直接使用 fetch
|
|
233
|
+
|
|
234
|
+
### 禁止事项
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
// ❌ 禁止 - 直接使用 fetch
|
|
238
|
+
const response = await fetch('/api/items')
|
|
239
|
+
const data = await response.json()
|
|
240
|
+
|
|
241
|
+
// ❌ 禁止 - 直接使用 new WebSocket()
|
|
242
|
+
const ws = new WebSocket('ws://localhost:3000/api/chat/ws')
|
|
243
|
+
|
|
244
|
+
// ❌ 禁止 - 直接使用 new EventSource()
|
|
245
|
+
const sse = new EventSource('/api/notifications/sse')
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**ESLint 规则**: `no-direct-ws-sse`, `no-restricted-globals`
|
|
249
|
+
|
|
250
|
+
### 正确使用方式
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
// ✅ 正确 - 使用 apiClient
|
|
254
|
+
import { apiClient } from '@client/services/apiClient'
|
|
255
|
+
|
|
256
|
+
// REST API 调用
|
|
257
|
+
const response = await apiClient.api.items.$get()
|
|
258
|
+
const result = await response.json()
|
|
259
|
+
|
|
260
|
+
if (result.success) {
|
|
261
|
+
console.log(result.data)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// WebSocket 连接
|
|
265
|
+
const wsClient = apiClient.api.chat.ws.$ws()
|
|
266
|
+
|
|
267
|
+
// SSE 连接
|
|
268
|
+
const sseClient = await apiClient.api.notifications.sse.$sse()
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## 🔌 WebSocket 客户端使用
|
|
272
|
+
|
|
273
|
+
### 获取 WebSocket 客户端
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
import { apiClient } from '@client/services/apiClient'
|
|
277
|
+
import type { WSClient, ChatProtocol } from '@shared/schemas'
|
|
278
|
+
|
|
279
|
+
const wsClient: WSClient<ChatProtocol> = apiClient.api.chat.ws.$ws()
|
|
280
|
+
|
|
281
|
+
// 连接状态监听
|
|
282
|
+
wsClient.onStatusChange(status => {
|
|
283
|
+
console.log('WebSocket status:', status)
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
// RPC 调用
|
|
287
|
+
const result = await wsClient.call('echo', { message: 'Hello' })
|
|
288
|
+
|
|
289
|
+
// 事件监听
|
|
290
|
+
wsClient.on('notification', payload => {
|
|
291
|
+
console.log('Notification:', payload)
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
// 发送事件
|
|
295
|
+
wsClient.emit('broadcast', { message: 'Hello', timestamp: Date.now() })
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
**ESLint 规则**: `protect-ws-sse-interface`
|
|
299
|
+
|
|
300
|
+
**路径**: `src/client/**/*.ts`, `src/client/**/*.tsx`
|
|
301
|
+
|
|
302
|
+
## 📡 SSE 客户端使用
|
|
303
|
+
|
|
304
|
+
### 获取 SSE 客户端
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
import { apiClient } from '@client/services/apiClient'
|
|
308
|
+
import type { SSEClient, AppSSEProtocol } from '@shared/schemas'
|
|
309
|
+
|
|
310
|
+
const sseClient: SSEClient<AppSSEProtocol> = await apiClient.api.notifications.sse.$sse()
|
|
311
|
+
|
|
312
|
+
// 连接状态监听
|
|
313
|
+
sseClient.onStatusChange(status => {
|
|
314
|
+
console.log('SSE status:', status)
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
// 事件监听
|
|
318
|
+
sseClient.on('notification', payload => {
|
|
319
|
+
console.log('Notification:', payload)
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
// 关闭连接
|
|
323
|
+
sseClient.abort()
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**ESLint 规则**: `protect-ws-sse-interface`
|
|
327
|
+
|
|
328
|
+
**路径**: `src/client/**/*.ts`, `src/client/**/*.tsx`
|
|
329
|
+
|
|
330
|
+
## 🖼️ 媒体类型方法
|
|
331
|
+
|
|
332
|
+
### 图片获取 ($image)
|
|
333
|
+
|
|
334
|
+
用于获取 `image/*` 类型的资源,返回 `Promise<Blob>`。
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
import { apiClient } from '@client/services/apiClient'
|
|
338
|
+
|
|
339
|
+
// 获取图片
|
|
340
|
+
const blob = await apiClient.api.admin.avatar[':id'].$image({
|
|
341
|
+
param: { id: 'user-123' },
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
// 创建图片 URL
|
|
345
|
+
const imageUrl = URL.createObjectURL(blob)
|
|
346
|
+
document.querySelector('img').src = imageUrl
|
|
347
|
+
|
|
348
|
+
// 记得释放 URL
|
|
349
|
+
URL.revokeObjectURL(imageUrl)
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**支持的 Content-Type**:
|
|
353
|
+
|
|
354
|
+
- `image/png`
|
|
355
|
+
- `image/jpeg`
|
|
356
|
+
- `image/gif`
|
|
357
|
+
- `image/webp`
|
|
358
|
+
- 其他 `image/*` 类型
|
|
359
|
+
|
|
360
|
+
### SVG 获取 ($svg)
|
|
361
|
+
|
|
362
|
+
用于获取 `image/svg+xml` 类型的资源,返回 `Promise<string>`。
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
import { apiClient } from '@client/services/apiClient'
|
|
366
|
+
|
|
367
|
+
// 获取 SVG 图标
|
|
368
|
+
const svgString = await apiClient.api.admin.icon[':name'].$svg({
|
|
369
|
+
param: { name: 'home' },
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
// 直接插入 DOM
|
|
373
|
+
document.querySelector('#icon-container').innerHTML = svgString
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
**支持的 Content-Type**:
|
|
377
|
+
|
|
378
|
+
- `image/svg+xml`
|
|
379
|
+
|
|
380
|
+
### 文件下载 ($download)
|
|
381
|
+
|
|
382
|
+
用于下载文件(Excel, PDF, ZIP 等),返回 `Promise<Blob>`。
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
import { apiClient } from '@client/services/apiClient'
|
|
386
|
+
|
|
387
|
+
// 下载文件
|
|
388
|
+
const blob = await apiClient.api.admin.todos.export.$download()
|
|
389
|
+
|
|
390
|
+
// 触发浏览器下载
|
|
391
|
+
const url = URL.createObjectURL(blob)
|
|
392
|
+
const a = document.createElement('a')
|
|
393
|
+
a.href = url
|
|
394
|
+
a.download = 'todos.csv'
|
|
395
|
+
a.click()
|
|
396
|
+
URL.revokeObjectURL(url)
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
**支持的 Content-Type**:
|
|
400
|
+
|
|
401
|
+
- `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` (Excel)
|
|
402
|
+
- `application/vnd.ms-excel` (Excel 97-2003)
|
|
403
|
+
- `application/pdf`
|
|
404
|
+
- `application/zip`
|
|
405
|
+
- `text/csv`
|
|
406
|
+
- 其他 `application/*` 类型
|
|
407
|
+
|
|
408
|
+
### 服务端定义示例
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
// src/server/module-admin/routes/admin-routes.ts
|
|
412
|
+
import { createRoute } from '@hono/zod-openapi'
|
|
413
|
+
import { z } from '@hono/zod-openapi'
|
|
414
|
+
|
|
415
|
+
// 图片路由
|
|
416
|
+
const getAvatarRoute = createRoute({
|
|
417
|
+
method: 'get',
|
|
418
|
+
path: '/admin/avatar/:id',
|
|
419
|
+
responses: {
|
|
420
|
+
200: {
|
|
421
|
+
content: {
|
|
422
|
+
'image/png': { schema: z.any().openapi({ type: 'string', format: 'binary' }) },
|
|
423
|
+
'image/jpeg': { schema: z.any().openapi({ type: 'string', format: 'binary' }) },
|
|
424
|
+
},
|
|
425
|
+
description: 'User avatar image',
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
// SVG 路由
|
|
431
|
+
const getIconRoute = createRoute({
|
|
432
|
+
method: 'get',
|
|
433
|
+
path: '/admin/icon/:name',
|
|
434
|
+
responses: {
|
|
435
|
+
200: {
|
|
436
|
+
content: {
|
|
437
|
+
'image/svg+xml': { schema: z.string() },
|
|
438
|
+
},
|
|
439
|
+
description: 'SVG icon',
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
// 文件下载路由
|
|
445
|
+
const exportTodosRoute = createRoute({
|
|
446
|
+
method: 'get',
|
|
447
|
+
path: '/admin/todos/export',
|
|
448
|
+
responses: {
|
|
449
|
+
200: {
|
|
450
|
+
content: {
|
|
451
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': {
|
|
452
|
+
schema: z.any().openapi({ type: 'string', format: 'binary' }),
|
|
453
|
+
},
|
|
454
|
+
'text/csv': { schema: z.string() },
|
|
455
|
+
},
|
|
456
|
+
description: 'Export todos as Excel or CSV',
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
})
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
**注意**: 对于二进制类型,使用 `z.any().openapi({ type: 'string', format: 'binary' })` 而不是 `z.instanceof(Blob)`。
|
|
463
|
+
|
|
464
|
+
## 📤 文件上传 (form)
|
|
465
|
+
|
|
466
|
+
### 客户端上传文件
|
|
467
|
+
|
|
468
|
+
使用 `form` 属性上传文件,Hono RPC 客户端会自动处理 `multipart/form-data`。
|
|
469
|
+
|
|
470
|
+
```typescript
|
|
471
|
+
import { apiClient } from '@client/services/apiClient'
|
|
472
|
+
|
|
473
|
+
// 上传单个文件
|
|
474
|
+
const response = await apiClient.api.todos[':id'].attachments.$post({
|
|
475
|
+
param: { id: '123' },
|
|
476
|
+
form: { file },
|
|
477
|
+
})
|
|
478
|
+
|
|
479
|
+
const result = await response.json()
|
|
480
|
+
if (result.success) {
|
|
481
|
+
console.log('File uploaded:', result.data)
|
|
482
|
+
}
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### 服务端定义
|
|
486
|
+
|
|
487
|
+
```typescript
|
|
488
|
+
// src/server/module-todos/routes/todos-routes.ts
|
|
489
|
+
import { createRoute, z } from '@hono/zod-openapi'
|
|
490
|
+
|
|
491
|
+
const UploadFileSchema = z.object({
|
|
492
|
+
file: z.any().openapi({ type: 'string', format: 'binary' }),
|
|
493
|
+
})
|
|
494
|
+
|
|
495
|
+
const uploadAttachmentRoute = createRoute({
|
|
496
|
+
method: 'post',
|
|
497
|
+
path: '/todos/{id}/attachments',
|
|
498
|
+
request: {
|
|
499
|
+
params: z.object({ id: z.string() }),
|
|
500
|
+
body: {
|
|
501
|
+
content: {
|
|
502
|
+
'multipart/form-data': {
|
|
503
|
+
schema: UploadFileSchema,
|
|
504
|
+
},
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
},
|
|
508
|
+
responses: {
|
|
509
|
+
201: successResponse(AttachmentSchema, 'File uploaded'),
|
|
510
|
+
400: errorResponse('Invalid file'),
|
|
511
|
+
},
|
|
512
|
+
})
|
|
513
|
+
// Handler 中获取文件
|
|
514
|
+
.openapi(uploadAttachmentRoute, async c => {
|
|
515
|
+
const { id } = c.req.valid('param')
|
|
516
|
+
const body = await c.req.valid('form')
|
|
517
|
+
const file = body['file']
|
|
518
|
+
|
|
519
|
+
if (!file || !(file instanceof File)) {
|
|
520
|
+
return c.json({ success: false, error: 'No file uploaded' }, 400)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// 处理文件...
|
|
524
|
+
const attachment = await uploadAttachment(parseInt(id), file)
|
|
525
|
+
return c.json({ success: true, data: attachment }, 201)
|
|
526
|
+
})
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
### 前端组件示例
|
|
530
|
+
|
|
531
|
+
```tsx
|
|
532
|
+
import { useRef } from 'react'
|
|
533
|
+
import { useTodoStore } from '../stores/todoStore'
|
|
534
|
+
|
|
535
|
+
function FileUploadButton({ todoId }: { todoId: number }) {
|
|
536
|
+
const fileInputRef = useRef<HTMLInputElement>(null)
|
|
537
|
+
const uploadAttachment = useTodoStore(state => state.uploadAttachment)
|
|
538
|
+
|
|
539
|
+
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
540
|
+
const file = e.target.files?.[0]
|
|
541
|
+
if (file) {
|
|
542
|
+
await uploadAttachment(todoId, file)
|
|
543
|
+
e.target.value = '' // 重置 input
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return (
|
|
548
|
+
<label className="cursor-pointer">
|
|
549
|
+
<Upload className="w-4 h-4" />
|
|
550
|
+
Upload
|
|
551
|
+
<input
|
|
552
|
+
ref={fileInputRef}
|
|
553
|
+
type="file"
|
|
554
|
+
accept=".jpg,.jpeg,.png,.pdf,.txt"
|
|
555
|
+
onChange={handleFileChange}
|
|
556
|
+
className="hidden"
|
|
557
|
+
/>
|
|
558
|
+
</label>
|
|
559
|
+
)
|
|
560
|
+
}
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
### 文件上传限制
|
|
564
|
+
|
|
565
|
+
服务端应验证文件大小和类型:
|
|
566
|
+
|
|
567
|
+
```typescript
|
|
568
|
+
// 服务端验证
|
|
569
|
+
const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10MB
|
|
570
|
+
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'application/pdf']
|
|
571
|
+
|
|
572
|
+
if (file.size > MAX_FILE_SIZE) {
|
|
573
|
+
return c.json({ success: false, error: 'File too large' }, 400)
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (!ALLOWED_TYPES.includes(file.type)) {
|
|
577
|
+
return c.json({ success: false, error: 'File type not allowed' }, 400)
|
|
578
|
+
}
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
### 注意事项
|
|
582
|
+
|
|
583
|
+
| 事项 | 说明 |
|
|
584
|
+
| ---------------- | ------------------------------------------------------------ |
|
|
585
|
+
| 使用 `form` 属性 | 不要使用 `body` 属性,Hono 会自动处理 FormData |
|
|
586
|
+
| 类型定义 | 使用 `z.any().openapi({ type: 'string', format: 'binary' })` |
|
|
587
|
+
| 服务端获取 | 使用 `c.req.valid('form')` 获取文件 |
|
|
588
|
+
| 禁止直接 fetch | 必须使用 apiClient RPC 方式 |
|
|
589
|
+
|
|
590
|
+
## 🌊 流式响应处理
|
|
591
|
+
|
|
592
|
+
对于大数据量的导出场景,可以使用流式响应来减少内存占用。
|
|
593
|
+
|
|
594
|
+
### 服务端实现
|
|
595
|
+
|
|
596
|
+
```typescript
|
|
597
|
+
import { createRoute } from '@hono/zod-openapi'
|
|
598
|
+
|
|
599
|
+
const exportStreamRoute = createRoute({
|
|
600
|
+
method: 'get',
|
|
601
|
+
path: '/admin/todos/export/stream',
|
|
602
|
+
responses: {
|
|
603
|
+
200: {
|
|
604
|
+
content: {
|
|
605
|
+
'text/csv': { schema: z.string() },
|
|
606
|
+
},
|
|
607
|
+
description: 'Stream export todos as CSV',
|
|
608
|
+
},
|
|
609
|
+
},
|
|
610
|
+
})
|
|
611
|
+
// Handler
|
|
612
|
+
.openapi(exportStreamRoute, async _c => {
|
|
613
|
+
const todos = await adminService.getAllTodos()
|
|
614
|
+
|
|
615
|
+
const stream = new ReadableStream({
|
|
616
|
+
async start(controller) {
|
|
617
|
+
// 发送 CSV 头
|
|
618
|
+
controller.enqueue('id,title,completed,created_at\n')
|
|
619
|
+
|
|
620
|
+
// 逐行发送数据
|
|
621
|
+
for (const todo of todos) {
|
|
622
|
+
const line = `${todo.id},"${todo.title}",${todo.completed},${todo.createdAt}\n`
|
|
623
|
+
controller.enqueue(line)
|
|
624
|
+
|
|
625
|
+
// 模拟慢速导出(可选)
|
|
626
|
+
await new Promise(resolve => setTimeout(resolve, 100))
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
controller.close()
|
|
630
|
+
},
|
|
631
|
+
})
|
|
632
|
+
|
|
633
|
+
return new Response(stream, {
|
|
634
|
+
headers: {
|
|
635
|
+
'Content-Type': 'text/csv',
|
|
636
|
+
'Content-Disposition': 'attachment; filename="todos.csv"',
|
|
637
|
+
},
|
|
638
|
+
})
|
|
639
|
+
})
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
### 客户端处理
|
|
643
|
+
|
|
644
|
+
```typescript
|
|
645
|
+
import { apiClient } from '@client/services/apiClient'
|
|
646
|
+
|
|
647
|
+
// 获取流式响应
|
|
648
|
+
const response = await apiClient.api.admin.todos.export.stream.$get()
|
|
649
|
+
|
|
650
|
+
// 使用 ReadableStream 读取数据
|
|
651
|
+
const reader = response.body?.getReader()
|
|
652
|
+
if (!reader) {
|
|
653
|
+
throw new Error('No response body')
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
const decoder = new TextDecoder()
|
|
657
|
+
let csvContent = ''
|
|
658
|
+
|
|
659
|
+
while (true) {
|
|
660
|
+
const { done, value } = await reader.read()
|
|
661
|
+
if (done) break
|
|
662
|
+
|
|
663
|
+
// 处理每个数据块
|
|
664
|
+
csvContent += decoder.decode(value)
|
|
665
|
+
console.log('Received chunk:', value.length, 'bytes')
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// 创建下载
|
|
669
|
+
const blob = new Blob([csvContent], { type: 'text/csv' })
|
|
670
|
+
const url = URL.createObjectURL(blob)
|
|
671
|
+
const a = document.createElement('a')
|
|
672
|
+
a.href = url
|
|
673
|
+
a.download = 'todos.csv'
|
|
674
|
+
a.click()
|
|
675
|
+
URL.revokeObjectURL(url)
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
### 实时进度显示
|
|
679
|
+
|
|
680
|
+
```typescript
|
|
681
|
+
const reader = response.body?.getReader()
|
|
682
|
+
const contentLength = parseInt(response.headers.get('Content-Length') || '0')
|
|
683
|
+
let receivedLength = 0
|
|
684
|
+
|
|
685
|
+
while (true) {
|
|
686
|
+
const { done, value } = await reader.read()
|
|
687
|
+
if (done) break
|
|
688
|
+
|
|
689
|
+
receivedLength += value.length
|
|
690
|
+
const progress = contentLength > 0 ? Math.round((receivedLength / contentLength) * 100) : 0
|
|
691
|
+
|
|
692
|
+
console.log(`Progress: ${progress}%`)
|
|
693
|
+
}
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
### 使用场景
|
|
697
|
+
|
|
698
|
+
| 场景 | 推荐方式 |
|
|
699
|
+
| -------------- | ------------- |
|
|
700
|
+
| 小文件 (< 1MB) | `$download()` |
|
|
701
|
+
| 大文件 (> 1MB) | 流式响应 |
|
|
702
|
+
| 实时进度显示 | 流式响应 |
|
|
703
|
+
| 大数据量导出 | 流式响应 |
|
|
704
|
+
|
|
705
|
+
**注意**: 流式响应使用 `$get()` 方法,返回 `ClientResponse`,其 `body` 属性是 `ReadableStream | null`。
|
|
706
|
+
|
|
707
|
+
## 🛡️ 框架保护
|
|
708
|
+
|
|
709
|
+
`apiClient.ts` 是框架层文件,带有 `@framework-baseline` 注释:
|
|
710
|
+
|
|
711
|
+
```typescript
|
|
712
|
+
/**
|
|
713
|
+
* @framework-baseline ab16e97716a7556e
|
|
714
|
+
*
|
|
715
|
+
* 此文件属于框架层代码。如需修改,请添加以下说明:
|
|
716
|
+
* @framework-modify
|
|
717
|
+
* @reason [必填] 修改原因
|
|
718
|
+
* @impact [必填] 影响范围
|
|
719
|
+
*/
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
**ESLint 规则**: `framework-protect`
|
|
723
|
+
|
|
724
|
+
## 📤 导出规范
|
|
725
|
+
|
|
726
|
+
```typescript
|
|
727
|
+
// ✅ 正确 - 导出 apiClient
|
|
728
|
+
export const apiClient = hc<AppType>(baseUrl, { ... })
|
|
729
|
+
|
|
730
|
+
// ❌ 错误 - 导出多个客户端实例
|
|
731
|
+
export const apiClient1 = hc<AppType>(url1)
|
|
732
|
+
export const apiClient2 = hc<AppType>(url2)
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
## 🚫 Anti-Patterns
|
|
736
|
+
|
|
737
|
+
```typescript
|
|
738
|
+
// ❌ 不要直接使用 fetch
|
|
739
|
+
const response = await fetch('/api/items')
|
|
740
|
+
|
|
741
|
+
// ✅ 应该使用 apiClient
|
|
742
|
+
const response = await apiClient.api.items.$get()
|
|
743
|
+
|
|
744
|
+
// ❌ 不要直接使用 new WebSocket()
|
|
745
|
+
const ws = new WebSocket('ws://localhost:3000/api/chat/ws')
|
|
746
|
+
|
|
747
|
+
// ✅ 应该使用 apiClient
|
|
748
|
+
const wsClient = apiClient.api.chat.ws.$ws()
|
|
749
|
+
|
|
750
|
+
// ❌ 不要直接使用 new EventSource()
|
|
751
|
+
const sse = new EventSource('/api/notifications/sse')
|
|
752
|
+
|
|
753
|
+
// ✅ 应该使用 apiClient
|
|
754
|
+
const sseClient = await apiClient.api.notifications.sse.$sse()
|
|
755
|
+
|
|
756
|
+
// ❌ 不要在组件中创建客户端实例
|
|
757
|
+
const client = hc<AppType>(baseUrl)
|
|
758
|
+
|
|
759
|
+
// ✅ 应该使用统一的 apiClient
|
|
760
|
+
import { apiClient } from '@client/services/apiClient'
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
## 📚 相关文档
|
|
764
|
+
|
|
765
|
+
- [API 类型推导规范](./10-api-type-inference.md) - Hono RPC 类型推导
|
|
766
|
+
- [WebSocket 规范](./50-websocket.md) - WebSocket 开发规范
|
|
767
|
+
- [SSE 规范](./51-sse.md) - SSE 开发规范
|
|
768
|
+
- [Client 组件规范](./30-client-components.md) - 组件开发规范
|