openapi-ts-request 0.4.11 → 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
@@ -13,7 +13,7 @@
13
13
  - 支持通过 npx、CLI、Nodejs 的方式使用
14
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
 
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.11",
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",