wrekenfile-converter 2.0.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 +185 -0
- package/dist/example-usage.d.ts +4 -0
- package/dist/example-usage.js +57 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +33 -0
- package/dist/mini-wrekenfile-generator.d.ts +18 -0
- package/dist/mini-wrekenfile-generator.js +246 -0
- package/dist/openapi-to-wreken.d.ts +2 -0
- package/dist/openapi-to-wreken.js +563 -0
- package/dist/openapi-v2-to-wrekenfile.d.ts +2 -0
- package/dist/openapi-v2-to-wrekenfile.js +467 -0
- package/dist/postman-to-wrekenfile.d.ts +11 -0
- package/dist/postman-to-wrekenfile.js +530 -0
- package/dist/wrekenfile-validator.d.ts +17 -0
- package/dist/wrekenfile-validator.js +653 -0
- package/package.json +78 -0
- package/wrekenfile.md +726 -0
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Wrekenfile Converter (Library)
|
|
2
|
+
|
|
3
|
+
A comprehensive TypeScript/JavaScript library for converting OpenAPI specifications (v2 and v3) and Postman collections into [Wrekenfile](./wrekenfile.md) YAML format, with advanced mini-chunking capabilities for vector database storage and AI context management.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Multi-format Support**: Convert OpenAPI v2 (Swagger), OpenAPI v3, and Postman collections
|
|
8
|
+
- **Complete Response Handling**: All response types (success and error) included in `RETURNS` arrays
|
|
9
|
+
- **AI-Optimized**: Response structs explicitly referenced for easy AI consumption
|
|
10
|
+
- **Mini Wrekenfile Generation**: Create focused, endpoint-grouped chunks for vector DB storage
|
|
11
|
+
- **Comprehensive Validation**: Built-in Wrekenfile validator with auto-fix capabilities
|
|
12
|
+
- **TypeScript Support**: Full TypeScript definitions and exports
|
|
13
|
+
- **Subproject Ready**: Designed to work as a dependency in larger projects
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
### As a Dependency (Recommended)
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install wrekenfile-converter
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
or
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn add wrekenfile-converter
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Importing the Library
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import {
|
|
35
|
+
generateWrekenfile, // OpenAPI v3
|
|
36
|
+
generateWrekenfileV2, // OpenAPI v2 (Swagger)
|
|
37
|
+
generateWrekenfileFromPostman, // Postman collections
|
|
38
|
+
validateWrekenfile,
|
|
39
|
+
generateMiniWrekenfiles,
|
|
40
|
+
MiniWrekenfile,
|
|
41
|
+
ValidationResult
|
|
42
|
+
} from 'wrekenfile-converter';
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Convert OpenAPI v3 to Wrekenfile
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import fs from 'fs';
|
|
49
|
+
import yaml from 'js-yaml';
|
|
50
|
+
import { generateWrekenfile } from 'wrekenfile-converter';
|
|
51
|
+
|
|
52
|
+
const fileContent = fs.readFileSync('./openapi.yaml', 'utf8');
|
|
53
|
+
const openapiSpec = yaml.load(fileContent);
|
|
54
|
+
const wrekenfileYaml = generateWrekenfile(openapiSpec, './');
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Convert OpenAPI v2 (Swagger) to Wrekenfile
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import fs from 'fs';
|
|
61
|
+
import yaml from 'js-yaml';
|
|
62
|
+
import { generateWrekenfile as generateWrekenfileV2 } from 'wrekenfile-converter';
|
|
63
|
+
|
|
64
|
+
const fileContent = fs.readFileSync('./swagger.yaml', 'utf8');
|
|
65
|
+
const swaggerSpec = yaml.load(fileContent);
|
|
66
|
+
const wrekenfileYaml = generateWrekenfileV2(swaggerSpec, './');
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Convert Postman Collection to Wrekenfile
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import fs from 'fs';
|
|
73
|
+
import { generateWrekenfileFromPostman } from 'wrekenfile-converter';
|
|
74
|
+
|
|
75
|
+
const collection = JSON.parse(fs.readFileSync('./collection.json', 'utf8'));
|
|
76
|
+
const variables = {}; // Optionally provide Postman environment variables
|
|
77
|
+
const wrekenfileYaml = generateWrekenfileFromPostman(collection, variables);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Validate a Wrekenfile
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { validateWrekenfile } from 'wrekenfile-converter';
|
|
84
|
+
|
|
85
|
+
const result = validateWrekenfile('./Wrekenfile.yaml');
|
|
86
|
+
console.log(result.isValid ? '✅ Valid' : '❌ Invalid');
|
|
87
|
+
console.log(result.errors, result.warnings);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Generate Mini Wrekenfiles
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { generateMiniWrekenfiles, MiniWrekenfile } from 'wrekenfile-converter';
|
|
94
|
+
|
|
95
|
+
const miniFiles: MiniWrekenfile[] = generateMiniWrekenfiles('./Wrekenfile.yaml');
|
|
96
|
+
// Each miniFile contains { content, metadata }
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## API Reference
|
|
100
|
+
|
|
101
|
+
### Core Functions
|
|
102
|
+
|
|
103
|
+
#### `generateWrekenfile(spec: any, baseDir: string): string`
|
|
104
|
+
Convert OpenAPI v3 specification to Wrekenfile format.
|
|
105
|
+
|
|
106
|
+
#### `generateWrekenfileV2(spec: any, baseDir: string): string`
|
|
107
|
+
Convert OpenAPI v2 (Swagger) specification to Wrekenfile format.
|
|
108
|
+
|
|
109
|
+
#### `generateWrekenfileFromPostman(collection: any, variables: Record<string, string>): string`
|
|
110
|
+
Convert Postman collection to Wrekenfile format.
|
|
111
|
+
|
|
112
|
+
#### `validateWrekenfile(filePath: string): ValidationResult`
|
|
113
|
+
Validate a Wrekenfile and return detailed results.
|
|
114
|
+
|
|
115
|
+
#### `generateMiniWrekenfiles(wrekenfilePath: string): MiniWrekenfile[]`
|
|
116
|
+
Generate mini Wrekenfiles grouped by endpoint.
|
|
117
|
+
|
|
118
|
+
### Types
|
|
119
|
+
|
|
120
|
+
#### `MiniWrekenfile`
|
|
121
|
+
```typescript
|
|
122
|
+
interface MiniWrekenfile {
|
|
123
|
+
content: string; // Complete YAML content
|
|
124
|
+
metadata: {
|
|
125
|
+
endpoint: string; // API endpoint path
|
|
126
|
+
methods: string[]; // HTTP methods (GET, POST, etc.)
|
|
127
|
+
structs: string[]; // Required struct names
|
|
128
|
+
filename: string; // Generated filename
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### `ValidationResult`
|
|
134
|
+
```typescript
|
|
135
|
+
interface ValidationResult {
|
|
136
|
+
isValid: boolean;
|
|
137
|
+
errors: string[];
|
|
138
|
+
warnings: string[];
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Development
|
|
143
|
+
|
|
144
|
+
### Build Commands
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
# Build TypeScript to dist/
|
|
148
|
+
npm run build
|
|
149
|
+
|
|
150
|
+
# Clean dist folder
|
|
151
|
+
npm run clean
|
|
152
|
+
|
|
153
|
+
# Watch mode for development
|
|
154
|
+
npm run dev
|
|
155
|
+
|
|
156
|
+
# Run example usage
|
|
157
|
+
npm run example
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Project Structure
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
src/
|
|
164
|
+
├── index.ts # Main exports
|
|
165
|
+
├── openapi-to-wreken.ts # OpenAPI v3 converter
|
|
166
|
+
├── openapi-v2-to-wrekenfile.ts # OpenAPI v2 converter
|
|
167
|
+
├── postman-to-wrekenfile.ts # Postman converter
|
|
168
|
+
├── wrekenfile-validator.ts # Validation logic
|
|
169
|
+
├── mini-wrekenfile-generator.ts # Mini chunk generator
|
|
170
|
+
└── example-usage.ts # Usage examples
|
|
171
|
+
|
|
172
|
+
dist/ # Compiled JavaScript + types
|
|
173
|
+
├── index.js
|
|
174
|
+
├── index.d.ts
|
|
175
|
+
└── ... (all compiled files)
|
|
176
|
+
|
|
177
|
+
mini-wrekenfiles/ # Generated mini chunks (if you save them)
|
|
178
|
+
├── mini-v2-app-projects.yaml
|
|
179
|
+
├── mini-v2-app-authenticate.yaml
|
|
180
|
+
└── ...
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
MIT
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { MiniWrekenfile } from './mini-wrekenfile-generator';
|
|
2
|
+
export declare function getMiniWrekenfilesForEndpoints(wrekenfileContent: string, targetEndpoints: string[]): MiniWrekenfile[];
|
|
3
|
+
export declare function getMiniWrekenfilesForMethods(wrekenfileContent: string, targetMethods: string[]): MiniWrekenfile[];
|
|
4
|
+
export declare function getMiniWrekenfileContent(wrekenfileContent: string, endpoint: string): string | null;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMiniWrekenfilesForEndpoints = getMiniWrekenfilesForEndpoints;
|
|
7
|
+
exports.getMiniWrekenfilesForMethods = getMiniWrekenfilesForMethods;
|
|
8
|
+
exports.getMiniWrekenfileContent = getMiniWrekenfileContent;
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
11
|
+
const index_1 = require("./index");
|
|
12
|
+
/**
|
|
13
|
+
* Example usage of the mini Wrekenfile generator
|
|
14
|
+
* This shows how to integrate with a vector database
|
|
15
|
+
*/
|
|
16
|
+
function exampleUsage() {
|
|
17
|
+
// OpenAPI v3 example
|
|
18
|
+
const openapiContent = fs_1.default.readFileSync('./examples/plaid.yml', 'utf8');
|
|
19
|
+
const openapiSpec = js_yaml_1.default.load(openapiContent);
|
|
20
|
+
const wrekenfileYaml = (0, index_1.generateWrekenfile)(openapiSpec, './examples');
|
|
21
|
+
console.log('OpenAPI v3 to Wrekenfile:', wrekenfileYaml.slice(0, 200) + '...');
|
|
22
|
+
// Postman example
|
|
23
|
+
const postmanContent = fs_1.default.readFileSync('./examples/Swytchcode API Docs.postman_collection.json', 'utf8');
|
|
24
|
+
const postmanCollection = JSON.parse(postmanContent);
|
|
25
|
+
const wrekenfileFromPostman = (0, index_1.generateWrekenfileFromPostman)(postmanCollection, {});
|
|
26
|
+
console.log('Postman to Wrekenfile:', wrekenfileFromPostman.slice(0, 200) + '...');
|
|
27
|
+
// Mini Wrekenfiles
|
|
28
|
+
fs_1.default.writeFileSync('./Wrekenfile.yaml', wrekenfileYaml);
|
|
29
|
+
const miniFiles = (0, index_1.generateMiniWrekenfiles)(wrekenfileYaml);
|
|
30
|
+
console.log('Mini Wrekenfiles count:', miniFiles.length);
|
|
31
|
+
if (miniFiles.length > 0) {
|
|
32
|
+
console.log('First mini Wrekenfile:', miniFiles[0].content.slice(0, 200) + '...');
|
|
33
|
+
}
|
|
34
|
+
// Validation
|
|
35
|
+
const validation = (0, index_1.validateWrekenfile)('./Wrekenfile.yaml');
|
|
36
|
+
console.log('Validation result:', validation.isValid, validation.errors, validation.warnings);
|
|
37
|
+
}
|
|
38
|
+
// Example: Function to get mini Wrekenfiles for specific endpoints
|
|
39
|
+
function getMiniWrekenfilesForEndpoints(wrekenfileContent, targetEndpoints) {
|
|
40
|
+
const allMiniFiles = (0, index_1.generateMiniWrekenfiles)(wrekenfileContent);
|
|
41
|
+
return allMiniFiles.filter(miniFile => targetEndpoints.includes(miniFile.metadata.endpoint));
|
|
42
|
+
}
|
|
43
|
+
// Example: Function to get mini Wrekenfiles for specific methods
|
|
44
|
+
function getMiniWrekenfilesForMethods(wrekenfileContent, targetMethods) {
|
|
45
|
+
const allMiniFiles = (0, index_1.generateMiniWrekenfiles)(wrekenfileContent);
|
|
46
|
+
return allMiniFiles.filter(miniFile => miniFile.metadata.methods.some(method => targetMethods.includes(method)));
|
|
47
|
+
}
|
|
48
|
+
// Example: Function to get mini Wrekenfile content as string for AI context
|
|
49
|
+
function getMiniWrekenfileContent(wrekenfileContent, endpoint) {
|
|
50
|
+
const allMiniFiles = (0, index_1.generateMiniWrekenfiles)(wrekenfileContent);
|
|
51
|
+
const miniFile = allMiniFiles.find(file => file.metadata.endpoint === endpoint);
|
|
52
|
+
return miniFile ? miniFile.content : null;
|
|
53
|
+
}
|
|
54
|
+
// Run example if this file is executed directly
|
|
55
|
+
if (require.main === module) {
|
|
56
|
+
exampleUsage();
|
|
57
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { generateWrekenfile } from './openapi-to-wreken';
|
|
2
|
+
export { generateWrekenfile as generateWrekenfileV2 } from './openapi-v2-to-wrekenfile';
|
|
3
|
+
export { generateWrekenfile as generateWrekenfileFromPostman, extractStructs, extractOperations, mapType, parseJsonExample, extractFieldsFromObject, loadEnvironmentFile, extractCollectionVariables, resolveVariables } from './postman-to-wrekenfile';
|
|
4
|
+
export { validateWrekenfile, fixWrekenfile, ValidationResult, WrekenfileStructure, printValidationResult } from './wrekenfile-validator';
|
|
5
|
+
export { generateMiniWrekenfiles, saveMiniWrekenfiles, MiniWrekenfile } from './mini-wrekenfile-generator';
|
|
6
|
+
export { getMiniWrekenfilesForEndpoints, getMiniWrekenfilesForMethods, getMiniWrekenfileContent } from './example-usage';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Main entry point for the wrekenfile-converter library (no CLI)
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.getMiniWrekenfileContent = exports.getMiniWrekenfilesForMethods = exports.getMiniWrekenfilesForEndpoints = exports.saveMiniWrekenfiles = exports.generateMiniWrekenfiles = exports.printValidationResult = exports.fixWrekenfile = exports.validateWrekenfile = exports.resolveVariables = exports.extractCollectionVariables = exports.loadEnvironmentFile = exports.extractFieldsFromObject = exports.parseJsonExample = exports.mapType = exports.extractOperations = exports.extractStructs = exports.generateWrekenfileFromPostman = exports.generateWrekenfileV2 = exports.generateWrekenfile = void 0;
|
|
5
|
+
// Export main conversion functions
|
|
6
|
+
var openapi_to_wreken_1 = require("./openapi-to-wreken");
|
|
7
|
+
Object.defineProperty(exports, "generateWrekenfile", { enumerable: true, get: function () { return openapi_to_wreken_1.generateWrekenfile; } });
|
|
8
|
+
var openapi_v2_to_wrekenfile_1 = require("./openapi-v2-to-wrekenfile");
|
|
9
|
+
Object.defineProperty(exports, "generateWrekenfileV2", { enumerable: true, get: function () { return openapi_v2_to_wrekenfile_1.generateWrekenfile; } });
|
|
10
|
+
var postman_to_wrekenfile_1 = require("./postman-to-wrekenfile");
|
|
11
|
+
Object.defineProperty(exports, "generateWrekenfileFromPostman", { enumerable: true, get: function () { return postman_to_wrekenfile_1.generateWrekenfile; } });
|
|
12
|
+
Object.defineProperty(exports, "extractStructs", { enumerable: true, get: function () { return postman_to_wrekenfile_1.extractStructs; } });
|
|
13
|
+
Object.defineProperty(exports, "extractOperations", { enumerable: true, get: function () { return postman_to_wrekenfile_1.extractOperations; } });
|
|
14
|
+
Object.defineProperty(exports, "mapType", { enumerable: true, get: function () { return postman_to_wrekenfile_1.mapType; } });
|
|
15
|
+
Object.defineProperty(exports, "parseJsonExample", { enumerable: true, get: function () { return postman_to_wrekenfile_1.parseJsonExample; } });
|
|
16
|
+
Object.defineProperty(exports, "extractFieldsFromObject", { enumerable: true, get: function () { return postman_to_wrekenfile_1.extractFieldsFromObject; } });
|
|
17
|
+
Object.defineProperty(exports, "loadEnvironmentFile", { enumerable: true, get: function () { return postman_to_wrekenfile_1.loadEnvironmentFile; } });
|
|
18
|
+
Object.defineProperty(exports, "extractCollectionVariables", { enumerable: true, get: function () { return postman_to_wrekenfile_1.extractCollectionVariables; } });
|
|
19
|
+
Object.defineProperty(exports, "resolveVariables", { enumerable: true, get: function () { return postman_to_wrekenfile_1.resolveVariables; } });
|
|
20
|
+
// Export validation function
|
|
21
|
+
var wrekenfile_validator_1 = require("./wrekenfile-validator");
|
|
22
|
+
Object.defineProperty(exports, "validateWrekenfile", { enumerable: true, get: function () { return wrekenfile_validator_1.validateWrekenfile; } });
|
|
23
|
+
Object.defineProperty(exports, "fixWrekenfile", { enumerable: true, get: function () { return wrekenfile_validator_1.fixWrekenfile; } });
|
|
24
|
+
Object.defineProperty(exports, "printValidationResult", { enumerable: true, get: function () { return wrekenfile_validator_1.printValidationResult; } });
|
|
25
|
+
// Export mini Wrekenfile generator
|
|
26
|
+
var mini_wrekenfile_generator_1 = require("./mini-wrekenfile-generator");
|
|
27
|
+
Object.defineProperty(exports, "generateMiniWrekenfiles", { enumerable: true, get: function () { return mini_wrekenfile_generator_1.generateMiniWrekenfiles; } });
|
|
28
|
+
Object.defineProperty(exports, "saveMiniWrekenfiles", { enumerable: true, get: function () { return mini_wrekenfile_generator_1.saveMiniWrekenfiles; } });
|
|
29
|
+
// Export utility functions
|
|
30
|
+
var example_usage_1 = require("./example-usage");
|
|
31
|
+
Object.defineProperty(exports, "getMiniWrekenfilesForEndpoints", { enumerable: true, get: function () { return example_usage_1.getMiniWrekenfilesForEndpoints; } });
|
|
32
|
+
Object.defineProperty(exports, "getMiniWrekenfilesForMethods", { enumerable: true, get: function () { return example_usage_1.getMiniWrekenfilesForMethods; } });
|
|
33
|
+
Object.defineProperty(exports, "getMiniWrekenfileContent", { enumerable: true, get: function () { return example_usage_1.getMiniWrekenfileContent; } });
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface MiniWrekenfile {
|
|
2
|
+
content: string;
|
|
3
|
+
metadata: {
|
|
4
|
+
endpoint: string;
|
|
5
|
+
methods: string[];
|
|
6
|
+
structs: string[];
|
|
7
|
+
filename: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Generates mini Wrekenfiles by grouping interfaces by endpoint
|
|
12
|
+
* Each mini Wrekenfile contains all methods for a single endpoint plus their required structs
|
|
13
|
+
*/
|
|
14
|
+
export declare function generateMiniWrekenfiles(wrekenfileContent: string): MiniWrekenfile[];
|
|
15
|
+
/**
|
|
16
|
+
* Saves mini Wrekenfiles to disk (optional utility function)
|
|
17
|
+
*/
|
|
18
|
+
export declare function saveMiniWrekenfiles(miniWrekenfiles: MiniWrekenfile[], outputDir?: string): void;
|
|
@@ -0,0 +1,246 @@
|
|
|
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.generateMiniWrekenfiles = generateMiniWrekenfiles;
|
|
37
|
+
exports.saveMiniWrekenfiles = saveMiniWrekenfiles;
|
|
38
|
+
const yaml = __importStar(require("js-yaml"));
|
|
39
|
+
const fs = __importStar(require("fs"));
|
|
40
|
+
/**
|
|
41
|
+
* Generates mini Wrekenfiles by grouping interfaces by endpoint
|
|
42
|
+
* Each mini Wrekenfile contains all methods for a single endpoint plus their required structs
|
|
43
|
+
*/
|
|
44
|
+
function generateMiniWrekenfiles(wrekenfileContent) {
|
|
45
|
+
if (!wrekenfileContent || typeof wrekenfileContent !== 'string') {
|
|
46
|
+
throw new Error("Argument 'wrekenfileContent' is required and must be a string");
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
// Parse the main Wrekenfile from YAML string
|
|
50
|
+
const data = yaml.load(wrekenfileContent);
|
|
51
|
+
if (!data.INTERFACES) {
|
|
52
|
+
throw new Error('No INTERFACES section found in Wrekenfile');
|
|
53
|
+
}
|
|
54
|
+
// Group interfaces by endpoint
|
|
55
|
+
const endpointGroups = groupInterfacesByEndpoint(data.INTERFACES);
|
|
56
|
+
const miniWrekenfiles = [];
|
|
57
|
+
// Generate a mini Wrekenfile for each endpoint group
|
|
58
|
+
for (const [endpoint, interfaces] of Object.entries(endpointGroups)) {
|
|
59
|
+
const miniWrekenfile = createMiniWrekenfile(data, endpoint, interfaces);
|
|
60
|
+
miniWrekenfiles.push(miniWrekenfile);
|
|
61
|
+
}
|
|
62
|
+
return miniWrekenfiles;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.error('Error generating mini Wrekenfiles:', error);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Groups interfaces by their endpoint path
|
|
71
|
+
*/
|
|
72
|
+
function groupInterfacesByEndpoint(interfaces) {
|
|
73
|
+
const groups = {};
|
|
74
|
+
for (const [interfaceName, interfaceData] of Object.entries(interfaces)) {
|
|
75
|
+
let endpoint = interfaceData.ENDPOINT;
|
|
76
|
+
if (!endpoint) {
|
|
77
|
+
console.warn(`Interface ${interfaceName} has no ENDPOINT, skipping`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
// Normalize endpoint: remove backticks and trim whitespace
|
|
81
|
+
if (typeof endpoint === 'string') {
|
|
82
|
+
endpoint = endpoint.trim();
|
|
83
|
+
if (endpoint.startsWith('`') && endpoint.endsWith('`')) {
|
|
84
|
+
endpoint = endpoint.slice(1, -1).trim();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (!groups[endpoint]) {
|
|
88
|
+
groups[endpoint] = {};
|
|
89
|
+
}
|
|
90
|
+
groups[endpoint][interfaceName] = interfaceData;
|
|
91
|
+
}
|
|
92
|
+
return groups;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates a complete mini Wrekenfile for a specific endpoint
|
|
96
|
+
*/
|
|
97
|
+
function createMiniWrekenfile(data, endpoint, interfaces) {
|
|
98
|
+
// Collect all structs referenced by the interfaces in this group
|
|
99
|
+
const requiredStructs = collectRequiredStructs(interfaces, data.STRUCTS || {});
|
|
100
|
+
// Create the mini Wrekenfile structure
|
|
101
|
+
const miniData = {
|
|
102
|
+
VERSION: data.VERSION,
|
|
103
|
+
INIT: data.INIT ? { DEFAULTS: data.INIT.DEFAULTS || [] } : undefined,
|
|
104
|
+
INTERFACES: interfaces,
|
|
105
|
+
STRUCTS: requiredStructs
|
|
106
|
+
};
|
|
107
|
+
// Convert to YAML
|
|
108
|
+
const content = yaml.dump(miniData, {
|
|
109
|
+
indent: 2,
|
|
110
|
+
lineWidth: -1,
|
|
111
|
+
noRefs: true
|
|
112
|
+
});
|
|
113
|
+
// Generate metadata
|
|
114
|
+
const methods = Object.values(interfaces).map((intf) => { var _a; return (_a = intf.HTTP) === null || _a === void 0 ? void 0 : _a.METHOD; }).filter(Boolean);
|
|
115
|
+
const structNames = Object.keys(requiredStructs);
|
|
116
|
+
const filename = generateFilename(endpoint);
|
|
117
|
+
return {
|
|
118
|
+
content,
|
|
119
|
+
metadata: {
|
|
120
|
+
endpoint,
|
|
121
|
+
methods,
|
|
122
|
+
structs: structNames,
|
|
123
|
+
filename
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Collects all structs required by the given interfaces
|
|
129
|
+
*/
|
|
130
|
+
function collectRequiredStructs(interfaces, allStructs) {
|
|
131
|
+
const requiredStructs = {};
|
|
132
|
+
const processedStructs = new Set();
|
|
133
|
+
// Extract struct references from interfaces
|
|
134
|
+
const structRefs = new Set();
|
|
135
|
+
for (const interfaceData of Object.values(interfaces)) {
|
|
136
|
+
// Check INPUTS
|
|
137
|
+
if (interfaceData.INPUTS) {
|
|
138
|
+
for (const input of interfaceData.INPUTS) {
|
|
139
|
+
if (input.type) {
|
|
140
|
+
const structNames = extractAllStructNames(input.type);
|
|
141
|
+
for (const structName of structNames) {
|
|
142
|
+
if (structName)
|
|
143
|
+
structRefs.add(structName);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// Check RETURNS
|
|
149
|
+
if (interfaceData.RETURNS) {
|
|
150
|
+
for (const ret of interfaceData.RETURNS) {
|
|
151
|
+
if (ret.RETURNTYPE) {
|
|
152
|
+
const structNames = extractAllStructNames(ret.RETURNTYPE);
|
|
153
|
+
for (const structName of structNames) {
|
|
154
|
+
if (structName)
|
|
155
|
+
structRefs.add(structName);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Recursively collect all required structs and their dependencies
|
|
162
|
+
for (const structName of structRefs) {
|
|
163
|
+
collectStructRecursively(structName, allStructs, requiredStructs, processedStructs);
|
|
164
|
+
}
|
|
165
|
+
return requiredStructs;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Recursively collects a struct and all its nested struct dependencies
|
|
169
|
+
*/
|
|
170
|
+
function collectStructRecursively(structName, allStructs, requiredStructs, processedStructs) {
|
|
171
|
+
if (processedStructs.has(structName)) {
|
|
172
|
+
return; // Already processed
|
|
173
|
+
}
|
|
174
|
+
processedStructs.add(structName);
|
|
175
|
+
if (!allStructs[structName]) {
|
|
176
|
+
console.warn(`Struct ${structName} not found in STRUCTS section`);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
// Add the struct
|
|
180
|
+
requiredStructs[structName] = allStructs[structName];
|
|
181
|
+
// Check for nested struct references
|
|
182
|
+
const structFields = allStructs[structName];
|
|
183
|
+
if (Array.isArray(structFields)) {
|
|
184
|
+
for (const field of structFields) {
|
|
185
|
+
if (field.type) {
|
|
186
|
+
// Handle STRUCT(SomeStruct) and []STRUCT(SomeStruct)
|
|
187
|
+
const nestedStructNames = extractAllStructNames(field.type);
|
|
188
|
+
for (const nestedStructName of nestedStructNames) {
|
|
189
|
+
if (nestedStructName) {
|
|
190
|
+
collectStructRecursively(nestedStructName, allStructs, requiredStructs, processedStructs);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Extracts all struct names from a type string, e.g. STRUCT(SomeStruct), []STRUCT(SomeStruct)
|
|
199
|
+
*/
|
|
200
|
+
function extractAllStructNames(typeString) {
|
|
201
|
+
const matches = [];
|
|
202
|
+
// Match STRUCT(SomeStruct)
|
|
203
|
+
const match1 = typeString.match(/^STRUCT\(([^)]+)\)/);
|
|
204
|
+
if (match1)
|
|
205
|
+
matches.push(match1[1]);
|
|
206
|
+
// Match []STRUCT(SomeStruct)
|
|
207
|
+
const match2 = typeString.match(/^\[\]STRUCT\(([^)]+)\)/);
|
|
208
|
+
if (match2)
|
|
209
|
+
matches.push(match2[1]);
|
|
210
|
+
return matches;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Extracts struct name from STRUCT(name) format
|
|
214
|
+
*/
|
|
215
|
+
function extractStructName(typeString) {
|
|
216
|
+
const match = typeString.match(/^STRUCT\(([^)]+)\)/) || typeString.match(/^\[\]STRUCT\(([^)]+)\)/);
|
|
217
|
+
return match ? match[1] : null;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Generates a filename for the mini Wrekenfile
|
|
221
|
+
*/
|
|
222
|
+
function generateFilename(endpoint) {
|
|
223
|
+
// Clean the endpoint to create a valid filename
|
|
224
|
+
const cleanEndpoint = endpoint
|
|
225
|
+
.replace(/^\/+/, '') // Remove leading slashes
|
|
226
|
+
.replace(/\/+$/, '') // Remove trailing slashes
|
|
227
|
+
.replace(/[^a-zA-Z0-9-_]/g, '-') // Replace special chars with hyphens
|
|
228
|
+
.replace(/-+/g, '-') // Replace multiple hyphens with single
|
|
229
|
+
.replace(/^-|-$/g, ''); // Remove leading/trailing hyphens
|
|
230
|
+
return `mini-${cleanEndpoint}.yaml`;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Saves mini Wrekenfiles to disk (optional utility function)
|
|
234
|
+
*/
|
|
235
|
+
function saveMiniWrekenfiles(miniWrekenfiles, outputDir = './mini-wrekenfiles') {
|
|
236
|
+
// Create output directory if it doesn't exist
|
|
237
|
+
if (!fs.existsSync(outputDir)) {
|
|
238
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
239
|
+
}
|
|
240
|
+
for (const miniFile of miniWrekenfiles) {
|
|
241
|
+
const filePath = `${outputDir}/${miniFile.metadata.filename}`;
|
|
242
|
+
fs.writeFileSync(filePath, miniFile.content);
|
|
243
|
+
console.log(`Saved: ${filePath}`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Only export the main functions at the end.
|