Skip to content

Commit

Permalink
feat: support work with npx, CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
rookie-luochao committed Jul 15, 2024
1 parent d9add5e commit 0f1a6a7
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-cars-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openapi-ts-request': minor
---

feat: support work with npx, CLI
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
'dist',
'node_modules',
'test',
'.eslintrc.cjs',
'commitlint.config.cjs',
'lint-staged.config.cjs',
'prettier.config.cjs',
Expand Down
47 changes: 46 additions & 1 deletion README-en_US.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Generate TS interfaces, request client, request mock service, enum, type field l

* support Swagger2.0/OpenAPI 3.0,3.1 specification
* generate TypeScript interface, reuquest client, request mock service, enum, type field label, JSON Schemas
* support work with npx, CLI, Nodejs
* support custom request function, Fetch、Axios、UniApp-request、Node.js、XHR client available
* support filter generate result by tags
* support JSON specification
Expand Down Expand Up @@ -100,6 +101,50 @@ generate result:
npm run openapi
```

### NPX

```
npx openapi-ts-request openapi -i ./openapi.json -o ./apis
```

### CLI

```
npm i openapi-ts-request -g
```

```
$ openapi --help
Usage: openapi [options]
Options:
-V, --version output the version number
-i, --input <string> OpenAPI specification, can be a path, url (required)
-o, --output <string> Output directory (required)
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch"
--allowedTags <string[]> Generate results from allowed tags
--requestOptionsType <string> Custom request method options parameter type (default: "{ [key:
string]: unknown }")
--requestImportStatement <string> custom request import statement, for example: "const request =
require(`@/request`)"
--apiPrefix <string> Custom the prefix of the api path, for example: "api"(variable),
`"api"`(string)
--isDisplayTypeLabel <boolean> Generate label matching type field (default: false) (default: false)
--isGenJsonSchemas <boolean> Generate JSON Schemas (default: false) (default: false)
--mockFolder <string> Mock file path, (default: "./mocks")
--nullable <boolean> null instead of optional (default: false) (default: false)
--isCamelCase <boolean> CamelCase naming of controller files and request client (default: true)
(default: true)
-h, --help display help for command
```

generate result:

```bash
openapi -i ./spec.json -o ./apis
```

## Parameter

| props | required | type | default | remark |
Expand All @@ -110,7 +155,7 @@ npm run openapi
| allowedTags | no | string[] | - | generate results from allowed tags |
| requestOptionsType | no | string | '{ [key: string]: unknown }' | custom request method options parameter type |
| requestImportStatement | no | string | - | custom request import statement, for example: "const request = require('@/request')" |
| apiPrefix | no | string | - | custom the prefix of the api path,例如:'api'(variable), "'api'"(string) |
| apiPrefix | no | string | - | custom the prefix of the api path, for example: 'api'(variable), "'api'"(string) |
| isDisplayTypeLabel | no | boolean | false | generate label matching type field |
| isGenJsonSchemas | no | boolean | false | generate JSON Schemas |
| mockFolder | no | string | './mocks' | mock file path |
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* 支持 Swagger2.0/OpenAPI 3.0,3.1 定义
* 生成 TS 类型, 请求客户端, 请求模拟服务, 枚举, 类型字段翻译, JSON Schemas
* 支持通过 npx、CLI、Nodejs 的方式使用
* 支持自定义请求工具函数, 支持 Fetch、Axios、UniApp-Request、Node.js、XHR 客户端
* 支持通过 tags 过滤生成结果
* 支持 JSON 定义文件
Expand Down Expand Up @@ -100,6 +101,50 @@ generateService({
npm run openapi
```

### NPX

```
npx openapi-ts-request openapi -i ./openapi.json -o ./apis
```

### CLI

```
npm i openapi-ts-request -g
```

```
$ openapi --help
Usage: openapi [options]
Options:
-V, --version output the version number
-i, --input <string> OpenAPI specification, can be a path, url (required)
-o, --output <string> Output directory (required)
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch"
--allowedTags <string[]> Generate results from allowed tags
--requestOptionsType <string> Custom request method options parameter type (default: "{ [key:
string]: unknown }")
--requestImportStatement <string> custom request import statement, for example: "const request =
require(`@/request`)"
--apiPrefix <string> Custom the prefix of the api path, for example: "api"(variable),
`"api"`(string)
--isDisplayTypeLabel <boolean> Generate label matching type field (default: false) (default: false)
--isGenJsonSchemas <boolean> Generate JSON Schemas (default: false) (default: false)
--mockFolder <string> Mock file path, (default: "./mocks")
--nullable <boolean> null instead of optional (default: false) (default: false)
--isCamelCase <boolean> CamelCase naming of controller files and request client (default: true)
(default: true)
-h, --help display help for command
```

生成结果:

```bash
openapi --i ./spec.json --o ./apis
```

## 参数

| 属性 | 必填 | 类型 | 默认值 | 说明 |
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"openapi-ts-request": "dist/cli.js"
"openapi": "dist/bin/index.js",
"openapi-ts-request": "dist/bin/cli.js"
},
"files": [
"dist",
Expand All @@ -35,6 +36,7 @@
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"axios": "^1.7.2",
"chalk": "^4.1.2",
"commander": "^12.1.0",
"cosmiconfig": "^9.0.0",
"glob": "^10.4.2",
"lodash": "^4.17.21",
Expand Down
76 changes: 74 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cli.ts → src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import chalk from 'chalk';
import { cosmiconfigSync } from 'cosmiconfig';

import { GenerateServiceProps, generateService } from './index';
import { GenerateServiceProps, generateService } from '../index';

const explorerSync = cosmiconfigSync('openapi-ts-request');
const config = explorerSync.search()?.config as
Expand Down
Loading

0 comments on commit 0f1a6a7

Please sign in to comment.