@gopowerteam/http-request-cli

0.4.22 • Public • Published

@gopowerteam/http-request-cli


npm version install size

基于@gopowerteam/http-request的接口请求配置代码生成器

目录


安装


// yarn use
# yarn add @gopowerteam/http-request-cli

// npm use
# npm install @gopowerteam/http-request-cli --save

示例


在根目录下创建http-request-cli.config.js配置文件

module.exports = {
  "gateway": "http://gateway.xxx.com",
  "swagger:": "v2/api-docs",
  "controllerDir": {
    "alias": "../controller",   // 控制器目录名别
    "path": "./src/controller"  // 控制器目录路径
  },
  "serviceDir": {
    "alias": "@/http/services", // 服务目录名别
    "path": "./src/services" // 服务目录名别
  }
}
# npx http-request-cli -g 
// OR
# npx http-request-cli --generate

执行后会在对应目录下生成配置文件,之后按照@gopowerteam/http-request文档中配置使用即可.

配置

名称 描述 必填
gateway 服务器/网管地址
swagger SwaggerJSON Api 地址
controllerDir.alias 接口controller目录别名配置
controllerDir.path 接口controller目录路径配置
serviceDir.alias 接口service目录别名配置
serviceDir.path 接口service目录路径配置
services 多服务配置
controllerResolver 自定义Contrller解析

多服务配置需要添加服务数组services

services:{
    "service-1":"service-1",
    "service-2":"service-2",
    "service-3":"service-3"
}

// key为生成service目录名称
// value为请求调用的service名称

多网管按数组配置即可

module.exports = [
  {
    ...
  },
  {
    ...
  }
];

自定义ControllerResolver

默认情况下会通过path来分析对应的Controller名称来生成文件名和类名,在一些情况下如果不满足需要可以自定义ControllerResolver来解决

  controllerResolver(path, currentTag, tags) {
    const tag = tags.find((x) => x.name === currentTag[0]);
    return tag.description.replace(/\s/g, "").replace(/Controller$/, "");
  }

需要服务重命名也可以使用controllerResolver

controllerResolver: () => "New Name"

自定义ActionResolver

默认情况下会通过获取operationId来获取action名称,在一些情况下如果不满足需要可以自定义ActionResolver来解决

  actionResolver(operationId, method, path) {
    return operationId.replace(/\_.*$/g, "");
  }

Vite插件支持

支持作为vite插件使用,这样可以不需要导入对应的service

vite.config.ts

import { requestPlugin } from '@gopowerteam/http-request-cli'


...
plugins: [
  requestPlugin({
        root: path.resolve(__dirname, 'src'),
        alias: '~',
        serviceDir: 'http/services',
        serviceDeclaration: 'typings/request.d.ts'
  })
]

配置插件后使用时如下即可:

import { useRequest } from 'virtual:http-request'

const posterService = useRequest(
  services => services.PosterService
)

posterService.create(new RequestParams(...))

Package Sidebar

Install

npm i @gopowerteam/http-request-cli

Weekly Downloads

6

Version

0.4.22

License

MIT

Unpacked Size

45.4 kB

Total Files

23

Last publish

Collaborators

  • zhuchentong
  • zct1989