@t0.labs/daxta 0.4.6 → 0.4.8

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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.stripViewerExtensions = stripViewerExtensions;
4
+ exports.exportPathFromTree = exportPathFromTree;
4
5
  exports.toTreeOrderedOpenApi = toTreeOrderedOpenApi;
5
6
  exports.readStrippedOpenApiJson = readStrippedOpenApiJson;
6
7
  const fs_1 = require("fs");
@@ -28,8 +29,25 @@ function treeSegmentsForOperation(urlPath, operation) {
28
29
  return urlPath.split('/').filter(Boolean);
29
30
  }
30
31
  /**
31
- * Export OpenAPI with real URL paths (correct for clients) and tags/folders
32
- * matching the docs sidebar (`x-tree-segments`).
32
+ * Build export path keys from sidebar tree segments (same order as docs),
33
+ * appending any `{param}` tail from the real URL so templates stay complete.
34
+ * Real URL is preserved on `x-daxta-url-path` when it differs.
35
+ */
36
+ function exportPathFromTree(urlPath, treeSegments) {
37
+ const originalParts = urlPath.split('/').filter(Boolean);
38
+ const paramIdx = originalParts.findIndex((part) => part.startsWith('{'));
39
+ const tail = paramIdx >= 0 ? originalParts.slice(paramIdx) : [];
40
+ const treeHasParam = treeSegments.some((part) => part.startsWith('{'));
41
+ const parts = treeSegments.length === 0
42
+ ? originalParts
43
+ : treeHasParam || paramIdx < 0
44
+ ? treeSegments
45
+ : [...treeSegments, ...tail];
46
+ return `/${parts.join('/')}`;
47
+ }
48
+ /**
49
+ * Export OpenAPI with path keys ordered like the docs sidebar (`x-tree-segments`).
50
+ * Real request URLs stay on `x-daxta-url-path` when remapped.
33
51
  */
34
52
  function toTreeOrderedOpenApi(raw) {
35
53
  const sourcePaths = (raw.paths ?? {});
@@ -38,11 +56,22 @@ function toTreeOrderedOpenApi(raw) {
38
56
  for (const [urlPath, pathItem] of Object.entries(sourcePaths)) {
39
57
  if (!pathItem || typeof pathItem !== 'object')
40
58
  continue;
41
- const exportPathItem = {};
59
+ let treeSegments = null;
60
+ for (const [key, value] of Object.entries(pathItem)) {
61
+ if (!HTTP_METHODS.has(key) || !value || typeof value !== 'object')
62
+ continue;
63
+ treeSegments = treeSegmentsForOperation(urlPath, value);
64
+ break;
65
+ }
66
+ if (!treeSegments)
67
+ treeSegments = urlPath.split('/').filter(Boolean);
68
+ const exportPath = exportPathFromTree(urlPath, treeSegments);
69
+ const exportPathItem = { ...(exportPaths[exportPath] ?? {}) };
42
70
  for (const [key, value] of Object.entries(pathItem)) {
43
71
  if (!HTTP_METHODS.has(key) || !value || typeof value !== 'object') {
44
- if (!HTTP_METHODS.has(key))
72
+ if (!HTTP_METHODS.has(key) && exportPathItem[key] === undefined) {
45
73
  exportPathItem[key] = stripViewerExtensions(value);
74
+ }
46
75
  continue;
47
76
  }
48
77
  const operation = value;
@@ -52,9 +81,12 @@ function toTreeOrderedOpenApi(raw) {
52
81
  const tag = tagParts.length ? tagParts.join(' / ') : 'API';
53
82
  cleaned.tags = [tag];
54
83
  tagNames.add(tag);
84
+ if (exportPath !== urlPath) {
85
+ cleaned['x-daxta-url-path'] = urlPath;
86
+ }
55
87
  exportPathItem[key] = cleaned;
56
88
  }
57
- exportPaths[urlPath] = exportPathItem;
89
+ exportPaths[exportPath] = exportPathItem;
58
90
  }
59
91
  const orderedPaths = {};
60
92
  for (const key of Object.keys(exportPaths).sort((a, b) => a.localeCompare(b))) {
@@ -0,0 +1,22 @@
1
+ export type ViewerStoreRow = {
2
+ key: string;
3
+ value: string;
4
+ };
5
+ export type ViewerEnvConfig = {
6
+ variables: ViewerStoreRow[];
7
+ headerGroups: Record<string, ViewerStoreRow[]>;
8
+ };
9
+ export type ViewerEnvStore = {
10
+ activeEnv: string;
11
+ activeGroup: string;
12
+ envs: Record<string, ViewerEnvConfig>;
13
+ };
14
+ /** Validate / coerce UI store JSON. Returns null if unusable. */
15
+ export declare function normalizeViewerEnvStore(raw: unknown): ViewerEnvStore | null;
16
+ export declare function readViewerEnvStore(): ViewerEnvStore | null;
17
+ export declare function writeViewerEnvStore(raw: unknown): ViewerEnvStore;
18
+ /**
19
+ * If viewer-store.json is missing, seed it from `envPresets` in daxta.config
20
+ * (local / staging / production baseUrls). Does not overwrite an existing file.
21
+ */
22
+ export declare function ensureViewerStoreSeeded(): string | null;
@@ -0,0 +1,135 @@
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.normalizeViewerEnvStore = normalizeViewerEnvStore;
37
+ exports.readViewerEnvStore = readViewerEnvStore;
38
+ exports.writeViewerEnvStore = writeViewerEnvStore;
39
+ exports.ensureViewerStoreSeeded = ensureViewerStoreSeeded;
40
+ const fs_1 = require("fs");
41
+ const path = __importStar(require("path"));
42
+ const config_1 = require("../config");
43
+ const paths_1 = require("./paths");
44
+ function isRow(value) {
45
+ return Boolean(value && typeof value === 'object' && typeof value.key === 'string');
46
+ }
47
+ function normalizeRows(value) {
48
+ if (!Array.isArray(value))
49
+ return [];
50
+ return value.filter(isRow).map((row) => ({ key: String(row.key || ''), value: String(row.value ?? '') }));
51
+ }
52
+ /** Validate / coerce UI store JSON. Returns null if unusable. */
53
+ function normalizeViewerEnvStore(raw) {
54
+ if (!raw || typeof raw !== 'object')
55
+ return null;
56
+ const input = raw;
57
+ const envsIn = input.envs;
58
+ if (!envsIn || typeof envsIn !== 'object')
59
+ return null;
60
+ const envs = {};
61
+ for (const [name, env] of Object.entries(envsIn)) {
62
+ if (!env || typeof env !== 'object')
63
+ continue;
64
+ const envObj = env;
65
+ const headerGroupsIn = envObj.headerGroups;
66
+ const headerGroups = {};
67
+ if (headerGroupsIn && typeof headerGroupsIn === 'object') {
68
+ for (const [group, rows] of Object.entries(headerGroupsIn)) {
69
+ headerGroups[group] = normalizeRows(rows);
70
+ }
71
+ }
72
+ if (!Object.keys(headerGroups).length) {
73
+ headerGroups.default = [{ key: 'content-type', value: 'application/json' }];
74
+ }
75
+ envs[name] = {
76
+ variables: normalizeRows(envObj.variables),
77
+ headerGroups,
78
+ };
79
+ }
80
+ if (!Object.keys(envs).length)
81
+ return null;
82
+ const activeEnv = typeof input.activeEnv === 'string' && envs[input.activeEnv] ? input.activeEnv : Object.keys(envs)[0];
83
+ const groups = Object.keys(envs[activeEnv].headerGroups);
84
+ const activeGroup = typeof input.activeGroup === 'string' && groups.includes(input.activeGroup) ? input.activeGroup : groups[0] || 'default';
85
+ return { activeEnv, activeGroup, envs };
86
+ }
87
+ function readViewerEnvStore() {
88
+ const filePath = (0, paths_1.getViewerStorePath)();
89
+ if (!(0, fs_1.existsSync)(filePath))
90
+ return null;
91
+ try {
92
+ return normalizeViewerEnvStore(JSON.parse((0, fs_1.readFileSync)(filePath, 'utf8')));
93
+ }
94
+ catch {
95
+ return null;
96
+ }
97
+ }
98
+ function writeViewerEnvStore(raw) {
99
+ const store = normalizeViewerEnvStore(raw);
100
+ if (!store)
101
+ throw new Error('Invalid viewer env store');
102
+ const filePath = (0, paths_1.getViewerStorePath)();
103
+ (0, fs_1.mkdirSync)(path.dirname(filePath), { recursive: true });
104
+ (0, fs_1.writeFileSync)(filePath, `${JSON.stringify(store, null, 2)}\n`);
105
+ return store;
106
+ }
107
+ /**
108
+ * If viewer-store.json is missing, seed it from `envPresets` in daxta.config
109
+ * (local / staging / production baseUrls). Does not overwrite an existing file.
110
+ */
111
+ function ensureViewerStoreSeeded() {
112
+ const filePath = (0, paths_1.getViewerStorePath)();
113
+ if ((0, fs_1.existsSync)(filePath))
114
+ return null;
115
+ const config = (0, config_1.getConfig)();
116
+ const presets = config.envPresets ?? { local: { baseUrl: config.baseUrl } };
117
+ const envs = {};
118
+ for (const [name, preset] of Object.entries(presets)) {
119
+ const variables = [{ key: 'baseUrl', value: String(preset.baseUrl || config.baseUrl) }];
120
+ for (const [key, value] of Object.entries(preset.variables ?? {})) {
121
+ if (!key || key === 'baseUrl')
122
+ continue;
123
+ variables.push({ key, value: String(value ?? '') });
124
+ }
125
+ envs[name] = {
126
+ variables,
127
+ headerGroups: { default: [{ key: 'content-type', value: 'application/json' }] },
128
+ };
129
+ }
130
+ const names = Object.keys(envs);
131
+ if (!names.length)
132
+ return null;
133
+ writeViewerEnvStore({ activeEnv: names.includes('local') ? 'local' : names[0], activeGroup: 'default', envs });
134
+ return filePath;
135
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t0.labs/daxta",
3
- "version": "0.4.6",
3
+ "version": "0.4.8",
4
4
  "description": "DAxTA \u2014 docs from Nest integration traffic + DTO field CLI / Try-it",
5
5
  "license": "MIT",
6
6
  "repository": {