# @gopowerteam/http-request-cli

> ---

Latest version **0.4.22** (published 2022-10-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @gopowerteam/http-request-cli
pnpm add @gopowerteam/http-request-cli
yarn add @gopowerteam/http-request-cli
bun add @gopowerteam/http-request-cli
```

Provides the command `http-request-cli`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.22 |
| Published | 2022-10-08 |
| First published | 2021-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 44.3 KB |
| Known vulnerabilities | 0 (+12 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | zhuchentong |
| Maintainers | zct1989 |
| Keywords | gopowerteam, http-request, http-request-cli |

## Links

- npm: https://www.npmjs.com/package/@gopowerteam/http-request-cli
- Repository: https://github.com/gopowerteam/package.http-request
- Issues: https://github.com/gopowerteam/package.http-request/issues
- npm.io page: https://npm.io/package/@gopowerteam/http-request-cli

## Dependencies (6)

- [chalk](https://npm.io/package/chalk.md) 4.1.1
- [mkdirp](https://npm.io/package/mkdirp.md) 1.0.4
- [rimraf](https://npm.io/package/rimraf.md) 3.0.2
- [commander](https://npm.io/package/commander.md) 7.2.0
- [handlebars](https://npm.io/package/handlebars.md) 4.7.7
- [node-fetch](https://npm.io/package/node-fetch.md) 2.6.1

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.4.22 (latest) — 2022-10-08
- 0.4.21 — 2022-08-21
- 0.4.20 — 2022-08-16
- 0.4.19 — 2022-08-16
- 0.4.18 — 2022-08-16
- 0.4.17 — 2022-04-19
- 0.4.16 — 2022-03-16
- 0.4.15 — 2022-01-20
- 0.4.14 — 2022-01-18
- 0.4.13 — 2022-01-18
- 0.4.12 — 2022-01-17
- 0.4.11 — 2022-01-17
- 0.4.10 — 2021-10-11
- 0.4.9 — 2021-10-08
- 0.4.8 — 2021-09-23
- … 28 more at https://npm.io/package/@gopowerteam/http-request-cli/versions

## README

### @gopowerteam/http-request-cli
---

[![npm version](https://img.shields.io/npm/v/@gopowerteam/http-request-cli.svg?style=flat-square)](https://www.npmjs.org/package/@gopowerteam/http-request-cli)
[![install size](https://packagephobia.now.sh/badge?p=@gopowerteam/http-request-cli)](https://packagephobia.now.sh/result?p=@gopowerteam/http-request-cli)


基于`@gopowerteam/http-request`的接口请求配置代码生成器

## 目录
---

  - [安装](#安装)
  - [示例](#示例)
  - [配置](#配置)
  - [自定义ControllerResolver](#自定义ControllerResolver)
  - [自定义ActionResolver](#自定义ActionResolver)
  - [Vite插件支持](#Vite插件支持)
  

## 安装
---

```shell
// yarn use
# yarn add @gopowerteam/http-request-cli

// npm use
# npm install @gopowerteam/http-request-cli --save
```

## 示例
---

在根目录下创建`http-request-cli.config.js`配置文件

```javascript
module.exports = {
  "gateway": "http://gateway.xxx.com",
  "swagger:": "v2/api-docs",
  "controllerDir": {
    "alias": "../controller",   // 控制器目录名别
    "path": "./src/controller"  // 控制器目录路径
  },
  "serviceDir": {
    "alias": "@/http/services", // 服务目录名别
    "path": "./src/services" // 服务目录名别
  }
}

```

```shell
# npx http-request-cli -g 
// OR
# npx http-request-cli --generate
```

执行后会在对应目录下生成配置文件,之后按照[@gopowerteam/http-request](https://www.npmjs.com/package/@gopowerteam/http-request)文档中配置使用即可.

## 配置


|        名称         |            描述            | 必填  |
| :-----------------: | :------------------------: | :---: |
|       gateway       |      服务器/网管地址       |  是   |
|       swagger       |    SwaggerJSON Api 地址    |  是   |
| controllerDir.alias | 接口controller目录别名配置 |  是   |
| controllerDir.path  | 接口controller目录路径配置 |  是   |
|  serviceDir.alias   |  接口service目录别名配置   |  是   |
|   serviceDir.path   |  接口service目录路径配置   |  是   |
|      services       |         多服务配置         |  否   |
| controllerResolver  |    自定义Contrller解析     |  否   |


> 多服务配置需要添加服务数组`services`

```json
services:{
    "service-1":"service-1",
    "service-2":"service-2",
    "service-3":"service-3"
}

// key为生成service目录名称
// value为请求调用的service名称
```

> 多网管按数组配置即可

```javascript
module.exports = [
  {
    ...
  },
  {
    ...
  }
];

```

## 自定义ControllerResolver

默认情况下会通过path来分析对应的`Controller`名称来生成文件名和类名,在一些情况下如果不满足需要可以自定义`ControllerResolver`来解决

```javascript
  controllerResolver(path, currentTag, tags) {
    const tag = tags.find((x) => x.name === currentTag[0]);
    return tag.description.replace(/\s/g, "").replace(/Controller$/, "");
  }
```

需要服务重命名也可以使用`controllerResolver`

```json
controllerResolver: () => "New Name"
```

## 自定义ActionResolver

默认情况下会通过获取operationId来获取action名称,在一些情况下如果不满足需要可以自定义`ActionResolver`来解决

```javascript
  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(...))
```

---
_Source: https://npm.io/package/@gopowerteam/http-request-cli · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
