@vibexnpm/talkx 2.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.
@@ -0,0 +1,305 @@
1
+ /**
2
+ * API Client
3
+ * REST API 호출을 위한 HTTP 클라이언트
4
+ */
5
+
6
+ import { ErrorTypes, DefaultConfig } from '../constants.js';
7
+ import Logger from './Logger.js';
8
+
9
+ class ApiClient {
10
+ /**
11
+ * @param {Object} options
12
+ * @param {string} options.baseUrl - API 기본 URL
13
+ * @param {string} options.apiKey - API 키
14
+ * @param {string} options.projectId - 프로젝트 ID
15
+ * @param {string} options.jwtToken - JWT 토큰
16
+ * @param {number} [options.timeout] - 요청 타임아웃 (ms)
17
+ * @param {number} [options.logLevel] - 로그 레벨
18
+ */
19
+ constructor(options) {
20
+ this.baseUrl = options.baseUrl.replace(/\/$/, '');
21
+ this.apiKey = options.apiKey;
22
+ this.projectId = options.projectId;
23
+ this.jwtToken = options.jwtToken;
24
+ this.timeout = options.timeout || DefaultConfig.apiTimeout;
25
+
26
+ this.logger = new Logger(options.logLevel, 'ApiClient');
27
+ }
28
+
29
+ /**
30
+ * JWT 토큰 업데이트.
31
+ * logout 경로에서 토큰 제거를 위해 {@code null} 도 전달 가능.
32
+ * @param {string|null} token
33
+ */
34
+ setJwtToken(token) {
35
+ this.jwtToken = token;
36
+ }
37
+
38
+ /**
39
+ * 공통 헤더 생성
40
+ * @returns {Object}
41
+ */
42
+ _getHeaders() {
43
+ const headers = {
44
+ 'Content-Type': 'application/json',
45
+ 'X-API-KEY': this.apiKey,
46
+ 'X-PROJECT-ID': this.projectId
47
+ };
48
+
49
+ if (this.jwtToken) {
50
+ headers['Authorization'] = this.jwtToken.startsWith('Bearer ')
51
+ ? this.jwtToken
52
+ : `Bearer ${this.jwtToken}`;
53
+ }
54
+
55
+ return headers;
56
+ }
57
+
58
+ /**
59
+ * HTTP 요청 실행
60
+ * @param {string} method - HTTP 메서드
61
+ * @param {string} path - API 경로
62
+ * @param {Object} [options] - 옵션
63
+ * @param {Object} [options.body] - 요청 본문
64
+ * @param {Object} [options.params] - URL 파라미터
65
+ * @returns {Promise<Object>}
66
+ */
67
+ async request(method, path, options = {}) {
68
+ const { body, params } = options;
69
+
70
+ // URL 빌드
71
+ let url = `${this.baseUrl}${path}`;
72
+ if (params) {
73
+ const searchParams = new URLSearchParams();
74
+ Object.entries(params).forEach(([key, value]) => {
75
+ if (value !== undefined && value !== null) {
76
+ searchParams.append(key, value);
77
+ }
78
+ });
79
+ const queryString = searchParams.toString();
80
+ if (queryString) {
81
+ url += `?${queryString}`;
82
+ }
83
+ }
84
+
85
+ // AbortController for timeout
86
+ const controller = new AbortController();
87
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
88
+
89
+ let response;
90
+ try {
91
+ this.logger.debug(`${method} ${url}`, body ? { body } : '');
92
+
93
+ response = await fetch(url, {
94
+ method,
95
+ headers: this._getHeaders(),
96
+ body: body ? JSON.stringify(body) : undefined,
97
+ signal: controller.signal
98
+ });
99
+ } catch (error) {
100
+ clearTimeout(timeoutId);
101
+
102
+ if (error.name === 'AbortError') {
103
+ const timeoutError = new Error('Request timeout');
104
+ timeoutError.code = ErrorTypes.API_TIMEOUT;
105
+ throw timeoutError;
106
+ }
107
+
108
+ this.logger.error(`API Error: ${method} ${url}`, error);
109
+ throw error;
110
+ }
111
+
112
+ clearTimeout(timeoutId);
113
+
114
+ // 응답 파싱
115
+ const contentType = response.headers.get('content-type');
116
+ let data;
117
+
118
+ if (contentType && contentType.includes('application/json')) {
119
+ data = await response.json();
120
+ } else {
121
+ data = await response.text();
122
+ }
123
+
124
+ // 에러 처리
125
+ if (!response.ok) {
126
+ const error = new Error(data?.message || `HTTP ${response.status}`);
127
+ error.code = ErrorTypes.API_ERROR;
128
+ error.status = response.status;
129
+ error.response = data;
130
+ this.logger.error(`API Error: ${method} ${url}`, error);
131
+ throw error;
132
+ }
133
+
134
+ this.logger.debug(`Response ${response.status}:`, data);
135
+ return data;
136
+ }
137
+
138
+ // HTTP 메서드 헬퍼
139
+
140
+ /**
141
+ * GET 요청
142
+ * @param {string} path - API 경로
143
+ * @param {Object} [params] - URL 파라미터
144
+ * @returns {Promise<Object>}
145
+ */
146
+ get(path, params) {
147
+ return this.request('GET', path, { params });
148
+ }
149
+
150
+ /**
151
+ * POST 요청
152
+ * @param {string} path - API 경로
153
+ * @param {Object} [body] - 요청 본문
154
+ * @param {Object} [params] - URL 파라미터
155
+ * @returns {Promise<Object>}
156
+ */
157
+ post(path, body, params) {
158
+ return this.request('POST', path, { body, params });
159
+ }
160
+
161
+ /**
162
+ * PUT 요청
163
+ * @param {string} path - API 경로
164
+ * @param {Object} [body] - 요청 본문
165
+ * @returns {Promise<Object>}
166
+ */
167
+ put(path, body) {
168
+ return this.request('PUT', path, { body });
169
+ }
170
+
171
+ /**
172
+ * PATCH 요청
173
+ * @param {string} path - API 경로
174
+ * @param {Object} [body] - 요청 본문
175
+ * @returns {Promise<Object>}
176
+ */
177
+ patch(path, body) {
178
+ return this.request('PATCH', path, { body });
179
+ }
180
+
181
+ /**
182
+ * DELETE 요청
183
+ * @param {string} path - API 경로
184
+ * @param {Object} [params] - URL 파라미터
185
+ * @returns {Promise<Object>}
186
+ */
187
+ delete(path, params) {
188
+ return this.request('DELETE', path, { params });
189
+ }
190
+
191
+ /**
192
+ * multipart/form-data 파일 업로드.
193
+ *
194
+ * <p>{@code fetch} 가 업로드 진행률을 지원하지 않아 {@code XMLHttpRequest} 사용.
195
+ * Content-Type 은 브라우저가 boundary 를 포함해 자동 세팅하므로 명시하지 않는다.</p>
196
+ *
197
+ * @param {string} path - API 경로 (baseUrl 뒤에 붙음)
198
+ * @param {File|Blob} file - 업로드할 파일
199
+ * @param {Object} [options]
200
+ * @param {string} [options.fieldName='file'] - multipart field 이름 (서버 @RequestParam 과 일치)
201
+ * @param {Function} [options.onProgress] - {@code ({loaded, total, percent}) => void} — 업로드 진행률 콜백
202
+ * @param {AbortSignal} [options.signal] - 업로드 취소용 AbortSignal
203
+ * @param {number} [options.timeout=600000] - 타임아웃 (ms). 기본 10분.
204
+ * @returns {Promise<Object>} 서버 응답 JSON (SuccessResponse 래퍼 포함)
205
+ */
206
+ upload(path, file, options = {}) {
207
+ const {
208
+ fieldName = 'file',
209
+ onProgress,
210
+ signal,
211
+ timeout = 600_000
212
+ } = options;
213
+
214
+ return new Promise((resolve, reject) => {
215
+ const url = `${this.baseUrl}${path}`;
216
+ const xhr = new XMLHttpRequest();
217
+ const form = new FormData();
218
+ form.append(fieldName, file);
219
+
220
+ xhr.open('POST', url);
221
+ xhr.timeout = timeout;
222
+
223
+ // Content-Type 은 XHR 이 multipart boundary 포함해 자동 세팅 — 명시 금지
224
+ xhr.setRequestHeader('X-API-KEY', this.apiKey);
225
+ xhr.setRequestHeader('X-PROJECT-ID', this.projectId);
226
+ if (this.jwtToken) {
227
+ xhr.setRequestHeader(
228
+ 'Authorization',
229
+ this.jwtToken.startsWith('Bearer ') ? this.jwtToken : `Bearer ${this.jwtToken}`
230
+ );
231
+ }
232
+
233
+ if (typeof onProgress === 'function') {
234
+ xhr.upload.onprogress = (e) => {
235
+ if (e.lengthComputable) {
236
+ onProgress({
237
+ loaded: e.loaded,
238
+ total: e.total,
239
+ percent: Math.round((e.loaded / e.total) * 100)
240
+ });
241
+ }
242
+ };
243
+ }
244
+
245
+ // 외부 AbortSignal 과 연동 (취소 지원)
246
+ const onAbort = () => xhr.abort();
247
+ if (signal) {
248
+ if (signal.aborted) {
249
+ reject(new Error('Upload cancelled'));
250
+ return;
251
+ }
252
+ signal.addEventListener('abort', onAbort);
253
+ }
254
+ const cleanupSignal = () => {
255
+ if (signal) signal.removeEventListener('abort', onAbort);
256
+ };
257
+
258
+ xhr.onload = () => {
259
+ cleanupSignal();
260
+ let parsed = xhr.responseText;
261
+ const contentType = xhr.getResponseHeader('content-type') || '';
262
+ if (contentType.includes('application/json')) {
263
+ try { parsed = JSON.parse(xhr.responseText); } catch { /* keep text */ }
264
+ }
265
+ if (xhr.status >= 200 && xhr.status < 300) {
266
+ this.logger.debug(`Upload ${xhr.status}:`, parsed);
267
+ resolve(parsed);
268
+ } else {
269
+ const error = new Error(parsed?.message || `HTTP ${xhr.status}`);
270
+ error.code = ErrorTypes.API_ERROR;
271
+ error.status = xhr.status;
272
+ error.response = parsed;
273
+ this.logger.error(`Upload Error: POST ${url}`, error);
274
+ reject(error);
275
+ }
276
+ };
277
+
278
+ xhr.onerror = () => {
279
+ cleanupSignal();
280
+ const err = new Error('Network error during upload');
281
+ err.code = ErrorTypes.API_ERROR;
282
+ this.logger.error(`Upload Network Error: POST ${url}`, err);
283
+ reject(err);
284
+ };
285
+
286
+ xhr.ontimeout = () => {
287
+ cleanupSignal();
288
+ const err = new Error('Upload timeout');
289
+ err.code = ErrorTypes.API_TIMEOUT;
290
+ this.logger.error(`Upload Timeout: POST ${url}`, err);
291
+ reject(err);
292
+ };
293
+
294
+ xhr.onabort = () => {
295
+ cleanupSignal();
296
+ reject(new Error('Upload cancelled'));
297
+ };
298
+
299
+ this.logger.debug(`POST ${url} (multipart, ${file.size} bytes)`);
300
+ xhr.send(form);
301
+ });
302
+ }
303
+ }
304
+
305
+ export default ApiClient;
@@ -0,0 +1,113 @@
1
+ /**
2
+ * EventEmitter 유틸리티
3
+ * 이벤트 기반 통신을 위한 간단한 구현
4
+ */
5
+
6
+ class EventEmitter {
7
+ constructor() {
8
+ this._events = new Map();
9
+ }
10
+
11
+ /**
12
+ * 이벤트 리스너 등록
13
+ * @param {string} event - 이벤트 이름
14
+ * @param {Function} listener - 콜백 함수
15
+ * @returns {Function} - 구독 해제 함수
16
+ */
17
+ on(event, listener) {
18
+ if (!this._events.has(event)) {
19
+ this._events.set(event, new Set());
20
+ }
21
+ this._events.get(event).add(listener);
22
+
23
+ // 구독 해제 함수 반환
24
+ return () => this.off(event, listener);
25
+ }
26
+
27
+ /**
28
+ * 일회성 이벤트 리스너 등록
29
+ * @param {string} event - 이벤트 이름
30
+ * @param {Function} listener - 콜백 함수
31
+ * @returns {Function} - 구독 해제 함수
32
+ */
33
+ once(event, listener) {
34
+ const onceWrapper = (...args) => {
35
+ this.off(event, onceWrapper);
36
+ listener.apply(this, args);
37
+ };
38
+ onceWrapper._originalListener = listener;
39
+ return this.on(event, onceWrapper);
40
+ }
41
+
42
+ /**
43
+ * 이벤트 리스너 제거
44
+ * @param {string} event - 이벤트 이름
45
+ * @param {Function} listener - 콜백 함수
46
+ */
47
+ off(event, listener) {
48
+ const listeners = this._events.get(event);
49
+ if (listeners) {
50
+ // once로 등록된 리스너도 제거 가능하도록
51
+ for (const l of listeners) {
52
+ if (l === listener || l._originalListener === listener) {
53
+ listeners.delete(l);
54
+ break;
55
+ }
56
+ }
57
+ if (listeners.size === 0) {
58
+ this._events.delete(event);
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * 이벤트 발생.
65
+ * payload 가 없는 이벤트({@code loggedOut}, {@code pushEnabled} 등) 는 {@code data} 생략 가능.
66
+ * @param {string} event - 이벤트 이름
67
+ * @param {*} [data] - 이벤트 데이터 (선택)
68
+ */
69
+ emit(event, data) {
70
+ const listeners = this._events.get(event);
71
+ if (listeners) {
72
+ listeners.forEach(listener => {
73
+ try {
74
+ listener(data);
75
+ } catch (error) {
76
+ console.error(`Error in event listener for '${event}':`, error);
77
+ }
78
+ });
79
+ }
80
+ }
81
+
82
+ /**
83
+ * 특정 이벤트의 모든 리스너 제거 (이벤트명 생략 시 전체 제거)
84
+ * @param {string} [event] - 이벤트 이름 (선택)
85
+ */
86
+ removeAllListeners(event) {
87
+ if (event) {
88
+ this._events.delete(event);
89
+ } else {
90
+ this._events.clear();
91
+ }
92
+ }
93
+
94
+ /**
95
+ * 이벤트 리스너 수 반환
96
+ * @param {string} event - 이벤트 이름
97
+ * @returns {number}
98
+ */
99
+ listenerCount(event) {
100
+ const listeners = this._events.get(event);
101
+ return listeners ? listeners.size : 0;
102
+ }
103
+
104
+ /**
105
+ * 등록된 이벤트 이름 목록
106
+ * @returns {string[]}
107
+ */
108
+ eventNames() {
109
+ return Array.from(this._events.keys());
110
+ }
111
+ }
112
+
113
+ export default EventEmitter;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Logger 유틸리티
3
+ * 로그 레벨에 따른 콘솔 출력 관리
4
+ */
5
+
6
+ import { LogLevel } from '../constants.js';
7
+
8
+ class Logger {
9
+ /**
10
+ * @param {number} [level] - 로그 레벨
11
+ * @param {string} [prefix] - 로그 접두사
12
+ */
13
+ constructor(level = LogLevel.WARN, prefix = 'TalkFlow') {
14
+ this.level = level;
15
+ this.prefix = prefix;
16
+ }
17
+
18
+ /**
19
+ * 로그 레벨 설정
20
+ * @param {number} level - 로그 레벨
21
+ */
22
+ setLevel(level) {
23
+ this.level = level;
24
+ }
25
+
26
+ /**
27
+ * 로그 접두사 설정
28
+ * @param {string} prefix - 로그 접두사
29
+ */
30
+ setPrefix(prefix) {
31
+ this.prefix = prefix;
32
+ }
33
+
34
+ /**
35
+ * 로그 포맷 생성
36
+ * @private
37
+ * @param {string} level - 로그 레벨 문자열
38
+ * @param {...*} args - 로그 인자들
39
+ * @returns {Array} 포맷된 로그 배열
40
+ */
41
+ _format(level, ...args) {
42
+ const timestamp = new Date().toISOString();
43
+ return [`[${timestamp}] [${level}] [${this.prefix}]`, ...args];
44
+ }
45
+
46
+ /**
47
+ * 디버그 로그 출력
48
+ * @param {...*} args - 로그 인자들
49
+ */
50
+ debug(...args) {
51
+ if (this.level <= LogLevel.DEBUG) {
52
+ console.debug(...this._format('DEBUG', ...args));
53
+ }
54
+ }
55
+
56
+ /**
57
+ * 정보 로그 출력
58
+ * @param {...*} args - 로그 인자들
59
+ */
60
+ info(...args) {
61
+ if (this.level <= LogLevel.INFO) {
62
+ console.info(...this._format('INFO', ...args));
63
+ }
64
+ }
65
+
66
+ /**
67
+ * 경고 로그 출력
68
+ * @param {...*} args - 로그 인자들
69
+ */
70
+ warn(...args) {
71
+ if (this.level <= LogLevel.WARN) {
72
+ console.warn(...this._format('WARN', ...args));
73
+ }
74
+ }
75
+
76
+ /**
77
+ * 에러 로그 출력
78
+ * @param {...*} args - 로그 인자들
79
+ */
80
+ error(...args) {
81
+ if (this.level <= LogLevel.ERROR) {
82
+ console.error(...this._format('ERROR', ...args));
83
+ }
84
+ }
85
+ }
86
+
87
+ export default Logger;
88
+ export { LogLevel };