@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 @@
|
|
|
1
|
+
export default function daxtaGlobalTeardown(): Promise<void>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = daxtaGlobalTeardown;
|
|
4
|
+
const incremental_1 = require("../build/incremental");
|
|
5
|
+
const build_spec_1 = require("../build/build-spec");
|
|
6
|
+
async function daxtaGlobalTeardown() {
|
|
7
|
+
try {
|
|
8
|
+
const result = (0, build_spec_1.buildDaxtaSpec)({ silent: true });
|
|
9
|
+
if (result.changed) {
|
|
10
|
+
console.log(`DAxTA docs updated (${result.hits} hits, ${result.operations} operations — ${result.updated} updated, ${result.unchanged} unchanged)`);
|
|
11
|
+
}
|
|
12
|
+
else if (!result.skipped) {
|
|
13
|
+
console.log(`DAxTA docs ready (${result.hits} hits, ${result.operations} operations)`);
|
|
14
|
+
}
|
|
15
|
+
(0, incremental_1.clearPartialMarker)();
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
console.error('DAxTA build failed:', error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
class DaxtaJestReporter {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.suitesDone = 0;
|
|
5
|
+
}
|
|
6
|
+
onTestResult() {
|
|
7
|
+
this.suitesDone += 1;
|
|
8
|
+
this.rebuild(`suite #${this.suitesDone}`, { html: this.suitesDone % 5 === 0 });
|
|
9
|
+
}
|
|
10
|
+
onRunComplete() {
|
|
11
|
+
this.rebuild('run complete', { html: true });
|
|
12
|
+
}
|
|
13
|
+
rebuild(label, options) {
|
|
14
|
+
try {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
16
|
+
require('reflect-metadata');
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
18
|
+
const { rebuildFromDisk } = require('../build/incremental');
|
|
19
|
+
const result = rebuildFromDisk({ silent: true, force: true, html: options.html });
|
|
20
|
+
if (result && !result.skipped) {
|
|
21
|
+
console.log(`DAxTA ${result.changed ? 'updated' : 'checked'} after ${label}: ${result.hits} hits, ${result.operations} ops${options.html ? '' : ' (json only)'}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.error('DAxTA incremental rebuild skipped:', error instanceof Error ? error.message : error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
module.exports = DaxtaJestReporter;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function toOpenApiTemplate(path: string): string;
|
|
2
|
+
export declare function templateFromTestName(test?: string): string | null;
|
|
3
|
+
export declare function pathMatchesTemplate(template: string, actual: string): boolean;
|
|
4
|
+
export declare function templatize(pathname: string, test?: string): string | null;
|
|
5
|
+
export declare function pathParamNames(pathTemplate: string): string[];
|
|
6
|
+
export declare function extractPathParams(pathTemplate: string, actualPath: string): Record<string, string>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toOpenApiTemplate = toOpenApiTemplate;
|
|
4
|
+
exports.templateFromTestName = templateFromTestName;
|
|
5
|
+
exports.pathMatchesTemplate = pathMatchesTemplate;
|
|
6
|
+
exports.templatize = templatize;
|
|
7
|
+
exports.pathParamNames = pathParamNames;
|
|
8
|
+
exports.extractPathParams = extractPathParams;
|
|
9
|
+
const config_1 = require("./config");
|
|
10
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
11
|
+
const NUMERIC_ID_RE = /^\d{5,}$/;
|
|
12
|
+
const TOKENISH_RE = /^[A-Za-z0-9_-]{16,}$/;
|
|
13
|
+
function toOpenApiTemplate(path) {
|
|
14
|
+
return path.replace(/:([A-Za-z_][A-Za-z0-9_]*)/g, '{$1}');
|
|
15
|
+
}
|
|
16
|
+
function templateFromTestName(test) {
|
|
17
|
+
if (!test)
|
|
18
|
+
return null;
|
|
19
|
+
const match = test.match(/^\/?(\S+)\s+\[(?:GET|POST|PUT|PATCH|DELETE|UPDATE|UPSERT)\]/i);
|
|
20
|
+
if (!match)
|
|
21
|
+
return null;
|
|
22
|
+
const raw = `/${match[1].replace(/^\/+/, '')}`.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, ':$1').replace(/\/+$/, '') || '/';
|
|
23
|
+
return toOpenApiTemplate(raw);
|
|
24
|
+
}
|
|
25
|
+
function pathMatchesTemplate(template, actual) {
|
|
26
|
+
const templateParts = template.split('/').filter(Boolean);
|
|
27
|
+
const actualParts = (actual.replace(/\/+$/, '') || '/').split('/').filter(Boolean);
|
|
28
|
+
if (templateParts.length !== actualParts.length)
|
|
29
|
+
return false;
|
|
30
|
+
return templateParts.every((part, index) => /^\{.+\}$/.test(part) || part === actualParts[index]);
|
|
31
|
+
}
|
|
32
|
+
function isDynamicSegment(segment, previous) {
|
|
33
|
+
if (UUID_RE.test(segment))
|
|
34
|
+
return true;
|
|
35
|
+
if (NUMERIC_ID_RE.test(segment))
|
|
36
|
+
return true;
|
|
37
|
+
if (TOKENISH_RE.test(segment) && previous && !/^v\d+$/i.test(previous))
|
|
38
|
+
return true;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function camelFromKebab(value) {
|
|
42
|
+
return value.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
43
|
+
}
|
|
44
|
+
function singularize(segment) {
|
|
45
|
+
const camel = camelFromKebab(segment);
|
|
46
|
+
if (camel.endsWith('ies') && camel.length > 3)
|
|
47
|
+
return camel.slice(0, -3) + 'y';
|
|
48
|
+
if (camel.endsWith('ses') && camel.length > 3)
|
|
49
|
+
return camel.slice(0, -2);
|
|
50
|
+
if (camel.endsWith('s') && !camel.endsWith('ss') && camel.length > 1)
|
|
51
|
+
return camel.slice(0, -1);
|
|
52
|
+
return camel;
|
|
53
|
+
}
|
|
54
|
+
function uniqueParamName(base, used) {
|
|
55
|
+
let name = base || 'id';
|
|
56
|
+
let suffix = 2;
|
|
57
|
+
while (used.has(name)) {
|
|
58
|
+
name = `${base || 'id'}${suffix}`;
|
|
59
|
+
suffix += 1;
|
|
60
|
+
}
|
|
61
|
+
used.add(name);
|
|
62
|
+
return name;
|
|
63
|
+
}
|
|
64
|
+
function paramName(previous, used) {
|
|
65
|
+
const byId = previous.match(/^by-(.+)-id$/i);
|
|
66
|
+
const base = byId ? `${camelFromKebab(byId[1])}Id` : `${singularize(previous)}Id`;
|
|
67
|
+
return uniqueParamName(base, used);
|
|
68
|
+
}
|
|
69
|
+
function heuristicTemplatize(pathname) {
|
|
70
|
+
const parts = pathname.split('/').filter(Boolean);
|
|
71
|
+
const used = new Set();
|
|
72
|
+
const templated = parts.map((part, index) => {
|
|
73
|
+
const previous = parts[index - 1];
|
|
74
|
+
if (!isDynamicSegment(part, previous))
|
|
75
|
+
return part;
|
|
76
|
+
if (!previous || /^v\d+$/i.test(previous))
|
|
77
|
+
return `{${uniqueParamName('id', used)}}`;
|
|
78
|
+
return `{${paramName(previous, used)}}`;
|
|
79
|
+
});
|
|
80
|
+
return `/${templated.join('/')}`;
|
|
81
|
+
}
|
|
82
|
+
function templatize(pathname, test) {
|
|
83
|
+
const trimmed = (pathname.split('?')[0] || '/').replace(/\/+$/, '') || '/';
|
|
84
|
+
if (trimmed === '/')
|
|
85
|
+
return null;
|
|
86
|
+
const custom = (0, config_1.getConfig)().templatize?.(trimmed, test);
|
|
87
|
+
if (custom)
|
|
88
|
+
return custom;
|
|
89
|
+
const declared = templateFromTestName(test);
|
|
90
|
+
if (declared && pathMatchesTemplate(declared, trimmed))
|
|
91
|
+
return declared;
|
|
92
|
+
return heuristicTemplatize(trimmed);
|
|
93
|
+
}
|
|
94
|
+
function pathParamNames(pathTemplate) {
|
|
95
|
+
return [...pathTemplate.matchAll(/\{([^}]+)\}/g)].map((match) => match[1]);
|
|
96
|
+
}
|
|
97
|
+
function extractPathParams(pathTemplate, actualPath) {
|
|
98
|
+
const actual = (actualPath.split('?')[0] || '').replace(/\/+$/, '') || '/';
|
|
99
|
+
const templateParts = pathTemplate.split('/');
|
|
100
|
+
const actualParts = actual.split('/');
|
|
101
|
+
const params = {};
|
|
102
|
+
for (let index = 0; index < templateParts.length; index += 1) {
|
|
103
|
+
const match = templateParts[index].match(/^\{(.+)\}$/);
|
|
104
|
+
if (match && actualParts[index])
|
|
105
|
+
params[match[1]] = decodeURIComponent(actualParts[index]);
|
|
106
|
+
}
|
|
107
|
+
return params;
|
|
108
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type RecordedHit = {
|
|
2
|
+
method: string;
|
|
3
|
+
path: string;
|
|
4
|
+
status: number;
|
|
5
|
+
reqBody: unknown;
|
|
6
|
+
resBody: unknown;
|
|
7
|
+
query?: Record<string, string>;
|
|
8
|
+
pathParams?: Record<string, string>;
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
test?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function install(): void;
|
|
13
|
+
export declare function flush(): void;
|
|
14
|
+
export declare function clearWorkerHits(): void;
|
|
15
|
+
export declare function getHits(): RecordedHit[];
|
|
@@ -0,0 +1,176 @@
|
|
|
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.install = install;
|
|
37
|
+
exports.flush = flush;
|
|
38
|
+
exports.clearWorkerHits = clearWorkerHits;
|
|
39
|
+
exports.getHits = getHits;
|
|
40
|
+
const fs_1 = require("fs");
|
|
41
|
+
const module_1 = require("module");
|
|
42
|
+
const path = __importStar(require("path"));
|
|
43
|
+
const config_1 = require("../config");
|
|
44
|
+
const path_util_1 = require("../path.util");
|
|
45
|
+
const g = globalThis;
|
|
46
|
+
const hits = (g.__DAXTA_HITS__ ??= []);
|
|
47
|
+
const nodeRequire = (0, module_1.createRequire)(__filename);
|
|
48
|
+
const SKIP_HEADERS = new Set(['host', 'accept-encoding', 'connection', 'content-length', 'user-agent', 'accept']);
|
|
49
|
+
function outDir() {
|
|
50
|
+
return (0, config_1.getConfig)().outDir;
|
|
51
|
+
}
|
|
52
|
+
function currentTestName() {
|
|
53
|
+
try {
|
|
54
|
+
const jestExpect = globalThis.expect;
|
|
55
|
+
return jestExpect?.getState?.().currentTestName;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function pickHeaders(request) {
|
|
62
|
+
const source = request.header ?? {};
|
|
63
|
+
const headers = {};
|
|
64
|
+
for (const [rawName, rawValue] of Object.entries(source)) {
|
|
65
|
+
const name = rawName.toLowerCase();
|
|
66
|
+
if (SKIP_HEADERS.has(name))
|
|
67
|
+
continue;
|
|
68
|
+
if (name !== 'content-type' && !name.startsWith('x-'))
|
|
69
|
+
continue;
|
|
70
|
+
if (rawValue == null || rawValue === '')
|
|
71
|
+
continue;
|
|
72
|
+
headers[name] = String(rawValue);
|
|
73
|
+
}
|
|
74
|
+
return Object.keys(headers).length ? headers : undefined;
|
|
75
|
+
}
|
|
76
|
+
function recordFromSuperagent(request, res) {
|
|
77
|
+
const rawUrl = request.url || '';
|
|
78
|
+
const url = new URL(rawUrl, 'http://localhost');
|
|
79
|
+
const test = currentTestName();
|
|
80
|
+
const pathTemplate = (0, path_util_1.templatize)(url.pathname, test);
|
|
81
|
+
if (!pathTemplate)
|
|
82
|
+
return;
|
|
83
|
+
const query = Object.fromEntries(url.searchParams.entries());
|
|
84
|
+
const pathParams = (0, path_util_1.extractPathParams)(pathTemplate, url.pathname);
|
|
85
|
+
hits.push({
|
|
86
|
+
method: (request.method || 'GET').toLowerCase(),
|
|
87
|
+
path: pathTemplate,
|
|
88
|
+
status: res.status ?? 0,
|
|
89
|
+
reqBody: request._data,
|
|
90
|
+
resBody: res.body,
|
|
91
|
+
query: Object.keys(query).length ? query : undefined,
|
|
92
|
+
pathParams: Object.keys(pathParams).length ? pathParams : undefined,
|
|
93
|
+
headers: pickHeaders(request),
|
|
94
|
+
test,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function patchSuperagent(superagent) {
|
|
98
|
+
const proto = superagent.Request?.prototype;
|
|
99
|
+
if (!proto?.end || proto.__daxtaPatched)
|
|
100
|
+
return false;
|
|
101
|
+
proto.__daxtaPatched = true;
|
|
102
|
+
const originalEnd = proto.end;
|
|
103
|
+
proto.end = function (callback) {
|
|
104
|
+
return originalEnd.call(this, (err, res) => {
|
|
105
|
+
try {
|
|
106
|
+
if (res)
|
|
107
|
+
recordFromSuperagent(this, res);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// recording must never fail a test
|
|
111
|
+
}
|
|
112
|
+
if (typeof callback === 'function')
|
|
113
|
+
callback(err, res);
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
function install() {
|
|
119
|
+
const candidates = new Set();
|
|
120
|
+
try {
|
|
121
|
+
const supertestDir = path.dirname(nodeRequire.resolve('supertest'));
|
|
122
|
+
candidates.add(nodeRequire.resolve('superagent', { paths: [supertestDir] }));
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// ignore
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
candidates.add(nodeRequire.resolve('superagent'));
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// ignore
|
|
132
|
+
}
|
|
133
|
+
for (const candidate of candidates) {
|
|
134
|
+
try {
|
|
135
|
+
patchSuperagent(nodeRequire(candidate));
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// ignore
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (!g.__DAXTA_FLUSH_HOOK__) {
|
|
142
|
+
g.__DAXTA_FLUSH_HOOK__ = true;
|
|
143
|
+
const flushSafe = () => {
|
|
144
|
+
try {
|
|
145
|
+
flush();
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// ignore
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
process.once('beforeExit', flushSafe);
|
|
152
|
+
process.once('exit', flushSafe);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function hitFilePath() {
|
|
156
|
+
const workerId = process.env.JEST_WORKER_ID ?? '0';
|
|
157
|
+
return path.join(outDir(), `hits-w${workerId}-p${process.pid}.json`);
|
|
158
|
+
}
|
|
159
|
+
function flush() {
|
|
160
|
+
const dir = outDir();
|
|
161
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
162
|
+
(0, fs_1.writeFileSync)(hitFilePath(), `${JSON.stringify(hits, null, 2)}\n`);
|
|
163
|
+
}
|
|
164
|
+
function clearWorkerHits() {
|
|
165
|
+
const dir = outDir();
|
|
166
|
+
if (!(0, fs_1.existsSync)(dir))
|
|
167
|
+
return;
|
|
168
|
+
for (const fileName of (0, fs_1.readdirSync)(dir)) {
|
|
169
|
+
if (fileName.startsWith('hits-w') && fileName.endsWith('.json')) {
|
|
170
|
+
(0, fs_1.rmSync)(path.join(dir, fileName));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function getHits() {
|
|
175
|
+
return hits;
|
|
176
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/register.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Side-effect entry: `node -r @t0.labs/daxta/register`
|
|
5
|
+
* Auto-mounts DAxTA `/docs` on NestFactory.create (non-production).
|
|
6
|
+
*/
|
|
7
|
+
const middleware_1 = require("./serve/middleware");
|
|
8
|
+
function patchNestFactory() {
|
|
9
|
+
try {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
11
|
+
const core = require('@nestjs/core');
|
|
12
|
+
const nestFactory = core.NestFactory;
|
|
13
|
+
if (!nestFactory?.create || nestFactory.__daxtaPatched)
|
|
14
|
+
return;
|
|
15
|
+
nestFactory.__daxtaPatched = true;
|
|
16
|
+
const originalCreate = nestFactory.create.bind(nestFactory);
|
|
17
|
+
nestFactory.create = async (...args) => {
|
|
18
|
+
const app = await originalCreate(...args);
|
|
19
|
+
(0, middleware_1.mountDaxtaDocs)(app, { enabled: process.env.NODE_ENV !== 'production' });
|
|
20
|
+
return app;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// @nestjs/core not available in this process
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
patchNestFactory();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as http from 'http';
|
|
2
|
+
export { createDaxtaDocsMiddleware, mountDaxtaDocs, type DaxtaDocsMiddleware, type DaxtaDocsMiddlewareOptions, } from './middleware';
|
|
3
|
+
export { getDocsBasePath, getHtmlPath, getOutDir, getSpecJsonPath } from './paths';
|
|
4
|
+
/** Standalone docs HTTP server (CLI: `daxta serve`). Prefer `mountDaxtaDocs` on the app port. */
|
|
5
|
+
export declare function startDaxtaDocsServer(options?: {
|
|
6
|
+
port?: number;
|
|
7
|
+
}): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
8
|
+
/** @deprecated Use `startDaxtaDocsServer` */
|
|
9
|
+
export declare const startServe: typeof startDaxtaDocsServer;
|
|
@@ -0,0 +1,126 @@
|
|
|
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.startServe = exports.getSpecJsonPath = exports.getOutDir = exports.getHtmlPath = exports.getDocsBasePath = exports.mountDaxtaDocs = exports.createDaxtaDocsMiddleware = void 0;
|
|
37
|
+
exports.startDaxtaDocsServer = startDaxtaDocsServer;
|
|
38
|
+
const fs_1 = require("fs");
|
|
39
|
+
const http = __importStar(require("http"));
|
|
40
|
+
const url_1 = require("url");
|
|
41
|
+
const config_1 = require("../config");
|
|
42
|
+
const paths_1 = require("./paths");
|
|
43
|
+
const proxy_1 = require("./proxy");
|
|
44
|
+
const spec_utils_1 = require("./spec-utils");
|
|
45
|
+
var middleware_1 = require("./middleware");
|
|
46
|
+
Object.defineProperty(exports, "createDaxtaDocsMiddleware", { enumerable: true, get: function () { return middleware_1.createDaxtaDocsMiddleware; } });
|
|
47
|
+
Object.defineProperty(exports, "mountDaxtaDocs", { enumerable: true, get: function () { return middleware_1.mountDaxtaDocs; } });
|
|
48
|
+
var paths_2 = require("./paths");
|
|
49
|
+
Object.defineProperty(exports, "getDocsBasePath", { enumerable: true, get: function () { return paths_2.getDocsBasePath; } });
|
|
50
|
+
Object.defineProperty(exports, "getHtmlPath", { enumerable: true, get: function () { return paths_2.getHtmlPath; } });
|
|
51
|
+
Object.defineProperty(exports, "getOutDir", { enumerable: true, get: function () { return paths_2.getOutDir; } });
|
|
52
|
+
Object.defineProperty(exports, "getSpecJsonPath", { enumerable: true, get: function () { return paths_2.getSpecJsonPath; } });
|
|
53
|
+
function send(res, status, body, headers = {}) {
|
|
54
|
+
res.writeHead(status, headers);
|
|
55
|
+
res.end(body);
|
|
56
|
+
}
|
|
57
|
+
/** Standalone docs HTTP server (CLI: `daxta serve`). Prefer `mountDaxtaDocs` on the app port. */
|
|
58
|
+
function startDaxtaDocsServer(options = {}) {
|
|
59
|
+
const config = (0, config_1.getConfig)();
|
|
60
|
+
const port = options.port ?? Number(process.env.DAXTA_DOCS_PORT || process.env.OPENAPI_DOCS_PORT || config.port);
|
|
61
|
+
const htmlPath = (0, paths_1.getHtmlPath)();
|
|
62
|
+
const docsBase = (0, paths_1.getDocsBasePath)();
|
|
63
|
+
const server = http.createServer(async (req, res) => {
|
|
64
|
+
const url = new url_1.URL(req.url || '/', `http://127.0.0.1:${port}`);
|
|
65
|
+
const method = (req.method || 'GET').toUpperCase();
|
|
66
|
+
if (method === 'OPTIONS') {
|
|
67
|
+
send(res, 204, '', { ...proxy_1.CORS_PREFLIGHT_HEADERS });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (url.pathname === '/__proxy') {
|
|
71
|
+
const chunks = [];
|
|
72
|
+
for await (const chunk of req)
|
|
73
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
74
|
+
const headers = {};
|
|
75
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
76
|
+
if (value == null)
|
|
77
|
+
continue;
|
|
78
|
+
const lower = key.toLowerCase();
|
|
79
|
+
if (lower === 'host' || lower === 'connection' || lower === 'content-length')
|
|
80
|
+
continue;
|
|
81
|
+
headers[key] = Array.isArray(value) ? value.join(',') : value;
|
|
82
|
+
}
|
|
83
|
+
const proxied = await (0, proxy_1.forwardProxyRequest)({
|
|
84
|
+
target: url.searchParams.get('target') || '',
|
|
85
|
+
method,
|
|
86
|
+
headers,
|
|
87
|
+
body: Buffer.concat(chunks),
|
|
88
|
+
});
|
|
89
|
+
send(res, proxied.status, proxied.body, proxied.headers);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (url.pathname === '/' || url.pathname === docsBase || url.pathname === `${docsBase}/`) {
|
|
93
|
+
if (!(0, fs_1.existsSync)(htmlPath)) {
|
|
94
|
+
send(res, 404, 'openapi.html not found. Run `daxta build` first.');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
send(res, 200, (0, fs_1.readFileSync)(htmlPath), {
|
|
98
|
+
'content-type': 'text/html; charset=utf-8',
|
|
99
|
+
'cache-control': 'no-store',
|
|
100
|
+
'last-modified': (0, fs_1.statSync)(htmlPath).mtime.toUTCString(),
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (url.pathname === '/openapi.json' || url.pathname === `${docsBase}/openapi.json`) {
|
|
105
|
+
const body = (0, spec_utils_1.readStrippedOpenApiJson)();
|
|
106
|
+
if (!body) {
|
|
107
|
+
send(res, 404, 'openapi.json not found. Run `daxta build` first.');
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
send(res, 200, body, {
|
|
111
|
+
'content-type': 'application/json; charset=utf-8',
|
|
112
|
+
'content-disposition': 'attachment; filename="openapi.json"',
|
|
113
|
+
'cache-control': 'no-store',
|
|
114
|
+
});
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
send(res, 404, 'Not found');
|
|
118
|
+
});
|
|
119
|
+
server.listen(port, '127.0.0.1', () => {
|
|
120
|
+
console.log(`DAxTA docs: http://127.0.0.1:${port}${docsBase}`);
|
|
121
|
+
console.log(`DAxTA OpenAPI: http://127.0.0.1:${port}${docsBase}/openapi.json`);
|
|
122
|
+
});
|
|
123
|
+
return server;
|
|
124
|
+
}
|
|
125
|
+
/** @deprecated Use `startDaxtaDocsServer` */
|
|
126
|
+
exports.startServe = startDaxtaDocsServer;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type DaxtaDocsMiddlewareOptions = {
|
|
2
|
+
/** URL prefix for the viewer. Default from config (`docsPath`) or `/docs`. */
|
|
3
|
+
basePath?: string;
|
|
4
|
+
};
|
|
5
|
+
type LooseRequest = {
|
|
6
|
+
method?: string;
|
|
7
|
+
path?: string;
|
|
8
|
+
url?: string;
|
|
9
|
+
originalUrl?: string;
|
|
10
|
+
query?: Record<string, unknown>;
|
|
11
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
12
|
+
body?: unknown;
|
|
13
|
+
};
|
|
14
|
+
type LooseResponse = {
|
|
15
|
+
statusCode?: number;
|
|
16
|
+
status?: (code: number) => LooseResponse;
|
|
17
|
+
type?: (value: string) => LooseResponse;
|
|
18
|
+
setHeader: (name: string, value: string) => void;
|
|
19
|
+
send?: (body: string | Buffer | object) => void;
|
|
20
|
+
end: (body?: string | Buffer) => void;
|
|
21
|
+
writeHead?: (code: number, headers?: Record<string, string>) => void;
|
|
22
|
+
};
|
|
23
|
+
export type DaxtaDocsMiddleware = (req: LooseRequest, res: LooseResponse, next: (error?: unknown) => void) => void | Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Express / Nest compatible middleware that serves the DAxTA viewer under `/docs`
|
|
26
|
+
* (or `basePath`) and the Try-it proxy at `/__proxy`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createDaxtaDocsMiddleware(options?: DaxtaDocsMiddlewareOptions): DaxtaDocsMiddleware;
|
|
29
|
+
/**
|
|
30
|
+
* Mount DAxTA docs on a Nest / Express app (`app.use(...)`).
|
|
31
|
+
* Pass `{ enabled: false }` to no-op (e.g. production).
|
|
32
|
+
*/
|
|
33
|
+
export declare function mountDaxtaDocs(app: {
|
|
34
|
+
use: (...handlers: unknown[]) => unknown;
|
|
35
|
+
}, options?: DaxtaDocsMiddlewareOptions & {
|
|
36
|
+
enabled?: boolean;
|
|
37
|
+
}): void;
|
|
38
|
+
export {};
|