Browse Source

♻️ refactor(api): 重构API客户端配置和路径处理

- 移除API_VERSION常量及相关BASE_URL构建代码
- 修改createRpcClient函数,直接使用API_BASE_URL作为基础路径
- 更新各客户端初始化方式,使用嵌套路径访问形式:`.api.v1.{resource}`
- 删除未使用的401错误处理代码块和响应适配代码
- 移除默认导出的客户端对象及类型定义导出
yourname 4 months ago
parent
commit
8f17eb6a30
1 changed files with 6 additions and 47 deletions
  1. 6 47
      mini/src/utils/api.ts

+ 6 - 47
mini/src/utils/api.ts

@@ -4,10 +4,9 @@ import type { AuthRoutes, UserRoutes, RoleRoutes, FileRoutes } from '@/server/ap
 
 
 // API配置
 // API配置
 const API_BASE_URL = process.env.TARO_APP_API_BASE_URL || 'http://localhost:3000'
 const API_BASE_URL = process.env.TARO_APP_API_BASE_URL || 'http://localhost:3000'
-const API_VERSION = process.env.TARO_APP_API_VERSION || 'v1'
 
 
 // 完整的API地址
 // 完整的API地址
-const BASE_URL = `${API_BASE_URL}/api/${API_VERSION}`
+// const BASE_URL = `${API_BASE_URL}/api/${API_VERSION}`
 
 
 // 创建自定义fetch函数,适配Taro.request
 // 创建自定义fetch函数,适配Taro.request
 const taroFetch: typeof fetch = async (input, init) => {
 const taroFetch: typeof fetch = async (input, init) => {
@@ -36,30 +35,6 @@ const taroFetch: typeof fetch = async (input, init) => {
 
 
   try {
   try {
     const response = await Taro.request(options)
     const response = await Taro.request(options)
-    
-    // // 处理401未授权
-    // if (response.statusCode === 401) {
-    //   Taro.removeStorageSync('token')
-    //   Taro.removeStorageSync('userInfo')
-    //   Taro.navigateTo({ url: '/pages/login/index' })
-    //   throw new Error('请重新登录')
-    // }
-
-    // 适配fetch响应格式
-    // return {
-    //   ok: response.statusCode >= 200 && response.statusCode < 300,
-    //   status: response.statusCode,
-    //   statusText: response.errMsg || 'OK',
-    //   headers: new Headers(response.header || {}),
-    //   url: response.data?.url || url,
-    //   json: async () => response.data,
-    //   text: async () => JSON.stringify(response.data),
-    //   blob: async () => new Blob([JSON.stringify(response.data)]),
-    //   arrayBuffer: async () => new TextEncoder().encode(JSON.stringify(response.data)),
-    //   clone: () => ({}) as Response,
-    //   body: null as any,
-    //   bodyUsed: true
-    // } as Response
 
 
     const responseHeaders = new Headers();
     const responseHeaders = new Headers();
     if (response.header) {
     if (response.header) {
@@ -95,29 +70,13 @@ const taroFetch: typeof fetch = async (input, init) => {
 
 
 // 创建Hono RPC客户端
 // 创建Hono RPC客户端
 const createRpcClient = <T>(basePath: string) => {
 const createRpcClient = <T>(basePath: string) => {
-  return hc<T>(`${BASE_URL}${basePath}`, {
+  return hc<T>(`${API_BASE_URL}`, {
     fetch: taroFetch
     fetch: taroFetch
   })
   })
 }
 }
 
 
 // 创建各个模块的RPC客户端
 // 创建各个模块的RPC客户端
-export const authClient = createRpcClient<AuthRoutes>('/auth')
-export const userClient = createRpcClient<UserRoutes>('/users')
-export const roleClient = createRpcClient<RoleRoutes>('/roles')
-export const fileClient = createRpcClient<FileRoutes>('/files')
-
-// 类型定义
-export type {
-  AuthRoutes,
-  UserRoutes,
-  RoleRoutes,
-  FileRoutes
-}
-
-// 默认导出RPC客户端
-export default {
-  auth: authClient,
-  users: userClient,
-  roles: roleClient,
-  files: fileClient,
-}
+export const authClient = createRpcClient<AuthRoutes>('/').api.v1.auth
+export const userClient = createRpcClient<UserRoutes>('/').api.v1.users
+export const roleClient = createRpcClient<RoleRoutes>('/').api.v1.roles
+export const fileClient = createRpcClient<FileRoutes>('/').api.v1.files