@thatcompany/axios@thatcompany/axios
主页
  • 介绍
  • 效果
  • 引入
  • 请求类型
  • 功能类型
  • 基类
  • restful风格类
  • 服务规范
  • 日志服务
  • 安全服务
  • 通知服务
  • 文件服务
主页
  • 介绍
  • 效果
  • 引入
  • 请求类型
  • 功能类型
  • 基类
  • restful风格类
  • 服务规范
  • 日志服务
  • 安全服务
  • 通知服务
  • 文件服务
  • 功能装饰器

RequestParam

注解用于参数上

  • 用于实际请求路径的参数名。
  • 接收参数类型为string。
  • 可以理解为参数别名。
  • 为了防止后端参数名是前端关键字,或者取名习惯去后端不一致。

下面代码演示一个后端需要的参数是delete (delete是TS关键字)

@RequestMapping("/proxy/file")
class FileService {
    // 实际请求是 /proxy/file/download?delete=true&filePath=xxx
    @GetMapping("/download")
    @ResponseType("blob")
    async downloadFile(@RequestParam("delete") isDelete: boolean, filePath: string) {}
}

ResponseType

注解用于方法上

  • 用于指定响应类型。
  • 接受参数类型为AxiosResponseType => json、text、blob、arraybuffer、stream、document、formdata。
  • 默认响应类型为json。

指定响应类型为blob,则返回的响应类型为二进制数据,配合文件服务实现文件下载功能。

@RequestMapping("/proxy/file")
class FileService {
    @GetMapping("/download")
    @ResponseType("blob")
    async downloadFile(@RequestParam("delete") isDelete: boolean, filePath: string) {}
}

RequestHeaders

注解用于方法上

  • 用于指定请求头。
  • 接收参数类型为Object。
  • 可以指定多个请求头参数,如Content-Type、Authorization等。
@RequestMapping("/proxy/file")
class FileService {
  @PostMapping("/upload")
  @RequestHeaders({
      "Content-Type": "multipart/form-data", 
      "X-Custom-Header": "custom-value"
  })
   async uploadFile(file: File) {}
}

RequestConfig

注解用于方法上

  • 用于指定请求配置。
  • 接收参数类型为AxiosRequestConfig。
  • 可以指定追加的axios配置,如headers。

RequestLog

注解用于方法上

  • 用于记录请求日志。
  • 无需配置参数与括号。
  • 详情配置请参考日志服务。

在控制台打印的是默认收缩的请求日志组

img.png

打开内容如下:

img_1.png

RequestSecurityPermit

注解用于方法上

  • 用于指定请求安全许可。
  • 无需配置参数与括号。
  • 详情配置请参考安全服务。

放行这个方法的请求,不需要进行安全验证。如用在登录、注册等通用接口。

@RequestMapping("/login")
class LoginService {
    @PostMapping("")
    @RequestSecurityPermit
    async login(user: User) {}
}
Last Updated:
Contributors: 钟意