|
|
@@ -57,14 +57,13 @@ curl -X POST http://localhost:3000/api/v1/auth/register/simple \
|
|
|
|
|
|
#### JavaScript/TypeScript示例
|
|
|
```typescript
|
|
|
-// 使用fetch API
|
|
|
+import { authClient } from '@/client/api';
|
|
|
+import type { InferResponseType, InferRequestType } from 'hono/client';
|
|
|
+
|
|
|
+// 使用RPC客户端
|
|
|
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) {
|
|
|
@@ -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 {
|
|
|
const result = await registerSimple('myusername');
|
|
|
@@ -90,6 +93,7 @@ try {
|
|
|
#### React示例
|
|
|
```typescript
|
|
|
import { useState } from 'react';
|
|
|
+import { authClient } from '@/client/api';
|
|
|
|
|
|
const SimpleRegisterForm = () => {
|
|
|
const [username, setUsername] = useState('');
|
|
|
@@ -100,10 +104,8 @@ const SimpleRegisterForm = () => {
|
|
|
setLoading(true);
|
|
|
|
|
|
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) {
|
|
|
@@ -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. **自动用户名处理**:
|