2
0
Просмотр исходного кода

📝 docs(auth): update authentication documentation with RPC client usage

- replace fetch API examples with authClient RPC calls for type-safe requests
- add RPC usage instructions highlighting type safety and code提示 benefits
- include type definition examples for request and response types
- update React component example to use RPC client
- add type imports for Hono client type inference utilities
yourname 6 месяцев назад
Родитель
Сommit
b90d292017
1 измененных файлов с 37 добавлено и 11 удалено
  1. 37 11
      .roo/commands/auth-使用简单注册路由.md

+ 37 - 11
.roo/commands/auth-使用简单注册路由.md

@@ -57,14 +57,13 @@ curl -X POST http://localhost:3000/api/v1/auth/register/simple \
 
 
 #### JavaScript/TypeScript示例
 #### JavaScript/TypeScript示例
 ```typescript
 ```typescript
-// 使用fetch API
+import { authClient } from '@/client/api';
+import type { InferResponseType, InferRequestType } from 'hono/client';
+
+// 使用RPC客户端
 const registerSimple = async (username: string) => {
 const registerSimple = async (username: string) => {
-  const response = await fetch('/api/v1/auth/register/simple', {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: JSON.stringify({ username }),
+  const response = await authClient.register.simple.$post({
+    json: { username }
   });
   });
   
   
   if (response.status === 201) {
   if (response.status === 201) {
@@ -77,6 +76,10 @@ const registerSimple = async (username: string) => {
   }
   }
 };
 };
 
 
+// 类型定义(可选)
+type SimpleRegisterRequest = InferRequestType<typeof authClient.register.simple.$post>['json'];
+type SimpleRegisterResponse = InferResponseType<typeof authClient.register.simple.$post, 201>;
+
 // 使用示例
 // 使用示例
 try {
 try {
   const result = await registerSimple('myusername');
   const result = await registerSimple('myusername');
@@ -90,6 +93,7 @@ try {
 #### React示例
 #### React示例
 ```typescript
 ```typescript
 import { useState } from 'react';
 import { useState } from 'react';
+import { authClient } from '@/client/api';
 
 
 const SimpleRegisterForm = () => {
 const SimpleRegisterForm = () => {
   const [username, setUsername] = useState('');
   const [username, setUsername] = useState('');
@@ -100,10 +104,8 @@ const SimpleRegisterForm = () => {
     setLoading(true);
     setLoading(true);
     
     
     try {
     try {
-      const response = await fetch('/api/v1/auth/register/simple', {
-        method: 'POST',
-        headers: { 'Content-Type': 'application/json' },
-        body: JSON.stringify({ username }),
+      const response = await authClient.register.simple.$post({
+        json: { username }
       });
       });
       
       
       if (response.ok) {
       if (response.ok) {
@@ -140,6 +142,30 @@ const SimpleRegisterForm = () => {
 };
 };
 ```
 ```
 
 
+### RPC使用说明
+
+本项目使用Hono RPC客户端进行类型安全的API调用,提供以下优势:
+
+1. **类型安全**: 自动推断请求和响应类型
+2. **代码提示**: IDE提供完整的代码补全和类型检查
+3. **错误处理**: 统一的错误处理机制
+4. **路径自动补全**: 自动生成正确的API路径
+
+#### 类型定义示例
+```typescript
+import type { InferResponseType, InferRequestType } from 'hono/client';
+import { authClient } from '@/client/api';
+
+// 请求类型定义
+type SimpleRegisterRequest = InferRequestType<typeof authClient.register.simple.$post>['json'];
+
+// 响应类型定义
+type SimpleRegisterResponse = InferResponseType<typeof authClient.register.simple.$post, 201>;
+
+// 错误响应类型
+type ErrorResponse = InferResponseType<typeof authClient.register.simple.$post, 400 | 500>;
+```
+
 ### 功能特点
 ### 功能特点
 
 
 1. **自动用户名处理**:
 1. **自动用户名处理**: