openapi-ts-request 0.4.5 → 0.4.7

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 CHANGED
@@ -33,14 +33,18 @@ pnpm i openapi-ts-request -D
33
33
  > 配置文件还支持 ***.openapi-ts-request.ts***, ***openapi-ts-request.config.cjs*** 等格式,参考 [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig?tab=readme-ov-file#cosmiconfig)
34
34
 
35
35
  ```ts
36
+ import type { GenerateServiceProps } from 'openapi-ts-request'
37
+
36
38
  export default {
37
39
  schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
38
- }
40
+ } as GenerateServiceProps
39
41
  ```
40
42
 
41
43
  支持传入数组配置进行生成
42
44
 
43
45
  ```ts
46
+ import type { GenerateServiceProps } from 'openapi-ts-request'
47
+
44
48
  export default [
45
49
  {
46
50
  schemaPath: 'http://app.swagger.io/v2/swagger.json',
@@ -50,7 +54,7 @@ export default [
50
54
  schemaPath: 'http://auth.swagger.io/v2/swagger.json',
51
55
  serversPath: './src/apis/auth',
52
56
  }
53
- ]
57
+ ] as GenerateServiceProps[]
54
58
  ```
55
59
 
56
60
  在 ```package.json``` 的 ```script``` 中添加命令: ```"openapi": "openapi-ts",```
@@ -129,21 +133,20 @@ $ openapi --help
129
133
  Options:
130
134
  -V, --version output the version number
131
135
  -i, --input <string> OpenAPI specification, can be a path, url (required)
132
- -o, --output <string> Output directory (required)
136
+ -o, --output <string> output directory (required)
133
137
  --requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch"
134
- --allowedTags <string[]> Generate results from allowed tags
135
- --requestOptionsType <string> Custom request method options parameter type (default: "{ [key:
138
+ --allowedTags <string[]> generate results from allowed tags
139
+ --requestOptionsType <string> custom request method options parameter type (default: "{ [key:
136
140
  string]: unknown }")
137
141
  --requestImportStatement <string> custom request import statement, for example: "const request =
138
- require(`@/request`)"
139
- --apiPrefix <string> Custom the prefix of the api path, for example: "api"(variable),
140
- `"api"`(string)
141
- --isDisplayTypeLabel <boolean> Generate label matching type field (default: false) (default: false)
142
- --isGenJsonSchemas <boolean> Generate JSON Schemas (default: false) (default: false)
143
- --mockFolder <string> Mock file path, (default: "./mocks")
144
- --nullable <boolean> null instead of optional (default: false) (default: false)
145
- --isCamelCase <boolean> CamelCase naming of controller files and request client (default: true)
146
- (default: true)
142
+ require('@/request')"
143
+ --apiPrefix <string> custom the prefix of the api path, for example: "api"(variable),
144
+ "'api'"(string)
145
+ --isDisplayTypeLabel <boolean> generate label matching type field (default: false)
146
+ --isGenJsonSchemas <boolean> generate JSON Schemas (default: false)
147
+ --mockFolder <string> mock file path (default: "./mocks")
148
+ --nullable <boolean> null instead of optional (default: false)
149
+ --isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
147
150
  -h, --help display help for command
148
151
  ```
149
152
 
@@ -11,17 +11,17 @@ const params = commander_1.program
11
11
  .usage('[options]')
12
12
  .version(pkg.version)
13
13
  .requiredOption('-i, --input <string>', 'OpenAPI specification, can be a path, url (required)')
14
- .requiredOption('-o, --output <string>', 'Output directory (required)')
14
+ .requiredOption('-o, --output <string>', 'output directory (required)')
15
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)
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', false)
21
+ .option('--isGenJsonSchemas <boolean>', 'generate JSON Schemas', false)
22
+ .option('--mockFolder <string>', 'Mock file path (default: "./mocks")')
23
+ .option('--nullable <boolean>', 'null instead of optional', false)
24
+ .option('--isCamelCase <boolean>', 'camelCase naming of controller files and request client', true)
25
25
  .parse(process.argv)
26
26
  .opts();
27
27
  function getPath(path) {
@@ -79,11 +79,14 @@ class ServiceGenerator {
79
79
  list: interfaceTPConfigs,
80
80
  });
81
81
  // 生成枚举翻译
82
- this.genFileFromTemplate(`${config_1.displayEnumLabelFileName}.ts`, config_1.TypescriptFileType.displayEnumLabel, {
83
- list: (0, lodash_1.filter)(interfaceTPConfigs, (item) => item.isEnum),
84
- namespace: this.config.namespace,
85
- interfaceFileName: config_1.interfaceFileName,
86
- });
82
+ const enums = (0, lodash_1.filter)(interfaceTPConfigs, (item) => item.isEnum);
83
+ if (!(0, lodash_1.isEmpty)(enums)) {
84
+ this.genFileFromTemplate(`${config_1.displayEnumLabelFileName}.ts`, config_1.TypescriptFileType.displayEnumLabel, {
85
+ list: enums,
86
+ namespace: this.config.namespace,
87
+ interfaceFileName: config_1.interfaceFileName,
88
+ });
89
+ }
87
90
  // 生成 type 翻译
88
91
  if (this.config.isDisplayTypeLabel) {
89
92
  this.genFileFromTemplate(`${config_1.displayTypeLabelFileName}.ts`, config_1.TypescriptFileType.displayTypeLabel, {
@@ -206,7 +209,7 @@ class ServiceGenerator {
206
209
  }
207
210
  // 判断哪些 schema 需要添加进 type, schemas 渲染数组
208
211
  if ((0, lodash_1.isEmpty)(this.config.allowedTags) ||
209
- schema.isAllowed) {
212
+ (schema === null || schema === void 0 ? void 0 : schema.isAllowed)) {
210
213
  const isEnum = result.isEnum;
211
214
  const typeName = (0, util_1.resolveTypeName)(schemaKey);
212
215
  lastTypes.push({
@@ -279,7 +279,7 @@ function markAllowSchema(schemaStr, schemas) {
279
279
  (0, lodash_1.forEach)(refs, (ref) => {
280
280
  const refPaths = ref.split('/');
281
281
  const schema = schemas === null || schemas === void 0 ? void 0 : schemas[refPaths[refPaths.length - 1]];
282
- if (!(schema === null || schema === void 0 ? void 0 : schema.isAllowed)) {
282
+ if (schema && !schema.isAllowed) {
283
283
  schema.isAllowed = true;
284
284
  markAllowSchema(JSON.stringify(schema), schemas);
285
285
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
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",