@ahhaohho/response-dto 1.2.0 → 1.3.1

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.
Files changed (2) hide show
  1. package/ResponseDTO.js +16 -77
  2. package/package.json +1 -1
package/ResponseDTO.js CHANGED
@@ -1,16 +1,4 @@
1
- /**
2
- * ResponseDTO 클래스
3
- * API 응답을 표준화하기 위한 클래스입니다.
4
- * 성공 응답, 클라이언트 오류, 시스템 오류 등 모든 API 응답에 대해 일관된 구조를 제공합니다.
5
- */
6
1
  class ResponseDTO {
7
- /**
8
- * ResponseDTO 생성자
9
- * @param {string} type - 응답 유형 ('success', 'client', 또는 'system')
10
- * @param {number} status - HTTP 상태 코드
11
- * @param {object|null} data - 응답 데이터 (항상 포함, 없을 경우 null)
12
- * @param {string|null} message - 응답 메시지 (성공/실패에 대한 설명)
13
- */
14
2
  constructor(type, status, data = null, message = null) {
15
3
  this.type = type;
16
4
  this.status = status;
@@ -18,119 +6,70 @@ class ResponseDTO {
18
6
  this.message = message;
19
7
  }
20
8
 
21
- /**
22
- * 상태 코드 200의 성공 응답을 생성합니다.
23
- * @param {object|null} data - 응답에 포함될 데이터
24
- * @param {string|null} message - 성공 메시지
25
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
26
- */
27
9
  static success(data = null, message = null) {
28
10
  return new ResponseDTO('success', 200, data, message);
29
11
  }
30
12
 
31
- /**
32
- * 리소스 생성 성공을 나타내는 상태 코드 201의 응답을 생성합니다.
33
- * @param {object|null} data - 생성된 리소스의 데이터
34
- * @param {string|null} message - 생성 성공 메시지
35
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
36
- */
37
13
  static created(data = null, message = null) {
38
14
  return new ResponseDTO('success', 201, data, message);
39
15
  }
40
16
 
41
- /**
42
- * 콘텐츠가 없는 성공 응답 (상태 코드 204)을 생성합니다.
43
- * @param {string|null} message - 성공 메시지
44
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
45
- */
46
17
  static noContent(message = null) {
47
18
  return new ResponseDTO('success', 204, null, message);
48
19
  }
49
20
 
50
- /**
51
- * 잘못된 요청에 대한 클라이언트 오류 응답 (상태 코드 400)을 생성합니다.
52
- * @param {string} message - 오류 메시지
53
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
54
- */
21
+ static redirect(url, message = null, data = null, redirect = null) {
22
+ return new ResponseDTO('redirect', 200, data, redirect, message);
23
+ }
24
+
55
25
  static badRequest(message) {
56
26
  return new ResponseDTO('client', 400, null, message);
57
27
  }
58
28
 
59
- /**
60
- * 인증되지 않은 접근에 대한 클라이언트 오류 응답 (상태 코드 401)을 생성합니다.
61
- * @param {string} message - 오류 메시지
62
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
63
- */
64
29
  static unauthorized(message) {
65
30
  return new ResponseDTO('client', 401, null, message);
66
31
  }
67
32
 
68
- /**
69
- * 금지된 접근에 대한 클라이언트 오류 응답 (상태 코드 403)을 생성합니다.
70
- * @param {string} message - 오류 메시지
71
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
72
- */
73
33
  static forbidden(message) {
74
34
  return new ResponseDTO('client', 403, null, message);
75
35
  }
76
36
 
77
- /**
78
- * 리소스를 찾을 수 없는 경우의 클라이언트 오류 응답 (상태 코드 404)을 생성합니다.
79
- * @param {string} message - 오류 메시지
80
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
81
- */
82
37
  static notFound(message) {
83
38
  return new ResponseDTO('client', 404, null, message);
84
39
  }
85
40
 
86
- /**
87
- * 충돌 상황에 대한 클라이언트 오류 응답 (상태 코드 409)을 생성합니다.
88
- * @param {string} message - 오류 메시지
89
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
90
- */
91
41
  static conflict(message) {
92
42
  return new ResponseDTO('client', 409, null, message);
93
43
  }
94
44
 
95
- /**
96
- * 유효성 검사 오류에 대한 클라이언트 오류 응답 (상태 코드 422)을 생성합니다.
97
- * @param {string} message - 오류 메시지
98
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
99
- */
100
45
  static validationError(message) {
101
46
  return new ResponseDTO('client', 422, null, message);
102
47
  }
103
48
 
104
- /**
105
- * 시스템 오류 응답 (상태 코드 500)을 생성합니다.
106
- * @param {string} message - 오류 메시지
107
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
108
- */
109
49
  static systemError(message) {
110
50
  return new ResponseDTO('system', 500, null, message);
111
51
  }
112
52
 
113
- /**
114
- * 서비스 이용 불가 상황에 대한 시스템 오류 응답 (상태 코드 503)을 생성합니다.
115
- * @param {string} message - 오류 메시지
116
- * @returns {ResponseDTO} 새로운 ResponseDTO 인스턴스
117
- */
118
53
  static serviceUnavailable(message) {
119
54
  return new ResponseDTO('system', 503, null, message);
120
55
  }
121
56
 
122
- /**
123
- * ResponseDTO 인스턴스를 일반 JavaScript 객체로 변환합니다.
124
- * @returns {object} ResponseDTO의 일반 JavaScript 객체 표현
125
- */
126
57
  toJSON() {
127
- return {
58
+ const response = {
128
59
  type: this.type,
129
60
  status: this.status,
130
61
  data: this.data,
131
62
  message: this.message
132
63
  };
64
+
65
+ // 리다이렉트의 경우 redirect 객체를 최상위로 이동
66
+ if (this.type === 'redirect' && this.data?.redirect) {
67
+ response.redirect = this.data.redirect;
68
+ delete response.data;
69
+ }
70
+
71
+ return response;
133
72
  }
134
- }
135
-
136
- module.exports = ResponseDTO;
73
+ }
74
+
75
+ module.exports = ResponseDTO;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahhaohho/response-dto",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "AhhaOhho API 응답을 위한 DTO 클래스",
5
5
  "main": "index.js",
6
6
  "scripts": {