@t0.labs/daxta 1.1.8 → 1.2.1
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 +54 -26
- package/assets/viewer.html +52 -11
- package/dist/build/build-spec.d.ts +2 -0
- package/dist/build/build-spec.js +85 -14
- package/dist/build/incremental.d.ts +2 -0
- package/dist/build/incremental.js +6 -2
- package/dist/catalog/index.d.ts +3 -1
- package/dist/catalog/index.js +39 -3
- package/dist/catalog/it-case.js +16 -5
- package/dist/catalog/test-title.js +3 -0
- package/dist/cli/discover-test.js +24 -22
- package/dist/cli/inject-main.js +22 -22
- package/dist/cli/install.d.ts +1 -0
- package/dist/cli/install.js +20 -6
- package/dist/cli/main.js +282 -23
- package/dist/cli/test.js +9 -4
- package/dist/cli/titles.js +57 -35
- package/dist/cli/tree.js +230 -75
- package/dist/cli/ui.d.ts +18 -0
- package/dist/cli/ui.js +124 -1
- package/dist/cli/uninstall.js +5 -3
- package/dist/config.d.ts +37 -1
- package/dist/config.js +9 -0
- package/dist/export/postman.d.ts +150 -0
- package/dist/export/postman.js +357 -0
- package/dist/jest/reporter.js +1 -1
- package/dist/recorder/index.js +85 -9
- package/dist/setup.js +6 -0
- package/package.json +1 -1
package/dist/config.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export type TreeLayout = 'role-resource' | 'resource-role';
|
|
|
3
3
|
/** @deprecated use role-resource / resource-role */
|
|
4
4
|
export type LegacyTreeLayout = 'url-order' | 'resource-first';
|
|
5
5
|
export type ExampleLabelStyle = 'status-case' | 'status-title-case' | 'full';
|
|
6
|
+
export type WriteMode = 'on-complete' | 'incremental';
|
|
7
|
+
export type SubResourceMode = 'nested' | 'merge' | 'root';
|
|
6
8
|
export type SpecInfo = {
|
|
7
9
|
title?: string;
|
|
8
10
|
version?: string;
|
|
@@ -48,7 +50,19 @@ export type DaxtaConfig = {
|
|
|
48
50
|
* One level only. Default true.
|
|
49
51
|
*/
|
|
50
52
|
treeCollapseSingle?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* How segments after the main resource are grouped into folders:
|
|
55
|
+
* - nested → v1 › shopping-cart › public › items (default)
|
|
56
|
+
* - merge → v1 › shopping-cart-items › public
|
|
57
|
+
* - root → v1 › shopping-cart › public (all sub-routes under main resource)
|
|
58
|
+
*/
|
|
59
|
+
treeSubResourceMode?: SubResourceMode;
|
|
51
60
|
treePathOverrides?: Record<string, string[]>;
|
|
61
|
+
/**
|
|
62
|
+
* Merge multi-segment resource paths into a single folder name, e.g.
|
|
63
|
+
* `{ 'shopping-cart/items': 'shopping-cart-items' }`.
|
|
64
|
+
*/
|
|
65
|
+
treeResourceAliases?: Record<string, string>;
|
|
52
66
|
/**
|
|
53
67
|
* OpenAPI example + scenario labels:
|
|
54
68
|
* - status-case → `201 — cyprus payload shape`
|
|
@@ -57,6 +71,12 @@ export type DaxtaConfig = {
|
|
|
57
71
|
* Ideal it(): docs take the when-clause. Parametric it(): pre-when field context is kept.
|
|
58
72
|
*/
|
|
59
73
|
exampleLabelStyle?: ExampleLabelStyle;
|
|
74
|
+
/**
|
|
75
|
+
* When to build OpenAPI + HTML during Jest runs:
|
|
76
|
+
* - on-complete → once after all tests finish (fastest, default)
|
|
77
|
+
* - incremental → after each test suite finishes (live updates)
|
|
78
|
+
*/
|
|
79
|
+
writeMode?: WriteMode;
|
|
60
80
|
port?: number;
|
|
61
81
|
fieldsFile?: string;
|
|
62
82
|
/**
|
|
@@ -77,12 +97,28 @@ export type DaxtaConfig = {
|
|
|
77
97
|
operationTag?: (pathTemplate: string, method: string) => string | undefined;
|
|
78
98
|
operationTitle?: (method: string, pathTemplate: string) => string | undefined;
|
|
79
99
|
securitySchemeId?: (headerName: string) => string | undefined;
|
|
100
|
+
/**
|
|
101
|
+
* Naming style for auto-generated header template variables in Postman & Try-it:
|
|
102
|
+
* - camel → x-admin-id becomes {{adminId}} (default)
|
|
103
|
+
* - upper-snake → x-admin-id becomes {{X_ADMIN_ID}}
|
|
104
|
+
*/
|
|
105
|
+
headerVarStyle?: 'camel' | 'upper-snake';
|
|
106
|
+
/**
|
|
107
|
+
* Map recorded header names to Postman/Try-it template values, e.g.:
|
|
108
|
+
* `{ 'x-user-id': '{{X_USER_ID}}', authorization: 'Bearer {{AUTH_TOKEN}}' }`
|
|
109
|
+
*/
|
|
110
|
+
headerVariables?: Record<string, string>;
|
|
111
|
+
/**
|
|
112
|
+
* Dynamic header-to-template-value resolver, e.g.:
|
|
113
|
+
* `(name) => \`{{\${name.toUpperCase().replace(/-/g, '_')}}}\``
|
|
114
|
+
*/
|
|
115
|
+
headerVariable?: (headerName: string) => string | undefined;
|
|
80
116
|
specInfo?: () => SpecInfo | undefined;
|
|
81
117
|
operationOrder?: (opKey: string) => number | undefined;
|
|
82
118
|
includeHit?: (method: string, pathTemplate: string, test?: string) => boolean;
|
|
83
119
|
treePathSegments?: (pathTemplate: string) => string[] | undefined;
|
|
84
120
|
};
|
|
85
|
-
export type ResolvedDaxtaConfig = Required<Pick<DaxtaConfig, 'workspace' | 'baseUrl' | 'controllersRoot' | 'outDir' | 'treeLayout' | 'treePathOverrides' | 'treeSkipParams' | 'treeCollapseSingle' | 'exampleLabelStyle' | 'port' | 'fieldsFile' | 'docsPath' | 'viewerStoreFile'>> & DaxtaConfig;
|
|
121
|
+
export type ResolvedDaxtaConfig = Required<Pick<DaxtaConfig, 'workspace' | 'baseUrl' | 'controllersRoot' | 'outDir' | 'treeLayout' | 'treePathOverrides' | 'treeResourceAliases' | 'treeSkipParams' | 'treeCollapseSingle' | 'treeSubResourceMode' | 'exampleLabelStyle' | 'writeMode' | 'port' | 'fieldsFile' | 'docsPath' | 'viewerStoreFile'>> & DaxtaConfig;
|
|
86
122
|
export declare const DEFAULT_DOCS_PATH = "/docs";
|
|
87
123
|
export declare function normalizeDocsPath(raw?: string | null): string;
|
|
88
124
|
export declare function defineConfig(config: DaxtaConfig): DaxtaConfig;
|
package/dist/config.js
CHANGED
|
@@ -53,8 +53,13 @@ const DEFAULTS = {
|
|
|
53
53
|
treeLayout: 'role-resource',
|
|
54
54
|
treeSkipParams: true,
|
|
55
55
|
treeCollapseSingle: true,
|
|
56
|
+
treeSubResourceMode: 'nested',
|
|
56
57
|
treePathOverrides: {},
|
|
58
|
+
treeResourceAliases: {},
|
|
59
|
+
headerVarStyle: 'camel',
|
|
60
|
+
headerVariables: {},
|
|
57
61
|
exampleLabelStyle: 'status-title-case',
|
|
62
|
+
writeMode: 'on-complete',
|
|
58
63
|
port: 5199,
|
|
59
64
|
fieldsFile: '.daxta/out/fields.json',
|
|
60
65
|
docsPath: '/docs',
|
|
@@ -118,6 +123,10 @@ function resolveConfig(cwd = process.cwd(), overrides) {
|
|
|
118
123
|
merged.treeCollapseSingle = DEFAULTS.treeCollapseSingle;
|
|
119
124
|
if (merged.exampleLabelStyle == null)
|
|
120
125
|
merged.exampleLabelStyle = DEFAULTS.exampleLabelStyle;
|
|
126
|
+
if (merged.writeMode == null)
|
|
127
|
+
merged.writeMode = DEFAULTS.writeMode;
|
|
128
|
+
if (merged.treeSubResourceMode == null)
|
|
129
|
+
merged.treeSubResourceMode = DEFAULTS.treeSubResourceMode;
|
|
121
130
|
merged.envPresets = normalizeEnvPresets(merged.envPresets, merged.baseUrl);
|
|
122
131
|
merged.docsPath = normalizeDocsPath(process.env.DAXTA_DOCS_PATH || merged.docsPath);
|
|
123
132
|
merged.controllersRoot = path.isAbsolute(merged.controllersRoot)
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
type OpenApiExample = {
|
|
2
|
+
summary?: string;
|
|
3
|
+
description?: string;
|
|
4
|
+
value?: unknown;
|
|
5
|
+
};
|
|
6
|
+
type OpenApiScenario = {
|
|
7
|
+
name: string;
|
|
8
|
+
group?: string;
|
|
9
|
+
status: number;
|
|
10
|
+
pathParams?: Record<string, string>;
|
|
11
|
+
query?: Record<string, string>;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
reqBody?: unknown;
|
|
14
|
+
resBody?: unknown;
|
|
15
|
+
};
|
|
16
|
+
type OpenApiOperation = {
|
|
17
|
+
summary?: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
tags?: string[];
|
|
20
|
+
security?: Array<Record<string, unknown[]>>;
|
|
21
|
+
parameters?: Array<{
|
|
22
|
+
name: string;
|
|
23
|
+
in: string;
|
|
24
|
+
required?: boolean;
|
|
25
|
+
example?: unknown;
|
|
26
|
+
}>;
|
|
27
|
+
requestBody?: {
|
|
28
|
+
content?: Record<string, {
|
|
29
|
+
examples?: Record<string, OpenApiExample>;
|
|
30
|
+
}>;
|
|
31
|
+
};
|
|
32
|
+
responses?: Record<string, {
|
|
33
|
+
description?: string;
|
|
34
|
+
content?: Record<string, {
|
|
35
|
+
examples?: Record<string, OpenApiExample>;
|
|
36
|
+
}>;
|
|
37
|
+
}>;
|
|
38
|
+
'x-tree-segments'?: string[];
|
|
39
|
+
'x-scenarios'?: OpenApiScenario[];
|
|
40
|
+
'x-daxta-url-path'?: string;
|
|
41
|
+
};
|
|
42
|
+
type OpenApiSpecLike = {
|
|
43
|
+
info?: {
|
|
44
|
+
title?: string;
|
|
45
|
+
version?: string;
|
|
46
|
+
description?: string;
|
|
47
|
+
'x-workspace'?: string;
|
|
48
|
+
};
|
|
49
|
+
servers?: Array<{
|
|
50
|
+
url?: string;
|
|
51
|
+
description?: string;
|
|
52
|
+
}>;
|
|
53
|
+
paths?: Record<string, Record<string, OpenApiOperation>>;
|
|
54
|
+
};
|
|
55
|
+
export type PostmanHeader = {
|
|
56
|
+
key: string;
|
|
57
|
+
value: string;
|
|
58
|
+
type?: string;
|
|
59
|
+
};
|
|
60
|
+
export type PostmanRequest = {
|
|
61
|
+
method: string;
|
|
62
|
+
header: PostmanHeader[];
|
|
63
|
+
url: {
|
|
64
|
+
raw: string;
|
|
65
|
+
host: string[];
|
|
66
|
+
path: string[];
|
|
67
|
+
query?: Array<{
|
|
68
|
+
key: string;
|
|
69
|
+
value: string;
|
|
70
|
+
}>;
|
|
71
|
+
variable?: Array<{
|
|
72
|
+
key: string;
|
|
73
|
+
value: string;
|
|
74
|
+
}>;
|
|
75
|
+
};
|
|
76
|
+
body?: {
|
|
77
|
+
mode: 'raw';
|
|
78
|
+
raw: string;
|
|
79
|
+
options: {
|
|
80
|
+
raw: {
|
|
81
|
+
language: 'json';
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
description?: string;
|
|
86
|
+
};
|
|
87
|
+
export type PostmanResponseExample = {
|
|
88
|
+
name: string;
|
|
89
|
+
originalRequest: PostmanRequest;
|
|
90
|
+
status: string;
|
|
91
|
+
code: number;
|
|
92
|
+
_postman_previewlanguage: 'json';
|
|
93
|
+
header: Array<{
|
|
94
|
+
key: string;
|
|
95
|
+
value: string;
|
|
96
|
+
}>;
|
|
97
|
+
body: string;
|
|
98
|
+
};
|
|
99
|
+
export type PostmanRequestItem = {
|
|
100
|
+
name: string;
|
|
101
|
+
request: PostmanRequest;
|
|
102
|
+
response: PostmanResponseExample[];
|
|
103
|
+
};
|
|
104
|
+
export type PostmanFolderItem = {
|
|
105
|
+
name: string;
|
|
106
|
+
item: Array<PostmanFolderItem | PostmanRequestItem>;
|
|
107
|
+
};
|
|
108
|
+
export type PostmanCollection = {
|
|
109
|
+
info: {
|
|
110
|
+
_postman_id: string;
|
|
111
|
+
name: string;
|
|
112
|
+
description: string;
|
|
113
|
+
schema: string;
|
|
114
|
+
};
|
|
115
|
+
item: Array<PostmanFolderItem | PostmanRequestItem>;
|
|
116
|
+
};
|
|
117
|
+
export type PostmanEnvironment = {
|
|
118
|
+
id: string;
|
|
119
|
+
name: string;
|
|
120
|
+
values: Array<{
|
|
121
|
+
key: string;
|
|
122
|
+
value: string;
|
|
123
|
+
type: 'default' | 'secret';
|
|
124
|
+
enabled: boolean;
|
|
125
|
+
}>;
|
|
126
|
+
_postman_variable_scope: 'environment';
|
|
127
|
+
_postman_exported_at: string;
|
|
128
|
+
_postman_exported_using: string;
|
|
129
|
+
};
|
|
130
|
+
export declare function buildPostmanCollection(spec: OpenApiSpecLike, options?: {
|
|
131
|
+
useEnvVar?: boolean;
|
|
132
|
+
envName?: string;
|
|
133
|
+
}): {
|
|
134
|
+
collection: PostmanCollection;
|
|
135
|
+
operations: number;
|
|
136
|
+
examples: number;
|
|
137
|
+
};
|
|
138
|
+
export declare function buildPostmanEnvironment(envName?: string): PostmanEnvironment;
|
|
139
|
+
export declare function exportPostmanFiles(options?: {
|
|
140
|
+
out?: string;
|
|
141
|
+
envName?: string;
|
|
142
|
+
includeEnv?: boolean;
|
|
143
|
+
useEnvVar?: boolean;
|
|
144
|
+
}): {
|
|
145
|
+
collectionPath: string;
|
|
146
|
+
environmentPath?: string;
|
|
147
|
+
operations: number;
|
|
148
|
+
examples: number;
|
|
149
|
+
};
|
|
150
|
+
export {};
|
|
@@ -0,0 +1,357 @@
|
|
|
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.buildPostmanCollection = buildPostmanCollection;
|
|
37
|
+
exports.buildPostmanEnvironment = buildPostmanEnvironment;
|
|
38
|
+
exports.exportPostmanFiles = exportPostmanFiles;
|
|
39
|
+
const crypto_1 = require("crypto");
|
|
40
|
+
const fs_1 = require("fs");
|
|
41
|
+
const path = __importStar(require("path"));
|
|
42
|
+
const catalog_1 = require("../catalog");
|
|
43
|
+
const config_1 = require("../config");
|
|
44
|
+
const paths_1 = require("../serve/paths");
|
|
45
|
+
const HTTP_METHODS = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head'];
|
|
46
|
+
function postmanUrl(serverUrl, pathTemplate, pathParams, query) {
|
|
47
|
+
const base = (serverUrl || '{{baseUrl}}').replace(/\/$/, '');
|
|
48
|
+
const cleanPath = pathTemplate.startsWith('/') ? pathTemplate : `/${pathTemplate}`;
|
|
49
|
+
const segments = cleanPath.split('/').filter(Boolean);
|
|
50
|
+
const paramKeys = [...cleanPath.matchAll(/\{([^}]+)\}/g)].map((m) => m[1]);
|
|
51
|
+
const queryEntries = Object.entries(query ?? {}).map(([key, value]) => ({
|
|
52
|
+
key,
|
|
53
|
+
value: value ?? '',
|
|
54
|
+
}));
|
|
55
|
+
const querySuffix = queryEntries.length
|
|
56
|
+
? `?${queryEntries.map((q) => `${encodeURIComponent(q.key)}=${encodeURIComponent(q.value)}`).join('&')}`
|
|
57
|
+
: '';
|
|
58
|
+
return {
|
|
59
|
+
raw: `${base}${cleanPath.replace(/\{([^}]+)\}/g, ':$1')}${querySuffix}`,
|
|
60
|
+
host: base.startsWith('{{') ? [base] : [base.replace(/^https?:\/\//, '').split('/')[0]],
|
|
61
|
+
path: segments.map((seg) => (seg.startsWith('{') && seg.endsWith('}') ? `:${seg.slice(1, -1)}` : seg)),
|
|
62
|
+
...(queryEntries.length ? { query: queryEntries } : {}),
|
|
63
|
+
...(paramKeys.length
|
|
64
|
+
? { variable: paramKeys.map((key) => ({ key, value: pathParams?.[key] ?? '' })) }
|
|
65
|
+
: {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function resolveConfiguredHeaderValue(key) {
|
|
69
|
+
const config = (0, config_1.getConfig)();
|
|
70
|
+
const lower = key.toLowerCase();
|
|
71
|
+
const fromFn = config.headerVariable?.(lower) ?? config.headerVariable?.(key);
|
|
72
|
+
if (fromFn)
|
|
73
|
+
return fromFn;
|
|
74
|
+
const map = config.headerVariables ?? {};
|
|
75
|
+
for (const [k, v] of Object.entries(map)) {
|
|
76
|
+
if (k.toLowerCase() === lower)
|
|
77
|
+
return v;
|
|
78
|
+
}
|
|
79
|
+
if (config.headerVarStyle === 'upper-snake') {
|
|
80
|
+
if (lower === 'authorization')
|
|
81
|
+
return 'Bearer {{AUTH_TOKEN}}';
|
|
82
|
+
if (lower.startsWith('x-'))
|
|
83
|
+
return `{{${lower.toUpperCase().replace(/-/g, '_')}}}`;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
function buildHeaders(operation, scenarioHeaders, hasBody) {
|
|
88
|
+
const byLower = new Map();
|
|
89
|
+
const add = (key, value) => {
|
|
90
|
+
const lower = key.toLowerCase();
|
|
91
|
+
if (lower === 'host' || lower === 'content-length')
|
|
92
|
+
return;
|
|
93
|
+
const configured = lower === 'content-type' ? undefined : resolveConfiguredHeaderValue(key);
|
|
94
|
+
byLower.set(lower, { key, value: configured ?? value, type: 'text' });
|
|
95
|
+
};
|
|
96
|
+
for (const param of operation.parameters ?? []) {
|
|
97
|
+
if (param.in === 'header' && param.name) {
|
|
98
|
+
add(param.name, param.example != null ? String(param.example) : '');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
for (const sec of operation.security ?? []) {
|
|
102
|
+
if ('bearerAuth' in sec && !byLower.has('authorization')) {
|
|
103
|
+
add('Authorization', 'Bearer {{authToken}}');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const [key, value] of Object.entries(scenarioHeaders ?? {})) {
|
|
107
|
+
add(key, value);
|
|
108
|
+
}
|
|
109
|
+
if (hasBody && !byLower.has('content-type')) {
|
|
110
|
+
add('Content-Type', 'application/json');
|
|
111
|
+
}
|
|
112
|
+
return [...byLower.values()];
|
|
113
|
+
}
|
|
114
|
+
function stringifyJson(value) {
|
|
115
|
+
if (value === undefined || value === null)
|
|
116
|
+
return '';
|
|
117
|
+
return typeof value === 'string' ? value : JSON.stringify(value, null, 2);
|
|
118
|
+
}
|
|
119
|
+
function folderSegmentsForOperation(urlPath, operation) {
|
|
120
|
+
const fromTree = operation['x-tree-segments'];
|
|
121
|
+
const parts = Array.isArray(fromTree) && fromTree.length ? fromTree.map(String) : urlPath.split('/').filter(Boolean);
|
|
122
|
+
const named = parts.filter((part) => part && !part.startsWith('{'));
|
|
123
|
+
return named.length ? named : ['API'];
|
|
124
|
+
}
|
|
125
|
+
function pushIntoFolderTree(root, segments, requestItem) {
|
|
126
|
+
let level = root;
|
|
127
|
+
for (let i = 0; i < segments.length; i += 1) {
|
|
128
|
+
const name = segments[i];
|
|
129
|
+
let folder = level.find((entry) => entry.name === name && 'item' in entry && !('request' in entry));
|
|
130
|
+
if (!folder) {
|
|
131
|
+
folder = { name, item: [] };
|
|
132
|
+
level.push(folder);
|
|
133
|
+
}
|
|
134
|
+
if (i === segments.length - 1)
|
|
135
|
+
folder.item.push(requestItem);
|
|
136
|
+
else
|
|
137
|
+
level = folder.item;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function sortCollectionTree(items) {
|
|
141
|
+
const folders = items.filter((entry) => 'item' in entry && !('request' in entry));
|
|
142
|
+
const requests = items.filter((entry) => 'request' in entry);
|
|
143
|
+
folders.sort((a, b) => a.name.localeCompare(b.name));
|
|
144
|
+
for (const folder of folders)
|
|
145
|
+
folder.item = sortCollectionTree(folder.item);
|
|
146
|
+
requests.sort((a, b) => a.name.localeCompare(b.name));
|
|
147
|
+
return [...folders, ...requests];
|
|
148
|
+
}
|
|
149
|
+
function resolveEnvPreset(envName) {
|
|
150
|
+
const config = (0, config_1.getConfig)();
|
|
151
|
+
const presets = config.envPresets ?? {};
|
|
152
|
+
if (envName && presets[envName])
|
|
153
|
+
return { name: envName, preset: presets[envName] };
|
|
154
|
+
const firstKey = Object.keys(presets)[0];
|
|
155
|
+
if (firstKey)
|
|
156
|
+
return { name: firstKey, preset: presets[firstKey] };
|
|
157
|
+
return { name: 'local', preset: { baseUrl: config.baseUrl, baseUrlKey: 'baseUrl' } };
|
|
158
|
+
}
|
|
159
|
+
function buildPostmanCollection(spec, options = {}) {
|
|
160
|
+
const useEnvVar = options.useEnvVar !== false;
|
|
161
|
+
const { preset } = resolveEnvPreset(options.envName);
|
|
162
|
+
const baseUrlKey = preset.baseUrlKey || 'baseUrl';
|
|
163
|
+
const serverUrl = useEnvVar ? `{{${baseUrlKey}}}` : spec.servers?.[0]?.url || preset.baseUrl;
|
|
164
|
+
const root = [];
|
|
165
|
+
let operations = 0;
|
|
166
|
+
let totalExamples = 0;
|
|
167
|
+
for (const [urlPath, pathItem] of Object.entries(spec.paths ?? {})) {
|
|
168
|
+
if (!pathItem || typeof pathItem !== 'object')
|
|
169
|
+
continue;
|
|
170
|
+
for (const method of HTTP_METHODS) {
|
|
171
|
+
const operation = pathItem[method];
|
|
172
|
+
if (!operation || typeof operation !== 'object')
|
|
173
|
+
continue;
|
|
174
|
+
operations += 1;
|
|
175
|
+
const requestPath = operation['x-daxta-url-path'] || urlPath;
|
|
176
|
+
const scenarios = Array.isArray(operation['x-scenarios']) ? operation['x-scenarios'] : [];
|
|
177
|
+
const primaryScenario = scenarios.find((s) => s.status >= 200 && s.status < 300) ?? scenarios[0];
|
|
178
|
+
const reqExamples = operation.requestBody?.content?.['application/json']?.examples ?? {};
|
|
179
|
+
const firstReqExample = Object.values(reqExamples)[0];
|
|
180
|
+
const defaultReqBody = primaryScenario?.reqBody !== undefined ? primaryScenario.reqBody : firstReqExample?.value;
|
|
181
|
+
const hasDefaultBody = defaultReqBody !== undefined;
|
|
182
|
+
const baseRequest = {
|
|
183
|
+
method: method.toUpperCase(),
|
|
184
|
+
header: buildHeaders(operation, primaryScenario?.headers, hasDefaultBody),
|
|
185
|
+
url: postmanUrl(serverUrl, requestPath, primaryScenario?.pathParams, primaryScenario?.query),
|
|
186
|
+
description: operation.summary || operation.description || '',
|
|
187
|
+
...(hasDefaultBody
|
|
188
|
+
? {
|
|
189
|
+
body: {
|
|
190
|
+
mode: 'raw',
|
|
191
|
+
raw: stringifyJson(defaultReqBody),
|
|
192
|
+
options: { raw: { language: 'json' } },
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
: {}),
|
|
196
|
+
};
|
|
197
|
+
const responses = [];
|
|
198
|
+
if (scenarios.length) {
|
|
199
|
+
for (const scenario of scenarios) {
|
|
200
|
+
const hasBody = scenario.reqBody !== undefined;
|
|
201
|
+
const originalRequest = {
|
|
202
|
+
method: method.toUpperCase(),
|
|
203
|
+
header: buildHeaders(operation, scenario.headers, hasBody),
|
|
204
|
+
url: postmanUrl(serverUrl, requestPath, scenario.pathParams, scenario.query),
|
|
205
|
+
description: operation.summary || '',
|
|
206
|
+
...(hasBody
|
|
207
|
+
? {
|
|
208
|
+
body: {
|
|
209
|
+
mode: 'raw',
|
|
210
|
+
raw: stringifyJson(scenario.reqBody),
|
|
211
|
+
options: { raw: { language: 'json' } },
|
|
212
|
+
},
|
|
213
|
+
}
|
|
214
|
+
: {}),
|
|
215
|
+
};
|
|
216
|
+
const code = Number(scenario.status) || 0;
|
|
217
|
+
responses.push({
|
|
218
|
+
name: scenario.name,
|
|
219
|
+
originalRequest,
|
|
220
|
+
status: catalog_1.STATUS_TEXT[String(code)] ?? String(code),
|
|
221
|
+
code,
|
|
222
|
+
_postman_previewlanguage: 'json',
|
|
223
|
+
header: [{ key: 'Content-Type', value: 'application/json' }],
|
|
224
|
+
body: stringifyJson(scenario.resBody),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
for (const [status, response] of Object.entries(operation.responses ?? {})) {
|
|
230
|
+
const examples = response?.content?.['application/json']?.examples ?? {};
|
|
231
|
+
const entries = Object.entries(examples);
|
|
232
|
+
const code = Number(status) || 0;
|
|
233
|
+
if (!entries.length) {
|
|
234
|
+
responses.push({
|
|
235
|
+
name: response.description || catalog_1.STATUS_TEXT[status] || status,
|
|
236
|
+
originalRequest: baseRequest,
|
|
237
|
+
status: catalog_1.STATUS_TEXT[status] || response.description || status,
|
|
238
|
+
code,
|
|
239
|
+
_postman_previewlanguage: 'json',
|
|
240
|
+
header: [{ key: 'Content-Type', value: 'application/json' }],
|
|
241
|
+
body: '',
|
|
242
|
+
});
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
for (const [exKey, example] of entries) {
|
|
246
|
+
const name = example.summary || example.description || exKey;
|
|
247
|
+
const reqMatch = reqExamples[exKey] || reqExamples[name] || firstReqExample;
|
|
248
|
+
const hasBody = reqMatch?.value !== undefined;
|
|
249
|
+
const originalRequest = {
|
|
250
|
+
...baseRequest,
|
|
251
|
+
header: buildHeaders(operation, undefined, hasBody),
|
|
252
|
+
...(hasBody
|
|
253
|
+
? {
|
|
254
|
+
body: {
|
|
255
|
+
mode: 'raw',
|
|
256
|
+
raw: stringifyJson(reqMatch.value),
|
|
257
|
+
options: { raw: { language: 'json' } },
|
|
258
|
+
},
|
|
259
|
+
}
|
|
260
|
+
: {}),
|
|
261
|
+
};
|
|
262
|
+
responses.push({
|
|
263
|
+
name,
|
|
264
|
+
originalRequest,
|
|
265
|
+
status: catalog_1.STATUS_TEXT[status] || status,
|
|
266
|
+
code,
|
|
267
|
+
_postman_previewlanguage: 'json',
|
|
268
|
+
header: [{ key: 'Content-Type', value: 'application/json' }],
|
|
269
|
+
body: stringifyJson(example.value),
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
totalExamples += responses.length;
|
|
275
|
+
pushIntoFolderTree(root, folderSegmentsForOperation(urlPath, operation), {
|
|
276
|
+
name: operation.summary || `${method.toUpperCase()} ${requestPath}`,
|
|
277
|
+
request: baseRequest,
|
|
278
|
+
response: responses,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const collection = {
|
|
283
|
+
info: {
|
|
284
|
+
_postman_id: (0, crypto_1.randomUUID)(),
|
|
285
|
+
name: spec.info?.title || spec.info?.['x-workspace'] || (0, config_1.getConfig)().workspace || 'API',
|
|
286
|
+
description: spec.info?.description || 'Generated by DAxTA from observed test execution.',
|
|
287
|
+
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
|
|
288
|
+
},
|
|
289
|
+
item: sortCollectionTree(root),
|
|
290
|
+
};
|
|
291
|
+
return { collection, operations, examples: totalExamples };
|
|
292
|
+
}
|
|
293
|
+
function buildPostmanEnvironment(envName) {
|
|
294
|
+
const { name, preset } = resolveEnvPreset(envName);
|
|
295
|
+
const baseUrlKey = preset.baseUrlKey || 'baseUrl';
|
|
296
|
+
const values = [
|
|
297
|
+
{ key: baseUrlKey, value: preset.baseUrl, type: 'default', enabled: true },
|
|
298
|
+
];
|
|
299
|
+
const known = new Set([baseUrlKey]);
|
|
300
|
+
for (const [key, value] of Object.entries(preset.variables ?? {})) {
|
|
301
|
+
if (key === baseUrlKey)
|
|
302
|
+
continue;
|
|
303
|
+
known.add(key);
|
|
304
|
+
values.push({
|
|
305
|
+
key,
|
|
306
|
+
value: String(value ?? ''),
|
|
307
|
+
type: /token|secret|key|password/i.test(key) ? 'secret' : 'default',
|
|
308
|
+
enabled: true,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
for (const tmpl of Object.values((0, config_1.getConfig)().headerVariables ?? {})) {
|
|
312
|
+
for (const match of String(tmpl).matchAll(/\{\{\s*([a-zA-Z0-9_]+)\s*\}\}/g)) {
|
|
313
|
+
if (known.has(match[1]))
|
|
314
|
+
continue;
|
|
315
|
+
known.add(match[1]);
|
|
316
|
+
values.push({
|
|
317
|
+
key: match[1],
|
|
318
|
+
value: '',
|
|
319
|
+
type: /token|secret|key|password/i.test(match[1]) ? 'secret' : 'default',
|
|
320
|
+
enabled: true,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
id: (0, crypto_1.randomUUID)(),
|
|
326
|
+
name: `${(0, config_1.getConfig)().workspace || 'API'} (${name})`,
|
|
327
|
+
values,
|
|
328
|
+
_postman_variable_scope: 'environment',
|
|
329
|
+
_postman_exported_at: new Date().toISOString(),
|
|
330
|
+
_postman_exported_using: 'DAxTA',
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function exportPostmanFiles(options = {}) {
|
|
334
|
+
const specPath = (0, paths_1.getSpecJsonPath)();
|
|
335
|
+
if (!(0, fs_1.existsSync)(specPath)) {
|
|
336
|
+
throw new Error(`OpenAPI spec not found at ${specPath}. Run \`daxta generate\` or your tests first.`);
|
|
337
|
+
}
|
|
338
|
+
const spec = JSON.parse((0, fs_1.readFileSync)(specPath, 'utf8'));
|
|
339
|
+
const { collection, operations, examples } = buildPostmanCollection(spec, {
|
|
340
|
+
useEnvVar: options.useEnvVar,
|
|
341
|
+
envName: options.envName,
|
|
342
|
+
});
|
|
343
|
+
const outDir = (0, paths_1.getOutDir)();
|
|
344
|
+
const collectionPath = options.out
|
|
345
|
+
? path.resolve(options.out.endsWith('.json') ? options.out : path.join(options.out, 'postman_collection.json'))
|
|
346
|
+
: path.join(outDir, 'postman_collection.json');
|
|
347
|
+
(0, fs_1.mkdirSync)(path.dirname(collectionPath), { recursive: true });
|
|
348
|
+
(0, fs_1.writeFileSync)(collectionPath, `${JSON.stringify(collection, null, 2)}\n`);
|
|
349
|
+
let environmentPath;
|
|
350
|
+
if (options.includeEnv !== false) {
|
|
351
|
+
const envDir = path.dirname(collectionPath);
|
|
352
|
+
const baseName = path.basename(collectionPath).replace(/\.postman_collection\.json$|\.json$/i, '');
|
|
353
|
+
environmentPath = path.join(envDir, `${baseName}.postman_environment.json`);
|
|
354
|
+
(0, fs_1.writeFileSync)(environmentPath, `${JSON.stringify(buildPostmanEnvironment(options.envName), null, 2)}\n`);
|
|
355
|
+
}
|
|
356
|
+
return { collectionPath, environmentPath, operations, examples };
|
|
357
|
+
}
|
package/dist/jest/reporter.js
CHANGED
|
@@ -49,7 +49,7 @@ class DaxtaJestReporter {
|
|
|
49
49
|
require('reflect-metadata');
|
|
50
50
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
51
51
|
const { rebuildFromDisk } = require('../build/incremental');
|
|
52
|
-
const result = rebuildFromDisk({ silent: true, force: true, html: true });
|
|
52
|
+
const result = rebuildFromDisk({ silent: true, force: true, html: true, clearWorkers: true });
|
|
53
53
|
if (result && !result.skipped) {
|
|
54
54
|
printDocsReady(result.hits ?? 0, result.operations ?? 0);
|
|
55
55
|
return;
|