Skip to content

Conversation

@Bluemoon105
Copy link
Collaborator

@Bluemoon105 Bluemoon105 commented Feb 9, 2025

요구사항

기본 요구사항

공통

  • Github에 위클리 미션 PR을 만들어 주세요.

  • React 및 Express를 사용해 진행합니다.

  • TypeScript를 활용해 프로젝트의 필요한 곳에 타입을 명시해 주세요.

  • any 타입의 사용은 최소화해 주세요.

  • 복잡한 객체 구조나 배열 구조를 가진 변수에 인터페이스 또는 타입 별칭을 사용하세요.

  • Union, Intersection, Generics 등 고급 타입을 적극적으로 사용해 주세요.

  • 타입 별칭 또는 유틸리티 타입을 사용해 타입 복잡성을 줄여주세요.

  • 타입스크립트 컴파일러가 에러 없이 정상적으로 작동해야 합니다.

백엔드

  • 기존 Express.js 프로젝트를 타입스크립트 프로젝트로 마이그레이션 해주세요.
  • tsconfig.json 파일을 생성하고, 필요한 컴파일러 옵션을 설정해야 합니다. (예: outDir).
  • TypeScript 관련 명령어를 package.json에 설정해 주세요. (예: 빌드 및 개발 서버 실행 명령어).
  • ts-node와 nodemon을 사용하여 개발 환경을 구성합니다.
  • nodemon과 함께 ts-node를 사용하여 . ts 파일이 변경될 때 서버를 자동으로 재시작하도록 설정합니다.
  • Mongoose나 Prisma 등 ORM을 사용하는 경우, 모델에 대한 인터페이스 또는 타입을 정의합니다.
  • 필요한 경우, declare를 사용하여 타입을 오버라이드하거나 확장합니다.

멘토에게

  • 셀프 코드 리뷰를 통해 질문 이어가겠습니다.

@Bluemoon105 Bluemoon105 changed the title Express 임예지sprint11 [임예지] sprint11 Feb 9, 2025
@Bluemoon105 Bluemoon105 requested a review from kimjong95 February 9, 2025 13:33
Copy link
Collaborator

@kimjong95 kimjong95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescript로 잘 마이그레이션 해주신것 같아요.

typescript 좀더 활용해본다면 request로 들어온 값들에대한 유효성검사를 할때 (prisma가 해주고 있지만)타입가드를 활용하거나 pagination 옵션으로 공통으로 사용되는 타입을들 구조화해서 사용할 수 있을것 같아요.

또한 typescript에서 interface를 적극적으로 활용해볼수도 있을것 같습니다.

"skipLibCheck": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요구사항에따른 any타입에 대한 경고를위해 해당옵션은 true로 두는게 좋을것 같아요!

import { diskStorage } from 'multer';

// 🔹 req.user를 올바르게 인식하도록 인터페이스 정의
interface AuthRequest extends Request {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AuthRequest부분이 여러 파일에 중복되어있는것 같아요.
Auth도메인에서 타입을 export해서 사용해주시면 좋을것 같습니다.

tags?: string[];
}

export class UpdateProductDto {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 필드가 CreateProductDto의 optional 형태라면 유틸타입을 사용해볼 수도 있을것 같아요

- export class UpdateProductDto {
- @IsOptional()
-   @IsString()
-   name?: string;
- 
-   @IsOptional()
-   @IsString()
-   description?: string;
- 
-   @IsOptional()
-   @IsNumber()
-   price?: number;
- 
-   @IsOptional()
-   @IsString({ each: true })
-   tags?: string[];
- }
+ export class UpdateProductDto extends PartialType(CreateProductDto) {}

return user;
}
return null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위함수에서 유저의 검증이필요하고, 이에대한 에러처리를 각 컨트롤러에서 해주고 계실것 같아요.

result패턴을 조금 경량화해서 활용한다면 좀더 명확한 검사가 가능할것 같아요.

type ValidationResult = 
  | { isValid: true; user: User }
  | { isValid: false; reason: 'USER_NOT_FOUND' | 'INVALID_PASSWORD' }; 
  // reason대신 맞는 Error객체를 상속하여 커스텀하여도 좋을것 같아요

...
async validateUser(email: string, password: string): Promise<ValidationResult> {
    const user = await this.prisma.user.findUnique({ where: { email } });
    
    if (!user) {
      return {
        isValid: false,
        reason: 'USER_NOT_FOUND'
      };
    }

    const isPasswordValid = await bcrypt.compare(password, user.password);
    
    if (!isPasswordValid) {
      return {
        isValid: false,
        reason: 'INVALID_PASSWORD'
      };
    }

    return {
      isValid: true,
      user
    };
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants