0.4.22 • Published 2 years ago

@gopowerteam/http-request-cli v0.4.22

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@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服务器/网管地址
swaggerSwaggerJSON 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(...))
0.4.22

2 years ago

0.4.21

2 years ago

0.4.20

2 years ago

0.4.19

2 years ago

0.4.18

2 years ago

0.4.17

2 years ago

0.4.16

2 years ago

0.4.15

2 years ago

0.4.13

2 years ago

0.4.14

2 years ago

0.4.11

2 years ago

0.4.12

2 years ago

0.4.9

3 years ago

0.4.10

3 years ago

0.4.8

3 years ago

0.4.5

3 years ago

0.3.6

3 years ago

0.4.4

3 years ago

0.3.5

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.0

3 years ago

0.3.1

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago