1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299# Nest Swagger Builder
[](https://badge.fury.io/js/nest-swagger-builder)
[](https://opensource.org/licenses/MIT)
NestJSμμ Swagger λ¬Έμ μμ±μ ν¨μν μ€νμΌλ‘, νμ
μμ νκ³ μ§κ΄μ μΌλ‘ ꡬμ±ν μ μλ μ νΈλ¦¬ν° λΌμ΄λΈλ¬λ¦¬μ
λλ€.
[English](README.md)
## β¨ μ£Όμ νΉμ§
- **보μΌλ¬νλ μ΄νΈ κ°μ**: 볡μ‘ν Swagger λ°μ½λ μ΄ν°λ₯Ό κ°κ²°νκ² μ μΈ
- **νμ
μμ μ±**: Nest 컨νΈλ‘€λ¬ λ©μλλ₯Ό κΈ°μ€μΌλ‘ API ν€ μμ±
- **κ°λ
μ± λ° μΌκ΄μ±**: μ μΈμ μ΄κ³ λͺ
νν Swagger ꡬμ±
- **μ μ§λ³΄μμ± ν₯μ**: Swagger ꡬμ±μ λ³λ νμΌλ‘ λΆλ¦¬ κ°λ₯
- **μ μ°ν 컀μ€ν°λ§μ΄μ§**: μλ΅ κ΅¬μ‘°(statusKey, wrapperKey)λ₯Ό μν©μ λ§κ² μ‘°μ κ°λ₯
## π¦ μ€μΉ
```bash
npm install nest-swagger-builder
```
### νμ Peer Dependencies:
```bash
npm install @nestjs/common @nestjs/swagger class-transformer class-validator
```
## β
μ§μ λ²μ
- **NestJS**: ^8.0.0, ^9.0.0, ^10.0.0
- **Swagger**: ^5.0.0 ~ ^8.0.0
- **class-validator**: ^0.13.2 ~ ^0.14.0
- **class-transformer**: ^0.5.1
## π λΉ λ₯΄κ² μμνκΈ°
### 1. Swagger μ μΈ νμΌ μμ±
```typescript
// src/controllers/swagger/user.swagger.ts
import { HttpStatus } from "@nestjs/common";
import { ApiDecoratorBuilder, ApiOperator } from "nest-swagger-builder";
import { UserController } from "../user.controller";
import { UserDto } from "../../dto/user.dto";
export const ApiUser: ApiOperator<keyof UserController> = {
GetUsers: (apiOperationOptions) =>
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withBearerAuth()
.withBodyResponse(HttpStatus.OK, "ApiUser_GetUsers", [UserDto])
.build(),
};
```
### 2. 컨νΈλ‘€λ¬μμ μ¬μ©
```typescript
@ApiTags("users")
@Controller("users")
export class UserController {
@ApiUser.GetUsers({ summary: "λͺ¨λ μ¬μ©μ μ‘°ν" })
@Get()
getUsers() {
return this.users;
}
}
```
## π§© μλ΅ κ΅¬μ‘° 컀μ€ν°λ§μ΄μ§ (New)
νμ
μμλ ν/μλΉμ€λ§λ€ Interceptorμμ λ°ννλ JSON κ΅¬μ‘°κ° λ€λ¦
λλ€.
μ:
```typescript
// A μλΉμ€
{ "statusCode": 200, "data": { ... } }
// B μλΉμ€
{ "status": 200, "result": { ... } }
```
`nest-swagger-builder`λ κΈ°λ³Έμ μΌλ‘ μμ Swagger ꡬ쑰(`{}`)λ₯Ό λ°νν©λλ€.
νμ§λ§ λ€μ λ κ°μ§ λ°©μμΌλ‘ 컀μ€ν°λ§μ΄μ§λ μλ΅ κ΅¬μ‘°λ₯Ό Swaggerμ μμ½κ² λ°μν μ μμ΅λλ€:
### β
κ°λ³ μλ΅μμ μ§μ μ€μ
```typescript
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withBodyResponse(HttpStatus.OK, "UserDetail", UserDto, {
statusKey: "status",
wrapperKey: "data",
})
.build();
```
### β
κ³΅ν΅ μ€μ λΉλ μ¬μ©
```typescript
// src/config/custom-swagger-builder.ts
export const CustomSwaggerBuilder = new ApiDecoratorBuilder({
statusKey: "status",
wrapperKey: "data",
});
```
μ¬μ© μμ:
```typescript
CustomSwaggerBuilder.withOperation(apiOperationOptions)
.withBodyResponse(HttpStatus.OK, "UserDetail", UserDto)
.build();
```
## π API μλ΅ ν¨ν΄
### μν μ½λλ§ μλ μλ΅
```typescript
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withBearerAuth()
.withStatusResponse(HttpStatus.CREATED, "UserCreated", { statusKey: "statusCode" })
.build();
```
### λ°μ΄ν°κ° ν¬ν¨λ μλ΅
```typescript
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withBearerAuth()
.withBodyResponse(HttpStatus.OK, "UserProfile", UserDto, {
statusKey: "status",
wrapperKey: "data",
})
.build();
```
### λ°°μ΄ μλ΅
```typescript
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withBearerAuth()
.withBodyResponse(HttpStatus.OK, "UsersList", [UserDto], {
statusKey: "status",
wrapperKey: "data",
})
.build();
```
### μ€λ₯ μλ΅
```typescript
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withBearerAuth()
.withStatusResponse(HttpStatus.NO_CONTENT, "UserDeleted")
.withUnauthorizedResponse([
{
name: "InvalidToken",
error: "μλͺ»λ ν ν°",
description: "μ 곡λ μΈμ¦ ν ν°μ΄ μ ν¨νμ§ μμ΅λλ€",
},
])
.withNotFoundResponse([
{
name: "UserNotFound",
error: "μ¬μ©μλ₯Ό μ°Ύμ μ μμ",
description: "μ£Όμ΄μ§ IDμ μ¬μ©μλ₯Ό μ°Ύμ μ μμ΅λλ€",
},
])
.build();
```
### νμΌ μ
λ‘λ (Multipart Form Data)
```typescript
// λ¨μΌ νμΌ μ
λ‘λ
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withFormDataRequest("ProfileImage", "image")
.withBodyResponse(HttpStatus.CREATED, "ImageUploaded", ImageDto)
.build();
// λ€μ€ νμΌ μ
λ‘λ
new ApiDecoratorBuilder()
.withOperation(apiOperationOptions)
.withFormDataRequest("GalleryImages", "images", { isArray: true })
.withBodyResponse(HttpStatus.CREATED, "ImagesUploaded", [ImageDto])
.build();
```
컨νΈλ‘€λ¬μμ μ¬μ© μμ:
```typescript
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
@ApiUser.UploadImageFile({ summary: "λ¨μΌ μ΄λ―Έμ§ μ
λ‘λ" })
@UseInterceptors(FileInterceptor('image'))
@Post('upload')
uploadFile(@UploadedFile() file: Express.Multer.File) {
return { filename: file.originalname, size: file.size };
}
```
## π API μ°Έμ‘°
### ApiDecoratorBuilder
λ©μλ 체μ΄λμ ν΅ν΄ Swagger λ°μ½λ μ΄ν°λ₯Ό μμ±νλ ν΄λμ€μ
λλ€.
#### μμ±μ
```typescript
new ApiDecoratorBuilder(config?: ApiDecoratorBuilderConfig)
```
| λ§€κ°λ³μ | μ€λͺ
| νμ
|
| -------- | ------------------------------------ | --------------------------- |
| `config` | μλ΅ νμ κΈ°λ³Έ μ€μ κ°μ²΄ (μ ν μ¬ν) | `ApiDecoratorBuilderConfig` |
#### ApiDecoratorBuilderConfig
```typescript
interface ApiDecoratorBuilderConfig {
wrapperKey?: string | undefined; // μλ΅ λ°μ΄ν°λ₯Ό κ°μΈλ νλ‘νΌν° μ΄λ¦ (μ: "data")
statusKey?: string | undefined; // μν μ½λ νλ‘νΌν° μ΄λ¦ (μ: "statusCode")
}
```
#### μ£Όμ λ©μλ
| λ©μλ | μ€λͺ
| λ§€κ°λ³μ |
| -------------------------- | ---------------------------- | ------------------------------------------------------------------------------ |
| `withOperation` | API μμ
μ 보 μΆκ° | `options: ApiOperationOptions` |
| `withBearerAuth` | Bearer μΈμ¦ μΆκ° | `name?: string` |
| `withStatusResponse` | μν μ½λλ§ μλ μλ΅ μΆκ° | `status: number, key: string, options?: ResponseOptions` |
| `withBodyResponse` | λ°μ΄ν°κ° ν¬ν¨λ μλ΅ μΆκ° | `status: number, key: string, type: Type \| Type[], options?: ResponseOptions` |
| `withFormDataRequest` | νμΌ μ
λ‘λ μ§μ μΆκ° | `key: string, fileFieldName: string, options?: Record<string, any>` |
| `withErrorResponses` | 400 μ€λ₯ μλ΅ μΆκ° | `errors: ApiErrorResponse[]` |
| `withUnauthorizedResponse` | 401 μ€λ₯ μλ΅ μΆκ° | `errors: ApiErrorResponse[]` |
| `withForbiddenResponse` | 403 μ€λ₯ μλ΅ μΆκ° | `errors: ApiErrorResponse[]` |
| `withNotFoundResponse` | 404 μ€λ₯ μλ΅ μΆκ° | `errors: ApiErrorResponse[]` |
| `withDecorator` | μ¬μ©μ μ μ λ°μ½λ μ΄ν° μΆκ° | `decorator: MethodDecorator \| PropertyDecorator` |
| `build` | λͺ¨λ λ°μ½λ μ΄ν° κ²°ν© λ° λ°ν | - |
### μΈν°νμ΄μ€
#### ResponseOptions
```typescript
interface ResponseOptions {
statusKey?: string; // μλ΅μ ν¬ν¨λ μν μ½λ νλ μ΄λ¦
wrapperKey?: string; // μλ΅μ ν¬ν¨λ λ°μ΄ν° νλ μ΄λ¦
}
```
#### ApiOperator
```typescript
type ApiOperator<M extends string> = {
[key in Capitalize<M>]: (
apiOperationOptions: Required<Pick<ApiOperationOptions, "summary">> & ApiOperationOptions
) => PropertyDecorator;
};
```
## π μμ
μ΄ μ μ₯μμλ λΌμ΄λΈλ¬λ¦¬ μ¬μ© λ°©λ²μ 보μ¬μ£Όλ μμ NestJS μ ν리μΌμ΄μ
μ΄ ν¬ν¨λμ΄ μμ΅λλ€:
```bash
# μ μ₯μ 볡μ
git clone https://github.com/Kimsoo0119/nest-swagger-builder.git
# λ©μΈ νλ‘μ νΈμ μμ‘΄μ± μ€μΉ
cd nest-swagger-builder
npm install
npm run build
# μμ μ ν리μΌμ΄μ
μ€ν
cd examples
npm install
npm run start:dev
# Swagger UI μ κ·Ό
λΈλΌμ°μ μμ http://localhost:3000/api μ΄κΈ°
```
## π λΌμ΄μΌμ€
μ΄ λΌμ΄λΈλ¬λ¦¬λ MIT λΌμ΄μΌμ€ νμ λ°°ν¬λ©λλ€ - μμΈν λ΄μ©μ [LICENSE](LICENSE) νμΌμ μ°Έμ‘°νμΈμ.