import { logger } from './logger'; import * as fs from 'fs'; export interface PostmanRequest { name: string; request: { method: string; header: { key: string; value: string }[]; url: { raw: string; protocol: string; host: string[]; path: string[]; query?: { key: string; value: string }[]; }; body?: { mode: string; raw?: string; }; }; response: { name: string; originalRequest: { method: string; header: { key: string; value: string }[]; url: { raw: string }; body?: { raw: string }; }; status: string; code: number; header: { key: string; value: string }[]; body: string; }[]; } export interface PostmanCollection { info: { name: string; description?: string; }; item: PostmanRequest[]; } export class PostmanImporter { static async importCollection(collectionData: PostmanCollection): Promise> { const patterns = new Map(); try { for (const item of collectionData.item) { const method = item.request.method; const path = '/' + item.request.url.path.join('/'); const key = `${method} ${path}`; if (!patterns.has(key)) { patterns.set(key, []); } // Extract response examples for (const response of item.response) { try { const responseBody = JSON.parse(response.body); patterns.get(key)!.push(responseBody); } catch (_error) { logger.warn(`Failed to parse response body for ${key}`, { error: error.message }); } } logger.debug(`Imported ${item.response.length} responses for ${key}`); } logger.info(`Imported Postman collection: ${collectionData.info.name} with ${patterns.size} patterns`); return patterns; } catch (_error) { logger.error('Failed to import Postman collection', { error: error.message }); return new Map(); } } static async importFromUrl(collectionUrl: string): Promise> { try { // In a real implementation, you would fetch the collection from the URL // For now, we'll return an empty map and log the attempt logger.info(`Attempting to import Postman collection from: ${collectionUrl}`); // This would be implemented with actual HTTP requests to fetch the collection // const response = await fetch(collectionUrl); // const collectionData = await response.json(); // return this.importCollection(collectionData); return new Map(); } catch (_error) { logger.error('Failed to import Postman collection from URL', { error: error.message }); return new Map(); } } static async importFromFile(filePath: string): Promise> { try { const collectionData = JSON.parse(fs.readFileSync(filePath, 'utf-8')); return this.importCollection(collectionData); } catch (_error) { logger.error('Failed to import Postman collection from file', { error: error.message }); return new Map(); } } static extractEndpointFromUrl(_: string): string { try { const urlObj = new URL(url); return urlObj.pathname; } catch { // If URL parsing fails, try to extract path from string const pathMatch = url.match(/\/[^?]*/); return pathMatch ? pathMatch[0] : '/'; } } static extractMethodFromUrl(_: string): string { // This is a simplified method - in a real Postman collection, the method would be specified // For now, we'll default to GET return 'GET'; } }