Skip to content

Commit 0f1a6a7

Browse files
feat: support work with npx, CLI
1 parent d9add5e commit 0f1a6a7

File tree

11 files changed

+267
-8
lines changed

11 files changed

+267
-8
lines changed

.changeset/fluffy-cars-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': minor
3+
---
4+
5+
feat: support work with npx, CLI

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
'dist',
1010
'node_modules',
1111
'test',
12+
'.eslintrc.cjs',
1213
'commitlint.config.cjs',
1314
'lint-staged.config.cjs',
1415
'prettier.config.cjs',

README-en_US.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Generate TS interfaces, request client, request mock service, enum, type field l
1212

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

104+
### NPX
105+
106+
```
107+
npx openapi-ts-request openapi -i ./openapi.json -o ./apis
108+
```
109+
110+
### CLI
111+
112+
```
113+
npm i openapi-ts-request -g
114+
```
115+
116+
```
117+
$ openapi --help
118+
119+
Usage: openapi [options]
120+
121+
Options:
122+
-V, --version output the version number
123+
-i, --input <string> OpenAPI specification, can be a path, url (required)
124+
-o, --output <string> Output directory (required)
125+
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch"
126+
--allowedTags <string[]> Generate results from allowed tags
127+
--requestOptionsType <string> Custom request method options parameter type (default: "{ [key:
128+
string]: unknown }")
129+
--requestImportStatement <string> custom request import statement, for example: "const request =
130+
require(`@/request`)"
131+
--apiPrefix <string> Custom the prefix of the api path, for example: "api"(variable),
132+
`"api"`(string)
133+
--isDisplayTypeLabel <boolean> Generate label matching type field (default: false) (default: false)
134+
--isGenJsonSchemas <boolean> Generate JSON Schemas (default: false) (default: false)
135+
--mockFolder <string> Mock file path, (default: "./mocks")
136+
--nullable <boolean> null instead of optional (default: false) (default: false)
137+
--isCamelCase <boolean> CamelCase naming of controller files and request client (default: true)
138+
(default: true)
139+
-h, --help display help for command
140+
```
141+
142+
generate result:
143+
144+
```bash
145+
openapi -i ./spec.json -o ./apis
146+
```
147+
103148
## Parameter
104149

105150
| props | required | type | default | remark |
@@ -110,7 +155,7 @@ npm run openapi
110155
| allowedTags | no | string[] | - | generate results from allowed tags |
111156
| requestOptionsType | no | string | '{ [key: string]: unknown }' | custom request method options parameter type |
112157
| requestImportStatement | no | string | - | custom request import statement, for example: "const request = require('@/request')" |
113-
| apiPrefix | no | string | - | custom the prefix of the api path,例如:'api'(variable), "'api'"(string) |
158+
| apiPrefix | no | string | - | custom the prefix of the api path, for example: 'api'(variable), "'api'"(string) |
114159
| isDisplayTypeLabel | no | boolean | false | generate label matching type field |
115160
| isGenJsonSchemas | no | boolean | false | generate JSON Schemas |
116161
| mockFolder | no | string | './mocks' | mock file path |

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

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

104+
### NPX
105+
106+
```
107+
npx openapi-ts-request openapi -i ./openapi.json -o ./apis
108+
```
109+
110+
### CLI
111+
112+
```
113+
npm i openapi-ts-request -g
114+
```
115+
116+
```
117+
$ openapi --help
118+
119+
Usage: openapi [options]
120+
121+
Options:
122+
-V, --version output the version number
123+
-i, --input <string> OpenAPI specification, can be a path, url (required)
124+
-o, --output <string> Output directory (required)
125+
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch"
126+
--allowedTags <string[]> Generate results from allowed tags
127+
--requestOptionsType <string> Custom request method options parameter type (default: "{ [key:
128+
string]: unknown }")
129+
--requestImportStatement <string> custom request import statement, for example: "const request =
130+
require(`@/request`)"
131+
--apiPrefix <string> Custom the prefix of the api path, for example: "api"(variable),
132+
`"api"`(string)
133+
--isDisplayTypeLabel <boolean> Generate label matching type field (default: false) (default: false)
134+
--isGenJsonSchemas <boolean> Generate JSON Schemas (default: false) (default: false)
135+
--mockFolder <string> Mock file path, (default: "./mocks")
136+
--nullable <boolean> null instead of optional (default: false) (default: false)
137+
--isCamelCase <boolean> CamelCase naming of controller files and request client (default: true)
138+
(default: true)
139+
-h, --help display help for command
140+
```
141+
142+
生成结果:
143+
144+
```bash
145+
openapi --i ./spec.json --o ./apis
146+
```
147+
103148
## 参数
104149

105150
| 属性 | 必填 | 类型 | 默认值 | 说明 |

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"main": "dist/index.js",
1313
"types": "dist/index.d.ts",
1414
"bin": {
15-
"openapi-ts-request": "dist/cli.js"
15+
"openapi": "dist/bin/index.js",
16+
"openapi-ts-request": "dist/bin/cli.js"
1617
},
1718
"files": [
1819
"dist",
@@ -35,6 +36,7 @@
3536
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
3637
"axios": "^1.7.2",
3738
"chalk": "^4.1.2",
39+
"commander": "^12.1.0",
3840
"cosmiconfig": "^9.0.0",
3941
"glob": "^10.4.2",
4042
"lodash": "^4.17.21",

pnpm-lock.yaml

Lines changed: 74 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.ts renamed to src/bin/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import chalk from 'chalk';
33
import { cosmiconfigSync } from 'cosmiconfig';
44

5-
import { GenerateServiceProps, generateService } from './index';
5+
import { GenerateServiceProps, generateService } from '../index';
66

77
const explorerSync = cosmiconfigSync('openapi-ts-request');
88
const config = explorerSync.search()?.config as

0 commit comments

Comments
 (0)