Created
March 7, 2026 22:15
-
-
Save feelchi1star/ff7e53e3c9e67d45dfd9a3e0fabc6ad5 to your computer and use it in GitHub Desktop.
Scripts to Generate Postman Export collection from Nestjs App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as fs from 'fs'; | |
| import * as path from 'path'; | |
| import * as glob from 'glob'; | |
| interface PostmanItem { | |
| name: string; | |
| request: { | |
| method: string; | |
| header: any[]; | |
| url: { | |
| raw: string; | |
| host: string[]; | |
| path: string[]; | |
| variable?: any[]; | |
| }; | |
| description?: string; | |
| }; | |
| response: any[]; | |
| } | |
| interface PostmanFolder { | |
| name: string; | |
| item: (PostmanItem | PostmanFolder)[]; | |
| } | |
| const BASE_URL = '{{baseUrl}}'; | |
| function parseController(filePath: string): PostmanFolder | null { | |
| const content = fs.readFileSync(filePath, 'utf-8'); | |
| // Extract base path | |
| const controllerMatch = content.match(/@Controller\(['"](.*?)['"]\)/); | |
| if (!controllerMatch) return null; | |
| const basePath = controllerMatch[1]; | |
| // Extract class name | |
| const classMatch = content.match(/class\s+(\w+)/); | |
| const className = classMatch ? classMatch[1] : path.basename(filePath); | |
| const items: PostmanItem[] = []; | |
| // Regex for methods: @Get('path'), @Post(), etc. | |
| // It handles: @Get(), @Get('/'), @Get(':id'), etc. | |
| const methodRegex = | |
| /@(Get|Post|Patch|Put|Delete)\s*\(\s*['"]?(.*?)['"]?\s*\)/g; | |
| let match; | |
| while ((match = methodRegex.exec(content)) !== null) { | |
| const method = match[1].toUpperCase(); | |
| let subPath = match[2] || ''; | |
| // Clean up subPath | |
| if (subPath.startsWith('/')) subPath = subPath.substring(1); | |
| const fullPath = [basePath, subPath] | |
| .filter((p) => p !== '' && p !== '/') | |
| .join('/'); | |
| const cleanPath = fullPath.replace(/\/+/g, '/').replace(/^\/+/, ''); | |
| const pathSegments = cleanPath.split('/').filter((s) => s !== ''); | |
| // Attempt to find method name (heuristic: next line or same line function) | |
| const afterMatch = content.substring(match.index + match[0].length); | |
| const funcMatch = afterMatch.match(/async\s+(\w+)|(\w+)\s*\(/); | |
| const funcName = funcMatch ? funcMatch[1] || funcMatch[2] : 'request'; | |
| items.push({ | |
| name: `${method} /${cleanPath} (${funcName})`, | |
| request: { | |
| method: method, | |
| header: [], | |
| url: { | |
| raw: `${BASE_URL}/${cleanPath}`, | |
| host: [BASE_URL], | |
| path: pathSegments, | |
| }, | |
| }, | |
| response: [], | |
| }); | |
| } | |
| if (items.length === 0) return null; | |
| return { | |
| name: className, | |
| item: items, | |
| }; | |
| } | |
| async function main() { | |
| const searchDir = | |
| '/home/feechi1star/Desktop/workspace/krikia-backend/@apps/client-service/src'; | |
| const controllers = glob.sync('**/*.controller.ts', { | |
| cwd: searchDir, | |
| absolute: true, | |
| }); | |
| console.log(`Found ${controllers.length} controllers.`); | |
| const folders: PostmanFolder[] = []; | |
| for (const file of controllers) { | |
| const folder = parseController(file); | |
| if (folder) { | |
| folders.push(folder); | |
| } | |
| } | |
| const collection = { | |
| info: { | |
| name: 'Krikia API (Static Import)', | |
| schema: | |
| 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json', | |
| }, | |
| item: folders, | |
| variable: [ | |
| { | |
| key: 'baseUrl', | |
| value: 'http://localhost:3000', | |
| type: 'string', | |
| }, | |
| ], | |
| }; | |
| const outputPath = path.resolve(__dirname, 'krikia-postman-collection.json'); | |
| fs.writeFileSync(outputPath, JSON.stringify(collection, null, 2)); | |
| console.log('Postman collection generated at:', outputPath); | |
| } | |
| main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment