@su-record/vibe 0.4.6 โ†’ 0.4.7

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.
@@ -0,0 +1,375 @@
1
+ # ๐ŸŸข TypeScript + Node.js Backend ํ’ˆ์งˆ ๊ทœ์น™
2
+
3
+ ## ํ•ต์‹ฌ ์›์น™ (core์—์„œ ์ƒ์†)
4
+
5
+ ```markdown
6
+ โœ… ๋‹จ์ผ ์ฑ…์ž„ (SRP)
7
+ โœ… ์ค‘๋ณต ์ œ๊ฑฐ (DRY)
8
+ โœ… ์žฌ์‚ฌ์šฉ์„ฑ
9
+ โœ… ๋‚ฎ์€ ๋ณต์žก๋„
10
+ โœ… ํ•จ์ˆ˜ โ‰ค 30์ค„
11
+ โœ… ์ค‘์ฒฉ โ‰ค 3๋‹จ๊ณ„
12
+ โœ… Cyclomatic complexity โ‰ค 10
13
+ ```
14
+
15
+ ## Express.js ๊ทœ์น™
16
+
17
+ ### 1. ๋ผ์šฐํ„ฐ ๊ตฌ์กฐํ™”
18
+
19
+ ```typescript
20
+ // โœ… routes/user.routes.ts
21
+ import { Router } from 'express';
22
+ import { UserController } from '@/controllers/user.controller';
23
+ import { authMiddleware } from '@/middleware/auth';
24
+ import { validate } from '@/middleware/validate';
25
+ import { createUserSchema, updateUserSchema } from '@/schemas/user.schema';
26
+
27
+ const router = Router();
28
+ const controller = new UserController();
29
+
30
+ router.get('/', controller.findAll);
31
+ router.get('/:id', controller.findOne);
32
+ router.post('/', validate(createUserSchema), controller.create);
33
+ router.put('/:id', authMiddleware, validate(updateUserSchema), controller.update);
34
+ router.delete('/:id', authMiddleware, controller.delete);
35
+
36
+ export default router;
37
+ ```
38
+
39
+ ### 2. Controller ํŒจํ„ด
40
+
41
+ ```typescript
42
+ // โœ… controllers/user.controller.ts
43
+ import { Request, Response, NextFunction } from 'express';
44
+ import { UserService } from '@/services/user.service';
45
+ import { CreateUserDto, UpdateUserDto } from '@/dto/user.dto';
46
+
47
+ export class UserController {
48
+ private userService = new UserService();
49
+
50
+ findAll = async (req: Request, res: Response, next: NextFunction) => {
51
+ try {
52
+ const { page = 1, limit = 10 } = req.query;
53
+ const users = await this.userService.findAll({
54
+ page: Number(page),
55
+ limit: Number(limit),
56
+ });
57
+ res.json(users);
58
+ } catch (error) {
59
+ next(error);
60
+ }
61
+ };
62
+
63
+ findOne = async (req: Request, res: Response, next: NextFunction) => {
64
+ try {
65
+ const user = await this.userService.findById(req.params.id);
66
+ if (!user) {
67
+ return res.status(404).json({ message: '์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
68
+ }
69
+ res.json(user);
70
+ } catch (error) {
71
+ next(error);
72
+ }
73
+ };
74
+
75
+ create = async (req: Request, res: Response, next: NextFunction) => {
76
+ try {
77
+ const dto: CreateUserDto = req.body;
78
+ const user = await this.userService.create(dto);
79
+ res.status(201).json(user);
80
+ } catch (error) {
81
+ next(error);
82
+ }
83
+ };
84
+ }
85
+ ```
86
+
87
+ ### 3. Service ๋ ˆ์ด์–ด
88
+
89
+ ```typescript
90
+ // โœ… services/user.service.ts
91
+ import { prisma } from '@/lib/prisma';
92
+ import { CreateUserDto, UpdateUserDto } from '@/dto/user.dto';
93
+ import { hashPassword } from '@/utils/crypto';
94
+ import { AppError } from '@/utils/errors';
95
+
96
+ export class UserService {
97
+ async findAll(options: { page: number; limit: number }) {
98
+ const { page, limit } = options;
99
+ const skip = (page - 1) * limit;
100
+
101
+ const [users, total] = await Promise.all([
102
+ prisma.user.findMany({ skip, take: limit }),
103
+ prisma.user.count(),
104
+ ]);
105
+
106
+ return {
107
+ data: users,
108
+ meta: {
109
+ page,
110
+ limit,
111
+ total,
112
+ totalPages: Math.ceil(total / limit),
113
+ },
114
+ };
115
+ }
116
+
117
+ async findById(id: string) {
118
+ return prisma.user.findUnique({ where: { id } });
119
+ }
120
+
121
+ async create(dto: CreateUserDto) {
122
+ const existing = await prisma.user.findUnique({
123
+ where: { email: dto.email },
124
+ });
125
+
126
+ if (existing) {
127
+ throw new AppError('์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค', 409);
128
+ }
129
+
130
+ const hashedPassword = await hashPassword(dto.password);
131
+
132
+ return prisma.user.create({
133
+ data: {
134
+ ...dto,
135
+ password: hashedPassword,
136
+ },
137
+ });
138
+ }
139
+ }
140
+ ```
141
+
142
+ ## NestJS ๊ทœ์น™
143
+
144
+ ### 1. Module ๊ตฌ์กฐ
145
+
146
+ ```typescript
147
+ // โœ… user/user.module.ts
148
+ import { Module } from '@nestjs/common';
149
+ import { UserController } from './user.controller';
150
+ import { UserService } from './user.service';
151
+ import { UserRepository } from './user.repository';
152
+
153
+ @Module({
154
+ controllers: [UserController],
155
+ providers: [UserService, UserRepository],
156
+ exports: [UserService],
157
+ })
158
+ export class UserModule {}
159
+ ```
160
+
161
+ ### 2. Controller (NestJS)
162
+
163
+ ```typescript
164
+ // โœ… user/user.controller.ts
165
+ import {
166
+ Controller,
167
+ Get,
168
+ Post,
169
+ Put,
170
+ Delete,
171
+ Param,
172
+ Body,
173
+ Query,
174
+ UseGuards,
175
+ ParseIntPipe,
176
+ } from '@nestjs/common';
177
+ import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
178
+ import { UserService } from './user.service';
179
+ import { CreateUserDto, UpdateUserDto, UserQueryDto } from './dto';
180
+ import { JwtAuthGuard } from '@/auth/guards/jwt-auth.guard';
181
+ import { CurrentUser } from '@/auth/decorators/current-user.decorator';
182
+
183
+ @ApiTags('users')
184
+ @Controller('users')
185
+ export class UserController {
186
+ constructor(private readonly userService: UserService) {}
187
+
188
+ @Get()
189
+ @ApiOperation({ summary: '์‚ฌ์šฉ์ž ๋ชฉ๋ก ์กฐํšŒ' })
190
+ async findAll(@Query() query: UserQueryDto) {
191
+ return this.userService.findAll(query);
192
+ }
193
+
194
+ @Get(':id')
195
+ @ApiOperation({ summary: '์‚ฌ์šฉ์ž ์ƒ์„ธ ์กฐํšŒ' })
196
+ async findOne(@Param('id', ParseIntPipe) id: number) {
197
+ return this.userService.findById(id);
198
+ }
199
+
200
+ @Post()
201
+ @ApiOperation({ summary: '์‚ฌ์šฉ์ž ์ƒ์„ฑ' })
202
+ async create(@Body() dto: CreateUserDto) {
203
+ return this.userService.create(dto);
204
+ }
205
+
206
+ @Put(':id')
207
+ @UseGuards(JwtAuthGuard)
208
+ @ApiBearerAuth()
209
+ @ApiOperation({ summary: '์‚ฌ์šฉ์ž ์ˆ˜์ •' })
210
+ async update(
211
+ @Param('id', ParseIntPipe) id: number,
212
+ @Body() dto: UpdateUserDto,
213
+ @CurrentUser() user: User,
214
+ ) {
215
+ return this.userService.update(id, dto, user);
216
+ }
217
+ }
218
+ ```
219
+
220
+ ### 3. Service (NestJS)
221
+
222
+ ```typescript
223
+ // โœ… user/user.service.ts
224
+ import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
225
+ import { UserRepository } from './user.repository';
226
+ import { CreateUserDto, UpdateUserDto } from './dto';
227
+ import * as bcrypt from 'bcrypt';
228
+
229
+ @Injectable()
230
+ export class UserService {
231
+ constructor(private readonly userRepository: UserRepository) {}
232
+
233
+ async findAll(query: UserQueryDto) {
234
+ return this.userRepository.findAll(query);
235
+ }
236
+
237
+ async findById(id: number) {
238
+ const user = await this.userRepository.findById(id);
239
+ if (!user) {
240
+ throw new NotFoundException('์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค');
241
+ }
242
+ return user;
243
+ }
244
+
245
+ async create(dto: CreateUserDto) {
246
+ const existing = await this.userRepository.findByEmail(dto.email);
247
+ if (existing) {
248
+ throw new ConflictException('์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค');
249
+ }
250
+
251
+ const hashedPassword = await bcrypt.hash(dto.password, 10);
252
+ return this.userRepository.create({
253
+ ...dto,
254
+ password: hashedPassword,
255
+ });
256
+ }
257
+ }
258
+ ```
259
+
260
+ ### 4. DTO์™€ Validation
261
+
262
+ ```typescript
263
+ // โœ… user/dto/create-user.dto.ts
264
+ import { IsEmail, IsString, MinLength, IsOptional } from 'class-validator';
265
+ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
266
+
267
+ export class CreateUserDto {
268
+ @ApiProperty({ example: 'user@example.com' })
269
+ @IsEmail({}, { message: '์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค' })
270
+ email: string;
271
+
272
+ @ApiProperty({ example: 'password123' })
273
+ @IsString()
274
+ @MinLength(8, { message: '๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค' })
275
+ password: string;
276
+
277
+ @ApiProperty({ example: 'ํ™๊ธธ๋™' })
278
+ @IsString()
279
+ name: string;
280
+
281
+ @ApiPropertyOptional({ example: '010-1234-5678' })
282
+ @IsOptional()
283
+ @IsString()
284
+ phone?: string;
285
+ }
286
+ ```
287
+
288
+ ## ๊ณตํ†ต ๊ทœ์น™
289
+
290
+ ### ์—๋Ÿฌ ์ฒ˜๋ฆฌ
291
+
292
+ ```typescript
293
+ // โœ… utils/errors.ts
294
+ export class AppError extends Error {
295
+ constructor(
296
+ message: string,
297
+ public statusCode: number = 500,
298
+ public code?: string,
299
+ ) {
300
+ super(message);
301
+ this.name = 'AppError';
302
+ }
303
+ }
304
+
305
+ // โœ… middleware/error.middleware.ts
306
+ import { Request, Response, NextFunction } from 'express';
307
+ import { AppError } from '@/utils/errors';
308
+
309
+ export function errorHandler(
310
+ err: Error,
311
+ req: Request,
312
+ res: Response,
313
+ next: NextFunction,
314
+ ) {
315
+ console.error(err);
316
+
317
+ if (err instanceof AppError) {
318
+ return res.status(err.statusCode).json({
319
+ success: false,
320
+ message: err.message,
321
+ code: err.code,
322
+ });
323
+ }
324
+
325
+ res.status(500).json({
326
+ success: false,
327
+ message: '์„œ๋ฒ„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค',
328
+ });
329
+ }
330
+ ```
331
+
332
+ ### Validation (Zod)
333
+
334
+ ```typescript
335
+ // โœ… schemas/user.schema.ts
336
+ import { z } from 'zod';
337
+
338
+ export const createUserSchema = z.object({
339
+ body: z.object({
340
+ email: z.string().email('์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค'),
341
+ password: z.string().min(8, '๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
342
+ name: z.string().min(1, '์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”'),
343
+ phone: z.string().optional(),
344
+ }),
345
+ });
346
+
347
+ export type CreateUserInput = z.infer<typeof createUserSchema>['body'];
348
+ ```
349
+
350
+ ## ํŒŒ์ผ ๊ตฌ์กฐ
351
+
352
+ ```
353
+ src/
354
+ โ”œโ”€โ”€ controllers/ # ๋ผ์šฐํŠธ ํ•ธ๋“ค๋Ÿฌ
355
+ โ”œโ”€โ”€ services/ # ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง
356
+ โ”œโ”€โ”€ repositories/ # ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค
357
+ โ”œโ”€โ”€ dto/ # Data Transfer Objects
358
+ โ”œโ”€โ”€ schemas/ # Validation ์Šคํ‚ค๋งˆ (Zod)
359
+ โ”œโ”€โ”€ middleware/ # Express ๋ฏธ๋“ค์›จ์–ด
360
+ โ”œโ”€โ”€ routes/ # ๋ผ์šฐํ„ฐ ์ •์˜
361
+ โ”œโ”€โ”€ utils/ # ์œ ํ‹ธ๋ฆฌํ‹ฐ
362
+ โ”œโ”€โ”€ types/ # TypeScript ํƒ€์ž…
363
+ โ”œโ”€โ”€ config/ # ์„ค์ •
364
+ โ””โ”€โ”€ lib/ # ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ž˜ํผ
365
+ ```
366
+
367
+ ## ์ฒดํฌ๋ฆฌ์ŠคํŠธ
368
+
369
+ - [ ] Controller โ†’ Service โ†’ Repository ๋ ˆ์ด์–ด ๋ถ„๋ฆฌ
370
+ - [ ] DTO๋กœ ์ž…์ถœ๋ ฅ ํƒ€์ž… ์ •์˜
371
+ - [ ] Zod/class-validator๋กœ ์ž…๋ ฅ ๊ฒ€์ฆ
372
+ - [ ] ์ปค์Šคํ…€ ์—๋Ÿฌ ํด๋ž˜์Šค ์‚ฌ์šฉ
373
+ - [ ] ์—๋Ÿฌ ๋ฏธ๋“ค์›จ์–ด๋กœ ์ค‘์•™ ์ฒ˜๋ฆฌ
374
+ - [ ] `any` ํƒ€์ž… ์‚ฌ์šฉ ๊ธˆ์ง€
375
+ - [ ] async/await + try/catch ๋˜๋Š” ์—๋Ÿฌ ๋ฏธ๋“ค์›จ์–ด