@eqxjs/swagger-codegen 1.0.0-beta.1 โ 1.0.0-beta.2
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 +251 -161
- package/QUICKSTART.md +404 -70
- package/README.md +132 -12
- package/SUMMARY.md +273 -40
- package/dist/cli.js +13 -1
- package/dist/cli.js.map +1 -1
- package/dist/generator.d.ts +1 -1
- package/dist/generator.d.ts.map +1 -1
- package/dist/generator.js +46 -2
- package/dist/generator.js.map +1 -1
- package/dist/generators/controller.spec.generator.d.ts +7 -0
- package/dist/generators/controller.spec.generator.d.ts.map +1 -0
- package/dist/generators/controller.spec.generator.js +82 -0
- package/dist/generators/controller.spec.generator.js.map +1 -0
- package/dist/generators/dto.generator.d.ts +2 -2
- package/dist/generators/dto.generator.d.ts.map +1 -1
- package/dist/generators/dto.generator.js +119 -13
- package/dist/generators/dto.generator.js.map +1 -1
- package/dist/generators/dto.spec.generator.d.ts +6 -0
- package/dist/generators/dto.spec.generator.d.ts.map +1 -0
- package/dist/generators/dto.spec.generator.js +42 -0
- package/dist/generators/dto.spec.generator.js.map +1 -0
- package/dist/generators/service.spec.generator.d.ts +7 -0
- package/dist/generators/service.spec.generator.d.ts.map +1 -0
- package/dist/generators/service.spec.generator.js +49 -0
- package/dist/generators/service.spec.generator.js.map +1 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +7 -7
package/QUICKSTART.md
CHANGED
|
@@ -1,118 +1,452 @@
|
|
|
1
1
|
# Quick Start Guide
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Get started with `@eqxjs/swagger-codegen` in minutes!
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
- **`example-swagger.json`** (8.1KB) - Users & Posts API
|
|
7
|
-
- **`example-swagger.yaml`** (5.8KB) - Users & Posts API
|
|
8
|
-
- 5 DTOs, 2 Services, 2 Controllers, 2 Modules
|
|
5
|
+
## ๐ฆ Installation
|
|
9
6
|
|
|
10
|
-
###
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
### Option 1: Global Installation
|
|
8
|
+
```bash
|
|
9
|
+
npm install -g @eqxjs/swagger-codegen
|
|
10
|
+
```
|
|
14
11
|
|
|
15
|
-
|
|
12
|
+
### Option 2: Use with npx (No Installation)
|
|
13
|
+
```bash
|
|
14
|
+
npx @eqxjs/swagger-codegen generate -i ./swagger.json -o ./generated
|
|
15
|
+
```
|
|
16
16
|
|
|
17
|
+
### Option 3: Development from Source
|
|
17
18
|
```bash
|
|
18
|
-
|
|
19
|
+
git clone <repository-url>
|
|
20
|
+
cd swagger-nestjs-codegen
|
|
19
21
|
npm install
|
|
20
22
|
npm run build
|
|
23
|
+
```
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
npm run dev -- generate -i ./example-swagger.json -o ./generated
|
|
25
|
+
---
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
npm run test:all
|
|
27
|
+
## ๐ Basic Usage
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
### Generate Server Code (Default)
|
|
30
|
+
```bash
|
|
31
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./generated
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Generate with Test Files
|
|
35
|
+
```bash
|
|
36
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./generated --with-test
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Generate Client Code
|
|
40
|
+
```bash
|
|
41
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./generated --mode client
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Generate Both Server & Client
|
|
45
|
+
```bash
|
|
46
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./generated --mode both
|
|
30
47
|
```
|
|
31
48
|
|
|
32
|
-
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## ๐ Available Example Files
|
|
52
|
+
|
|
53
|
+
This repository includes example Swagger/OpenAPI files for testing:
|
|
33
54
|
|
|
34
|
-
|
|
55
|
+
| File | Format | Spec | Description |
|
|
56
|
+
|------|--------|------|-------------|
|
|
57
|
+
| `example-swagger.json` | JSON | Swagger 2.0 | Users & Posts API (5 DTOs, 2 modules) |
|
|
58
|
+
| `example-swagger.yaml` | YAML | Swagger 2.0 | Same API, 28% smaller file |
|
|
59
|
+
| `openapi3-example.json` | JSON | OpenAPI 3.0 | Products & Categories API (6 DTOs, 2 modules) |
|
|
60
|
+
| `openapi3-example.yaml` | YAML | OpenAPI 3.0 | Same API, 29% smaller file |
|
|
61
|
+
|
|
62
|
+
### Try the Examples
|
|
35
63
|
|
|
36
64
|
```bash
|
|
37
|
-
# Swagger 2.0 - JSON
|
|
38
|
-
|
|
65
|
+
# Swagger 2.0 - JSON format
|
|
66
|
+
eqxjs-swagger-codegen generate -i ./example-swagger.json -o ./output
|
|
39
67
|
|
|
40
|
-
# Swagger 2.0 - YAML
|
|
41
|
-
|
|
68
|
+
# Swagger 2.0 - YAML format
|
|
69
|
+
eqxjs-swagger-codegen generate -i ./example-swagger.yaml -o ./output
|
|
42
70
|
|
|
43
|
-
# OpenAPI 3.0 - JSON
|
|
44
|
-
|
|
71
|
+
# OpenAPI 3.0 - JSON format
|
|
72
|
+
eqxjs-swagger-codegen generate -i ./openapi3-example.json -o ./output
|
|
45
73
|
|
|
46
|
-
# OpenAPI 3.0 - YAML
|
|
47
|
-
|
|
74
|
+
# OpenAPI 3.0 - YAML format
|
|
75
|
+
eqxjs-swagger-codegen generate -i ./openapi3-example.yaml -o ./output
|
|
48
76
|
```
|
|
49
77
|
|
|
50
|
-
|
|
78
|
+
### Development Mode Commands
|
|
79
|
+
|
|
80
|
+
If you're developing the tool itself:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Install dependencies
|
|
84
|
+
npm install
|
|
85
|
+
|
|
86
|
+
# Test with an example
|
|
87
|
+
npm run dev -- generate -i ./example-swagger.json -o ./generated
|
|
88
|
+
|
|
89
|
+
# Test all formats at once
|
|
90
|
+
npm run test:all
|
|
51
91
|
|
|
92
|
+
# Clean generated files
|
|
93
|
+
npm run clean
|
|
52
94
|
```
|
|
53
|
-
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## ๐ Generated Structure
|
|
101
|
+
|
|
102
|
+
### Without Tests (Default)
|
|
103
|
+
```
|
|
104
|
+
generated/
|
|
105
|
+
โโโ dtos/
|
|
106
|
+
โ โโโ user.dto.ts # Data Transfer Objects with validation
|
|
107
|
+
โ โโโ post.dto.ts
|
|
108
|
+
โ โโโ product.dto.ts
|
|
109
|
+
โโโ users/
|
|
110
|
+
โ โโโ users.service.ts # Injectable service with business logic
|
|
111
|
+
โ โโโ users.controller.ts # REST API endpoints
|
|
112
|
+
โ โโโ users.module.ts # Feature module
|
|
113
|
+
โโโ posts/
|
|
114
|
+
โ โโโ posts.service.ts
|
|
115
|
+
โ โโโ posts.controller.ts
|
|
116
|
+
โ โโโ posts.module.ts
|
|
117
|
+
โโโ app.module.ts # Root application module (auto-updated)
|
|
118
|
+
โโโ index.ts # Barrel exports
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### With Tests (`--with-test` flag)
|
|
122
|
+
```
|
|
123
|
+
generated/
|
|
54
124
|
โโโ dtos/
|
|
55
|
-
โ โโโ
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
โ โโโ
|
|
59
|
-
โ
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
โ โโโ
|
|
63
|
-
โ
|
|
64
|
-
|
|
65
|
-
โโโ
|
|
125
|
+
โ โโโ user.dto.ts
|
|
126
|
+
โ โโโ post.dto.ts
|
|
127
|
+
โโโ users/
|
|
128
|
+
โ โโโ users.service.ts
|
|
129
|
+
โ โโโ users.controller.ts
|
|
130
|
+
โ โโโ users.module.ts
|
|
131
|
+
โโโ tests/ # Separate test directory
|
|
132
|
+
โ โโโ dtos/
|
|
133
|
+
โ โ โโโ user.dto.spec.ts # DTO validation tests
|
|
134
|
+
โ โ โโโ post.dto.spec.ts
|
|
135
|
+
โ โโโ users/
|
|
136
|
+
โ โโโ users.service.spec.ts # Service unit tests
|
|
137
|
+
โ โโโ users.controller.spec.ts # Controller tests with mocks
|
|
138
|
+
โโโ app.module.ts
|
|
139
|
+
โโโ index.ts
|
|
66
140
|
```
|
|
67
141
|
|
|
68
|
-
**
|
|
142
|
+
**Key Points:**
|
|
143
|
+
- Each resource (Swagger tag) gets its own folder with service, controller, and module
|
|
144
|
+
- All DTOs are grouped in the `dtos/` folder
|
|
145
|
+
- Tests mirror the source structure in a separate `tests/` directory
|
|
146
|
+
- `app.module.ts` is automatically updated with all generated modules
|
|
147
|
+
|
|
148
|
+
---
|
|
69
149
|
|
|
70
|
-
## Example Output
|
|
150
|
+
## ๐ Example Output
|
|
71
151
|
|
|
72
|
-
### DTO
|
|
152
|
+
### DTO with Validation
|
|
73
153
|
```typescript
|
|
74
154
|
import { ApiProperty } from '@nestjs/swagger';
|
|
75
155
|
|
|
76
|
-
export class
|
|
77
|
-
@ApiProperty({ description: '
|
|
78
|
-
|
|
156
|
+
export class CreateUserDto {
|
|
157
|
+
@ApiProperty({ description: 'User email address', type: String })
|
|
158
|
+
email: string;
|
|
79
159
|
|
|
80
|
-
@ApiProperty({ description: '
|
|
160
|
+
@ApiProperty({ description: 'User full name', type: String })
|
|
81
161
|
name: string;
|
|
162
|
+
|
|
163
|
+
@ApiProperty({ description: 'User age', type: Number, required: false })
|
|
164
|
+
age?: number;
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Service
|
|
169
|
+
```typescript
|
|
170
|
+
import { Injectable } from '@nestjs/common';
|
|
171
|
+
import { CreateUserDto } from '../dtos/create-user.dto';
|
|
172
|
+
import { User } from '../dtos/user.dto';
|
|
173
|
+
|
|
174
|
+
@Injectable()
|
|
175
|
+
export class UsersService {
|
|
176
|
+
async createUser(createUserDto: CreateUserDto): Promise<User> {
|
|
177
|
+
// Implementation here
|
|
178
|
+
throw new Error('Not implemented');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async getUser(id: string): Promise<User> {
|
|
182
|
+
// Implementation here
|
|
183
|
+
throw new Error('Not implemented');
|
|
184
|
+
}
|
|
82
185
|
}
|
|
83
186
|
```
|
|
84
187
|
|
|
85
188
|
### Controller
|
|
86
189
|
```typescript
|
|
87
|
-
@
|
|
88
|
-
@
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
190
|
+
import { Controller, Get, Post, Body, Param } from '@nestjs/common';
|
|
191
|
+
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
|
192
|
+
import { UsersService } from './users.service';
|
|
193
|
+
import { CreateUserDto } from '../dtos/create-user.dto';
|
|
194
|
+
import { User } from '../dtos/user.dto';
|
|
195
|
+
|
|
196
|
+
@ApiTags('users')
|
|
197
|
+
@Controller('users')
|
|
198
|
+
export class UsersController {
|
|
199
|
+
constructor(private readonly usersService: UsersService) {}
|
|
200
|
+
|
|
201
|
+
@ApiOperation({ summary: 'Create a new user' })
|
|
202
|
+
@ApiResponse({ status: 201, description: 'User created', type: User })
|
|
203
|
+
@Post()
|
|
204
|
+
async createUser(@Body() createUserDto: CreateUserDto): Promise<User> {
|
|
205
|
+
return this.usersService.createUser(createUserDto);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
@ApiOperation({ summary: 'Get user by ID' })
|
|
209
|
+
@ApiResponse({ status: 200, description: 'User found', type: User })
|
|
210
|
+
@Get(':id')
|
|
211
|
+
async getUser(@Param('id') id: string): Promise<User> {
|
|
212
|
+
return this.usersService.getUser(id);
|
|
94
213
|
}
|
|
95
214
|
}
|
|
96
215
|
```
|
|
97
216
|
|
|
98
|
-
|
|
217
|
+
### Generated Test (with `--with-test`)
|
|
218
|
+
```typescript
|
|
219
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
220
|
+
import { UsersController } from '../../users/users.controller';
|
|
221
|
+
import { UsersService } from '../../users/users.service';
|
|
222
|
+
|
|
223
|
+
describe('UsersController', () => {
|
|
224
|
+
let controller: UsersController;
|
|
225
|
+
let service: UsersService;
|
|
226
|
+
|
|
227
|
+
beforeEach(async () => {
|
|
228
|
+
const module: TestingModule = await Test.createTestingModule({
|
|
229
|
+
controllers: [UsersController],
|
|
230
|
+
providers: [
|
|
231
|
+
{
|
|
232
|
+
provide: UsersService,
|
|
233
|
+
useValue: {
|
|
234
|
+
createUser: jest.fn(),
|
|
235
|
+
getUser: jest.fn(),
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
],
|
|
239
|
+
}).compile();
|
|
240
|
+
|
|
241
|
+
controller = module.get<UsersController>(UsersController);
|
|
242
|
+
service = module.get<UsersService>(UsersService);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('should be defined', () => {
|
|
246
|
+
expect(controller).toBeDefined();
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## โ๏ธ Command Options
|
|
254
|
+
|
|
255
|
+
```bash
|
|
256
|
+
eqxjs-swagger-codegen generate [options]
|
|
257
|
+
|
|
258
|
+
Options:
|
|
259
|
+
-i, --input <path> Path to Swagger/OpenAPI file (JSON or YAML) [required]
|
|
260
|
+
-o, --output <path> Output directory for generated files (default: "./generated")
|
|
261
|
+
-m, --mode <mode> Generation mode: server, client, or both (default: "server")
|
|
262
|
+
--with-test Generate test files (.spec.ts) for all generated code
|
|
263
|
+
--with-validate Add class-validator decorators to DTOs
|
|
264
|
+
--only-dtos Generate only DTOs (skip services, controllers, modules)
|
|
265
|
+
-h, --help Display help for command
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Examples
|
|
269
|
+
|
|
270
|
+
```bash
|
|
271
|
+
# Basic server generation
|
|
272
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./src/generated
|
|
273
|
+
|
|
274
|
+
# Client SDK generation
|
|
275
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./client --mode client
|
|
276
|
+
|
|
277
|
+
# Both server and client
|
|
278
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./generated --mode both
|
|
279
|
+
|
|
280
|
+
# With comprehensive test suite
|
|
281
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./generated --with-test
|
|
282
|
+
|
|
283
|
+
# With validation decorators
|
|
284
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./generated --with-validate
|
|
285
|
+
|
|
286
|
+
# Only DTOs with validators (no services/controllers)
|
|
287
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./dtos --only-dtos --with-validate
|
|
288
|
+
|
|
289
|
+
# Complete setup: server + tests + validators
|
|
290
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./generated --with-test --with-validate
|
|
291
|
+
|
|
292
|
+
# Using npx (no installation)
|
|
293
|
+
npx @eqxjs/swagger-codegen generate -i ./api.yaml -o ./generated --with-validate
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## โจ Key Features
|
|
301
|
+
|
|
302
|
+
| Feature | Description |
|
|
303
|
+
|---------|-------------|
|
|
304
|
+
| ๐ **Multiple Specs** | Supports Swagger 2.0 & OpenAPI 3.0 |
|
|
305
|
+
| ๐ **Multiple Formats** | Accepts both JSON & YAML files |
|
|
306
|
+
| ๐ฏ **Type Safety** | Generates fully typed TypeScript code |
|
|
307
|
+
| ๐๏ธ **Complete Modules** | Creates services, controllers, and modules |
|
|
308
|
+
| ๐ **Swagger Decorators** | Includes @ApiTags, @ApiOperation, @ApiResponse, etc. |
|
|
309
|
+
| ๐ **Schema References** | Handles $ref, allOf, oneOf, anyOf compositions |
|
|
310
|
+
| ๐งช **Test Generation** | Creates comprehensive test suites with `--with-test` |
|
|
311
|
+
| โ
**Validation** | Adds class-validator decorators with `--with-validate` |
|
|
312
|
+
| ๐ฆ **Flexible Output** | Generate server, client, both, or DTOs only |
|
|
313
|
+
| ๐ **Three Modes** | Generate server, client, or both |
|
|
314
|
+
| ๐ง **Auto Module Import** | Updates app.module.ts automatically |
|
|
315
|
+
| ๐ซ **No Self-Imports** | Prevents circular dependency issues in DTOs |
|
|
316
|
+
|
|
317
|
+
---
|
|
318
|
+
|
|
319
|
+
## ๐ฏ Generation Modes
|
|
320
|
+
|
|
321
|
+
### Server Mode (Default)
|
|
322
|
+
Generates NestJS backend code:
|
|
323
|
+
- Controllers with REST endpoints
|
|
324
|
+
- Injectable services
|
|
325
|
+
- Feature modules
|
|
326
|
+
- DTOs with Swagger decorators
|
|
327
|
+
|
|
328
|
+
```bash
|
|
329
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./server
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Client Mode
|
|
333
|
+
Generates TypeScript client SDK:
|
|
334
|
+
- Axios-based HTTP client
|
|
335
|
+
- Typed request/response interfaces
|
|
336
|
+
- All API methods ready to use
|
|
337
|
+
|
|
338
|
+
```bash
|
|
339
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./client --mode client
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### Both Mode
|
|
343
|
+
Generates both server and client code in the same output directory:
|
|
344
|
+
|
|
345
|
+
```bash
|
|
346
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./generated --mode both
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## ๐ Documentation
|
|
352
|
+
|
|
353
|
+
| Document | Description |
|
|
354
|
+
|----------|-------------|
|
|
355
|
+
| [README.md](README.md) | Complete documentation and usage guide |
|
|
356
|
+
| [FORMATS.md](FORMATS.md) | JSON vs YAML format comparison |
|
|
357
|
+
| [OPENAPI-COMPARISON.md](OPENAPI-COMPARISON.md) | Swagger 2.0 vs OpenAPI 3.0 differences |
|
|
358
|
+
| [ARCHITECTURE.md](ARCHITECTURE.md) | System architecture and design decisions |
|
|
359
|
+
| [SUMMARY.md](SUMMARY.md) | Project overview and summary |
|
|
360
|
+
|
|
361
|
+
---
|
|
362
|
+
|
|
363
|
+
## ๐ Common Use Cases
|
|
364
|
+
|
|
365
|
+
### Use Case 1: Generate Backend from Existing API Spec
|
|
366
|
+
```bash
|
|
367
|
+
# You have an API specification and want to generate NestJS backend
|
|
368
|
+
eqxjs-swagger-codegen generate -i ./my-api.yaml -o ./src/generated
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### Use Case 2: Generate Client SDK for Frontend
|
|
372
|
+
```bash
|
|
373
|
+
# Generate TypeScript client for your React/Angular/Vue app
|
|
374
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./src/api-client --mode client
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### Use Case 3: Full-Stack with Tests
|
|
378
|
+
```bash
|
|
379
|
+
# Generate both server and client with complete test coverage
|
|
380
|
+
eqxjs-swagger-codegen generate -i ./api.json -o ./generated --mode both --with-test
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Use Case 4: Quick Prototyping with Validation
|
|
384
|
+
```bash
|
|
385
|
+
# Generate DTOs with built-in validation for rapid API development
|
|
386
|
+
npx @eqxjs/swagger-codegen generate -i ./prototype-api.yaml -o ./src --with-validate
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### Use Case 5: Shared Type Library
|
|
390
|
+
```bash
|
|
391
|
+
# Generate only DTOs for a shared package across microservices
|
|
392
|
+
eqxjs-swagger-codegen generate -i ./api-contract.json -o ./packages/types --only-dtos
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
---
|
|
396
|
+
|
|
397
|
+
## ๐ก Tips
|
|
398
|
+
|
|
399
|
+
- **Use YAML files** - They're 28-29% smaller than JSON and easier to read
|
|
400
|
+
- **Enable test generation** - Add `--with-test` for instant test coverage
|
|
401
|
+
- **Add validators** - Use `--with-validate` for automatic DTO validation with class-validator
|
|
402
|
+
- **DTOs only mode** - Use `--only-dtos` when you only need type definitions
|
|
403
|
+
- **Check app.module.ts** - It's automatically updated with all generated modules
|
|
404
|
+
- **Review generated code** - Services have placeholder implementations to fill in
|
|
405
|
+
- **Run type checking** - Use `tsc --noEmit` to verify generated code compiles
|
|
406
|
+
- **Install dependencies** - Generated tests require `@nestjs/testing` and `jest`
|
|
407
|
+
- **Install validators** - `--with-validate` requires `class-validator` and `class-transformer`
|
|
408
|
+
|
|
409
|
+
---
|
|
410
|
+
|
|
411
|
+
## ๐ Next Steps
|
|
412
|
+
|
|
413
|
+
1. **Install the package**
|
|
414
|
+
```bash
|
|
415
|
+
npm install -g @eqxjs/swagger-codegen
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
2. **Generate code from your Swagger/OpenAPI file**
|
|
419
|
+
```bash
|
|
420
|
+
eqxjs-swagger-codegen generate -i ./your-api.yaml -o ./generated --with-test
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
3. **Review the generated code**
|
|
424
|
+
- Check DTOs in `generated/dtos/`
|
|
425
|
+
- Review controllers and services
|
|
426
|
+
- See `generated/app.module.ts` for module imports
|
|
427
|
+
|
|
428
|
+
4. **Implement business logic**
|
|
429
|
+
- Fill in service method implementations
|
|
430
|
+
- Add database integration
|
|
431
|
+
- Implement error handling
|
|
432
|
+
|
|
433
|
+
5. **Run tests**
|
|
434
|
+
```bash
|
|
435
|
+
npm test
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
6. **Read the full documentation**
|
|
439
|
+
- See [README.md](README.md) for advanced features
|
|
440
|
+
- Check [ARCHITECTURE.md](ARCHITECTURE.md) for design details
|
|
99
441
|
|
|
100
|
-
|
|
101
|
-
- **[FORMATS.md](FORMATS.md)** - JSON vs YAML comparison
|
|
102
|
-
- **[OPENAPI-COMPARISON.md](OPENAPI-COMPARISON.md)** - Swagger 2.0 vs OpenAPI 3.0
|
|
103
|
-
- **[ARCHITECTURE.md](ARCHITECTURE.md)** - System architecture
|
|
104
|
-
- **[SUMMARY.md](SUMMARY.md)** - Project overview
|
|
442
|
+
---
|
|
105
443
|
|
|
106
|
-
##
|
|
444
|
+
## ๐ Need Help?
|
|
107
445
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
โ
Creates complete NestJS modules
|
|
112
|
-
โ
Includes Swagger decorators
|
|
113
|
-
โ
Handles complex schemas & references
|
|
114
|
-
โ
Supports path, query, and body parameters
|
|
446
|
+
- **Documentation**: [README.md](README.md)
|
|
447
|
+
- **Issues**: Check the repository issues page
|
|
448
|
+
- **Examples**: See the included example files in this repository
|
|
115
449
|
|
|
116
|
-
|
|
450
|
+
---
|
|
117
451
|
|
|
118
|
-
|
|
452
|
+
**Happy Coding! ๐**
|
package/README.md
CHANGED
|
@@ -31,7 +31,17 @@ A TypeScript CLI tool to generate NestJS modules, controllers, services, and DTO
|
|
|
31
31
|
|
|
32
32
|
## Installation
|
|
33
33
|
|
|
34
|
+
### From NPM
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install -g @eqxjs/swagger-codegen
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### From Source
|
|
41
|
+
|
|
34
42
|
```bash
|
|
43
|
+
git clone <repository-url>
|
|
44
|
+
cd swagger-nestjs-codegen
|
|
35
45
|
npm install
|
|
36
46
|
npm run build
|
|
37
47
|
```
|
|
@@ -74,28 +84,138 @@ npm run dev -- generate -i ./openapi3-example.yaml -o ./generated
|
|
|
74
84
|
### Production Mode
|
|
75
85
|
|
|
76
86
|
```bash
|
|
77
|
-
#
|
|
78
|
-
|
|
87
|
+
# Using the global CLI
|
|
88
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./generated
|
|
79
89
|
|
|
80
|
-
#
|
|
81
|
-
|
|
90
|
+
# Using npx (no installation required)
|
|
91
|
+
npx @eqxjs/swagger-codegen generate -i ./swagger.json -o ./generated
|
|
82
92
|
|
|
83
|
-
#
|
|
84
|
-
|
|
93
|
+
# With YAML files
|
|
94
|
+
eqxjs-swagger-codegen generate -i ./swagger.yaml -o ./generated
|
|
85
95
|
|
|
86
|
-
#
|
|
87
|
-
|
|
96
|
+
# Local installation
|
|
97
|
+
node dist/cli.js generate -i ./swagger.json -o ./generated
|
|
88
98
|
```
|
|
89
99
|
|
|
90
100
|
### Command Options
|
|
91
101
|
|
|
92
102
|
```bash
|
|
93
|
-
swagger-
|
|
103
|
+
eqxjs-swagger-codegen generate [options]
|
|
94
104
|
|
|
95
105
|
Options:
|
|
96
|
-
-i, --input <path>
|
|
97
|
-
-o, --output <path>
|
|
98
|
-
-
|
|
106
|
+
-i, --input <path> Path to Swagger/OpenAPI file (JSON or YAML) [required]
|
|
107
|
+
-o, --output <path> Output directory for generated files (default: "./generated")
|
|
108
|
+
-m, --mode <mode> Generation mode: server, client, or both (default: "server")
|
|
109
|
+
--with-test Generate test files (.spec.ts) for all generated files
|
|
110
|
+
--with-validate Add class-validator decorators to DTOs for validation
|
|
111
|
+
--only-dtos Generate only DTOs (skip services, controllers, modules)
|
|
112
|
+
-h, --help Display help for command
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Generating with Test Files
|
|
116
|
+
|
|
117
|
+
Use the `--with-test` flag to automatically generate test files for all controllers, services, and DTOs:
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
# Generate with test files
|
|
121
|
+
eqxjs-swagger-codegen generate -i ./example-swagger.json -o ./generated --with-test
|
|
122
|
+
|
|
123
|
+
# Using npx
|
|
124
|
+
npx @eqxjs/swagger-codegen generate -i ./swagger.json -o ./generated --with-test
|
|
125
|
+
|
|
126
|
+
# Development mode
|
|
127
|
+
npm run dev -- generate -i ./example-swagger.json -o ./generated --with-test
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Generating with Validation Decorators
|
|
131
|
+
|
|
132
|
+
Use the `--with-validate` flag to add class-validator decorators to all DTO properties:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
# Generate DTOs with validation decorators
|
|
136
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./generated --with-validate
|
|
137
|
+
|
|
138
|
+
# Combine with test generation
|
|
139
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./generated --with-validate --with-test
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**Generated validators include:**
|
|
143
|
+
- `@IsString()`, `@IsNumber()`, `@IsInt()`, `@IsBoolean()`, `@IsDate()` for basic types
|
|
144
|
+
- `@IsEmail()` for email format
|
|
145
|
+
- `@IsUUID()` for UUID format
|
|
146
|
+
- `@IsUrl()` for URL format
|
|
147
|
+
- `@IsEnum()` for enum values
|
|
148
|
+
- `@IsArray()` with `@IsString({ each: true })` for arrays
|
|
149
|
+
- `@ValidateNested()` with `@Type()` for nested objects
|
|
150
|
+
- `@Min()`, `@Max()` for number constraints
|
|
151
|
+
- `@MinLength()`, `@MaxLength()` for string length
|
|
152
|
+
- `@Matches()` for regex patterns
|
|
153
|
+
|
|
154
|
+
**Example output:**
|
|
155
|
+
```typescript
|
|
156
|
+
import { ApiProperty } from '@nestjs/swagger';
|
|
157
|
+
import { IsString, IsEmail, IsInt, Min } from 'class-validator';
|
|
158
|
+
|
|
159
|
+
export class CreateUserDto {
|
|
160
|
+
@IsString()
|
|
161
|
+
@IsEmail()
|
|
162
|
+
@ApiProperty({ description: 'User email', type: String })
|
|
163
|
+
email!: string;
|
|
164
|
+
|
|
165
|
+
@IsInt()
|
|
166
|
+
@Min(18)
|
|
167
|
+
@ApiProperty({ description: 'User age', type: Number })
|
|
168
|
+
age!: number;
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Generating Only DTOs
|
|
173
|
+
|
|
174
|
+
Use the `--only-dtos` flag to generate only Data Transfer Objects without services, controllers, or modules:
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
# Generate only DTOs
|
|
178
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./dtos --only-dtos
|
|
179
|
+
|
|
180
|
+
# DTOs with validators
|
|
181
|
+
eqxjs-swagger-codegen generate -i ./swagger.json -o ./dtos --only-dtos --with-validate
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
This is useful when you:
|
|
185
|
+
- Only need the data models for a separate project
|
|
186
|
+
- Want to share DTOs between multiple services
|
|
187
|
+
- Are building a monorepo and need consistent type definitions
|
|
188
|
+
- Want to generate TypeScript interfaces from your API spec
|
|
189
|
+
|
|
190
|
+
This will create test files alongside their corresponding source files:
|
|
191
|
+
- **Controller specs**: Test files with mocked services and test cases for each endpoint
|
|
192
|
+
- **Service specs**: Test files with basic setup and method existence checks
|
|
193
|
+
- **DTO specs**: Test files with class-validator integration for validating DTOs
|
|
194
|
+
|
|
195
|
+
**Example output:**
|
|
196
|
+
```
|
|
197
|
+
generated/
|
|
198
|
+
โโโ dtos/
|
|
199
|
+
โ โโโ user.dto.ts
|
|
200
|
+
โ โโโ post.dto.ts
|
|
201
|
+
โโโ users/
|
|
202
|
+
โ โโโ users.service.ts
|
|
203
|
+
โ โโโ users.controller.ts
|
|
204
|
+
โ โโโ users.module.ts
|
|
205
|
+
โโโ posts/
|
|
206
|
+
โ โโโ posts.service.ts
|
|
207
|
+
โ โโโ posts.controller.ts
|
|
208
|
+
โ โโโ posts.module.ts
|
|
209
|
+
โโโ tests/ # ๐งช Test files with same structure
|
|
210
|
+
โโโ dtos/
|
|
211
|
+
โ โโโ user.dto.spec.ts
|
|
212
|
+
โ โโโ post.dto.spec.ts
|
|
213
|
+
โโโ users/
|
|
214
|
+
โ โโโ users.controller.spec.ts
|
|
215
|
+
โ โโโ users.service.spec.ts
|
|
216
|
+
โโโ posts/
|
|
217
|
+
โโโ posts.controller.spec.ts
|
|
218
|
+
โโโ posts.service.spec.ts
|
|
99
219
|
```
|
|
100
220
|
|
|
101
221
|
## Generated File Structure
|