@lark-apaas/coding-preset-vite-react 1.0.13-alpha.13 → 1.0.13-alpha.3
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/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -6
- package/lib/index.js.map +1 -1
- package/lib/plugins/server-log.d.ts +17 -7
- package/lib/plugins/server-log.d.ts.map +1 -1
- package/lib/plugins/server-log.js +27 -7
- package/lib/plugins/server-log.js.map +1 -1
- package/lib/plugins/view-context.d.ts +0 -5
- package/lib/plugins/view-context.d.ts.map +1 -1
- package/lib/plugins/view-context.js +0 -3
- package/lib/plugins/view-context.js.map +1 -1
- package/lib/server-log/openapi/controller.d.ts +7 -0
- package/lib/server-log/openapi/controller.d.ts.map +1 -0
- package/lib/server-log/openapi/controller.js +60 -0
- package/lib/server-log/openapi/controller.js.map +1 -0
- package/lib/server-log/openapi/index.d.ts +10 -0
- package/lib/server-log/openapi/index.d.ts.map +1 -0
- package/lib/server-log/openapi/index.js +48 -0
- package/lib/server-log/openapi/index.js.map +1 -0
- package/lib/server-log/openapi/router.d.ts +20 -0
- package/lib/server-log/openapi/router.d.ts.map +1 -0
- package/lib/server-log/openapi/router.js +26 -0
- package/lib/server-log/openapi/router.js.map +1 -0
- package/lib/server-log/openapi/services.d.ts +15 -0
- package/lib/server-log/openapi/services.d.ts.map +1 -0
- package/lib/server-log/openapi/services.js +276 -0
- package/lib/server-log/openapi/services.js.map +1 -0
- package/lib/server-log/openapi/spec-controller.d.ts +4 -0
- package/lib/server-log/openapi/spec-controller.d.ts.map +1 -0
- package/lib/server-log/openapi/spec-controller.js +136 -0
- package/lib/server-log/openapi/spec-controller.js.map +1 -0
- package/lib/server-log/openapi/types.d.ts +56 -0
- package/lib/server-log/openapi/types.d.ts.map +1 -0
- package/lib/server-log/openapi/types.js +3 -0
- package/lib/server-log/openapi/types.js.map +1 -0
- package/lib/server-log/openapi/utils.d.ts +18 -0
- package/lib/server-log/openapi/utils.d.ts.map +1 -0
- package/lib/server-log/openapi/utils.js +128 -0
- package/lib/server-log/openapi/utils.js.map +1 -0
- package/package.json +1 -1
- package/lib/plugins/basename-injection.d.ts +0 -29
- package/lib/plugins/basename-injection.d.ts.map +0 -1
- package/lib/plugins/basename-injection.js +0 -114
- package/lib/plugins/basename-injection.js.map +0 -1
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.enhanceOpenApiWithSourceInfo = enhanceOpenApiWithSourceInfo;
|
|
40
|
+
exports.parseApiRoutes = parseApiRoutes;
|
|
41
|
+
const node_fs_1 = require("node:fs");
|
|
42
|
+
const fsSync = __importStar(require("node:fs"));
|
|
43
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
44
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
45
|
+
const utils_1 = require("./utils");
|
|
46
|
+
/**
|
|
47
|
+
* Enhances OpenAPI JSON with source file location metadata
|
|
48
|
+
* Can be called programmatically or run as a script
|
|
49
|
+
*/
|
|
50
|
+
async function enhanceOpenApiWithSourceInfo(options = {}) {
|
|
51
|
+
const startTime = Date.now();
|
|
52
|
+
const openapiPath = options.openapiPath || node_path_1.default.resolve(__dirname, '../client/src/api/gen/openapi.json');
|
|
53
|
+
const serverDir = options.serverDir || node_path_1.default.resolve(__dirname, '../server');
|
|
54
|
+
const writeFile = options.writeFile !== false;
|
|
55
|
+
let openapi;
|
|
56
|
+
if (options.openapiData) {
|
|
57
|
+
// Use provided data (for in-memory enhancement)
|
|
58
|
+
openapi = JSON.parse(JSON.stringify(options.openapiData)); // Deep clone
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Read from file
|
|
62
|
+
const openapiContent = await node_fs_1.promises.readFile(openapiPath, 'utf-8');
|
|
63
|
+
openapi = JSON.parse(openapiContent);
|
|
64
|
+
}
|
|
65
|
+
const controllerFiles = await (0, utils_1.findControllerFiles)(serverDir);
|
|
66
|
+
const sourceMap = await (0, utils_1.buildSourceMap)(controllerFiles, processControllerFile);
|
|
67
|
+
const enhanced = (0, utils_1.enhanceOpenApiPaths)(openapi, sourceMap);
|
|
68
|
+
if (writeFile) {
|
|
69
|
+
await node_fs_1.promises.writeFile(openapiPath, JSON.stringify(openapi, null, 2) + '\n', 'utf-8');
|
|
70
|
+
}
|
|
71
|
+
const duration = Date.now() - startTime;
|
|
72
|
+
return {
|
|
73
|
+
openapi,
|
|
74
|
+
stats: {
|
|
75
|
+
duration,
|
|
76
|
+
controllersFound: controllerFiles.length,
|
|
77
|
+
endpointsExtracted: sourceMap.size,
|
|
78
|
+
endpointsEnhanced: enhanced,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Process a single controller file
|
|
84
|
+
*/
|
|
85
|
+
async function processControllerFile(filePath) {
|
|
86
|
+
const relativePath = node_path_1.default.relative(process.cwd(), filePath);
|
|
87
|
+
// Parse file
|
|
88
|
+
const content = await node_fs_1.promises.readFile(filePath, 'utf-8');
|
|
89
|
+
const sourceFile = typescript_1.default.createSourceFile(filePath, content, typescript_1.default.ScriptTarget.Latest, true);
|
|
90
|
+
return extractControllerMetadata(sourceFile, relativePath);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Extract controller metadata from TypeScript source file
|
|
94
|
+
*/
|
|
95
|
+
function extractControllerMetadata(sourceFile, filePath) {
|
|
96
|
+
const metadata = new Map();
|
|
97
|
+
let controllerPath = '';
|
|
98
|
+
let className = '';
|
|
99
|
+
// Helper function to get decorators from both old and new TypeScript APIs
|
|
100
|
+
function getDecorators(node) {
|
|
101
|
+
// TypeScript 5.x: decorators are in modifiers array
|
|
102
|
+
if ('modifiers' in node && Array.isArray(node.modifiers)) {
|
|
103
|
+
return node.modifiers.filter((mod) => mod.kind === typescript_1.default.SyntaxKind.Decorator);
|
|
104
|
+
}
|
|
105
|
+
// TypeScript 4.x: decorators are in decorators array
|
|
106
|
+
if ('decorators' in node && Array.isArray(node.decorators)) {
|
|
107
|
+
return node.decorators;
|
|
108
|
+
}
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
function visit(node) {
|
|
112
|
+
// Extract @Controller decorator and its path
|
|
113
|
+
if (typescript_1.default.isClassDeclaration(node)) {
|
|
114
|
+
const decorators = getDecorators(node);
|
|
115
|
+
// Extract class name
|
|
116
|
+
if (node.name) {
|
|
117
|
+
className = node.name.getText(sourceFile);
|
|
118
|
+
}
|
|
119
|
+
for (const decorator of decorators) {
|
|
120
|
+
if (typescript_1.default.isCallExpression(decorator.expression)) {
|
|
121
|
+
const expression = decorator.expression;
|
|
122
|
+
const decoratorName = expression.expression.getText(sourceFile);
|
|
123
|
+
if (decoratorName === 'Controller') {
|
|
124
|
+
if (expression.arguments.length > 0) {
|
|
125
|
+
const arg = expression.arguments[0];
|
|
126
|
+
if (typescript_1.default.isStringLiteral(arg)) {
|
|
127
|
+
controllerPath = arg.text;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Extract methods with HTTP decorators
|
|
135
|
+
if (typescript_1.default.isMethodDeclaration(node) && node.name) {
|
|
136
|
+
const methodName = node.name.getText(sourceFile);
|
|
137
|
+
let httpMethod = '';
|
|
138
|
+
let routePath = '';
|
|
139
|
+
const { line } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
140
|
+
const decorators = getDecorators(node);
|
|
141
|
+
for (const decorator of decorators) {
|
|
142
|
+
if (typescript_1.default.isCallExpression(decorator.expression)) {
|
|
143
|
+
const decoratorName = decorator.expression.expression.getText(sourceFile);
|
|
144
|
+
if (['Get', 'Post', 'Put', 'Delete', 'Patch', 'Options', 'Head', 'All'].includes(decoratorName)) {
|
|
145
|
+
httpMethod = decoratorName.toLowerCase();
|
|
146
|
+
if (decorator.expression.arguments.length > 0) {
|
|
147
|
+
const arg = decorator.expression.arguments[0];
|
|
148
|
+
if (typescript_1.default.isStringLiteral(arg)) {
|
|
149
|
+
routePath = arg.text;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (httpMethod && methodName && className) {
|
|
156
|
+
const operationId = `${className}_${methodName}`;
|
|
157
|
+
metadata.set(operationId, {
|
|
158
|
+
file: filePath,
|
|
159
|
+
line: line + 1,
|
|
160
|
+
method: httpMethod,
|
|
161
|
+
controllerPath,
|
|
162
|
+
routePath,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
typescript_1.default.forEachChild(node, visit);
|
|
167
|
+
}
|
|
168
|
+
visit(sourceFile);
|
|
169
|
+
return metadata;
|
|
170
|
+
}
|
|
171
|
+
// --- API Route Parsing (synchronous, for build-time injection) ---
|
|
172
|
+
const HTTP_DECORATOR_NAMES = ['Get', 'Post', 'Put', 'Delete', 'Patch', 'Options', 'Head', 'All'];
|
|
173
|
+
function findControllerFilesSync(dir) {
|
|
174
|
+
const files = [];
|
|
175
|
+
function scan(currentDir) {
|
|
176
|
+
let entries;
|
|
177
|
+
try {
|
|
178
|
+
entries = fsSync.readdirSync(currentDir, { withFileTypes: true });
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
for (const entry of entries) {
|
|
184
|
+
const fullPath = node_path_1.default.join(currentDir, entry.name);
|
|
185
|
+
if (entry.isDirectory()) {
|
|
186
|
+
scan(fullPath);
|
|
187
|
+
}
|
|
188
|
+
else if (entry.isFile() && entry.name.endsWith('.controller.ts')) {
|
|
189
|
+
files.push(fullPath);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
scan(dir);
|
|
194
|
+
return files;
|
|
195
|
+
}
|
|
196
|
+
function normalizeApiPath(controllerPath, routePath) {
|
|
197
|
+
const combined = routePath ? `${controllerPath}/${routePath}` : controllerPath;
|
|
198
|
+
let normalized = combined.replace(/\/+/g, '/');
|
|
199
|
+
if (!normalized.startsWith('/'))
|
|
200
|
+
normalized = `/${normalized}`;
|
|
201
|
+
if (normalized.length > 1 && normalized.endsWith('/'))
|
|
202
|
+
normalized = normalized.slice(0, -1);
|
|
203
|
+
return normalized;
|
|
204
|
+
}
|
|
205
|
+
function parseControllerRoutes(filePath) {
|
|
206
|
+
let content;
|
|
207
|
+
try {
|
|
208
|
+
content = fsSync.readFileSync(filePath, 'utf-8');
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
return [];
|
|
212
|
+
}
|
|
213
|
+
const sourceFile = typescript_1.default.createSourceFile(filePath, content, typescript_1.default.ScriptTarget.Latest, true);
|
|
214
|
+
const routes = [];
|
|
215
|
+
function getDecoratorsCompat(node) {
|
|
216
|
+
if ('modifiers' in node && Array.isArray(node.modifiers)) {
|
|
217
|
+
return node.modifiers.filter((mod) => mod.kind === typescript_1.default.SyntaxKind.Decorator);
|
|
218
|
+
}
|
|
219
|
+
if ('decorators' in node && Array.isArray(node.decorators)) {
|
|
220
|
+
return node.decorators;
|
|
221
|
+
}
|
|
222
|
+
return [];
|
|
223
|
+
}
|
|
224
|
+
function getStringArg(expr) {
|
|
225
|
+
if (expr.arguments.length > 0 && typescript_1.default.isStringLiteral(expr.arguments[0])) {
|
|
226
|
+
return expr.arguments[0].text;
|
|
227
|
+
}
|
|
228
|
+
return '';
|
|
229
|
+
}
|
|
230
|
+
function visit(node) {
|
|
231
|
+
if (typescript_1.default.isClassDeclaration(node)) {
|
|
232
|
+
let controllerPath = '';
|
|
233
|
+
for (const dec of getDecoratorsCompat(node)) {
|
|
234
|
+
if (typescript_1.default.isCallExpression(dec.expression)) {
|
|
235
|
+
const name = dec.expression.expression.getText(sourceFile);
|
|
236
|
+
if (name === 'Controller') {
|
|
237
|
+
controllerPath = getStringArg(dec.expression);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (const member of node.members) {
|
|
243
|
+
if (!typescript_1.default.isMethodDeclaration(member))
|
|
244
|
+
continue;
|
|
245
|
+
for (const dec of getDecoratorsCompat(member)) {
|
|
246
|
+
if (!typescript_1.default.isCallExpression(dec.expression))
|
|
247
|
+
continue;
|
|
248
|
+
const decoratorName = dec.expression.expression.getText(sourceFile);
|
|
249
|
+
if (!HTTP_DECORATOR_NAMES.includes(decoratorName))
|
|
250
|
+
continue;
|
|
251
|
+
const routePath = getStringArg(dec.expression);
|
|
252
|
+
const fullPath = normalizeApiPath(controllerPath, routePath);
|
|
253
|
+
const method = decoratorName === 'All' ? '*' : decoratorName.toUpperCase();
|
|
254
|
+
routes.push({ method, path: fullPath });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
typescript_1.default.forEachChild(node, visit);
|
|
259
|
+
}
|
|
260
|
+
visit(sourceFile);
|
|
261
|
+
return routes;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Scan serverDir for NestJS controller files and extract all parameterized API routes.
|
|
265
|
+
* Synchronous — safe to call at preset config time (before bundling).
|
|
266
|
+
*/
|
|
267
|
+
function parseApiRoutes(serverDir) {
|
|
268
|
+
const resolvedDir = node_path_1.default.resolve(serverDir);
|
|
269
|
+
const controllerFiles = findControllerFilesSync(resolvedDir);
|
|
270
|
+
const routes = [];
|
|
271
|
+
for (const filePath of controllerFiles) {
|
|
272
|
+
routes.push(...parseControllerRoutes(filePath));
|
|
273
|
+
}
|
|
274
|
+
return routes;
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=services.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.js","sourceRoot":"","sources":["../../../src/server-log/openapi/services.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,oEAoCC;AAkND,wCAQC;AAzQD,qCAAyC;AACzC,gDAAkC;AAClC,0DAA6B;AAC7B,4DAA4B;AAE5B,mCAAmF;AAEnF;;;GAGG;AACI,KAAK,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AC,UAAU,CAAC,CAAC;oBACpE,IAAI,CAAC,oBAAoB,CAAC,QAAQ,CAAC,aAAa,CAAC;wBAAE,SAAS;oBAE5D,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;oBAC/C,MAAM,QAAQ,GAAG,gBAAgB,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;oBAC7D,MAAM,MAAM,GAAG,aAAa,KAAK,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC;oBAC3E,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;gBAC1C,CAAC;YACH,CAAC;QACH,CAAC;QACD,oBAAE,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,CAAC;IAClB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,SAAgB,cAAc,CAAC,SAAiB;IAC9C,MAAM,WAAW,GAAG,mBAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAC5C,MAAM,eAAe,GAAG,uBAAuB,CAAC,WAAW,CAAC,CAAC;IAC7D,MAAM,MAAM,GAA4C,EAAE,CAAC;IAC3D,KAAK,MAAM,QAAQ,IAAI,eAAe,EAAE,CAAC;QACvC,MAAM,CAAC,IAAI,CAAC,GAAG,qBAAqB,CAAC,QAAQ,CAAC,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import type { MiddlewareContext } from '../types';
|
|
3
|
+
export declare function createOpenapiSpecHandler(openapiSpecFilePath: string): (_req: Request, res: Response, context: MiddlewareContext) => Promise<Response<any, Record<string, any>> | undefined>;
|
|
4
|
+
//# sourceMappingURL=spec-controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spec-controller.d.ts","sourceRoot":"","sources":["../../../src/server-log/openapi/spec-controller.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAclD,wBAAgB,wBAAwB,CAAC,mBAAmB,EAAE,MAAM,IAGpD,MAAM,OAAO,EAAE,KAAK,QAAQ,EAAE,SAAS,iBAAiB,6DAqDvE"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createOpenapiSpecHandler = createOpenapiSpecHandler;
|
|
7
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
9
|
+
const MAX_REF_DEPTH = 5;
|
|
10
|
+
const EMPTY_OPENAPI_SPEC = {
|
|
11
|
+
openapi: '3.0.0',
|
|
12
|
+
info: { title: '开放 API', version: '0.0.0' },
|
|
13
|
+
paths: {},
|
|
14
|
+
components: { schemas: {} },
|
|
15
|
+
};
|
|
16
|
+
function createOpenapiSpecHandler(openapiSpecFilePath) {
|
|
17
|
+
let cache = null;
|
|
18
|
+
return async (_req, res, context) => {
|
|
19
|
+
try {
|
|
20
|
+
let fileBuffer;
|
|
21
|
+
try {
|
|
22
|
+
fileBuffer = await promises_1.default.readFile(openapiSpecFilePath, 'utf-8');
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
if (err?.code === 'ENOENT') {
|
|
26
|
+
return res.json(EMPTY_OPENAPI_SPEC);
|
|
27
|
+
}
|
|
28
|
+
throw err;
|
|
29
|
+
}
|
|
30
|
+
const currentHash = node_crypto_1.default.createHash('md5').update(fileBuffer).digest('hex');
|
|
31
|
+
if (cache && cache.fileHash === currentHash) {
|
|
32
|
+
return res.json(cache.data);
|
|
33
|
+
}
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = JSON.parse(fileBuffer);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
const message = err instanceof Error ? err.message : 'Invalid JSON';
|
|
40
|
+
return res.status(500).json({
|
|
41
|
+
error: 'Failed to load OpenAPI spec',
|
|
42
|
+
message: `${openapiSpecFilePath} invalid: ${message}`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (!parsed || typeof parsed !== 'object' || !parsed.paths || typeof parsed.paths !== 'object') {
|
|
46
|
+
return res.status(500).json({
|
|
47
|
+
error: 'Failed to load OpenAPI spec',
|
|
48
|
+
message: `${openapiSpecFilePath} missing "paths"`,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const result = buildFilteredSpec(parsed, context.basePath ?? '');
|
|
52
|
+
cache = {
|
|
53
|
+
data: result,
|
|
54
|
+
fileHash: currentHash,
|
|
55
|
+
};
|
|
56
|
+
res.json(result);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
61
|
+
res.status(500).json({
|
|
62
|
+
error: 'Failed to load OpenAPI spec',
|
|
63
|
+
message,
|
|
64
|
+
});
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function buildFilteredSpec(spec, globalPrefix) {
|
|
70
|
+
const schemas = (spec.components?.schemas ?? {});
|
|
71
|
+
const paths = (spec.paths ?? {});
|
|
72
|
+
const filteredPaths = {};
|
|
73
|
+
const cleanPrefix = globalPrefix.replace(/\/+$/, '');
|
|
74
|
+
for (const [pathKey, methods] of Object.entries(paths)) {
|
|
75
|
+
const cleanPath = stripPrefix(pathKey, cleanPrefix);
|
|
76
|
+
if (!cleanPath.startsWith('/openapi'))
|
|
77
|
+
continue;
|
|
78
|
+
filteredPaths[cleanPath] = {};
|
|
79
|
+
for (const [method, operation] of Object.entries(methods)) {
|
|
80
|
+
if (['parameters', 'summary', 'description', 'servers'].includes(method))
|
|
81
|
+
continue;
|
|
82
|
+
filteredPaths[cleanPath][method] = resolveRefsDeep(structuredClone(operation), schemas);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
openapi: spec.openapi ?? '3.0.0',
|
|
87
|
+
paths: filteredPaths,
|
|
88
|
+
components: {
|
|
89
|
+
schemas: resolveAllSchemaRefs(structuredClone(schemas)),
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function stripPrefix(pathStr, prefix) {
|
|
94
|
+
if (prefix && pathStr.startsWith(prefix)) {
|
|
95
|
+
return pathStr.slice(prefix.length);
|
|
96
|
+
}
|
|
97
|
+
return pathStr;
|
|
98
|
+
}
|
|
99
|
+
function resolveRefsDeep(obj, schemas, depth = 0) {
|
|
100
|
+
if (depth > MAX_REF_DEPTH || obj === null || obj === undefined)
|
|
101
|
+
return obj;
|
|
102
|
+
if (typeof obj !== 'object')
|
|
103
|
+
return obj;
|
|
104
|
+
if (Array.isArray(obj)) {
|
|
105
|
+
return obj.map((item) => resolveRefsDeep(item, schemas, depth));
|
|
106
|
+
}
|
|
107
|
+
const record = obj;
|
|
108
|
+
if (typeof record['$ref'] === 'string') {
|
|
109
|
+
const resolved = resolveRef(record['$ref'], schemas);
|
|
110
|
+
if (resolved !== undefined) {
|
|
111
|
+
return resolveRefsDeep(structuredClone(resolved), schemas, depth + 1);
|
|
112
|
+
}
|
|
113
|
+
return record;
|
|
114
|
+
}
|
|
115
|
+
const result = {};
|
|
116
|
+
for (const [key, value] of Object.entries(record)) {
|
|
117
|
+
result[key] = resolveRefsDeep(value, schemas, depth);
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
function resolveRef(ref, schemas) {
|
|
122
|
+
const prefix = '#/components/schemas/';
|
|
123
|
+
if (ref.startsWith(prefix)) {
|
|
124
|
+
const name = ref.slice(prefix.length);
|
|
125
|
+
return schemas[name];
|
|
126
|
+
}
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
function resolveAllSchemaRefs(schemas) {
|
|
130
|
+
const result = {};
|
|
131
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
132
|
+
result[name] = resolveRefsDeep(schema, schemas);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=spec-controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spec-controller.js","sourceRoot":"","sources":["../../../src/server-log/openapi/spec-controller.ts"],"names":[],"mappings":";;;;;AAmBA,4DAwDC;AA3ED,gEAAkC;AAClC,8DAAiC;AAMjC,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB,MAAM,kBAAkB,GAAG;IACzB,OAAO,EAAE,OAAO;IAChB,IAAI,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE;IAC3C,KAAK,EAAE,EAAE;IACT,UAAU,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;CAC5B,CAAC;AAKF,SAAgB,wBAAwB,CAAC,mBAA2B;IAClE,IAAI,KAAK,GAAwB,IAAI,CAAC;IAEtC,OAAO,KAAK,EAAE,IAAa,EAAE,GAAa,EAAE,OAA0B,EAAE,EAAE;QACxE,IAAI,CAAC;YACH,IAAI,UAAkB,CAAC;YACvB,IAAI,CAAC;gBACH,UAAU,GAAG,MAAM,kBAAE,CAAC,QAAQ,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;YAC/D,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,IAAK,GAA6B,EAAE,IAAI,KAAK,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+
openapiFilePath: string;
|
|
7
|
+
/** Path to the OpenAPI spec source file (when set, serves /openapi/spec with /openapi/* filtered and $refs resolved) */
|
|
8
|
+
openapiSpecFilePath?: string;
|
|
9
|
+
/** Enable source code enhancement */
|
|
10
|
+
enableEnhancement?: boolean;
|
|
11
|
+
/** Server directory for source code scanning (defaults to context.rootDir) */
|
|
12
|
+
serverDir?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Source file information for API endpoint
|
|
16
|
+
*/
|
|
17
|
+
export interface SourceInfo {
|
|
18
|
+
file: string;
|
|
19
|
+
line: number;
|
|
20
|
+
method: string;
|
|
21
|
+
controllerPath: string;
|
|
22
|
+
routePath: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for enhancing OpenAPI specification
|
|
26
|
+
*/
|
|
27
|
+
export interface EnhanceOptions {
|
|
28
|
+
openapiPath?: string;
|
|
29
|
+
serverDir?: string;
|
|
30
|
+
openapiData?: any;
|
|
31
|
+
writeFile?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Statistics about the enhancement process
|
|
35
|
+
*/
|
|
36
|
+
export interface EnhanceStats {
|
|
37
|
+
duration: number;
|
|
38
|
+
controllersFound: number;
|
|
39
|
+
endpointsExtracted: number;
|
|
40
|
+
endpointsEnhanced: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Result of OpenAPI enhancement
|
|
44
|
+
*/
|
|
45
|
+
export interface EnhanceResult {
|
|
46
|
+
openapi: any;
|
|
47
|
+
stats: EnhanceStats;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* OpenAPI cache entry
|
|
51
|
+
*/
|
|
52
|
+
export interface OpenapiCache {
|
|
53
|
+
data: any;
|
|
54
|
+
fileHash: string;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/server-log/openapi/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,oCAAoC;IACpC,eAAe,EAAE,MAAM,CAAC;IACxB,wHAAwH;IACxH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,qCAAqC;IACrC,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,8EAA8E;IAC9E,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,GAAG,CAAC;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,EAAE,MAAM,CAAC;IACzB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,GAAG,CAAC;IACb,KAAK,EAAE,YAAY,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,GAAG,CAAC;IACV,QAAQ,EAAE,MAAM,CAAC;CAClB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/server-log/openapi/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { SourceInfo } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Find all controller files in a directory
|
|
4
|
+
*/
|
|
5
|
+
export declare function findControllerFiles(dir: string): Promise<string[]>;
|
|
6
|
+
/**
|
|
7
|
+
* Build source map from controller files
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildSourceMap(controllerFiles: string[], processFile: (filePath: string) => Promise<Map<string, SourceInfo>>): Promise<Map<string, SourceInfo>>;
|
|
10
|
+
/**
|
|
11
|
+
* Enhance OpenAPI paths with source information
|
|
12
|
+
*/
|
|
13
|
+
export declare function enhanceOpenApiPaths(openapi: any, sourceMap: Map<string, SourceInfo>): number;
|
|
14
|
+
/**
|
|
15
|
+
* Transform OpenAPI paths by removing basePath prefix
|
|
16
|
+
*/
|
|
17
|
+
export declare function transformOpenapiPaths(openapi: any, basePath: string): any;
|
|
18
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/server-log/openapi/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C;;GAEG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAmBxE;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,eAAe,EAAE,MAAM,EAAE,EACzB,WAAW,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,GAClE,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAqBlC;AAqCD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,MAAM,CAyB5F;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,GAAG,GAAG,CAgBzE"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.findControllerFiles = findControllerFiles;
|
|
7
|
+
exports.buildSourceMap = buildSourceMap;
|
|
8
|
+
exports.enhanceOpenApiPaths = enhanceOpenApiPaths;
|
|
9
|
+
exports.transformOpenapiPaths = transformOpenapiPaths;
|
|
10
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
11
|
+
const node_fs_1 = require("node:fs");
|
|
12
|
+
/**
|
|
13
|
+
* Find all controller files in a directory
|
|
14
|
+
*/
|
|
15
|
+
async function findControllerFiles(dir) {
|
|
16
|
+
const files = [];
|
|
17
|
+
async function scan(currentDir) {
|
|
18
|
+
const entries = await node_fs_1.promises.readdir(currentDir, { withFileTypes: true });
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
const fullPath = node_path_1.default.join(currentDir, entry.name);
|
|
21
|
+
if (entry.isDirectory()) {
|
|
22
|
+
await scan(fullPath);
|
|
23
|
+
}
|
|
24
|
+
else if (entry.isFile() && entry.name.endsWith('.controller.ts')) {
|
|
25
|
+
files.push(fullPath);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
await scan(dir);
|
|
30
|
+
return files;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Build source map from controller files
|
|
34
|
+
*/
|
|
35
|
+
async function buildSourceMap(controllerFiles, processFile) {
|
|
36
|
+
const sourceMap = new Map();
|
|
37
|
+
// Process files in parallel with a concurrency limit
|
|
38
|
+
const concurrency = 10;
|
|
39
|
+
const results = [];
|
|
40
|
+
for (let i = 0; i < controllerFiles.length; i += concurrency) {
|
|
41
|
+
const batch = controllerFiles.slice(i, i + concurrency);
|
|
42
|
+
const batchResults = await Promise.all(batch.map((filePath) => processFile(filePath)));
|
|
43
|
+
results.push(...batchResults);
|
|
44
|
+
}
|
|
45
|
+
// Merge results
|
|
46
|
+
for (const metadata of results) {
|
|
47
|
+
for (const [operationId, info] of metadata.entries()) {
|
|
48
|
+
sourceMap.set(operationId, info);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return sourceMap;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Try to match operationId with different formats
|
|
55
|
+
* Supports:
|
|
56
|
+
* - Direct match: ClassName_methodName
|
|
57
|
+
* - Camel case: classNameMethodName
|
|
58
|
+
* - Method only: methodName
|
|
59
|
+
*/
|
|
60
|
+
function findSourceInfo(operationId, sourceMap) {
|
|
61
|
+
// Try direct match first
|
|
62
|
+
const directMatch = sourceMap.get(operationId);
|
|
63
|
+
if (directMatch) {
|
|
64
|
+
return directMatch;
|
|
65
|
+
}
|
|
66
|
+
// Try matching with different formats
|
|
67
|
+
for (const [key, value] of sourceMap.entries()) {
|
|
68
|
+
// key format: ClassName_methodName
|
|
69
|
+
const [className, methodName] = key.split('_');
|
|
70
|
+
if (!className || !methodName)
|
|
71
|
+
continue;
|
|
72
|
+
// Try camelCase format: classNameMethodName
|
|
73
|
+
const camelCaseId = className.charAt(0).toLowerCase() + className.slice(1) + methodName.charAt(0).toUpperCase() + methodName.slice(1);
|
|
74
|
+
if (operationId === camelCaseId) {
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
// Try method name only
|
|
78
|
+
if (operationId === methodName) {
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Enhance OpenAPI paths with source information
|
|
86
|
+
*/
|
|
87
|
+
function enhanceOpenApiPaths(openapi, sourceMap) {
|
|
88
|
+
let enhancedCount = 0;
|
|
89
|
+
if (!openapi.paths) {
|
|
90
|
+
return enhancedCount;
|
|
91
|
+
}
|
|
92
|
+
for (const pathItem of Object.values(openapi.paths)) {
|
|
93
|
+
if (!pathItem || typeof pathItem !== 'object')
|
|
94
|
+
continue;
|
|
95
|
+
for (const operation of Object.values(pathItem)) {
|
|
96
|
+
if (operation && typeof operation === 'object' && 'operationId' in operation) {
|
|
97
|
+
const sourceInfo = findSourceInfo(operation.operationId, sourceMap);
|
|
98
|
+
if (sourceInfo) {
|
|
99
|
+
operation['x-source'] = {
|
|
100
|
+
file: sourceInfo.file,
|
|
101
|
+
line: sourceInfo.line,
|
|
102
|
+
};
|
|
103
|
+
enhancedCount++;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return enhancedCount;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Transform OpenAPI paths by removing basePath prefix
|
|
112
|
+
*/
|
|
113
|
+
function transformOpenapiPaths(openapi, basePath) {
|
|
114
|
+
if (basePath === '/' || !openapi.paths) {
|
|
115
|
+
return openapi;
|
|
116
|
+
}
|
|
117
|
+
const newPaths = {};
|
|
118
|
+
Object.keys(openapi.paths).forEach((key) => {
|
|
119
|
+
const staticApiKey = key.startsWith(basePath) ? key.slice(basePath.length) : key;
|
|
120
|
+
newPaths[staticApiKey] = openapi.paths[key];
|
|
121
|
+
});
|
|
122
|
+
return {
|
|
123
|
+
...openapi,
|
|
124
|
+
paths: newPaths,
|
|
125
|
+
basePath,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=utils.js.map
|