openapi-ts-request 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -0
- package/dist/{cli.js → bin/cli.js} +1 -1
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.js +57 -0
- package/dist/generator/file.js +1 -1
- package/package.json +4 -2
- package/templates/serviceIndex.njk +0 -1
- /package/dist/{cli.d.ts → bin/cli.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
* 支持 Swagger2.0/OpenAPI 3.0,3.1 定义
|
|
14
14
|
* 生成 TS 类型, 请求客户端, 请求模拟服务, 枚举, 类型字段翻译, JSON Schemas
|
|
15
|
+
* 支持通过 npx、CLI、Nodejs 的方式使用
|
|
15
16
|
* 支持自定义请求工具函数, 支持 Fetch、Axios、UniApp-Request、Node.js、XHR 客户端
|
|
16
17
|
* 支持通过 tags 过滤生成结果
|
|
17
18
|
* 支持 JSON 定义文件
|
|
@@ -100,6 +101,50 @@ generateService({
|
|
|
100
101
|
npm run openapi
|
|
101
102
|
```
|
|
102
103
|
|
|
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
|
+
|
|
103
148
|
## 参数
|
|
104
149
|
|
|
105
150
|
| 属性 | 必填 | 类型 | 默认值 | 说明 |
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
7
7
|
const cosmiconfig_1 = require("cosmiconfig");
|
|
8
|
-
const index_1 = require("
|
|
8
|
+
const index_1 = require("../index");
|
|
9
9
|
const explorerSync = (0, cosmiconfig_1.cosmiconfigSync)('openapi-ts-request');
|
|
10
10
|
const config = (_a = explorerSync.search()) === null || _a === void 0 ? void 0 : _a.config;
|
|
11
11
|
function run() {
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const commander_1 = require("commander");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const pkg = tslib_1.__importStar(require("../../package.json"));
|
|
8
|
+
const index_1 = require("../index");
|
|
9
|
+
const params = commander_1.program
|
|
10
|
+
.name('openapi')
|
|
11
|
+
.usage('[options]')
|
|
12
|
+
.version(pkg.version)
|
|
13
|
+
.requiredOption('-i, --input <string>', 'OpenAPI specification, can be a path, url (required)')
|
|
14
|
+
.requiredOption('-o, --output <string>', 'Output directory (required)')
|
|
15
|
+
.option('--requestLibPath <string>', 'custom request lib path, for example: "@/request", "node-fetch"')
|
|
16
|
+
.option('--allowedTags <string[]>', 'Generate results from allowed tags')
|
|
17
|
+
.option('--requestOptionsType <string>', 'Custom request method options parameter type (default: "{ [key: string]: unknown }")')
|
|
18
|
+
.option('--requestImportStatement <string>', 'custom request import statement, for example: "const request = require(`@/request`)"')
|
|
19
|
+
.option('--apiPrefix <string>', 'Custom the prefix of the api path, for example: "api"(variable), `"api"`(string)')
|
|
20
|
+
.option('--isDisplayTypeLabel <boolean>', 'Generate label matching type field (default: false)', false)
|
|
21
|
+
.option('--isGenJsonSchemas <boolean>', 'Generate JSON Schemas (default: false)', false)
|
|
22
|
+
.option('--mockFolder <string>', 'Mock file path, (default: "./mocks")')
|
|
23
|
+
.option('--nullable <boolean>', 'null instead of optional (default: false)', false)
|
|
24
|
+
.option('--isCamelCase <boolean>', 'CamelCase naming of controller files and request client (default: true)', true)
|
|
25
|
+
.parse(process.argv)
|
|
26
|
+
.opts();
|
|
27
|
+
function getPath(path) {
|
|
28
|
+
const cwd = process.cwd();
|
|
29
|
+
return (0, path_1.join)(cwd, path);
|
|
30
|
+
}
|
|
31
|
+
function run() {
|
|
32
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
try {
|
|
34
|
+
const input = getPath(params.input);
|
|
35
|
+
const output = getPath(params.output);
|
|
36
|
+
yield (0, index_1.generateService)({
|
|
37
|
+
schemaPath: input,
|
|
38
|
+
serversPath: output,
|
|
39
|
+
requestLibPath: params.requestLibPath,
|
|
40
|
+
allowedTags: params.allowedTags,
|
|
41
|
+
requestOptionsType: params.requestOptionsType,
|
|
42
|
+
apiPrefix: params.apiPrefix,
|
|
43
|
+
isDisplayTypeLabel: JSON.parse(params.isDisplayTypeLabel) === true,
|
|
44
|
+
isGenJsonSchemas: JSON.parse(params.isGenJsonSchemas) === true,
|
|
45
|
+
mockFolder: params.mockFolder,
|
|
46
|
+
nullable: JSON.parse(params.nullable) === true,
|
|
47
|
+
isCamelCase: JSON.parse(params.isCamelCase) === true,
|
|
48
|
+
});
|
|
49
|
+
process.exit(0);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error('this is error: ', error);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
void run();
|
package/dist/generator/file.js
CHANGED
|
@@ -10,7 +10,7 @@ const prettierFile = (content) => {
|
|
|
10
10
|
let result = content;
|
|
11
11
|
let hasError = false;
|
|
12
12
|
try {
|
|
13
|
-
result = (0, sync_1.format)(content, Object.assign(Object.assign({}, prettier_config_cjs_1.default), { parser: 'typescript',
|
|
13
|
+
result = (0, sync_1.format)(content, Object.assign(Object.assign({}, prettier_config_cjs_1.default), { parser: 'typescript', plugins: [] }));
|
|
14
14
|
}
|
|
15
15
|
catch (error) {
|
|
16
16
|
hasError = true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openapi-ts-request",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Swagger2/OpenAPI3 to TypeScript, request client, request mock service, enum, type field label, JSON Schemas",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"main": "dist/index.js",
|
|
12
12
|
"types": "dist/index.d.ts",
|
|
13
13
|
"bin": {
|
|
14
|
-
"openapi
|
|
14
|
+
"openapi": "dist/bin/index.js",
|
|
15
|
+
"openapi-ts-request": "dist/bin/cli.js"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
17
18
|
"dist",
|
|
@@ -23,6 +24,7 @@
|
|
|
23
24
|
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
24
25
|
"axios": "^1.7.2",
|
|
25
26
|
"chalk": "^4.1.2",
|
|
27
|
+
"commander": "^12.1.0",
|
|
26
28
|
"cosmiconfig": "^9.0.0",
|
|
27
29
|
"glob": "^10.4.2",
|
|
28
30
|
"lodash": "^4.17.21",
|
|
@@ -4,7 +4,6 @@ import * as {{ namespace }} from './{{ interfaceFileName }}';
|
|
|
4
4
|
{%- if isGenJsonSchemas %}
|
|
5
5
|
import * as schemas from './{{ schemaFileName }}';
|
|
6
6
|
{%- endif %}
|
|
7
|
-
|
|
8
7
|
{% for api in list -%}
|
|
9
8
|
import * as {{ api.controllerName }} from './{{ api.fileName }}';
|
|
10
9
|
{% endfor -%}
|
|
File without changes
|