@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.
Files changed (49) hide show
  1. package/README.md +40 -0
  2. package/assets/favicon.png +0 -0
  3. package/assets/viewer.html +1835 -0
  4. package/bin/daxta.js +2 -0
  5. package/dist/build/build-spec.d.ts +28 -0
  6. package/dist/build/build-spec.js +704 -0
  7. package/dist/build/incremental.d.ts +12 -0
  8. package/dist/build/incremental.js +121 -0
  9. package/dist/call/index.d.ts +24 -0
  10. package/dist/call/index.js +204 -0
  11. package/dist/catalog/index.d.ts +27 -0
  12. package/dist/catalog/index.js +111 -0
  13. package/dist/cli/install.d.ts +6 -0
  14. package/dist/cli/install.js +333 -0
  15. package/dist/cli/main.d.ts +1 -0
  16. package/dist/cli/main.js +102 -0
  17. package/dist/config.d.ts +31 -0
  18. package/dist/config.js +106 -0
  19. package/dist/fields/dto-fields.d.ts +4 -0
  20. package/dist/fields/dto-fields.js +372 -0
  21. package/dist/fields/export-fields.d.ts +23 -0
  22. package/dist/fields/export-fields.js +218 -0
  23. package/dist/index.d.ts +10 -0
  24. package/dist/index.js +42 -0
  25. package/dist/jest/global-setup.d.ts +1 -0
  26. package/dist/jest/global-setup.js +7 -0
  27. package/dist/jest/global-teardown.d.ts +1 -0
  28. package/dist/jest/global-teardown.js +20 -0
  29. package/dist/jest/reporter.d.ts +7 -0
  30. package/dist/jest/reporter.js +29 -0
  31. package/dist/path.util.d.ts +6 -0
  32. package/dist/path.util.js +108 -0
  33. package/dist/recorder/index.d.ts +15 -0
  34. package/dist/recorder/index.js +176 -0
  35. package/dist/register.d.ts +1 -0
  36. package/dist/register.js +27 -0
  37. package/dist/serve/index.d.ts +9 -0
  38. package/dist/serve/index.js +126 -0
  39. package/dist/serve/middleware.d.ts +38 -0
  40. package/dist/serve/middleware.js +131 -0
  41. package/dist/serve/paths.d.ts +7 -0
  42. package/dist/serve/paths.js +66 -0
  43. package/dist/serve/proxy.d.ts +16 -0
  44. package/dist/serve/proxy.js +52 -0
  45. package/dist/serve/spec-utils.d.ts +2 -0
  46. package/dist/serve/spec-utils.js +27 -0
  47. package/dist/setup.d.ts +1 -0
  48. package/dist/setup.js +7 -0
  49. package/package.json +75 -0
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDaxtaDocsMiddleware = createDaxtaDocsMiddleware;
4
+ exports.mountDaxtaDocs = mountDaxtaDocs;
5
+ const fs_1 = require("fs");
6
+ const paths_1 = require("./paths");
7
+ const proxy_1 = require("./proxy");
8
+ const spec_utils_1 = require("./spec-utils");
9
+ function requestPathname(req) {
10
+ const raw = req.originalUrl || req.url || req.path || '/';
11
+ try {
12
+ return new URL(raw, 'http://localhost').pathname;
13
+ }
14
+ catch {
15
+ return String(raw).split('?')[0] || '/';
16
+ }
17
+ }
18
+ function write(res, status, body, headers = {}) {
19
+ for (const [key, value] of Object.entries(headers))
20
+ res.setHeader(key, value);
21
+ if (typeof res.status === 'function' && typeof res.send === 'function') {
22
+ const typed = res.status(status);
23
+ if (headers['content-type']?.includes('html') && typed.type)
24
+ typed.type('html');
25
+ if (headers['content-type']?.includes('json') && typed.type)
26
+ typed.type('json');
27
+ typed.send?.(body);
28
+ return;
29
+ }
30
+ if (typeof res.writeHead === 'function') {
31
+ res.writeHead(status, headers);
32
+ res.end(body);
33
+ return;
34
+ }
35
+ res.statusCode = status;
36
+ res.end(body);
37
+ }
38
+ function collectHeaders(req) {
39
+ const headers = {};
40
+ for (const [key, value] of Object.entries(req.headers ?? {})) {
41
+ if (value == null)
42
+ continue;
43
+ const lower = key.toLowerCase();
44
+ if (lower === 'host' || lower === 'connection' || lower === 'content-length')
45
+ continue;
46
+ headers[key] = Array.isArray(value) ? value.join(',') : value;
47
+ }
48
+ return headers;
49
+ }
50
+ function bodyToBuffer(body) {
51
+ if (body == null)
52
+ return undefined;
53
+ if (Buffer.isBuffer(body))
54
+ return body;
55
+ if (typeof body === 'string')
56
+ return Buffer.from(body);
57
+ return Buffer.from(JSON.stringify(body));
58
+ }
59
+ /**
60
+ * Express / Nest compatible middleware that serves the DAxTA viewer under `/docs`
61
+ * (or `basePath`) and the Try-it proxy at `/__proxy`.
62
+ */
63
+ function createDaxtaDocsMiddleware(options = {}) {
64
+ const basePath = (options.basePath ?? (0, paths_1.getDocsBasePath)()).replace(/\/$/, '') || '/docs';
65
+ return async (req, res, next) => {
66
+ const method = (req.method || 'GET').toUpperCase();
67
+ const pathname = requestPathname(req);
68
+ if (method === 'OPTIONS' && (pathname === '/__proxy' || pathname.startsWith(basePath))) {
69
+ write(res, 204, '', { ...proxy_1.CORS_PREFLIGHT_HEADERS });
70
+ return;
71
+ }
72
+ if (pathname === '/__proxy') {
73
+ const target = typeof req.query?.target === 'string'
74
+ ? req.query.target
75
+ : new URL(req.originalUrl || req.url || '/', 'http://localhost').searchParams.get('target') || '';
76
+ const proxied = await (0, proxy_1.forwardProxyRequest)({
77
+ target,
78
+ method,
79
+ headers: collectHeaders(req),
80
+ body: bodyToBuffer(req.body),
81
+ });
82
+ write(res, proxied.status, proxied.body, proxied.headers);
83
+ return;
84
+ }
85
+ const isDocsRoot = pathname === basePath || pathname === `${basePath}/`;
86
+ const isDocsSpec = pathname === `${basePath}/openapi.json`;
87
+ if (!isDocsRoot && !isDocsSpec) {
88
+ next();
89
+ return;
90
+ }
91
+ if (method !== 'GET' && method !== 'HEAD') {
92
+ next();
93
+ return;
94
+ }
95
+ if (isDocsSpec) {
96
+ const body = (0, spec_utils_1.readStrippedOpenApiJson)();
97
+ if (!body) {
98
+ write(res, 404, 'openapi.json not found. Run `daxta build` first.', { 'content-type': 'text/plain; charset=utf-8' });
99
+ return;
100
+ }
101
+ write(res, 200, body, {
102
+ 'content-type': 'application/json; charset=utf-8',
103
+ 'content-disposition': 'attachment; filename="openapi.json"',
104
+ 'cache-control': 'no-store',
105
+ });
106
+ return;
107
+ }
108
+ const htmlPath = (0, paths_1.getHtmlPath)();
109
+ if (!(0, fs_1.existsSync)(htmlPath)) {
110
+ write(res, 404, 'openapi.html not found. Run integration tests or `daxta build` first.', {
111
+ 'content-type': 'text/plain; charset=utf-8',
112
+ });
113
+ return;
114
+ }
115
+ const html = (0, fs_1.readFileSync)(htmlPath);
116
+ write(res, 200, html, {
117
+ 'content-type': 'text/html; charset=utf-8',
118
+ 'cache-control': 'no-store',
119
+ 'last-modified': (0, fs_1.statSync)(htmlPath).mtime.toUTCString(),
120
+ });
121
+ };
122
+ }
123
+ /**
124
+ * Mount DAxTA docs on a Nest / Express app (`app.use(...)`).
125
+ * Pass `{ enabled: false }` to no-op (e.g. production).
126
+ */
127
+ function mountDaxtaDocs(app, options = {}) {
128
+ if (options.enabled === false)
129
+ return;
130
+ app.use(createDaxtaDocsMiddleware(options));
131
+ }
@@ -0,0 +1,7 @@
1
+ export declare function getOutDir(): string;
2
+ export declare function getHtmlPath(): string;
3
+ export declare function getSpecJsonPath(): string;
4
+ export declare function getHitsJsonPath(): string;
5
+ export declare function getViewerAssetPath(): string;
6
+ export declare function getFaviconAssetPath(): string;
7
+ export declare function getDocsBasePath(): string;
@@ -0,0 +1,66 @@
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.getOutDir = getOutDir;
37
+ exports.getHtmlPath = getHtmlPath;
38
+ exports.getSpecJsonPath = getSpecJsonPath;
39
+ exports.getHitsJsonPath = getHitsJsonPath;
40
+ exports.getViewerAssetPath = getViewerAssetPath;
41
+ exports.getFaviconAssetPath = getFaviconAssetPath;
42
+ exports.getDocsBasePath = getDocsBasePath;
43
+ const path = __importStar(require("path"));
44
+ const config_1 = require("../config");
45
+ function getOutDir() {
46
+ return (0, config_1.getConfig)().outDir;
47
+ }
48
+ function getHtmlPath() {
49
+ return path.join(getOutDir(), 'openapi.html');
50
+ }
51
+ function getSpecJsonPath() {
52
+ return path.join(getOutDir(), 'openapi.json');
53
+ }
54
+ function getHitsJsonPath() {
55
+ return path.join(getOutDir(), 'hits.json');
56
+ }
57
+ function getViewerAssetPath() {
58
+ return path.join(__dirname, '..', '..', 'assets', 'viewer.html');
59
+ }
60
+ function getFaviconAssetPath() {
61
+ return path.join(__dirname, '..', '..', 'assets', 'favicon.png');
62
+ }
63
+ function getDocsBasePath() {
64
+ const configured = (0, config_1.getConfig)().docsPath ?? '/docs';
65
+ return configured.startsWith('/') ? configured.replace(/\/$/, '') || '/docs' : `/${configured}`.replace(/\/$/, '');
66
+ }
@@ -0,0 +1,16 @@
1
+ export declare function isAllowedProxyTarget(raw: string): boolean;
2
+ export declare function forwardProxyRequest(input: {
3
+ target: string;
4
+ method: string;
5
+ headers: Record<string, string>;
6
+ body?: Buffer | string | undefined;
7
+ }): Promise<{
8
+ status: number;
9
+ headers: Record<string, string>;
10
+ body: Buffer;
11
+ }>;
12
+ export declare const CORS_PREFLIGHT_HEADERS: {
13
+ readonly 'access-control-allow-origin': "*";
14
+ readonly 'access-control-allow-headers': "*";
15
+ readonly 'access-control-allow-methods': "GET,POST,PUT,PATCH,DELETE,OPTIONS";
16
+ };
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CORS_PREFLIGHT_HEADERS = void 0;
4
+ exports.isAllowedProxyTarget = isAllowedProxyTarget;
5
+ exports.forwardProxyRequest = forwardProxyRequest;
6
+ function isAllowedProxyTarget(raw) {
7
+ try {
8
+ const url = new URL(raw);
9
+ return url.protocol === 'http:' || url.protocol === 'https:';
10
+ }
11
+ catch {
12
+ return false;
13
+ }
14
+ }
15
+ async function forwardProxyRequest(input) {
16
+ if (!isAllowedProxyTarget(input.target)) {
17
+ return {
18
+ status: 400,
19
+ headers: { 'content-type': 'application/json' },
20
+ body: Buffer.from(JSON.stringify({ error: 'Invalid target URL' })),
21
+ };
22
+ }
23
+ try {
24
+ const upstream = await fetch(input.target, {
25
+ method: input.method || 'GET',
26
+ headers: input.headers,
27
+ body: input.method && !['GET', 'HEAD'].includes(input.method.toUpperCase()) ? input.body : undefined,
28
+ });
29
+ const responseBody = Buffer.from(await upstream.arrayBuffer());
30
+ const outHeaders = {
31
+ 'access-control-allow-origin': '*',
32
+ 'access-control-allow-headers': '*',
33
+ 'access-control-allow-methods': 'GET,POST,PUT,PATCH,DELETE,OPTIONS',
34
+ };
35
+ const contentType = upstream.headers.get('content-type');
36
+ if (contentType)
37
+ outHeaders['content-type'] = contentType;
38
+ return { status: upstream.status, headers: outHeaders, body: responseBody };
39
+ }
40
+ catch (error) {
41
+ return {
42
+ status: 502,
43
+ headers: { 'content-type': 'application/json', 'access-control-allow-origin': '*' },
44
+ body: Buffer.from(JSON.stringify({ error: error instanceof Error ? error.message : String(error) })),
45
+ };
46
+ }
47
+ }
48
+ exports.CORS_PREFLIGHT_HEADERS = {
49
+ 'access-control-allow-origin': '*',
50
+ 'access-control-allow-headers': '*',
51
+ 'access-control-allow-methods': 'GET,POST,PUT,PATCH,DELETE,OPTIONS',
52
+ };
@@ -0,0 +1,2 @@
1
+ export declare function stripViewerExtensions(value: unknown): unknown;
2
+ export declare function readStrippedOpenApiJson(): string | null;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stripViewerExtensions = stripViewerExtensions;
4
+ exports.readStrippedOpenApiJson = readStrippedOpenApiJson;
5
+ const fs_1 = require("fs");
6
+ const paths_1 = require("./paths");
7
+ const VIEWER_EXTENSION_KEYS = new Set(['x-viewer', 'x-docs', 'x-scenarios', 'x-tree-segments', 'x-workspace']);
8
+ function stripViewerExtensions(value) {
9
+ if (Array.isArray(value))
10
+ return value.map(stripViewerExtensions);
11
+ if (!value || typeof value !== 'object')
12
+ return value;
13
+ const out = {};
14
+ for (const [key, child] of Object.entries(value)) {
15
+ if (VIEWER_EXTENSION_KEYS.has(key))
16
+ continue;
17
+ out[key] = stripViewerExtensions(child);
18
+ }
19
+ return out;
20
+ }
21
+ function readStrippedOpenApiJson() {
22
+ const specPath = (0, paths_1.getSpecJsonPath)();
23
+ if (!(0, fs_1.existsSync)(specPath))
24
+ return null;
25
+ const raw = JSON.parse((0, fs_1.readFileSync)(specPath, 'utf8'));
26
+ return `${JSON.stringify(stripViewerExtensions(raw), null, 2)}\n`;
27
+ }
@@ -0,0 +1 @@
1
+ export {};
package/dist/setup.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const recorder_1 = require("./recorder");
4
+ (0, recorder_1.install)();
5
+ afterAll(() => {
6
+ (0, recorder_1.flush)();
7
+ });
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@t0.labs/daxta",
3
+ "version": "0.2.0",
4
+ "description": "DAxTA \u2014 docs from Nest integration traffic + DTO field CLI / Try-it",
5
+ "license": "MIT",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "daxta": "bin/daxta.js"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "bin",
14
+ "assets",
15
+ "README.md"
16
+ ],
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./setup": {
23
+ "types": "./dist/setup.d.ts",
24
+ "default": "./dist/setup.js"
25
+ },
26
+ "./register": {
27
+ "types": "./dist/register.d.ts",
28
+ "default": "./dist/register.js"
29
+ },
30
+ "./jest-reporter": {
31
+ "types": "./dist/jest/reporter.d.ts",
32
+ "default": "./dist/jest/reporter.js"
33
+ },
34
+ "./jest-global-setup": {
35
+ "types": "./dist/jest/global-setup.d.ts",
36
+ "default": "./dist/jest/global-setup.js"
37
+ },
38
+ "./jest-global-teardown": {
39
+ "types": "./dist/jest/global-teardown.d.ts",
40
+ "default": "./dist/jest/global-teardown.js"
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc -p tsconfig.json",
46
+ "prepublishOnly": "npm run build"
47
+ },
48
+ "peerDependencies": {
49
+ "class-transformer": ">=0.5.0",
50
+ "class-validator": ">=0.14.0",
51
+ "reflect-metadata": ">=0.1.13",
52
+ "typescript": ">=4.9.0"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "jest": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "dependencies": {
60
+ "jiti": "^2.4.2"
61
+ },
62
+ "devDependencies": {
63
+ "@types/node": "^20.17.0",
64
+ "class-transformer": "^0.5.1",
65
+ "class-validator": "^0.14.1",
66
+ "reflect-metadata": "^0.2.2",
67
+ "typescript": "^5.6.3"
68
+ },
69
+ "engines": {
70
+ "node": ">=18"
71
+ },
72
+ "publishConfig": {
73
+ "access": "public"
74
+ }
75
+ }