openapi-ts-request 0.4.10 → 0.5.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
@@ -11,9 +11,9 @@
11
11
  - 支持 Swagger2.0/OpenAPI 3.0,3.1 定义
12
12
  - 生成 TS 类型, 请求客户端, 请求模拟服务, 枚举, 类型字段翻译, JSON Schemas
13
13
  - 支持通过 npx、CLI、Nodejs 的方式使用
14
- - 支持自定义请求工具函数, 支持 Fetch、Axios、UniApp-Request、Node.js、XHR 客户端
14
+ - 支持自定义请求工具函数, 支持 Fetch、Axios、[UniApp-Request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Node.js、XHR 客户端
15
15
  - 支持通过 tags 过滤生成结果
16
- - 支持 JSON 定义文件
16
+ - 支持 JSON/YAML 定义文件
17
17
 
18
18
  ## 使用
19
19
 
@@ -200,6 +200,10 @@ export declare function patchSchema<T extends object>(
200
200
 
201
201
  目前使用 [mockjs](http://mockjs.com) 生成 mock 数据,mocks 文件启动需要借助 [@umijs/server](https://umijs.org/docs/guides/mock),后面会寻找其他方案以达到更好的 mock 体验
202
202
 
203
+ ## 适配uniapp
204
+
205
+ 适配 uniapp 推荐采用自定义 request 函数的方式,你也可以使用 `@uni-helper/axios-adapter` 适配器,详情见 [issue#46](https://github.com/openapi-ui/openapi-ts-request/issues/46)
206
+
203
207
  ## 贡献
204
208
 
205
209
  ### 环境要求
@@ -167,9 +167,10 @@ class ServiceGenerator {
167
167
  type: this.getType(parameter.schema),
168
168
  });
169
169
  });
170
- if (props.length > 0) {
170
+ const typeName = this.getTypeName(Object.assign(Object.assign({}, operationObject), { method, path: pathKey }));
171
+ if (props.length > 0 && typeName) {
171
172
  lastTypes.push({
172
- typeName: this.getTypeName(Object.assign(Object.assign({}, operationObject), { method, path: pathKey })),
173
+ typeName,
173
174
  type: 'Record<string, unknown>',
174
175
  props: [props],
175
176
  isEnum: false,
@@ -212,16 +213,18 @@ class ServiceGenerator {
212
213
  (schema === null || schema === void 0 ? void 0 : schema.isAllowed)) {
213
214
  const isEnum = result.isEnum;
214
215
  const typeName = (0, util_1.resolveTypeName)(schemaKey);
215
- lastTypes.push({
216
- typeName,
217
- type: getDefinesType(),
218
- props: (result.props || []),
219
- isEnum,
220
- displayLabelFuncName: isEnum
221
- ? (0, lodash_1.camelCase)(`display-${typeName}-Enum`)
222
- : '',
223
- enumLabelType: isEnum ? result.enumLabelType : '',
224
- });
216
+ if (typeName) {
217
+ lastTypes.push({
218
+ typeName,
219
+ type: getDefinesType(),
220
+ props: (result.props || []),
221
+ isEnum,
222
+ displayLabelFuncName: isEnum
223
+ ? (0, lodash_1.camelCase)(`display-${typeName}-Enum`)
224
+ : '',
225
+ enumLabelType: isEnum ? result.enumLabelType : '',
226
+ });
227
+ }
225
228
  if (this.config.isGenJsonSchemas) {
226
229
  this.schemaList.push({
227
230
  typeName: `$${(0, util_1.resolveTypeName)(schemaKey)}`,
@@ -529,15 +532,16 @@ class ServiceGenerator {
529
532
  .map((p) => this.resolveRefObject(p))
530
533
  .filter((p) => p.in === source)
531
534
  .map((p) => {
532
- var _a, _b, _c, _d;
533
- const isDirectObject = (((_a = p.schema) === null || _a === void 0 ? void 0 : _a.type) ||
535
+ var _a, _b, _c, _d, _e;
536
+ const isDirectObject = (((_a = p.schema) === null || _a === void 0 ? void 0 : _a.type) === 'object' ||
534
537
  p.type) === 'object';
535
538
  const refList = (((_b = p.schema) === null || _b === void 0 ? void 0 : _b.$ref) ||
536
539
  p.$ref ||
537
540
  '').split('/');
538
541
  const ref = refList[refList.length - 1];
539
542
  const deRefObj = (0, lodash_1.entries)((_c = this.openAPIData.components) === null || _c === void 0 ? void 0 : _c.schemas).find(([k]) => k === ref) || [];
540
- const isRefObject = ((_d = deRefObj[1]) === null || _d === void 0 ? void 0 : _d.type) === 'object';
543
+ const isRefObject = ((_d = deRefObj[1]) === null || _d === void 0 ? void 0 : _d.type) === 'object' &&
544
+ !(0, lodash_1.isEmpty)((_e = deRefObj[1]) === null || _e === void 0 ? void 0 : _e.properties);
541
545
  return Object.assign(Object.assign({}, p), { isObject: isDirectObject || isRefObject, type: this.getType(p.schema || config_1.DEFAULT_SCHEMA, this.config.namespace) });
542
546
  });
543
547
  if (params.length) {
package/dist/util.d.ts CHANGED
@@ -1,2 +1,4 @@
1
+ import { OpenAPI } from 'openapi-types';
1
2
  export declare const getImportStatement: (requestLibPath: string) => string;
2
- export declare const getOpenAPIConfig: (schemaPath: string) => Promise<unknown>;
3
+ export declare const getOpenAPIConfig: (schemaPath: string) => Promise<OpenAPI.Document<{}>>;
4
+ export declare function parseSwaggerOrOpenapi(content: string | OpenAPI.Document): Promise<OpenAPI.Document<{}>>;
package/dist/util.js CHANGED
@@ -1,10 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getOpenAPIConfig = exports.getImportStatement = void 0;
3
+ exports.parseSwaggerOrOpenapi = exports.getOpenAPIConfig = exports.getImportStatement = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const axios_1 = tslib_1.__importDefault(require("axios"));
6
6
  const http_1 = tslib_1.__importDefault(require("http"));
7
7
  const https_1 = tslib_1.__importDefault(require("https"));
8
+ const yaml = tslib_1.__importStar(require("js-yaml"));
9
+ const lodash_1 = require("lodash");
10
+ const node_fs_1 = require("node:fs");
8
11
  const swagger2openapi_1 = tslib_1.__importDefault(require("swagger2openapi"));
9
12
  const log_1 = tslib_1.__importDefault(require("./log"));
10
13
  const getImportStatement = (requestLibPath) => {
@@ -40,14 +43,22 @@ function getSchema(schemaPath) {
40
43
  if (require.cache[schemaPath]) {
41
44
  delete require.cache[schemaPath];
42
45
  }
43
- const schema = (yield require(schemaPath));
46
+ let schema = '';
47
+ try {
48
+ schema = (yield require(schemaPath));
49
+ }
50
+ catch (_a) {
51
+ try {
52
+ schema = (0, node_fs_1.readFileSync)(schemaPath, 'utf8');
53
+ }
54
+ catch (error) {
55
+ console.error('Error reading schema file:', error);
56
+ }
57
+ }
44
58
  return schema;
45
59
  });
46
60
  }
47
61
  function converterSwaggerToOpenApi(swagger) {
48
- if (!swagger.swagger) {
49
- return swagger;
50
- }
51
62
  return new Promise((resolve, reject) => {
52
63
  const convertOptions = {
53
64
  patch: true,
@@ -69,9 +80,43 @@ function converterSwaggerToOpenApi(swagger) {
69
80
  const getOpenAPIConfig = (schemaPath) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
70
81
  const schema = yield getSchema(schemaPath);
71
82
  if (!schema) {
72
- return null;
83
+ return;
73
84
  }
74
- const openAPI = yield converterSwaggerToOpenApi(schema);
85
+ const openAPI = yield parseSwaggerOrOpenapi(schema);
75
86
  return openAPI;
76
87
  });
77
88
  exports.getOpenAPIConfig = getOpenAPIConfig;
89
+ function parseSwaggerOrOpenapi(content) {
90
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
91
+ let openapi = {};
92
+ if ((0, lodash_1.isObject)(content)) {
93
+ openapi = content;
94
+ // if is swagger2.0 json, covert swagger2.0 to openapi3.0
95
+ if (openapi.swagger) {
96
+ openapi = yield converterSwaggerToOpenApi(openapi);
97
+ }
98
+ }
99
+ else {
100
+ if (isJSONString(content)) {
101
+ openapi = JSON.parse(content);
102
+ }
103
+ else {
104
+ openapi = yaml.load(content);
105
+ }
106
+ if (openapi.swagger) {
107
+ openapi = yield converterSwaggerToOpenApi(openapi);
108
+ }
109
+ }
110
+ return openapi;
111
+ });
112
+ }
113
+ exports.parseSwaggerOrOpenapi = parseSwaggerOrOpenapi;
114
+ function isJSONString(str) {
115
+ try {
116
+ JSON.parse(str);
117
+ return true;
118
+ }
119
+ catch (error) {
120
+ return false;
121
+ }
122
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "0.4.10",
3
+ "version": "0.5.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",
@@ -28,6 +28,7 @@
28
28
  "cosmiconfig": "^9.0.0",
29
29
  "cosmiconfig-typescript-loader": "^5.0.0",
30
30
  "glob": "^10.4.2",
31
+ "js-yaml": "^4.1.0",
31
32
  "lodash": "^4.17.21",
32
33
  "memoizee": "^0.4.17",
33
34
  "mockjs": "^1.1.0",
@@ -44,6 +45,7 @@
44
45
  "@changesets/cli": "^2.27.6",
45
46
  "@commitlint/cli": "^19.2.1",
46
47
  "@commitlint/config-conventional": "^19.2.2",
48
+ "@types/js-yaml": "^4.0.9",
47
49
  "@types/lodash": "^4.17.5",
48
50
  "@types/memoizee": "^0.4.11",
49
51
  "@types/mockjs": "^1.0.10",