openapi-ts-request 0.6.0 → 0.8.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 CHANGED
@@ -135,7 +135,7 @@ $ openapi --help
135
135
  -i, --input <string> OpenAPI specification, can be a path, url (required)
136
136
  -o, --output <string> output directory (required)
137
137
  --requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch", default is "axios"
138
- --allowedTags <string[]> generate results from allowed tags
138
+ --allowedTags <string[]> generate code from allowed tags
139
139
  --requestOptionsType <string> custom request method options parameter type (default: "{ [key:
140
140
  string]: unknown }")
141
141
  --requestImportStatement <string> custom request import statement, for example: "const request =
@@ -145,6 +145,7 @@ $ openapi --help
145
145
  --isDisplayTypeLabel <boolean> generate label matching type field (default: false)
146
146
  --isGenJsonSchemas <boolean> generate JSON Schemas (default: false)
147
147
  --mockFolder <string> mock file path
148
+ --authorization <string> docs authorization
148
149
  --nullable <boolean> null instead of optional (default: false)
149
150
  --isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
150
151
  --isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
@@ -164,13 +165,14 @@ openapi --i ./spec.json --o ./apis
164
165
  | schemaPath | 是 | string | - | Swagger2/OpenAPI3 地址 |
165
166
  | serversPath | 否 | string | './src/apis' | 生成结果的文件夹路径 |
166
167
  | requestLibPath | 否 | string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
167
- | allowedTags | 否 | string[] | - | 根据指定的 tags 生成结果 |
168
+ | allowedTags | 否 | string[] | - | 根据指定的 tags 生成代码 |
168
169
  | requestOptionsType | 否 | string | '{ [key: string]: unknown }' | 自定义请求方法 options 参数类型 |
169
170
  | requestImportStatement | 否 | string | - | 自定义请求方法表达式,例如:"const request = require('@/request')" |
170
171
  | apiPrefix | 否 | string | - | api path的前缀,例如:'api'(动态变量), "'api'"(字符串) |
171
172
  | isDisplayTypeLabel | 否 | boolean | false | 是否生成 type 对应的label |
172
173
  | isGenJsonSchemas | 否 | boolean | false | 是否生成 JSON Schemas |
173
174
  | mockFolder | 否 | string | './mocks' | mock文件路径 |
175
+ | authorization | 否 | string | - | 文档权限凭证 |
174
176
  | nullable | 否 | boolean | false | 使用 null 代替可选 |
175
177
  | isTranslateToEnglishTag | 否 | boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
176
178
  | isCamelCase | 否 | boolean | true | 小驼峰命名文件和请求函数 |
@@ -20,6 +20,7 @@ const params = commander_1.program
20
20
  .option('--isDisplayTypeLabel <boolean>', 'generate label matching type field', false)
21
21
  .option('--isGenJsonSchemas <boolean>', 'generate JSON Schemas', false)
22
22
  .option('--mockFolder <string>', 'mock file path')
23
+ .option('--authorization <string>', 'docs authorization')
23
24
  .option('--nullable <boolean>', 'null instead of optional', false)
24
25
  .option('--isTranslateToEnglishTag <boolean>', 'translate chinese tag name to english tag name', false)
25
26
  .option('--isCamelCase <boolean>', 'camelCase naming of controller files and request client', true)
@@ -47,6 +48,7 @@ function run() {
47
48
  isDisplayTypeLabel: JSON.parse(params.isDisplayTypeLabel) === true,
48
49
  isGenJsonSchemas: JSON.parse(params.isGenJsonSchemas) === true,
49
50
  mockFolder: params.mockFolder,
51
+ authorization: params.authorization,
50
52
  nullable: JSON.parse(params.nullable) === true,
51
53
  isTranslateToEnglishTag: JSON.parse(params.isTranslateToEnglishTag) === true,
52
54
  isCamelCase: JSON.parse(params.isCamelCase) === true,
@@ -87,10 +87,11 @@ class ServiceGenerator {
87
87
  interfaceFileName: config_1.interfaceFileName,
88
88
  });
89
89
  }
90
+ const displayTypeLabels = (0, lodash_1.filter)(interfaceTPConfigs, (item) => !item.isEnum);
90
91
  // 生成 type 翻译
91
- if (this.config.isDisplayTypeLabel) {
92
+ if (this.config.isDisplayTypeLabel && !(0, lodash_1.isEmpty)(displayTypeLabels)) {
92
93
  this.genFileFromTemplate(`${config_1.displayTypeLabelFileName}.ts`, config_1.TypescriptFileType.displayTypeLabel, {
93
- list: (0, lodash_1.filter)(interfaceTPConfigs, (item) => !item.isEnum),
94
+ list: displayTypeLabels,
94
95
  namespace: this.config.namespace,
95
96
  interfaceFileName: config_1.interfaceFileName,
96
97
  });
@@ -104,15 +105,7 @@ class ServiceGenerator {
104
105
  if (prettierError.includes(true)) {
105
106
  (0, log_1.default)('🚥 格式化失败,请检查 service controller 文件内可能存在的语法错误');
106
107
  }
107
- // 生成 service index 文件
108
- this.genFileFromTemplate(`${config_1.serviceEntryFileName}.ts`, config_1.TypescriptFileType.serviceIndex, {
109
- list: this.classNameList,
110
- namespace: this.config.namespace,
111
- interfaceFileName: config_1.interfaceFileName,
112
- schemaFileName: config_1.schemaFileName,
113
- isGenJsonSchemas: this.config.isGenJsonSchemas,
114
- });
115
- if (this.config.isGenJsonSchemas) {
108
+ if (this.config.isGenJsonSchemas && !(0, lodash_1.isEmpty)(this.schemaList)) {
116
109
  // 处理重复的 schemaName
117
110
  (0, util_1.handleDuplicateTypeNames)(this.schemaList);
118
111
  // 生成 schema 文件
@@ -120,6 +113,18 @@ class ServiceGenerator {
120
113
  list: this.schemaList,
121
114
  });
122
115
  }
116
+ // 生成 service index 文件
117
+ this.genFileFromTemplate(`${config_1.serviceEntryFileName}.ts`, config_1.TypescriptFileType.serviceIndex, {
118
+ list: this.classNameList,
119
+ namespace: this.config.namespace,
120
+ interfaceFileName: config_1.interfaceFileName,
121
+ isGenJsonSchemas: this.config.isGenJsonSchemas && !(0, lodash_1.isEmpty)(this.schemaList),
122
+ schemaFileName: config_1.schemaFileName,
123
+ isDisplayEnumLabel: !(0, lodash_1.isEmpty)(enums),
124
+ displayEnumLabelFileName: config_1.displayEnumLabelFileName,
125
+ isDisplayTypeLabel: this.config.isDisplayTypeLabel && !(0, lodash_1.isEmpty)(displayTypeLabels),
126
+ displayTypeLabelFileName: config_1.displayTypeLabelFileName,
127
+ });
123
128
  // 打印日志
124
129
  (0, log_1.default)('✅ 成功生成 api 文件');
125
130
  }
package/dist/index.d.ts CHANGED
@@ -56,6 +56,10 @@ export type GenerateServiceProps = {
56
56
  * mock目录
57
57
  */
58
58
  mockFolder?: string;
59
+ /**
60
+ * 文档权限凭证
61
+ */
62
+ authorization?: string;
59
63
  /**
60
64
  * 默认为false,true时使用null代替可选值
61
65
  */
@@ -135,4 +139,4 @@ export type GenerateServiceProps = {
135
139
  customFileNames?: (operationObject: OperationObject, apiPath: string, apiMethod: string) => string[] | null;
136
140
  };
137
141
  };
138
- export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, isTranslateToEnglishTag, ...rest }: GenerateServiceProps): Promise<void>;
142
+ export declare function generateService({ requestLibPath, schemaPath, mockFolder, allowedTags, authorization, isTranslateToEnglishTag, ...rest }: GenerateServiceProps): Promise<void>;
package/dist/index.js CHANGED
@@ -9,11 +9,11 @@ const util_1 = require("./util");
9
9
  tslib_1.__exportStar(require("./generator/patchSchema"), exports);
10
10
  function generateService(_a) {
11
11
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
12
- var { requestLibPath, schemaPath, mockFolder, allowedTags, isTranslateToEnglishTag } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "allowedTags", "isTranslateToEnglishTag"]);
12
+ var { requestLibPath, schemaPath, mockFolder, allowedTags, authorization, isTranslateToEnglishTag } = _a, rest = tslib_1.__rest(_a, ["requestLibPath", "schemaPath", "mockFolder", "allowedTags", "authorization", "isTranslateToEnglishTag"]);
13
13
  if (!schemaPath) {
14
14
  return;
15
15
  }
16
- const openAPI = (yield (0, util_1.getOpenAPIConfig)(schemaPath));
16
+ const openAPI = (yield (0, util_1.getOpenAPIConfig)(schemaPath, authorization));
17
17
  if ((0, lodash_1.isEmpty)(openAPI)) {
18
18
  return;
19
19
  }
package/dist/util.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { OpenAPI } from 'openapi-types';
2
2
  import { OpenAPIObject } from './type';
3
3
  export declare const getImportStatement: (requestLibPath: string) => string;
4
- export declare const getOpenAPIConfig: (schemaPath: string) => Promise<OpenAPI.Document<{}>>;
4
+ export declare const getOpenAPIConfig: (schemaPath: string, authorization?: string) => Promise<OpenAPI.Document<{}>>;
5
5
  export declare function parseSwaggerOrOpenapi(content: string | OpenAPI.Document): Promise<OpenAPI.Document<{}>>;
6
6
  export declare function translateChineseModuleNodeToEnglish(openAPI: OpenAPIObject): Promise<unknown>;
package/dist/util.js CHANGED
@@ -23,7 +23,7 @@ const getImportStatement = (requestLibPath) => {
23
23
  return `import request from 'axios';`;
24
24
  };
25
25
  exports.getImportStatement = getImportStatement;
26
- function getSchema(schemaPath) {
26
+ function getSchema(schemaPath, authorization) {
27
27
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
28
28
  if (schemaPath.startsWith('http')) {
29
29
  const isHttps = schemaPath.startsWith('https:');
@@ -34,7 +34,7 @@ function getSchema(schemaPath) {
34
34
  });
35
35
  const config = isHttps ? { httpsAgent: agent } : { httpAgent: agent };
36
36
  const json = yield axios_1.default
37
- .get(schemaPath, config)
37
+ .get(schemaPath, Object.assign(Object.assign({}, config), { headers: { authorization } }))
38
38
  .then((res) => res.data);
39
39
  return json;
40
40
  }
@@ -80,8 +80,8 @@ function converterSwaggerToOpenApi(swagger) {
80
80
  });
81
81
  });
82
82
  }
83
- const getOpenAPIConfig = (schemaPath) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
84
- const schema = yield getSchema(schemaPath);
83
+ const getOpenAPIConfig = (schemaPath, authorization) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
84
+ const schema = yield getSchema(schemaPath, authorization);
85
85
  if (!schema) {
86
86
  return;
87
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "0.6.0",
3
+ "version": "0.8.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",
@@ -60,7 +60,7 @@
60
60
  "husky": "^9.0.11",
61
61
  "lint-staged": "^15.2.5",
62
62
  "openapi-types": "^12.1.3",
63
- "typescript": "5.6.2"
63
+ "typescript": "5.6.3"
64
64
  },
65
65
  "keywords": [
66
66
  "openapi",
@@ -52,5 +52,5 @@
52
52
  {%- endif %}
53
53
  {% endfor %}
54
54
  {%- else %}
55
- export default {}
55
+ export {}
56
56
  {%- endif %}
@@ -3,6 +3,4 @@
3
3
  export const {{ type.typeName }} = {{ type.type }}
4
4
 
5
5
  {% endfor -%}
6
- {%- else %}
7
- export default {}
8
6
  {%- endif %}
@@ -1,6 +1,12 @@
1
1
  /* eslint-disable */
2
2
  // @ts-ignore
3
3
  import * as {{ namespace }} from './{{ interfaceFileName }}';
4
+ {%- if isDisplayEnumLabel %}
5
+ import * as displayEnumLabels from './{{ displayEnumLabelFileName }}';
6
+ {%- endif %}
7
+ {%- if isDisplayTypeLabel %}
8
+ import * as displayTypeLabels from './{{ displayTypeLabelFileName }}';
9
+ {%- endif %}
4
10
  {%- if isGenJsonSchemas %}
5
11
  import * as schemas from './{{ schemaFileName }}';
6
12
  {%- endif %}
@@ -8,11 +14,17 @@ import * as schemas from './{{ schemaFileName }}';
8
14
  import * as {{ api.controllerName }} from './{{ api.fileName }}';
9
15
  {% endfor -%}
10
16
 
11
- export default {
17
+ export {
18
+ {{ namespace }},
19
+ {%- if isDisplayEnumLabel %}
20
+ displayEnumLabels,
21
+ {%- endif %}
22
+ {%- if isDisplayTypeLabel %}
23
+ displayTypeLabels,
24
+ {%- endif %}
12
25
  {%- if isGenJsonSchemas %}
13
26
  schemas,
14
27
  {%- endif %}
15
- {{ namespace }},
16
28
  {% for api in list -%}
17
29
  {{ api.controllerName }},
18
30
  {% endfor -%}