swagger-service-generator-arf
v1.0.2
Published
Generates frontend service files from Swagger JSON
Maintainers
Readme
Swagger Service Generator
A utility tool to automatically generate TypeScript service files and DTOs from a Swagger/OpenAPI JSON specification. This tool helps streamline the creation of frontend API integration code.
Features
- Automatically parses Swagger/OpenAPI JSON specifications
- Generates service files with appropriate API methods
- Creates typed DTOs for request and response objects
- Organizes endpoints into logical groups based on the API path
- Follows a consistent naming convention for functions and types
- Preserves the base HTTP service structure
Installation
No installation required! You can run this tool directly using npx:
npx swagger-service-generator-arf <swagger-json-path> <output-directory>For example:
npx swagger-service-generator-arf swagger.json ./testThis will generate service files and DTOs in the ./test directory based on the endpoints found in swagger.json.
If you prefer to install the package:
# Install as a dev dependency
npm install --save-dev swagger-service-generator-arf
# Or install globally
npm install -g swagger-service-generator-arfGenerated Structure
The generated code follows this structure:
output-directory/
│
├── base/
│ ├── http.service.ts # Base HTTP service with common methods
│ └── base.dto.ts # Common DTOs used across services
│
├── endpoint1/
│ ├── endpoint1.service.ts # Service for the first endpoint group
│ └── endpoint1.dto.ts # DTOs for the first endpoint group
│
├── endpoint2/
│ ├── endpoint2.service.ts # Service for the second endpoint group
│ └── endpoint2.dto.ts # DTOs for the second endpoint group
│
└── ...Service Function Naming Convention
The generator follows these naming conventions for service functions:
| HTTP Method | Function Name | Example Endpoint | Function Name | |-------------|---------------|------------------|---------------| | GET | GetAll / Get | GET /tags | GetAll() | | GET | Get | GET /tags/{id} | Get(id) | | POST | Add | POST /tags | Add(dto) | | DELETE | Delete | DELETE /tags/{id}| Delete(id) | | PUT | Update | PUT /tags | Update(dto) | | GET | GetAsDataSource | GET /tags/getAsDataSource | GetAsDataSource() |
DTO Naming Convention
The DTOs follow these naming conventions:
- Base DTO:
EntityDto(e.g.,TagDto) - Create DTO:
CreateEntityDto(e.g.,CreateTagDto) - Update DTO:
UpdateEntityDto(e.g.,UpdateTagDto)
License
MIT
