@1szx1/vite-plugin-swagger2ts 1.0.12 → 1.0.13
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.
|
@@ -17,7 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/adapters
|
|
20
|
+
// src/adapters.ts
|
|
21
21
|
var adapters_exports = {};
|
|
22
22
|
__export(adapters_exports, {
|
|
23
23
|
axiosAdapter: () => axiosAdapter
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1szx1/vite-plugin-swagger2ts",
|
|
3
3
|
"description": "fork for https://github.com/1SZX1/vite-plugin-swagger2ts",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.13",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "index.d.ts",
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"import": "./dist/index.mjs"
|
|
23
23
|
},
|
|
24
24
|
"./adapters": {
|
|
25
|
-
"require": "./dist/adapters
|
|
26
|
-
"import": "./dist/adapters
|
|
25
|
+
"require": "./dist/adapters.cjs",
|
|
26
|
+
"import": "./dist/adapters.mjs"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"dev": "pnpm run build -- --watch --ignore-watch examples",
|
|
31
|
-
"build": "tsup src/index.ts src/adapters
|
|
31
|
+
"build": "tsup src/index.ts src/adapters.ts --dts --format cjs,esm",
|
|
32
32
|
"prepublishOnly": "pnpm run build",
|
|
33
33
|
"release": "pnpx git-ensure -a && pnpx bumpp --commit --tag --push",
|
|
34
34
|
"demo:dev": "pnpm -C examples/demo run dev",
|
package/dist/adapters/index.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/adapters/index.ts
|
|
21
|
-
var adapters_exports = {};
|
|
22
|
-
__export(adapters_exports, {
|
|
23
|
-
axiosAdapter: () => axiosAdapter
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(adapters_exports);
|
|
26
|
-
|
|
27
|
-
// src/adapters/utils.ts
|
|
28
|
-
function mergeUrlParam(url, paths) {
|
|
29
|
-
return url.replace(/{(.*?)}/g, (_, key) => paths[key]);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// src/adapters/axios.ts
|
|
33
|
-
function axiosAdapter(httpInstance) {
|
|
34
|
-
async function request(url, method, param, config) {
|
|
35
|
-
if (config?.path)
|
|
36
|
-
url = mergeUrlParam(url, config.path);
|
|
37
|
-
const response = await httpInstance.request({
|
|
38
|
-
url,
|
|
39
|
-
method,
|
|
40
|
-
params: method === "get" ? param : config?.params,
|
|
41
|
-
data: method !== "get" ? param : config?.data,
|
|
42
|
-
...config
|
|
43
|
-
});
|
|
44
|
-
return response.data;
|
|
45
|
-
}
|
|
46
|
-
return { request };
|
|
47
|
-
}
|
|
48
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
49
|
-
0 && (module.exports = {
|
|
50
|
-
axiosAdapter
|
|
51
|
-
});
|
package/dist/index.cjs
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
default: () => src_default
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(src_exports);
|
|
36
|
-
var import_fs = require("fs");
|
|
37
|
-
var import_path2 = require("path");
|
|
38
|
-
var import_swagger2openapi = require("swagger2openapi");
|
|
39
|
-
|
|
40
|
-
// src/utils.ts
|
|
41
|
-
var import_path = require("path");
|
|
42
|
-
var import_cross_fetch = __toESM(require("cross-fetch"), 1);
|
|
43
|
-
function resolveOptions(userOptions) {
|
|
44
|
-
const { output } = userOptions;
|
|
45
|
-
const root = process.cwd();
|
|
46
|
-
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
47
|
-
return Object.assign({ output: (0, import_path.resolve)(root, outputFile) }, userOptions);
|
|
48
|
-
}
|
|
49
|
-
async function fetchUrl(url) {
|
|
50
|
-
return await (0, import_cross_fetch.default)(url).then((res) => res.json());
|
|
51
|
-
}
|
|
52
|
-
function isTypeAny(type) {
|
|
53
|
-
if (type === true) {
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
if (typeof type === "object" && Object.keys(type).length <= 0) {
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
if (!type) {
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
function toPascalCase(str) {
|
|
65
|
-
return `${str.substring(0, 1).toUpperCase()}${str.substring(1)}`;
|
|
66
|
-
}
|
|
67
|
-
function replaceWithUpper(str, sp) {
|
|
68
|
-
let pointArray = str.split(sp);
|
|
69
|
-
pointArray = pointArray.map((point) => toPascalCase(point));
|
|
70
|
-
return pointArray.join("");
|
|
71
|
-
}
|
|
72
|
-
function getSchemaName(name) {
|
|
73
|
-
const removePound = replaceWithUpper(name, "#");
|
|
74
|
-
const removeDot = replaceWithUpper(removePound, ".");
|
|
75
|
-
const removeBackTick = replaceWithUpper(removeDot, "`");
|
|
76
|
-
const removeFirstBracket = replaceWithUpper(removeBackTick, "[");
|
|
77
|
-
const removeLastBracket = replaceWithUpper(removeFirstBracket, "]");
|
|
78
|
-
const removeDash = replaceWithUpper(removeLastBracket, "-");
|
|
79
|
-
const removeSlash = replaceWithUpper(removeDash, "/");
|
|
80
|
-
return removeSlash;
|
|
81
|
-
}
|
|
82
|
-
function getRefName($ref, prefix = "") {
|
|
83
|
-
return getSchemaName(prefix) + getSchemaName($ref || "");
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// src/generator.ts
|
|
87
|
-
function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
88
|
-
function getRefObject(ref) {
|
|
89
|
-
return ref.split("/").slice(1).reduce((obj, item) => {
|
|
90
|
-
return obj[item] || {};
|
|
91
|
-
}, input);
|
|
92
|
-
}
|
|
93
|
-
const pathsTypeString = Object.entries(input.paths).map(([endPoint, value]) => {
|
|
94
|
-
const pathValue = value;
|
|
95
|
-
const parametersExtended = pathValue.parameters;
|
|
96
|
-
const result = Object.entries(pathValue).map(([method, options]) => {
|
|
97
|
-
if (!["get", "put", "post", "delete", "options", "head", "patch", "trace"].includes(method)) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
let allParameters = [...parametersExtended || [], ...options.parameters || []].map((item) => {
|
|
101
|
-
if ("$ref" in item) {
|
|
102
|
-
return getRefObject(item.$ref);
|
|
103
|
-
} else {
|
|
104
|
-
return item;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
const queryParameters = allParameters.filter((item) => item.in === "query");
|
|
108
|
-
const pathParameters = allParameters.filter((item) => item.in === "path");
|
|
109
|
-
const bodyParameters = allParameters.filter((item) => !item.in);
|
|
110
|
-
const requestBody = options.requestBody ? "$ref" in options.requestBody ? getRefObject(options.requestBody.$ref) : options.requestBody : void 0;
|
|
111
|
-
let responses = Object.entries(options.responses).filter((items) => items[0] == "200" || items[0] == "default").sort()?.[0]?.[1];
|
|
112
|
-
if ("$ref" in responses) {
|
|
113
|
-
responses = getRefObject(responses.$ref);
|
|
114
|
-
}
|
|
115
|
-
const queryString = queryParameters.length > 0 ? jsDoc({}, { key: "query", result: `{${queryParameters.map((item) => generatorParameterType(item, docsName)).join("")}
|
|
116
|
-
}` }) : "";
|
|
117
|
-
const pathString = pathParameters.length > 0 ? jsDoc({}, { key: "path", result: `{${pathParameters.map((item) => generatorParameterType(item, docsName)).join("")}
|
|
118
|
-
}` }) : "";
|
|
119
|
-
const bodyExtraString = bodyParameters.length > 0 ? jsDoc({}, { key: "body", result: `{${bodyParameters.map((item) => generatorParameterType(item, docsName)).join("")}
|
|
120
|
-
}` }) : "";
|
|
121
|
-
const bodyString = requestBody ? generatorRequestBodyType(requestBody, docsName) : "";
|
|
122
|
-
const responseString = generatorResponseBodyType(responses, docsName);
|
|
123
|
-
const paramString = `${queryString}${pathString}${bodyString || bodyExtraString}`;
|
|
124
|
-
return jsDoc({}, { key: method, result: `{${paramString ? `
|
|
125
|
-
param:{${paramString}}` : ""}${responseString}}` });
|
|
126
|
-
}).join("");
|
|
127
|
-
return jsDoc({}, { key: `'${baseUrl}${endPoint}'`, result: `{${result}}` });
|
|
128
|
-
}).join("");
|
|
129
|
-
const typeString = input.components?.schemas ? Object.entries(input.components.schemas).map(([name, schema]) => {
|
|
130
|
-
if (typeof formatSchema == "function") {
|
|
131
|
-
schema = formatSchema(schema);
|
|
132
|
-
}
|
|
133
|
-
if ("$ref" in schema) {
|
|
134
|
-
schema = getRefObject(schema.$ref);
|
|
135
|
-
}
|
|
136
|
-
return generatorSchemaType(getRefName(`#/components/schemas/${name}`, docsName), schema, { isRoot: true, isExport: true, docsName });
|
|
137
|
-
}).join("") : "";
|
|
138
|
-
const apiString = `${typeString}
|
|
139
|
-
export interface Paths${getRefName(docsName)}{${pathsTypeString}}
|
|
140
|
-
`;
|
|
141
|
-
return apiString.replaceAll(/\n\n/g, "\n").replaceAll(/\n\n/g, "\n");
|
|
142
|
-
}
|
|
143
|
-
var TYPES = {
|
|
144
|
-
integer: "number",
|
|
145
|
-
number: "number",
|
|
146
|
-
boolean: "boolean",
|
|
147
|
-
object: "object",
|
|
148
|
-
string: "string",
|
|
149
|
-
array: "array"
|
|
150
|
-
};
|
|
151
|
-
function generatorSchemaType(key, schema, { isRoot = false, isRequired, isExport = false, docsName = "" } = {}) {
|
|
152
|
-
if (isTypeAny(schema)) {
|
|
153
|
-
return `
|
|
154
|
-
${key}${isRequired === false ? "?" : ""}: any;
|
|
155
|
-
`;
|
|
156
|
-
}
|
|
157
|
-
const { type, $ref, enum: Enum, items, properties, oneOf, additionalProperties, required, allOf } = schema;
|
|
158
|
-
let result = "";
|
|
159
|
-
if ($ref) {
|
|
160
|
-
result = getRefName($ref, docsName);
|
|
161
|
-
} else if (Enum) {
|
|
162
|
-
result = `${Enum.map((t) => `"${t}"`).join(" | ")}`;
|
|
163
|
-
} else if (items) {
|
|
164
|
-
result = `${generatorSchemaType("", items, { docsName })}[]`;
|
|
165
|
-
} else if (properties) {
|
|
166
|
-
result += `{${Object.entries(properties).map(([pName, _schema]) => generatorSchemaType(pName, _schema, { isRequired: !required || required.includes(pName) ? true : false, docsName })).join("")}
|
|
167
|
-
}`;
|
|
168
|
-
} else if (oneOf) {
|
|
169
|
-
result = `${oneOf.map((_schema) => `${generatorSchemaType("", _schema, { docsName })}`).join(" | ")}`;
|
|
170
|
-
} else if (allOf) {
|
|
171
|
-
result = `${allOf.map((_schema) => generatorSchemaType("", _schema, { docsName })).join(" & ")}`;
|
|
172
|
-
} else if (type === "object" && !result) {
|
|
173
|
-
if (additionalProperties) {
|
|
174
|
-
result = `{${generatorSchemaType("[x: string | number]", additionalProperties, { docsName })}
|
|
175
|
-
}`;
|
|
176
|
-
} else {
|
|
177
|
-
result = "{[x: string | number]: any}";
|
|
178
|
-
}
|
|
179
|
-
} else {
|
|
180
|
-
result = TYPES[type] || "string";
|
|
181
|
-
}
|
|
182
|
-
if (!key) {
|
|
183
|
-
return result;
|
|
184
|
-
}
|
|
185
|
-
return jsDoc(schema, { key, result, isExport, isRequired, isRoot });
|
|
186
|
-
}
|
|
187
|
-
function generatorParameterType(schemas, docsName) {
|
|
188
|
-
const { name, required, schema, description } = schemas;
|
|
189
|
-
const result = generatorSchemaType("", schema, { docsName });
|
|
190
|
-
return jsDoc({ description, ...schema }, { key: name, result, isRequired: required });
|
|
191
|
-
}
|
|
192
|
-
function generatorRequestBodyType(schemas, docsName) {
|
|
193
|
-
const { description, content, required } = schemas;
|
|
194
|
-
const schema = content && Object.values(content)[0]?.["schema"];
|
|
195
|
-
if (!schema)
|
|
196
|
-
return "";
|
|
197
|
-
const result = generatorSchemaType("", schema, { docsName });
|
|
198
|
-
return jsDoc({ description }, { key: "body", result, isRequired: required });
|
|
199
|
-
}
|
|
200
|
-
function generatorResponseBodyType(schemas, docsName) {
|
|
201
|
-
const { description, content } = schemas;
|
|
202
|
-
const schema = content && Object.values(content)[0]?.["schema"];
|
|
203
|
-
let result;
|
|
204
|
-
if (!schema) {
|
|
205
|
-
result = "any";
|
|
206
|
-
} else {
|
|
207
|
-
result = generatorSchemaType("", schema, { docsName });
|
|
208
|
-
}
|
|
209
|
-
return jsDoc({ description }, { key: "response", result });
|
|
210
|
-
}
|
|
211
|
-
function jsDoc({ description, title, format, example, default: default1 }, { key, result, isRoot, isExport, isRequired }) {
|
|
212
|
-
const jsDoc2 = `
|
|
213
|
-
/**
|
|
214
|
-
${title ? ` * @title ${title}
|
|
215
|
-
` : ""}${description ? ` * @description ${description}
|
|
216
|
-
` : ""}${format ? ` * @format ${format}
|
|
217
|
-
` : ""}${default1 ? ` * @default ${default1}
|
|
218
|
-
` : ""}${example ? ` * @example ${example}
|
|
219
|
-
` : ""} **/
|
|
220
|
-
`;
|
|
221
|
-
return `${jsDoc2 == "\n/**\n **/\n" ? "" : jsDoc2}${isRoot ? `${isExport ? "export " : ""}type ${key} = ${result};
|
|
222
|
-
` : `${key}${isRequired === false ? "?" : ""}: ${result};
|
|
223
|
-
`}`;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// src/index.ts
|
|
227
|
-
function convertObjPromise(docs) {
|
|
228
|
-
return new Promise((resolve3, reject) => {
|
|
229
|
-
(0, import_swagger2openapi.convertObj)(docs, { patch: true }, function(err, option) {
|
|
230
|
-
if (err || !option.openapi)
|
|
231
|
-
return reject(err);
|
|
232
|
-
resolve3(option.openapi);
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
function swagger2TsPlugin(userOptions) {
|
|
237
|
-
const { swaggerUrl, output, formatDocs, formatSchema } = resolveOptions(userOptions);
|
|
238
|
-
async function loadSwaggerSource() {
|
|
239
|
-
const sources = await fetchUrl(`${swaggerUrl}/swagger-resources`);
|
|
240
|
-
let code = "";
|
|
241
|
-
for (let i = 0; i < sources.length; i++) {
|
|
242
|
-
const { url, name: docsName } = sources[i];
|
|
243
|
-
try {
|
|
244
|
-
const docs = await fetchUrl(`${swaggerUrl}${url}`);
|
|
245
|
-
let openapiDocs = formatDocs ? formatDocs(docs) : docs;
|
|
246
|
-
if (!("openapi" in openapiDocs)) {
|
|
247
|
-
if (docs.swagger)
|
|
248
|
-
openapiDocs = await convertObjPromise(openapiDocs);
|
|
249
|
-
else
|
|
250
|
-
continue;
|
|
251
|
-
}
|
|
252
|
-
const apistrings = generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
253
|
-
code += apistrings;
|
|
254
|
-
} catch (error) {
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
const outputFile = (0, import_path2.resolve)(process.cwd(), output);
|
|
258
|
-
(0, import_fs.writeFileSync)(outputFile, code);
|
|
259
|
-
}
|
|
260
|
-
loadSwaggerSource();
|
|
261
|
-
return {
|
|
262
|
-
name: "vite-plugin-swagger2ts",
|
|
263
|
-
enforce: "pre",
|
|
264
|
-
apply: "serve"
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
var src_default = swagger2TsPlugin;
|
|
268
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
269
|
-
0 && (module.exports = {});
|
|
File without changes
|
|
File without changes
|