Skip to content

Instantly share code, notes, and snippets.

@Miguel07Alm
Last active March 3, 2024 18:19
Show Gist options
  • Save Miguel07Alm/ad7269e24174efbaaa09c36e1de467ce to your computer and use it in GitHub Desktop.
Save Miguel07Alm/ad7269e24174efbaaa09c36e1de467ce to your computer and use it in GitHub Desktop.
It creates automatically your postman collection based in NestJS endpoints
import json
# Absolute path of your file .ts where the endpoints are located (example: '/home/dev/project/app/driver/src/driver.controller.ts')
file_path = ''
postman_collection = {
"info": {
"name": "NestJS Endpoints Collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": []
}
with open(file_path, 'r') as file:
file_content = file.read()
import re
regex_patterns = {
"@Get\\(([^)]+)\\)": "GET",
"@Post\\(([^)]+)\\)": "POST",
"@Patch\\(([^)]+)\\)": "PATCH",
"@Delete\\(([^)]+)\\)": "DELETE"
}
endpoints = []
for pattern, method in regex_patterns.items():
matches = re.findall(pattern, file_content)
for match in matches:
route = match.strip("'").strip('"')
endpoints.append({"method": method, "route": route})
postman_collection["item"] = []
for endpoint in endpoints:
postman_collection["item"].append({
"name": f"{endpoint['method']} {endpoint['route']}",
"request": {
"method": endpoint["method"],
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": f"{{{{base_url}}}}/{endpoint['route']}",
"host": [
"{{base_url}}"
],
"path": [
endpoint['route']
]
}
},
"response": []
})
collection_file_path = './nestjs_endpoints_collection.json'
with open(collection_file_path, 'w') as file:
json.dump(postman_collection, file, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment