Skip to content
On this page

HTTP 基础设施

@grow-admin-rock/infrastructure 基于 Axios 封装 HTTP 请求,通过 IOC 注入 InfrastructureAxios 实例。宿主应用在 initIoc.ts 中绑定自定义 AxiosTransformInfrastructureOptions

架构

业务代码 / API 模块
        ↓ useRequest() 或 diKT(InfrastructureAxios)
InfrastructureAxios(拦截器链)
        ↓ AxiosTransform(宿主可扩展)
        ↓ checkStatus(401 等状态码处理)
后端 API

宿主扩展:GrowAxiosTransform

sample/src/apis/infrastructure.ts 演示如何继承 AxiosTransform

typescript
@Bean()
export class GrowAxiosTransform extends AxiosTransform {
  constructor(@Autowired(infrastructureLib.types.InfrastructureOptions) _options) {
    super()

    // 请求前:拼接 baseURL
    this.beforeRequestHook = (config, options) => {
      const { apiUrl } = options
      if (!config.baseURL && apiUrl) {
        config.baseURL = isString(apiUrl) ? apiUrl : ''
      }
      return config
    }

    // 响应后:统一解包 { data } / { result }
    this.transformRequestHook = (rawRes, options) => {
      const { data } = rawRes
      if (data.type === 'error') throw new Error(data.message || '请求失败')
      return data.data ?? data.result ?? data
    }
  }
}

initIoc.ts 中绑定:

typescript
appContext.iocModules.push(
  new AsyncIocModule(async (bind) => {
    bind(infrastructureLib.types.AxiosTransform).to(GrowAxiosTransform)
    bind(infrastructureLib.types.InfrastructureOptions).toDynamicValue(() => {
      const { apiUrl } = getGlobalConfig(import.meta.env)
      return { apiUrl }
    })
  }),
)

业务代码调用

typescript
import { useRequest } from '@/apis/infrastructure'

const request = useRequest()

// 示例:获取菜单列表(apps-home)
export function getMenuList() {
  return request.get({ url: '/api/menu/list' })
}

useRequest() 内部通过 diKT(infrastructureLib.types.InfrastructureAxios) 从 IOC 容器获取实例。

响应数据格式

GrowAxiosTransform 默认期望后端返回:

json
{
  "type": "success",
  "data": { ... },
  "message": "操作成功"
}

Mock 工具 resultSuccess / resultError@grow-admin-rock/mock/util)已按此格式封装,开发环境可直接对接。

字段说明
type: 'error'视为业务失败,抛出 message
data优先作为返回值
resultdata 不存在时的备选字段

请求选项

通过 RequestOptions@grow-admin-rock/types)控制单次请求行为:

选项说明
isReturnNativeResponse返回原始 Axios 响应
isTransformResponse是否走 transformRequestHook 解包
apiUrl覆盖 baseURL

错误处理与取消

能力位置说明
HTTP 状态码rock-infrastructure/src/checkStatus.ts401 等触发 InfrastructureOptions.onUnauthorized
请求取消RequestCanceler路由切换时可取消 pending 请求
全局 pendingprojectSetting.removeAllHttpPending切换路由时移除所有未完成请求

下一步