@t0.labs/daxta 0.2.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 +40 -0
- package/assets/favicon.png +0 -0
- package/assets/viewer.html +1835 -0
- package/bin/daxta.js +2 -0
- package/dist/build/build-spec.d.ts +28 -0
- package/dist/build/build-spec.js +704 -0
- package/dist/build/incremental.d.ts +12 -0
- package/dist/build/incremental.js +121 -0
- package/dist/call/index.d.ts +24 -0
- package/dist/call/index.js +204 -0
- package/dist/catalog/index.d.ts +27 -0
- package/dist/catalog/index.js +111 -0
- package/dist/cli/install.d.ts +6 -0
- package/dist/cli/install.js +333 -0
- package/dist/cli/main.d.ts +1 -0
- package/dist/cli/main.js +102 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.js +106 -0
- package/dist/fields/dto-fields.d.ts +4 -0
- package/dist/fields/dto-fields.js +372 -0
- package/dist/fields/export-fields.d.ts +23 -0
- package/dist/fields/export-fields.js +218 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +42 -0
- package/dist/jest/global-setup.d.ts +1 -0
- package/dist/jest/global-setup.js +7 -0
- package/dist/jest/global-teardown.d.ts +1 -0
- package/dist/jest/global-teardown.js +20 -0
- package/dist/jest/reporter.d.ts +7 -0
- package/dist/jest/reporter.js +29 -0
- package/dist/path.util.d.ts +6 -0
- package/dist/path.util.js +108 -0
- package/dist/recorder/index.d.ts +15 -0
- package/dist/recorder/index.js +176 -0
- package/dist/register.d.ts +1 -0
- package/dist/register.js +27 -0
- package/dist/serve/index.d.ts +9 -0
- package/dist/serve/index.js +126 -0
- package/dist/serve/middleware.d.ts +38 -0
- package/dist/serve/middleware.js +131 -0
- package/dist/serve/paths.d.ts +7 -0
- package/dist/serve/paths.js +66 -0
- package/dist/serve/proxy.d.ts +16 -0
- package/dist/serve/proxy.js +52 -0
- package/dist/serve/spec-utils.d.ts +2 -0
- package/dist/serve/spec-utils.js +27 -0
- package/dist/setup.d.ts +1 -0
- package/dist/setup.js +7 -0
- package/package.json +75 -0
|
@@ -0,0 +1,372 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.dtoRequired = dtoRequired;
|
|
37
|
+
/**
|
|
38
|
+
* Resolve Required / Optional from Nest controllers + class-validator DTO metadata.
|
|
39
|
+
* Controllers are parsed via TypeScript AST (not executed). DTO classes are loaded
|
|
40
|
+
* only to read reflect-metadata / class-validator / class-transformer.
|
|
41
|
+
*/
|
|
42
|
+
const fs_1 = require("fs");
|
|
43
|
+
const path = __importStar(require("path"));
|
|
44
|
+
require("reflect-metadata");
|
|
45
|
+
const storage_1 = require("class-transformer/cjs/storage");
|
|
46
|
+
const class_validator_1 = require("class-validator");
|
|
47
|
+
const ts = __importStar(require("typescript"));
|
|
48
|
+
const config_1 = require("../config");
|
|
49
|
+
function controllerRoot() {
|
|
50
|
+
return (0, config_1.getConfig)().controllersRoot;
|
|
51
|
+
}
|
|
52
|
+
const HTTP_DECORATORS = new Set(['Get', 'Post', 'Put', 'Patch', 'Delete', 'Options', 'Head', 'All']);
|
|
53
|
+
const PARAM_DECORATORS = {
|
|
54
|
+
Body: 'body',
|
|
55
|
+
Query: 'query',
|
|
56
|
+
Param: 'param',
|
|
57
|
+
};
|
|
58
|
+
let routeCache = null;
|
|
59
|
+
const requiredCache = new Map();
|
|
60
|
+
function walkFiles(dir, suffix, out = []) {
|
|
61
|
+
if (!(0, fs_1.existsSync)(dir))
|
|
62
|
+
return out;
|
|
63
|
+
for (const name of (0, fs_1.readdirSync)(dir)) {
|
|
64
|
+
const full = path.join(dir, name);
|
|
65
|
+
const stat = (0, fs_1.statSync)(full);
|
|
66
|
+
if (stat.isDirectory())
|
|
67
|
+
walkFiles(full, suffix, out);
|
|
68
|
+
else if (name.endsWith(suffix))
|
|
69
|
+
out.push(full);
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
function decoratorName(node) {
|
|
74
|
+
const expr = node.expression;
|
|
75
|
+
if (ts.isCallExpression(expr) && ts.isIdentifier(expr.expression))
|
|
76
|
+
return expr.expression.text;
|
|
77
|
+
if (ts.isIdentifier(expr))
|
|
78
|
+
return expr.text;
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
function decoratorArgs(node) {
|
|
82
|
+
const expr = node.expression;
|
|
83
|
+
if (ts.isCallExpression(expr))
|
|
84
|
+
return expr.arguments;
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
function litString(node) {
|
|
88
|
+
if (!node)
|
|
89
|
+
return undefined;
|
|
90
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
91
|
+
return node.text;
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
function objectProp(obj, key) {
|
|
95
|
+
for (const prop of obj.properties) {
|
|
96
|
+
if (!ts.isPropertyAssignment(prop))
|
|
97
|
+
continue;
|
|
98
|
+
const name = ts.isIdentifier(prop.name) ? prop.name.text : ts.isStringLiteral(prop.name) ? prop.name.text : undefined;
|
|
99
|
+
if (name === key)
|
|
100
|
+
return prop.initializer;
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
function normalizeSegments(...parts) {
|
|
105
|
+
const segments = parts
|
|
106
|
+
.flatMap((part) => String(part ?? '').split('/'))
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
.map((segment) => segment.replace(/^:(.+)$/, '{$1}'));
|
|
109
|
+
return '/' + segments.join('/');
|
|
110
|
+
}
|
|
111
|
+
function resolveImportPath(fromFile, specifier) {
|
|
112
|
+
if (!specifier.startsWith('.') && !specifier.startsWith('src/')) {
|
|
113
|
+
// Only resolve project sources for DTO loading
|
|
114
|
+
if (specifier.startsWith('@'))
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
let resolved;
|
|
118
|
+
if (specifier.startsWith('src/'))
|
|
119
|
+
resolved = path.join(process.cwd(), specifier);
|
|
120
|
+
else
|
|
121
|
+
resolved = path.resolve(path.dirname(fromFile), specifier);
|
|
122
|
+
const candidates = [`${resolved}.ts`, path.join(resolved, 'index.ts'), resolved];
|
|
123
|
+
for (const candidate of candidates) {
|
|
124
|
+
if ((0, fs_1.existsSync)(candidate) && candidate.endsWith('.ts'))
|
|
125
|
+
return candidate;
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
function collectImports(source, filePath) {
|
|
130
|
+
const map = new Map();
|
|
131
|
+
for (const stmt of source.statements) {
|
|
132
|
+
if (!ts.isImportDeclaration(stmt) || !stmt.importClause)
|
|
133
|
+
continue;
|
|
134
|
+
const spec = litString(stmt.moduleSpecifier);
|
|
135
|
+
if (!spec)
|
|
136
|
+
continue;
|
|
137
|
+
const resolved = resolveImportPath(filePath, spec);
|
|
138
|
+
if (!resolved)
|
|
139
|
+
continue;
|
|
140
|
+
const clause = stmt.importClause;
|
|
141
|
+
if (clause.name)
|
|
142
|
+
map.set(clause.name.text, resolved);
|
|
143
|
+
const bindings = clause.namedBindings;
|
|
144
|
+
if (bindings && ts.isNamedImports(bindings)) {
|
|
145
|
+
for (const el of bindings.elements)
|
|
146
|
+
map.set(el.name.text, resolved);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return map;
|
|
150
|
+
}
|
|
151
|
+
let dtoLoader = null;
|
|
152
|
+
function getDtoLoader() {
|
|
153
|
+
if (dtoLoader)
|
|
154
|
+
return dtoLoader;
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
156
|
+
const { createJiti } = require('jiti');
|
|
157
|
+
const cwd = process.cwd();
|
|
158
|
+
const jiti = createJiti(__filename, {
|
|
159
|
+
interopDefault: true,
|
|
160
|
+
alias: {
|
|
161
|
+
src: path.join(cwd, 'src'),
|
|
162
|
+
test: path.join(cwd, 'test'),
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
dtoLoader = (id) => jiti(id);
|
|
166
|
+
return dtoLoader;
|
|
167
|
+
}
|
|
168
|
+
function loadDto(filePath, exportName) {
|
|
169
|
+
try {
|
|
170
|
+
const mod = getDtoLoader()(filePath);
|
|
171
|
+
const ctor = mod[exportName];
|
|
172
|
+
return typeof ctor === 'function' ? ctor : undefined;
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
console.warn(`DAxTA: could not load DTO ${exportName} from ${filePath}:`, error instanceof Error ? error.message : error);
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function parseController(filePath) {
|
|
180
|
+
const text = (0, fs_1.readFileSync)(filePath, 'utf8');
|
|
181
|
+
const source = ts.createSourceFile(filePath, text, ts.ScriptTarget.Latest, true);
|
|
182
|
+
const imports = collectImports(source, filePath);
|
|
183
|
+
const routes = [];
|
|
184
|
+
for (const stmt of source.statements) {
|
|
185
|
+
if (!ts.isClassDeclaration(stmt) || !stmt.name)
|
|
186
|
+
continue;
|
|
187
|
+
const ctrlDecorators = ts.getDecorators?.(stmt) ?? stmt.decorators ?? [];
|
|
188
|
+
const controller = ctrlDecorators.find((decorator) => decoratorName(decorator) === 'Controller');
|
|
189
|
+
if (!controller)
|
|
190
|
+
continue;
|
|
191
|
+
let controllerPath = '';
|
|
192
|
+
let versions = [''];
|
|
193
|
+
const args = decoratorArgs(controller);
|
|
194
|
+
const first = args?.[0];
|
|
195
|
+
if (first && (ts.isStringLiteral(first) || ts.isNoSubstitutionTemplateLiteral(first))) {
|
|
196
|
+
controllerPath = first.text;
|
|
197
|
+
}
|
|
198
|
+
else if (first && ts.isObjectLiteralExpression(first)) {
|
|
199
|
+
const pathExpr = objectProp(first, 'path');
|
|
200
|
+
const versionExpr = objectProp(first, 'version');
|
|
201
|
+
controllerPath = litString(pathExpr) ?? '';
|
|
202
|
+
if (versionExpr) {
|
|
203
|
+
if (ts.isStringLiteral(versionExpr) || ts.isNoSubstitutionTemplateLiteral(versionExpr))
|
|
204
|
+
versions = [versionExpr.text];
|
|
205
|
+
else if (ts.isArrayLiteralExpression(versionExpr)) {
|
|
206
|
+
versions = versionExpr.elements.map((el) => litString(el) ?? '').filter(Boolean);
|
|
207
|
+
if (!versions.length)
|
|
208
|
+
versions = [''];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
for (const member of stmt.members) {
|
|
213
|
+
if (!ts.isMethodDeclaration(member) || !member.name || !ts.isIdentifier(member.name))
|
|
214
|
+
continue;
|
|
215
|
+
const methodDecorators = ts.getDecorators?.(member) ?? member.decorators ?? [];
|
|
216
|
+
const http = methodDecorators.find((decorator) => HTTP_DECORATORS.has(decoratorName(decorator) ?? ''));
|
|
217
|
+
if (!http)
|
|
218
|
+
continue;
|
|
219
|
+
const httpMethod = (decoratorName(http) ?? 'All').toLowerCase();
|
|
220
|
+
const routePath = litString(decoratorArgs(http)?.[0]) ?? '';
|
|
221
|
+
let bodyName;
|
|
222
|
+
let queryName;
|
|
223
|
+
let paramName;
|
|
224
|
+
for (const param of member.parameters ?? []) {
|
|
225
|
+
const paramDecorators = ts.getDecorators?.(param) ?? param.decorators ?? [];
|
|
226
|
+
for (const decorator of paramDecorators) {
|
|
227
|
+
const kind = PARAM_DECORATORS[decoratorName(decorator) ?? ''];
|
|
228
|
+
if (!kind)
|
|
229
|
+
continue;
|
|
230
|
+
const typeNode = param.type;
|
|
231
|
+
if (!typeNode || !ts.isTypeReferenceNode(typeNode) || !ts.isIdentifier(typeNode.typeName))
|
|
232
|
+
continue;
|
|
233
|
+
const typeName = typeNode.typeName.text;
|
|
234
|
+
if (kind === 'body')
|
|
235
|
+
bodyName = typeName;
|
|
236
|
+
if (kind === 'query')
|
|
237
|
+
queryName = typeName;
|
|
238
|
+
if (kind === 'param')
|
|
239
|
+
paramName = typeName;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const resolve = (name) => {
|
|
243
|
+
if (!name)
|
|
244
|
+
return undefined;
|
|
245
|
+
const dtoFile = imports.get(name);
|
|
246
|
+
return dtoFile ? loadDto(dtoFile, name) : undefined;
|
|
247
|
+
};
|
|
248
|
+
for (const version of versions) {
|
|
249
|
+
const versionPrefix = version ? `v${version.replace(/^v/i, '')}` : '';
|
|
250
|
+
routes.push({
|
|
251
|
+
method: httpMethod === 'all' ? 'post' : httpMethod,
|
|
252
|
+
pathTemplate: normalizeSegments(versionPrefix, controllerPath, routePath),
|
|
253
|
+
body: resolve(bodyName),
|
|
254
|
+
query: resolve(queryName),
|
|
255
|
+
param: resolve(paramName),
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return routes;
|
|
261
|
+
}
|
|
262
|
+
function getRoutes() {
|
|
263
|
+
if (routeCache)
|
|
264
|
+
return routeCache;
|
|
265
|
+
const files = walkFiles(controllerRoot(), '.controller.ts');
|
|
266
|
+
routeCache = files.flatMap((file) => {
|
|
267
|
+
try {
|
|
268
|
+
return parseController(file);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
console.warn(`DAxTA: failed parsing controller ${file}:`, error instanceof Error ? error.message : error);
|
|
272
|
+
return [];
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
return routeCache;
|
|
276
|
+
}
|
|
277
|
+
function findRoute(method, pathTemplate) {
|
|
278
|
+
const keyMethod = method.toLowerCase();
|
|
279
|
+
return getRoutes().find((route) => route.method === keyMethod && route.pathTemplate === pathTemplate);
|
|
280
|
+
}
|
|
281
|
+
function isOptionalMetas(metas) {
|
|
282
|
+
return metas.some((meta) => meta.type === 'conditionalValidation' || meta.name === 'isOptional');
|
|
283
|
+
}
|
|
284
|
+
function requiredFromMetas(metas) {
|
|
285
|
+
if (!metas.length)
|
|
286
|
+
return undefined;
|
|
287
|
+
if (isOptionalMetas(metas))
|
|
288
|
+
return false;
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
function nestedCtor(parent, property) {
|
|
292
|
+
try {
|
|
293
|
+
const typeMeta = storage_1.defaultMetadataStorage.findTypeMetadata(parent, property);
|
|
294
|
+
const fromTransformer = typeMeta?.typeFunction?.();
|
|
295
|
+
if (typeof fromTransformer === 'function' && fromTransformer !== Array)
|
|
296
|
+
return fromTransformer;
|
|
297
|
+
}
|
|
298
|
+
catch {
|
|
299
|
+
// ignore
|
|
300
|
+
}
|
|
301
|
+
const design = Reflect.getMetadata('design:type', parent.prototype, property);
|
|
302
|
+
if (typeof design === 'function' && design !== Object && design !== Array && design !== String && design !== Number && design !== Boolean) {
|
|
303
|
+
return design;
|
|
304
|
+
}
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
function buildRequiredMap(ctor, prefix = '', seen = new Set()) {
|
|
308
|
+
const map = new Map();
|
|
309
|
+
if (seen.has(ctor))
|
|
310
|
+
return map;
|
|
311
|
+
seen.add(ctor);
|
|
312
|
+
const storage = (0, class_validator_1.getMetadataStorage)();
|
|
313
|
+
const metas = storage.getTargetValidationMetadatas(ctor, '', false, false);
|
|
314
|
+
const byProp = storage.groupByPropertyName(metas);
|
|
315
|
+
for (const property of Object.keys(byProp)) {
|
|
316
|
+
const fieldPath = prefix ? `${prefix}.${property}` : property;
|
|
317
|
+
const required = requiredFromMetas((byProp[property] ?? []).map((meta) => ({
|
|
318
|
+
propertyName: meta.propertyName,
|
|
319
|
+
type: meta.type,
|
|
320
|
+
name: meta.name,
|
|
321
|
+
})));
|
|
322
|
+
if (required !== undefined)
|
|
323
|
+
map.set(fieldPath, required);
|
|
324
|
+
const child = nestedCtor(ctor, property);
|
|
325
|
+
if (!child)
|
|
326
|
+
continue;
|
|
327
|
+
// object nest: creditCard.cardNumber
|
|
328
|
+
for (const [nestedPath, nestedRequired] of buildRequiredMap(child, fieldPath, new Set(seen))) {
|
|
329
|
+
map.set(nestedPath, nestedRequired);
|
|
330
|
+
}
|
|
331
|
+
// array nest as recorded in Fields: saleAgreements[].id
|
|
332
|
+
const arrayPrefix = `${fieldPath}[]`;
|
|
333
|
+
for (const [nestedPath, nestedRequired] of buildRequiredMap(child, arrayPrefix, new Set(seen))) {
|
|
334
|
+
map.set(nestedPath, nestedRequired);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return map;
|
|
338
|
+
}
|
|
339
|
+
function mapFor(method, pathTemplate, location) {
|
|
340
|
+
const route = findRoute(method, pathTemplate);
|
|
341
|
+
if (!route)
|
|
342
|
+
return undefined;
|
|
343
|
+
const ctor = location === 'body' ? route.body : location === 'query' ? route.query : location === 'path' ? route.param : undefined;
|
|
344
|
+
if (!ctor)
|
|
345
|
+
return undefined;
|
|
346
|
+
const cacheKey = `${method} ${pathTemplate} ${location} ${ctor.name}`;
|
|
347
|
+
const cached = requiredCache.get(cacheKey);
|
|
348
|
+
if (cached)
|
|
349
|
+
return cached;
|
|
350
|
+
const built = buildRequiredMap(ctor);
|
|
351
|
+
requiredCache.set(cacheKey, built);
|
|
352
|
+
return built;
|
|
353
|
+
}
|
|
354
|
+
/** Returns required flag from DTO metadata, or undefined when unknown (no DTO / no validators). */
|
|
355
|
+
function dtoRequired(method, pathTemplate, location, fieldName) {
|
|
356
|
+
if (location === 'header')
|
|
357
|
+
return undefined;
|
|
358
|
+
if (location === 'path' && !mapFor(method, pathTemplate, 'path')) {
|
|
359
|
+
// Path params in the URL template are structurally required when no Param DTO exists.
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
const map = mapFor(method, pathTemplate, location);
|
|
363
|
+
if (!map)
|
|
364
|
+
return undefined;
|
|
365
|
+
if (map.has(fieldName))
|
|
366
|
+
return map.get(fieldName);
|
|
367
|
+
// Tolerate recorded array paths vs object paths
|
|
368
|
+
const alt = fieldName.includes('[]') ? fieldName.replace(/\[\]/g, '') : null;
|
|
369
|
+
if (alt && map.has(alt))
|
|
370
|
+
return map.get(alt);
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FieldDoc, OperationDoc } from '../catalog';
|
|
2
|
+
import type { RecordedHit } from '../recorder';
|
|
3
|
+
export type FieldsFile = {
|
|
4
|
+
version: 1;
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
generatedAt: string;
|
|
7
|
+
operations: Array<{
|
|
8
|
+
method: string;
|
|
9
|
+
path: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
fields: FieldDoc[];
|
|
12
|
+
}>;
|
|
13
|
+
};
|
|
14
|
+
export declare function inferOperationFields(method: string, pathTemplate: string, opHits: RecordedHit[]): OperationDoc;
|
|
15
|
+
export declare function buildFieldsFile(filter?: {
|
|
16
|
+
method?: string;
|
|
17
|
+
path?: string;
|
|
18
|
+
}): FieldsFile;
|
|
19
|
+
export declare function writeFieldsFile(filter?: {
|
|
20
|
+
method?: string;
|
|
21
|
+
path?: string;
|
|
22
|
+
}, outFile?: string): string;
|
|
23
|
+
export declare function readFieldsFile(filePath?: string): FieldsFile;
|
|
@@ -0,0 +1,218 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.inferOperationFields = inferOperationFields;
|
|
37
|
+
exports.buildFieldsFile = buildFieldsFile;
|
|
38
|
+
exports.writeFieldsFile = writeFieldsFile;
|
|
39
|
+
exports.readFieldsFile = readFieldsFile;
|
|
40
|
+
const fs_1 = require("fs");
|
|
41
|
+
const path = __importStar(require("path"));
|
|
42
|
+
const config_1 = require("../config");
|
|
43
|
+
const build_spec_1 = require("../build/build-spec");
|
|
44
|
+
const dto_fields_1 = require("./dto-fields");
|
|
45
|
+
const path_util_1 = require("../path.util");
|
|
46
|
+
function valueTypeLabel(value) {
|
|
47
|
+
if (value === null)
|
|
48
|
+
return 'null';
|
|
49
|
+
if (Array.isArray(value)) {
|
|
50
|
+
const item = value.find((entry) => entry !== null && entry !== undefined);
|
|
51
|
+
if (item === undefined)
|
|
52
|
+
return 'array';
|
|
53
|
+
if (Array.isArray(item))
|
|
54
|
+
return 'array';
|
|
55
|
+
if (typeof item === 'object')
|
|
56
|
+
return 'array<object>';
|
|
57
|
+
if (typeof item === 'number')
|
|
58
|
+
return Number.isInteger(item) ? 'array<integer>' : 'array<number>';
|
|
59
|
+
return `array<${typeof item}>`;
|
|
60
|
+
}
|
|
61
|
+
if (typeof value === 'object')
|
|
62
|
+
return 'object';
|
|
63
|
+
if (typeof value === 'number')
|
|
64
|
+
return Number.isInteger(value) ? 'integer' : 'number';
|
|
65
|
+
return typeof value;
|
|
66
|
+
}
|
|
67
|
+
function collectBodyFields(value, prefix, add) {
|
|
68
|
+
if (value === null || value === undefined) {
|
|
69
|
+
if (prefix)
|
|
70
|
+
add({ name: prefix, in: 'body', type: 'null' });
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (Array.isArray(value)) {
|
|
74
|
+
if (prefix)
|
|
75
|
+
add({ name: prefix, in: 'body', type: valueTypeLabel(value) });
|
|
76
|
+
const sample = value.find((entry) => entry !== null && entry !== undefined);
|
|
77
|
+
if (sample === undefined)
|
|
78
|
+
return;
|
|
79
|
+
if (Array.isArray(sample)) {
|
|
80
|
+
collectBodyFields(sample, `${prefix}[]`, add);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (typeof sample === 'object') {
|
|
84
|
+
for (const [key, nested] of Object.entries(sample)) {
|
|
85
|
+
const nestedPath = `${prefix}[].${key}`;
|
|
86
|
+
if (nested !== null && typeof nested === 'object')
|
|
87
|
+
collectBodyFields(nested, nestedPath, add);
|
|
88
|
+
else
|
|
89
|
+
add({ name: nestedPath, in: 'body', type: valueTypeLabel(nested) });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (typeof value === 'object') {
|
|
95
|
+
if (prefix)
|
|
96
|
+
add({ name: prefix, in: 'body', type: 'object' });
|
|
97
|
+
for (const [key, nested] of Object.entries(value)) {
|
|
98
|
+
const nestedPath = prefix ? `${prefix}.${key}` : key;
|
|
99
|
+
if (nested !== null && typeof nested === 'object')
|
|
100
|
+
collectBodyFields(nested, nestedPath, add);
|
|
101
|
+
else
|
|
102
|
+
add({ name: nestedPath, in: 'body', type: valueTypeLabel(nested) });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function inferOperationFields(method, pathTemplate, opHits) {
|
|
107
|
+
const fields = [];
|
|
108
|
+
const seen = new Set();
|
|
109
|
+
const add = (field) => {
|
|
110
|
+
const key = `${field.in}:${field.name}`;
|
|
111
|
+
if (seen.has(key))
|
|
112
|
+
return;
|
|
113
|
+
seen.add(key);
|
|
114
|
+
const required = (0, dto_fields_1.dtoRequired)(method, pathTemplate, field.in, field.name);
|
|
115
|
+
fields.push({ ...field, ...(required === undefined ? {} : { required }) });
|
|
116
|
+
};
|
|
117
|
+
for (const name of (0, path_util_1.pathParamNames)(pathTemplate))
|
|
118
|
+
add({ name, in: 'path', type: 'string' });
|
|
119
|
+
for (const hit of opHits) {
|
|
120
|
+
for (const name of Object.keys(hit.query ?? {}))
|
|
121
|
+
add({ name, in: 'query', type: 'string' });
|
|
122
|
+
for (const name of Object.keys(hit.headers ?? {})) {
|
|
123
|
+
if (name === 'content-type')
|
|
124
|
+
continue;
|
|
125
|
+
add({ name, in: 'header', type: 'string' });
|
|
126
|
+
}
|
|
127
|
+
if (hit.reqBody && typeof hit.reqBody === 'object') {
|
|
128
|
+
collectBodyFields(hit.reqBody, '', add);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return { title: pathTemplate, fields };
|
|
132
|
+
}
|
|
133
|
+
function loadSpecOperations() {
|
|
134
|
+
const specPath = path.join((0, config_1.getConfig)().outDir, 'openapi.json');
|
|
135
|
+
if (!(0, fs_1.existsSync)(specPath))
|
|
136
|
+
return [];
|
|
137
|
+
const spec = JSON.parse((0, fs_1.readFileSync)(specPath, 'utf8'));
|
|
138
|
+
const ops = [];
|
|
139
|
+
for (const [pathTemplate, methods] of Object.entries(spec.paths ?? {})) {
|
|
140
|
+
for (const [method, operation] of Object.entries(methods)) {
|
|
141
|
+
if (!operation || typeof operation !== 'object')
|
|
142
|
+
continue;
|
|
143
|
+
const docs = operation['x-docs'];
|
|
144
|
+
ops.push({
|
|
145
|
+
method: method.toLowerCase(),
|
|
146
|
+
path: pathTemplate,
|
|
147
|
+
title: docs?.title ?? operation.summary,
|
|
148
|
+
fields: docs?.fields ?? [],
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return ops;
|
|
153
|
+
}
|
|
154
|
+
function buildFieldsFile(filter) {
|
|
155
|
+
const config = (0, config_1.getConfig)();
|
|
156
|
+
const fromSpec = loadSpecOperations();
|
|
157
|
+
const hits = (0, build_spec_1.loadHits)();
|
|
158
|
+
const grouped = new Map();
|
|
159
|
+
for (const hit of hits) {
|
|
160
|
+
const key = `${hit.method} ${hit.path}`;
|
|
161
|
+
const list = grouped.get(key) ?? [];
|
|
162
|
+
list.push(hit);
|
|
163
|
+
grouped.set(key, list);
|
|
164
|
+
}
|
|
165
|
+
const byKey = new Map();
|
|
166
|
+
for (const op of fromSpec) {
|
|
167
|
+
byKey.set(`${op.method} ${op.path}`, op);
|
|
168
|
+
}
|
|
169
|
+
for (const [key, opHits] of grouped) {
|
|
170
|
+
const method = key.split(' ')[0];
|
|
171
|
+
const pathTemplate = key.slice(method.length + 1);
|
|
172
|
+
const inferred = inferOperationFields(method, pathTemplate, opHits);
|
|
173
|
+
const existing = byKey.get(key);
|
|
174
|
+
if (!existing || !existing.fields.length) {
|
|
175
|
+
byKey.set(key, { method, path: pathTemplate, title: inferred.title, fields: inferred.fields });
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// merge required flags from DTO when missing
|
|
179
|
+
const fieldMap = new Map(existing.fields.map((f) => [`${f.in}:${f.name}`, f]));
|
|
180
|
+
for (const field of inferred.fields) {
|
|
181
|
+
const k = `${field.in}:${field.name}`;
|
|
182
|
+
const prev = fieldMap.get(k);
|
|
183
|
+
if (!prev)
|
|
184
|
+
fieldMap.set(k, field);
|
|
185
|
+
else if (prev.required === undefined && field.required !== undefined)
|
|
186
|
+
fieldMap.set(k, { ...prev, required: field.required });
|
|
187
|
+
}
|
|
188
|
+
byKey.set(key, { ...existing, fields: [...fieldMap.values()] });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
let operations = [...byKey.values()].sort((a, b) => `${a.path} ${a.method}`.localeCompare(`${b.path} ${b.method}`));
|
|
192
|
+
if (filter?.method) {
|
|
193
|
+
operations = operations.filter((op) => op.method === filter.method.toLowerCase());
|
|
194
|
+
}
|
|
195
|
+
if (filter?.path) {
|
|
196
|
+
operations = operations.filter((op) => op.path === filter.path);
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
version: 1,
|
|
200
|
+
baseUrl: config.baseUrl,
|
|
201
|
+
generatedAt: new Date().toISOString(),
|
|
202
|
+
operations,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function writeFieldsFile(filter, outFile) {
|
|
206
|
+
const file = buildFieldsFile(filter);
|
|
207
|
+
const target = outFile ?? (0, config_1.getConfig)().fieldsFile;
|
|
208
|
+
(0, fs_1.mkdirSync)(path.dirname(target), { recursive: true });
|
|
209
|
+
(0, fs_1.writeFileSync)(target, `${JSON.stringify(file, null, 2)}\n`);
|
|
210
|
+
return target;
|
|
211
|
+
}
|
|
212
|
+
function readFieldsFile(filePath) {
|
|
213
|
+
const target = filePath ?? (0, config_1.getConfig)().fieldsFile;
|
|
214
|
+
if (!(0, fs_1.existsSync)(target)) {
|
|
215
|
+
throw new Error(`Fields file not found: ${target}. Run \`daxta fields\` first.`);
|
|
216
|
+
}
|
|
217
|
+
return JSON.parse((0, fs_1.readFileSync)(target, 'utf8'));
|
|
218
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { defineConfig, getConfig, resolveConfig, type DaxtaConfig, type TreeLayout, type SpecInfo } from './config';
|
|
2
|
+
export { buildDaxtaSpec, buildOpenApi, loadHits, type BuildResult, type Scenario } from './build/build-spec';
|
|
3
|
+
export { rebuildFromDisk, flushAndRebuild, clearPartialMarker, touchPartialMarker } from './build/incremental';
|
|
4
|
+
export { install, flush, clearWorkerHits, getHits, type RecordedHit } from './recorder';
|
|
5
|
+
export { dtoRequired, type FieldLocation } from './fields/dto-fields';
|
|
6
|
+
export { buildFieldsFile, writeFieldsFile, readFieldsFile, type FieldsFile } from './fields/export-fields';
|
|
7
|
+
export { startDaxtaDocsServer, startServe, createDaxtaDocsMiddleware, mountDaxtaDocs, getDocsBasePath, getHtmlPath, getOutDir, getSpecJsonPath, type DaxtaDocsMiddleware, type DaxtaDocsMiddlewareOptions, } from './serve';
|
|
8
|
+
export { runInteractiveCall, runFileCall, executeCall, type CallValues } from './call';
|
|
9
|
+
export type { FieldDoc, OperationDoc } from './catalog';
|
|
10
|
+
export { installDaxta, type InstallOptions } from './cli/install';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.installDaxta = exports.executeCall = exports.runFileCall = exports.runInteractiveCall = exports.getSpecJsonPath = exports.getOutDir = exports.getHtmlPath = exports.getDocsBasePath = exports.mountDaxtaDocs = exports.createDaxtaDocsMiddleware = exports.startServe = exports.startDaxtaDocsServer = exports.readFieldsFile = exports.writeFieldsFile = exports.buildFieldsFile = exports.dtoRequired = exports.getHits = exports.clearWorkerHits = exports.flush = exports.install = exports.touchPartialMarker = exports.clearPartialMarker = exports.flushAndRebuild = exports.rebuildFromDisk = exports.loadHits = exports.buildOpenApi = exports.buildDaxtaSpec = exports.resolveConfig = exports.getConfig = exports.defineConfig = void 0;
|
|
4
|
+
var config_1 = require("./config");
|
|
5
|
+
Object.defineProperty(exports, "defineConfig", { enumerable: true, get: function () { return config_1.defineConfig; } });
|
|
6
|
+
Object.defineProperty(exports, "getConfig", { enumerable: true, get: function () { return config_1.getConfig; } });
|
|
7
|
+
Object.defineProperty(exports, "resolveConfig", { enumerable: true, get: function () { return config_1.resolveConfig; } });
|
|
8
|
+
var build_spec_1 = require("./build/build-spec");
|
|
9
|
+
Object.defineProperty(exports, "buildDaxtaSpec", { enumerable: true, get: function () { return build_spec_1.buildDaxtaSpec; } });
|
|
10
|
+
Object.defineProperty(exports, "buildOpenApi", { enumerable: true, get: function () { return build_spec_1.buildOpenApi; } });
|
|
11
|
+
Object.defineProperty(exports, "loadHits", { enumerable: true, get: function () { return build_spec_1.loadHits; } });
|
|
12
|
+
var incremental_1 = require("./build/incremental");
|
|
13
|
+
Object.defineProperty(exports, "rebuildFromDisk", { enumerable: true, get: function () { return incremental_1.rebuildFromDisk; } });
|
|
14
|
+
Object.defineProperty(exports, "flushAndRebuild", { enumerable: true, get: function () { return incremental_1.flushAndRebuild; } });
|
|
15
|
+
Object.defineProperty(exports, "clearPartialMarker", { enumerable: true, get: function () { return incremental_1.clearPartialMarker; } });
|
|
16
|
+
Object.defineProperty(exports, "touchPartialMarker", { enumerable: true, get: function () { return incremental_1.touchPartialMarker; } });
|
|
17
|
+
var recorder_1 = require("./recorder");
|
|
18
|
+
Object.defineProperty(exports, "install", { enumerable: true, get: function () { return recorder_1.install; } });
|
|
19
|
+
Object.defineProperty(exports, "flush", { enumerable: true, get: function () { return recorder_1.flush; } });
|
|
20
|
+
Object.defineProperty(exports, "clearWorkerHits", { enumerable: true, get: function () { return recorder_1.clearWorkerHits; } });
|
|
21
|
+
Object.defineProperty(exports, "getHits", { enumerable: true, get: function () { return recorder_1.getHits; } });
|
|
22
|
+
var dto_fields_1 = require("./fields/dto-fields");
|
|
23
|
+
Object.defineProperty(exports, "dtoRequired", { enumerable: true, get: function () { return dto_fields_1.dtoRequired; } });
|
|
24
|
+
var export_fields_1 = require("./fields/export-fields");
|
|
25
|
+
Object.defineProperty(exports, "buildFieldsFile", { enumerable: true, get: function () { return export_fields_1.buildFieldsFile; } });
|
|
26
|
+
Object.defineProperty(exports, "writeFieldsFile", { enumerable: true, get: function () { return export_fields_1.writeFieldsFile; } });
|
|
27
|
+
Object.defineProperty(exports, "readFieldsFile", { enumerable: true, get: function () { return export_fields_1.readFieldsFile; } });
|
|
28
|
+
var serve_1 = require("./serve");
|
|
29
|
+
Object.defineProperty(exports, "startDaxtaDocsServer", { enumerable: true, get: function () { return serve_1.startDaxtaDocsServer; } });
|
|
30
|
+
Object.defineProperty(exports, "startServe", { enumerable: true, get: function () { return serve_1.startServe; } });
|
|
31
|
+
Object.defineProperty(exports, "createDaxtaDocsMiddleware", { enumerable: true, get: function () { return serve_1.createDaxtaDocsMiddleware; } });
|
|
32
|
+
Object.defineProperty(exports, "mountDaxtaDocs", { enumerable: true, get: function () { return serve_1.mountDaxtaDocs; } });
|
|
33
|
+
Object.defineProperty(exports, "getDocsBasePath", { enumerable: true, get: function () { return serve_1.getDocsBasePath; } });
|
|
34
|
+
Object.defineProperty(exports, "getHtmlPath", { enumerable: true, get: function () { return serve_1.getHtmlPath; } });
|
|
35
|
+
Object.defineProperty(exports, "getOutDir", { enumerable: true, get: function () { return serve_1.getOutDir; } });
|
|
36
|
+
Object.defineProperty(exports, "getSpecJsonPath", { enumerable: true, get: function () { return serve_1.getSpecJsonPath; } });
|
|
37
|
+
var call_1 = require("./call");
|
|
38
|
+
Object.defineProperty(exports, "runInteractiveCall", { enumerable: true, get: function () { return call_1.runInteractiveCall; } });
|
|
39
|
+
Object.defineProperty(exports, "runFileCall", { enumerable: true, get: function () { return call_1.runFileCall; } });
|
|
40
|
+
Object.defineProperty(exports, "executeCall", { enumerable: true, get: function () { return call_1.executeCall; } });
|
|
41
|
+
var install_1 = require("./cli/install");
|
|
42
|
+
Object.defineProperty(exports, "installDaxta", { enumerable: true, get: function () { return install_1.installDaxta; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function daxtaGlobalSetup(): Promise<void>;
|