wrekenfile-converter
Version:
Convert OpenAPI and Postman specs to Wrekenfile format with mini-chunking for vector DB storage
247 lines (246 loc) • 9.31 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateMiniWrekenfiles = generateMiniWrekenfiles;
exports.saveMiniWrekenfiles = saveMiniWrekenfiles;
const yaml = __importStar(require("js-yaml"));
const fs = __importStar(require("fs"));
/**
* Generates mini Wrekenfiles by grouping interfaces by endpoint
* Each mini Wrekenfile contains all methods for a single endpoint plus their required structs
*/
function generateMiniWrekenfiles(wrekenfileContent) {
if (!wrekenfileContent || typeof wrekenfileContent !== 'string') {
throw new Error("Argument 'wrekenfileContent' is required and must be a string");
}
try {
// Parse the main Wrekenfile from YAML string
const data = yaml.load(wrekenfileContent);
if (!data.INTERFACES) {
throw new Error('No INTERFACES section found in Wrekenfile');
}
// Group interfaces by endpoint
const endpointGroups = groupInterfacesByEndpoint(data.INTERFACES);
const miniWrekenfiles = [];
// Generate a mini Wrekenfile for each endpoint group
for (const [endpoint, interfaces] of Object.entries(endpointGroups)) {
const miniWrekenfile = createMiniWrekenfile(data, endpoint, interfaces);
miniWrekenfiles.push(miniWrekenfile);
}
return miniWrekenfiles;
}
catch (error) {
console.error('Error generating mini Wrekenfiles:', error);
throw error;
}
}
/**
* Groups interfaces by their endpoint path
*/
function groupInterfacesByEndpoint(interfaces) {
const groups = {};
for (const [interfaceName, interfaceData] of Object.entries(interfaces)) {
let endpoint = interfaceData.ENDPOINT;
if (!endpoint) {
console.warn(`Interface ${interfaceName} has no ENDPOINT, skipping`);
continue;
}
// Normalize endpoint: remove backticks and trim whitespace
if (typeof endpoint === 'string') {
endpoint = endpoint.trim();
if (endpoint.startsWith('`') && endpoint.endsWith('`')) {
endpoint = endpoint.slice(1, -1).trim();
}
}
if (!groups[endpoint]) {
groups[endpoint] = {};
}
groups[endpoint][interfaceName] = interfaceData;
}
return groups;
}
/**
* Creates a complete mini Wrekenfile for a specific endpoint
*/
function createMiniWrekenfile(data, endpoint, interfaces) {
// Collect all structs referenced by the interfaces in this group
const requiredStructs = collectRequiredStructs(interfaces, data.STRUCTS || {});
// Create the mini Wrekenfile structure
const miniData = {
VERSION: data.VERSION,
INIT: data.INIT ? { DEFAULTS: data.INIT.DEFAULTS || [] } : undefined,
INTERFACES: interfaces,
STRUCTS: requiredStructs
};
// Convert to YAML
const content = yaml.dump(miniData, {
indent: 2,
lineWidth: -1,
noRefs: true
});
// Generate metadata
const methods = Object.values(interfaces).map((intf) => { var _a; return (_a = intf.HTTP) === null || _a === void 0 ? void 0 : _a.METHOD; }).filter(Boolean);
const structNames = Object.keys(requiredStructs);
const filename = generateFilename(endpoint);
return {
content,
metadata: {
endpoint,
methods,
structs: structNames,
filename
}
};
}
/**
* Collects all structs required by the given interfaces
*/
function collectRequiredStructs(interfaces, allStructs) {
const requiredStructs = {};
const processedStructs = new Set();
// Extract struct references from interfaces
const structRefs = new Set();
for (const interfaceData of Object.values(interfaces)) {
// Check INPUTS
if (interfaceData.INPUTS) {
for (const input of interfaceData.INPUTS) {
if (input.type) {
const structNames = extractAllStructNames(input.type);
for (const structName of structNames) {
if (structName)
structRefs.add(structName);
}
}
}
}
// Check RETURNS
if (interfaceData.RETURNS) {
for (const ret of interfaceData.RETURNS) {
if (ret.RETURNTYPE) {
const structNames = extractAllStructNames(ret.RETURNTYPE);
for (const structName of structNames) {
if (structName)
structRefs.add(structName);
}
}
}
}
}
// Recursively collect all required structs and their dependencies
for (const structName of structRefs) {
collectStructRecursively(structName, allStructs, requiredStructs, processedStructs);
}
return requiredStructs;
}
/**
* Recursively collects a struct and all its nested struct dependencies
*/
function collectStructRecursively(structName, allStructs, requiredStructs, processedStructs) {
if (processedStructs.has(structName)) {
return; // Already processed
}
processedStructs.add(structName);
if (!allStructs[structName]) {
console.warn(`Struct ${structName} not found in STRUCTS section`);
return;
}
// Add the struct
requiredStructs[structName] = allStructs[structName];
// Check for nested struct references
const structFields = allStructs[structName];
if (Array.isArray(structFields)) {
for (const field of structFields) {
if (field.type) {
// Handle STRUCT(SomeStruct) and []STRUCT(SomeStruct)
const nestedStructNames = extractAllStructNames(field.type);
for (const nestedStructName of nestedStructNames) {
if (nestedStructName) {
collectStructRecursively(nestedStructName, allStructs, requiredStructs, processedStructs);
}
}
}
}
}
}
/**
* Extracts all struct names from a type string, e.g. STRUCT(SomeStruct), []STRUCT(SomeStruct)
*/
function extractAllStructNames(typeString) {
const matches = [];
// Match STRUCT(SomeStruct)
const match1 = typeString.match(/^STRUCT\(([^)]+)\)/);
if (match1)
matches.push(match1[1]);
// Match []STRUCT(SomeStruct)
const match2 = typeString.match(/^\[\]STRUCT\(([^)]+)\)/);
if (match2)
matches.push(match2[1]);
return matches;
}
/**
* Extracts struct name from STRUCT(name) format
*/
function extractStructName(typeString) {
const match = typeString.match(/^STRUCT\(([^)]+)\)/) || typeString.match(/^\[\]STRUCT\(([^)]+)\)/);
return match ? match[1] : null;
}
/**
* Generates a filename for the mini Wrekenfile
*/
function generateFilename(endpoint) {
// Clean the endpoint to create a valid filename
const cleanEndpoint = endpoint
.replace(/^\/+/, '') // Remove leading slashes
.replace(/\/+$/, '') // Remove trailing slashes
.replace(/[^a-zA-Z0-9-_]/g, '-') // Replace special chars with hyphens
.replace(/-+/g, '-') // Replace multiple hyphens with single
.replace(/^-|-$/g, ''); // Remove leading/trailing hyphens
return `mini-${cleanEndpoint}.yaml`;
}
/**
* Saves mini Wrekenfiles to disk (optional utility function)
*/
function saveMiniWrekenfiles(miniWrekenfiles, outputDir = './mini-wrekenfiles') {
// Create output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
for (const miniFile of miniWrekenfiles) {
const filePath = `${outputDir}/${miniFile.metadata.filename}`;
fs.writeFileSync(filePath, miniFile.content);
console.log(`Saved: ${filePath}`);
}
}
// Only export the main functions at the end.