@eqxjs/swagger-codegen 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +230 -0
- package/FORMATS.md +180 -0
- package/OPENAPI-COMPARISON.md +355 -0
- package/QUICKSTART.md +118 -0
- package/README.md +405 -0
- package/SUMMARY.md +184 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +49 -0
- package/dist/cli.js.map +1 -0
- package/dist/file-writer.d.ts +24 -0
- package/dist/file-writer.d.ts.map +1 -0
- package/dist/file-writer.js +95 -0
- package/dist/file-writer.js.map +1 -0
- package/dist/generator.d.ts +6 -0
- package/dist/generator.d.ts.map +1 -0
- package/dist/generator.js +104 -0
- package/dist/generator.js.map +1 -0
- package/dist/generators/client-service.generator.d.ts +6 -0
- package/dist/generators/client-service.generator.d.ts.map +1 -0
- package/dist/generators/client-service.generator.js +203 -0
- package/dist/generators/client-service.generator.js.map +1 -0
- package/dist/generators/controller.generator.d.ts +6 -0
- package/dist/generators/controller.generator.d.ts.map +1 -0
- package/dist/generators/controller.generator.js +241 -0
- package/dist/generators/controller.generator.js.map +1 -0
- package/dist/generators/dto.generator.d.ts +10 -0
- package/dist/generators/dto.generator.d.ts.map +1 -0
- package/dist/generators/dto.generator.js +221 -0
- package/dist/generators/dto.generator.js.map +1 -0
- package/dist/generators/module.generator.d.ts +10 -0
- package/dist/generators/module.generator.d.ts.map +1 -0
- package/dist/generators/module.generator.js +52 -0
- package/dist/generators/module.generator.js.map +1 -0
- package/dist/generators/service.generator.d.ts +6 -0
- package/dist/generators/service.generator.d.ts.map +1 -0
- package/dist/generators/service.generator.js +179 -0
- package/dist/generators/service.generator.js.map +1 -0
- package/dist/parser.d.ts +24 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +82 -0
- package/dist/parser.js.map +1 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +38 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +114 -0
- package/dist/utils.js.map +1 -0
- package/package.json +40 -0
- package/test-all-formats.sh +32 -0
- package/test.sh +27 -0
package/ARCHITECTURE.md
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# Architecture & Workflow
|
|
2
|
+
|
|
3
|
+
## System Architecture
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
7
|
+
│ CLI Entry Point │
|
|
8
|
+
│ (src/cli.ts) │
|
|
9
|
+
│ Commander.js Interface │
|
|
10
|
+
└──────────────────────────┬──────────────────────────────────┘
|
|
11
|
+
│
|
|
12
|
+
▼
|
|
13
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
14
|
+
│ Main Generator │
|
|
15
|
+
│ (src/generator.ts) │
|
|
16
|
+
│ Orchestrates the generation flow │
|
|
17
|
+
└──────┬──────────────────┬──────────────────┬────────────────┘
|
|
18
|
+
│ │ │
|
|
19
|
+
▼ ▼ ▼
|
|
20
|
+
┌───────────────┐ ┌─────────────┐ ┌──────────────────┐
|
|
21
|
+
│ Swagger │ │ Utilities │ │ File Writer │
|
|
22
|
+
│ Parser │ │ │ │ │
|
|
23
|
+
│ (parser.ts) │ │ (utils.ts) │ │(file-writer.ts) │
|
|
24
|
+
└───────┬───────┘ └──────┬──────┘ └────────┬─────────┘
|
|
25
|
+
│ │ │
|
|
26
|
+
│ ┌────────┴────────┐ │
|
|
27
|
+
│ │ │ │
|
|
28
|
+
▼ ▼ ▼ ▼
|
|
29
|
+
┌────────────────────────────────────────────────────────┐
|
|
30
|
+
│ Code Generators (src/generators/) │
|
|
31
|
+
├────────────┬─────────────┬──────────────┬──────────────┤
|
|
32
|
+
│ DTO │ Service │ Controller │ Module │
|
|
33
|
+
│ Generator │ Generator │ Generator │ Generator │
|
|
34
|
+
└────────────┴─────────────┴──────────────┴──────────────┘
|
|
35
|
+
│ │ │ │
|
|
36
|
+
└───────────┴────────────┴──────────────┘
|
|
37
|
+
│
|
|
38
|
+
▼
|
|
39
|
+
┌────────────────┐
|
|
40
|
+
│ Output Files │
|
|
41
|
+
│ (generated/) │
|
|
42
|
+
└────────────────┘
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Data Flow
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
1. Input
|
|
49
|
+
└─> Swagger/OpenAPI File (JSON/YAML)
|
|
50
|
+
|
|
51
|
+
2. Parsing
|
|
52
|
+
└─> SwaggerParser.validate()
|
|
53
|
+
└─> Validated SwaggerSpec object
|
|
54
|
+
|
|
55
|
+
3. Analysis
|
|
56
|
+
├─> Extract schemas (definitions/components.schemas)
|
|
57
|
+
├─> Group endpoints by tags
|
|
58
|
+
└─> Resolve $ref references
|
|
59
|
+
|
|
60
|
+
4. Generation (Parallel)
|
|
61
|
+
├─> DTOs
|
|
62
|
+
│ └─> For each schema:
|
|
63
|
+
│ ├─> Generate class
|
|
64
|
+
│ ├─> Add @ApiProperty decorators
|
|
65
|
+
│ └─> Handle types, arrays, refs
|
|
66
|
+
│
|
|
67
|
+
├─> Services
|
|
68
|
+
│ └─> For each tag:
|
|
69
|
+
│ ├─> Generate @Injectable class
|
|
70
|
+
│ ├─> Create method for each endpoint
|
|
71
|
+
│ └─> Add JSDoc comments
|
|
72
|
+
│
|
|
73
|
+
├─> Controllers
|
|
74
|
+
│ └─> For each tag:
|
|
75
|
+
│ ├─> Generate @Controller class
|
|
76
|
+
│ ├─> Add HTTP method decorators
|
|
77
|
+
│ ├─> Add @ApiTags, @ApiOperation
|
|
78
|
+
│ └─> Wire to service methods
|
|
79
|
+
│
|
|
80
|
+
└─> Modules
|
|
81
|
+
└─> For each tag:
|
|
82
|
+
├─> Generate @Module class
|
|
83
|
+
├─> Import controller & service
|
|
84
|
+
└─> Configure providers/exports
|
|
85
|
+
|
|
86
|
+
5. Output
|
|
87
|
+
└─> Write TypeScript files to disk
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Naming Convention Flow
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
Input: "/api/v1/user-profiles/{userId}"
|
|
94
|
+
|
|
95
|
+
┌──────────────────────────────────────┐
|
|
96
|
+
│ Path Analysis │
|
|
97
|
+
├──────────────────────────────────────┤
|
|
98
|
+
│ Extract tag: "user-profiles" │
|
|
99
|
+
│ Extract params: ["userId"] │
|
|
100
|
+
└───────────┬──────────────────────────┘
|
|
101
|
+
│
|
|
102
|
+
▼
|
|
103
|
+
┌──────────────────────────────────────┐
|
|
104
|
+
│ Name Transformations │
|
|
105
|
+
├──────────────────────────────────────┤
|
|
106
|
+
│ PascalCase: "UserProfiles" │
|
|
107
|
+
│ ├─> UserProfilesService │
|
|
108
|
+
│ ├─> UserProfilesController │
|
|
109
|
+
│ ├─> UserProfilesModule │
|
|
110
|
+
│ └─> UserProfileDto │
|
|
111
|
+
│ │
|
|
112
|
+
│ camelCase: "userProfiles" │
|
|
113
|
+
│ └─> userProfilesService (property) │
|
|
114
|
+
│ │
|
|
115
|
+
│ kebab-case: "user-profiles" │
|
|
116
|
+
│ ├─> user-profiles.service.ts │
|
|
117
|
+
│ ├─> user-profiles.controller.ts │
|
|
118
|
+
│ └─> user-profiles.module.ts │
|
|
119
|
+
└───────────────────────────────────────┘
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Type Mapping
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
Swagger Type TypeScript Type Decorator Type
|
|
126
|
+
─────────────────────────────────────────────────────────
|
|
127
|
+
string -> string -> type: String
|
|
128
|
+
integer -> number -> type: Number
|
|
129
|
+
number -> number -> type: Number
|
|
130
|
+
boolean -> boolean -> type: Boolean
|
|
131
|
+
array -> T[] -> isArray: true
|
|
132
|
+
object -> Record<string,any> -> type: Object
|
|
133
|
+
string(date) -> Date -> type: Date
|
|
134
|
+
string(date-time) -> Date -> type: Date
|
|
135
|
+
$ref -> ClassName -> type: () => ClassName
|
|
136
|
+
enum -> string -> enum: [values]
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Generation Workflow Example
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
Step 1: Parse Swagger
|
|
143
|
+
Input: example-swagger.json
|
|
144
|
+
Output: SwaggerSpec object
|
|
145
|
+
|
|
146
|
+
Step 2: Extract Schemas
|
|
147
|
+
Found: User, CreateUserDto, UpdateUserDto, Post, CreatePostDto
|
|
148
|
+
|
|
149
|
+
Step 3: Group Endpoints
|
|
150
|
+
Tags:
|
|
151
|
+
├─> users: [GET /users, POST /users, GET /users/{id}, ...]
|
|
152
|
+
└─> posts: [GET /posts, POST /posts]
|
|
153
|
+
|
|
154
|
+
Step 4: Generate DTOs
|
|
155
|
+
├─> user.dto.ts
|
|
156
|
+
├─> create-user-dto.dto.ts
|
|
157
|
+
├─> update-user-dto.dto.ts
|
|
158
|
+
├─> post.dto.ts
|
|
159
|
+
└─> create-post-dto.dto.ts
|
|
160
|
+
|
|
161
|
+
Step 5: Generate Services
|
|
162
|
+
├─> users.service.ts
|
|
163
|
+
│ Methods: getUsers(), createUser(), getUserById(), ...
|
|
164
|
+
└─> posts.service.ts
|
|
165
|
+
Methods: getPosts(), createPost()
|
|
166
|
+
|
|
167
|
+
Step 6: Generate Controllers
|
|
168
|
+
├─> users.controller.ts
|
|
169
|
+
│ Routes: @Get(''), @Post(''), @Get(':id'), ...
|
|
170
|
+
└─> posts.controller.ts
|
|
171
|
+
Routes: @Get(''), @Post('')
|
|
172
|
+
|
|
173
|
+
Step 7: Generate Modules
|
|
174
|
+
├─> users.module.ts
|
|
175
|
+
├─> posts.module.ts
|
|
176
|
+
└─> app.module.ts (imports both)
|
|
177
|
+
|
|
178
|
+
Step 8: Generate Index
|
|
179
|
+
└─> index.ts (exports all)
|
|
180
|
+
|
|
181
|
+
Done! ✅
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## File Organization
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
Project Root
|
|
188
|
+
├── src/ (Source code)
|
|
189
|
+
│ ├── cli.ts (Entry point)
|
|
190
|
+
│ ├── generator.ts (Main logic)
|
|
191
|
+
│ ├── parser.ts (Swagger parsing)
|
|
192
|
+
│ ├── types.ts (Type definitions)
|
|
193
|
+
│ ├── utils.ts (Helpers)
|
|
194
|
+
│ ├── file-writer.ts (File I/O)
|
|
195
|
+
│ └── generators/
|
|
196
|
+
│ ├── dto.generator.ts
|
|
197
|
+
│ ├── service.generator.ts
|
|
198
|
+
│ ├── controller.generator.ts
|
|
199
|
+
│ └── module.generator.ts
|
|
200
|
+
│
|
|
201
|
+
├── dist/ (Compiled JavaScript)
|
|
202
|
+
│
|
|
203
|
+
├── generated/ (Generated code output)
|
|
204
|
+
│ ├── dtos/ (All DTOs)
|
|
205
|
+
│ │ ├── *.dto.ts
|
|
206
|
+
│ ├── products/ (Product feature module)
|
|
207
|
+
│ │ ├── products.service.ts
|
|
208
|
+
│ │ ├── products.controller.ts
|
|
209
|
+
│ │ └── products.module.ts
|
|
210
|
+
│ ├── categories/ (Category feature module)
|
|
211
|
+
│ │ ├── categories.service.ts
|
|
212
|
+
│ │ ├── categories.controller.ts
|
|
213
|
+
│ │ └── categories.module.ts
|
|
214
|
+
│ ├── app.module.ts
|
|
215
|
+
│ └── index.ts
|
|
216
|
+
│
|
|
217
|
+
├── example-swagger.json (Example input)
|
|
218
|
+
├── package.json
|
|
219
|
+
├── tsconfig.json
|
|
220
|
+
├── README.md
|
|
221
|
+
└── SUMMARY.md
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Key Design Patterns
|
|
225
|
+
|
|
226
|
+
1. **Command Pattern**: CLI commands trigger specific actions
|
|
227
|
+
2. **Strategy Pattern**: Different generators for different code types
|
|
228
|
+
3. **Factory Pattern**: Dynamic creation of code based on schemas
|
|
229
|
+
4. **Template Pattern**: Consistent structure for generated files
|
|
230
|
+
5. **Builder Pattern**: Incremental construction of output code
|
package/FORMATS.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# Format Examples
|
|
2
|
+
|
|
3
|
+
This project supports both JSON and YAML formats for Swagger/OpenAPI specifications.
|
|
4
|
+
|
|
5
|
+
## File Format Comparison
|
|
6
|
+
|
|
7
|
+
### JSON Format (`example-swagger.json`)
|
|
8
|
+
|
|
9
|
+
**Advantages:**
|
|
10
|
+
- Native JavaScript format
|
|
11
|
+
- Easier to generate programmatically
|
|
12
|
+
- Better IDE support in some cases
|
|
13
|
+
- Stricter syntax (comma-separated)
|
|
14
|
+
|
|
15
|
+
**Example:**
|
|
16
|
+
```json
|
|
17
|
+
{
|
|
18
|
+
"swagger": "2.0",
|
|
19
|
+
"info": {
|
|
20
|
+
"title": "Example API",
|
|
21
|
+
"version": "1.0.0"
|
|
22
|
+
},
|
|
23
|
+
"paths": {
|
|
24
|
+
"/users": {
|
|
25
|
+
"get": {
|
|
26
|
+
"summary": "Get all users",
|
|
27
|
+
"responses": {
|
|
28
|
+
"200": {
|
|
29
|
+
"description": "Success"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### YAML Format (`example-swagger.yaml`)
|
|
39
|
+
|
|
40
|
+
**Advantages:**
|
|
41
|
+
- More human-readable
|
|
42
|
+
- Less verbose (no quotes, commas, or braces)
|
|
43
|
+
- Widely used in API specification
|
|
44
|
+
- Better for hand-editing
|
|
45
|
+
|
|
46
|
+
**Example:**
|
|
47
|
+
```yaml
|
|
48
|
+
swagger: '2.0'
|
|
49
|
+
info:
|
|
50
|
+
title: Example API
|
|
51
|
+
version: 1.0.0
|
|
52
|
+
paths:
|
|
53
|
+
/users:
|
|
54
|
+
get:
|
|
55
|
+
summary: Get all users
|
|
56
|
+
responses:
|
|
57
|
+
'200':
|
|
58
|
+
description: Success
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
Both formats work identically with this CLI tool:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# Swagger 2.0 - JSON format
|
|
67
|
+
npm run dev -- generate -i ./example-swagger.json -o ./generated
|
|
68
|
+
|
|
69
|
+
# Swagger 2.0 - YAML format
|
|
70
|
+
npm run dev -- generate -i ./example-swagger.yaml -o ./generated
|
|
71
|
+
|
|
72
|
+
# OpenAPI 3.0 - JSON format
|
|
73
|
+
npm run dev -- generate -i ./openapi3-example.json -o ./generated
|
|
74
|
+
|
|
75
|
+
# OpenAPI 3.0 - YAML format
|
|
76
|
+
npm run dev -- generate -i ./openapi3-example.yaml -o ./generated
|
|
77
|
+
|
|
78
|
+
# YAML with .yml extension (also supported)
|
|
79
|
+
npm run dev -- generate -i ./api-spec.yml -o ./generated
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## OpenAPI 3.x Support
|
|
83
|
+
|
|
84
|
+
The tool also supports OpenAPI 3.x specifications in both formats:
|
|
85
|
+
|
|
86
|
+
### OpenAPI 3.x (JSON)
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"openapi": "3.0.0",
|
|
90
|
+
"info": {
|
|
91
|
+
"title": "Example API",
|
|
92
|
+
"version": "1.0.0"
|
|
93
|
+
},
|
|
94
|
+
"paths": {
|
|
95
|
+
"/users": {
|
|
96
|
+
"get": {
|
|
97
|
+
"summary": "Get all users",
|
|
98
|
+
"responses": {
|
|
99
|
+
"200": {
|
|
100
|
+
"description": "Success",
|
|
101
|
+
"content": {
|
|
102
|
+
"application/json": {
|
|
103
|
+
"schema": {
|
|
104
|
+
"type": "array",
|
|
105
|
+
"items": {
|
|
106
|
+
"$ref": "#/components/schemas/User"
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
"components": {
|
|
117
|
+
"schemas": {
|
|
118
|
+
"User": {
|
|
119
|
+
"type": "object",
|
|
120
|
+
"properties": {
|
|
121
|
+
"id": { "type": "string" },
|
|
122
|
+
"name": { "type": "string" }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### OpenAPI 3.x (YAML)
|
|
131
|
+
```yaml
|
|
132
|
+
openapi: 3.0.0
|
|
133
|
+
info:
|
|
134
|
+
title: Example API
|
|
135
|
+
version: 1.0.0
|
|
136
|
+
paths:
|
|
137
|
+
/users:
|
|
138
|
+
get:
|
|
139
|
+
summary: Get all users
|
|
140
|
+
responses:
|
|
141
|
+
'200':
|
|
142
|
+
description: Success
|
|
143
|
+
content:
|
|
144
|
+
application/json:
|
|
145
|
+
schema:
|
|
146
|
+
type: array
|
|
147
|
+
items:
|
|
148
|
+
$ref: '#/components/schemas/User'
|
|
149
|
+
components:
|
|
150
|
+
schemas:
|
|
151
|
+
User:
|
|
152
|
+
type: object
|
|
153
|
+
properties:
|
|
154
|
+
id:
|
|
155
|
+
type: string
|
|
156
|
+
name:
|
|
157
|
+
type: string
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Key Differences: Swagger 2.0 vs OpenAPI 3.x
|
|
161
|
+
|
|
162
|
+
### Schema Location
|
|
163
|
+
- **Swagger 2.0**: `definitions`
|
|
164
|
+
- **OpenAPI 3.x**: `components.schemas`
|
|
165
|
+
|
|
166
|
+
### Request Body
|
|
167
|
+
- **Swagger 2.0**: `parameters` with `in: "body"`
|
|
168
|
+
- **OpenAPI 3.x**: `requestBody` with `content`
|
|
169
|
+
|
|
170
|
+
### Response Content
|
|
171
|
+
- **Swagger 2.0**: `schema` directly in response
|
|
172
|
+
- **OpenAPI 3.x**: `content` -> `application/json` -> `schema`
|
|
173
|
+
|
|
174
|
+
## Recommendation
|
|
175
|
+
|
|
176
|
+
Choose the format that best fits your workflow:
|
|
177
|
+
- Use **YAML** for hand-written specifications (more readable)
|
|
178
|
+
- Use **JSON** for programmatically generated specs (easier to work with in code)
|
|
179
|
+
|
|
180
|
+
The CLI tool handles both formats seamlessly with the same commands and produces identical output.
|