client.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. 'use client';
  2. import { clsx, type ClassValue } from 'clsx';
  3. import { twMerge } from 'tailwind-merge';
  4. import { TokenData, ResultDto, ApiError } from '@/types/response/common';
  5. import { CLAIM_NAME_IDENTIFIER, CLAIM_EMAIL, CLAIM_NAME } from '@/constants/common';
  6. export function cn(...inputs: ClassValue[]) {
  7. return twMerge(clsx(inputs))
  8. }
  9. // 클라이언트용 fetch 유틸 (Route Handler 호출용)
  10. interface FetchApiOptions extends Omit<RequestInit, 'body'> {
  11. body?: unknown;
  12. }
  13. // 토큰 갱신 중복 방지
  14. let refreshPromise: Promise<boolean> | null = null;
  15. async function tryRefreshToken(): Promise<boolean> {
  16. if (refreshPromise) {
  17. return refreshPromise;
  18. }
  19. refreshPromise = fetch('/api/auth/refresh-token', { method: 'POST' })
  20. .then(res => res.json())
  21. .then((res: ResultDto<unknown>) => res.success)
  22. .catch(() => false)
  23. .finally(() => { refreshPromise = null; });
  24. return refreshPromise;
  25. }
  26. export async function fetchApi<T>(url: string, options?: FetchApiOptions): Promise<ResultDto<T>> {
  27. const isFormData = options?.body instanceof FormData;
  28. const hasBody = options?.body !== undefined;
  29. const fetchOptions: RequestInit = {
  30. ...options,
  31. headers: {
  32. ...(!isFormData && hasBody ? { 'Content-Type': 'application/json' } : {}),
  33. ...options?.headers
  34. },
  35. body: isFormData ? options.body as BodyInit : (options?.body ? JSON.stringify(options.body) : undefined)
  36. };
  37. const res: ResultDto<T> = await (await fetch(url, fetchOptions)).json();
  38. // 401 시 토큰 갱신 후 재시도
  39. if (!res.success && res.status === 401) {
  40. const refreshed = await tryRefreshToken();
  41. if (refreshed) {
  42. return (await fetch(url, fetchOptions)).json();
  43. }
  44. }
  45. return res;
  46. }
  47. // JWT 토큰에서 사용자 정보 추출
  48. export function decodeAccessToken(token: string): TokenData|null {
  49. try {
  50. const base64URL = token.split('.')[1]; // JWT의 Payload 부분
  51. const base64 = base64URL.replace(/-/g, '+').replace(/_/g, '/'); // Base64 형식 변환
  52. const jsonPayload = decodeURIComponent(
  53. atob(base64).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join('')
  54. );
  55. const payload = JSON.parse(jsonPayload);
  56. return {
  57. id: payload[CLAIM_NAME_IDENTIFIER] || null,
  58. email: payload[CLAIM_EMAIL] || null,
  59. name: payload[CLAIM_NAME] || null
  60. };
  61. } catch {
  62. return null;
  63. }
  64. }
  65. // 날짜 형식 변환 표시
  66. export function formatDate(dateString: string|null): string {
  67. if (!dateString) {
  68. return '-';
  69. }
  70. const date = new Date(dateString);
  71. const now = new Date();
  72. const diffMs = now.getTime() - date.getTime();
  73. const diffSec = Math.floor(diffMs / 1000);
  74. const diffMin = Math.floor(diffSec / 60);
  75. const diffHour = Math.floor(diffMin / 60);
  76. const diffDay = Math.floor(diffHour / 24);
  77. const diffWeek = Math.floor(diffDay / 7);
  78. const diffMonth = Math.floor(diffDay / 30);
  79. const diffYear = Math.floor(diffDay / 365);
  80. if (diffSec < 60) return '방금 전';
  81. if (diffMin < 60) return `${diffMin}분 전`;
  82. if (diffHour < 24) return `${diffHour}시간 전`;
  83. if (diffDay < 7) return `${diffDay}일 전`;
  84. if (diffWeek < 5) return `${diffWeek}주 전`;
  85. if (diffMonth < 12) return `${diffMonth}개월 전`;
  86. return `${diffYear}년 전`;
  87. }
  88. // 2025.01.01. 12:00:00 으로 조회
  89. export function getDateTime(dateString: string|null) {
  90. if (!dateString) {
  91. return '-';
  92. }
  93. const date = new Date(dateString);
  94. return date.toLocaleString('ko-KR', {
  95. year: 'numeric',
  96. month: '2-digit',
  97. day: '2-digit',
  98. hour: '2-digit',
  99. minute: '2-digit',
  100. second: '2-digit',
  101. hour12: false
  102. }).replace(/\. /g, '.').replace(/\.(\d{2}:)/, ' $1');
  103. }
  104. // 파일 이름 생성
  105. export function getDateFilename(prefix: string): string {
  106. if (!prefix) {
  107. return '';
  108. }
  109. const now = new Date();
  110. const yyyy = now.getFullYear();
  111. const MM = String(now.getMonth() + 1).padStart(2, '0');
  112. const dd = String(now.getDate()).padStart(2, '0');
  113. const hh = String(now.getHours()).padStart(2, '0');
  114. const mm = String(now.getMinutes()).padStart(2, '0');
  115. const ss = String(now.getSeconds()).padStart(2, '0');
  116. return `${prefix}-${yyyy}${MM}${dd}${hh}${mm}${ss}.png`;
  117. }
  118. // HTML 태그 제거
  119. export function stripHtmlTags(str?: string|null): string {
  120. if (!str) {
  121. return '';
  122. }
  123. const div = document.createElement('div');
  124. div.innerHTML = str;
  125. return div.textContent || div.innerText || '';
  126. }
  127. // 비밀번호 정책 안내 문구 생성성
  128. export function getPasswordPolicyMessage(policy: {
  129. passwordUppercaseLength: number;
  130. passwordNumbersLength: number;
  131. passwordSpecialcharsLength: number;
  132. }) {
  133. const parts: string[] = [];
  134. if (policy.passwordUppercaseLength > 0) {
  135. parts.push(`영문 대문자 ${policy.passwordUppercaseLength}자 이상`);
  136. }
  137. if (policy.passwordNumbersLength > 0) {
  138. parts.push(`숫자 ${policy.passwordNumbersLength}자 이상`);
  139. }
  140. if (policy.passwordSpecialcharsLength > 0) {
  141. parts.push(`특수문자 ${policy.passwordSpecialcharsLength}자 이상`);
  142. }
  143. if (parts.length > 0) {
  144. return `비밀번호는 ${parts.join(', ')}를 포함해야 합니다.`;
  145. }
  146. return '';
  147. }
  148. // 특수문자 제거
  149. export function filterSpecialCharacters(str: string) {
  150. return str.replace(/[^\p{L}\p{N}\s]/gu, '');
  151. }
  152. // ResultDto에서 첫 번째 오류 조회
  153. export function getError(errors: ApiError[]|null|undefined, index = 0): string|undefined {
  154. if (!errors || errors.length === 0) {
  155. return undefined;
  156. }
  157. return errors[index]?.description;
  158. }
  159. // 서버 응답 메시지 분석
  160. export function throwError<T>(res: ResultDto<T>): void {
  161. if (res.success) {
  162. return;
  163. }
  164. let message: string|null|undefined = getError(res.errors);
  165. if (!message) {
  166. message = res.message;
  167. }
  168. if (!message) {
  169. switch (res.status) {
  170. case 400: message = '잘못된 요청입니다.'; break;
  171. case 401:
  172. message = '로그인 후 이용해 주세요.';
  173. // 인증 만료 이벤트 발행
  174. window.dispatchEvent(new CustomEvent('auth:unauthorized'));;
  175. break;
  176. case 403: message = '권한이 없습니다.'; break;
  177. case 404: message = '요청하신 페이지를 찾을 수 없습니다.'; break;
  178. case 500: message = '서버 오류가 발생했습니다. 다시 시도해 주세요.'; break;
  179. }
  180. }
  181. if (message) {
  182. throw new Error(message);
  183. }
  184. }
  185. // 1000회 이상 조회된 게시글인지 확인
  186. export function isHotPost(showHotIcon: boolean, post: { views: number; createdAt: string }): boolean {
  187. return (showHotIcon && post.views >= 1000 && Date.now() - new Date(post.createdAt).getTime() < 7 * 24 * 60 * 60 * 1000);
  188. }
  189. // 24시간 이내에 작성된 게시글인지 확인
  190. export function isNewPost(showNewIcon: boolean, post: { createdAt: string }): boolean {
  191. return (showNewIcon && Date.now() - new Date(post.createdAt).getTime() < 24 * 60 * 60 * 1000);
  192. }
  193. // a 태그에 target="_blank" 추가
  194. export function forceTargetBlank(html: string) {
  195. return html.replace(/<a\s+(?![^>]*target=)([^>]*href=["'][^"']+["'])/gi, '<a target="_blank" rel="noopener noreferrer" $1');
  196. };
  197. // 날짜가 현재 날짜보다 지났는지 확인
  198. export function isDateOverdue(dateString: string|null, days = 0): boolean {
  199. if (!dateString) {
  200. return false;
  201. }
  202. const limitDate = new Date(new Date(dateString));
  203. limitDate.setDate(limitDate.getDate() + days);
  204. const now = new Date();
  205. return now > limitDate;
  206. }