Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions src/pages/search/SearchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// SearchPage.tsx
import { useEffect, useMemo, useState } from 'react';
import { Link, useSearchParams } from 'react-router-dom';

import type { SemanticArticle } from '@/pages/search/types/searchTypes';
import LoadingFallback from '@/routes/LoadingFallback';

import { isSemanticSearchApiError, semanticSearch } from './apis/searchApi';
import { semanticSearch } from './apis/searchApi';

const MIN_LENGTH_MESSAGE = '검색어는 두 글자 이상이어야 합니다.';
const EMPTY_RESULT_MESSAGE = '검색 결과가 없습니다.';
Expand Down Expand Up @@ -44,32 +43,28 @@ const SearchPage = () => {
setLoading(true);
setShowLengthPopup(false);
setError(null);
console.log('[SearchPage] runSearch:start', { q, page, size, threshold });

try {
const result = await semanticSearch({ query: q, page, size, threshold });
setArticles(result.articles ?? []);
} catch (e: any) {
const status = e?.response?.status;
const message = e?.response?.data?.message;

console.log('[SearchPage] runSearch:done', {
q_from_url: q,
q_in_result: result.query,
articles_len: result.articles.length,
totalCount: result.totalCount,
});

setArticles(result.articles);
} catch (e) {
console.log('[SearchPage] runSearch:error', e);
setShowLengthPopup(false);
if (isSemanticSearchApiError(e) && e.code === 'FEED_9606') {
setError(EMPTY_RESULT_MESSAGE);
setArticles(e.payload?.articles ?? []);
} else if (e instanceof Error) {
setError(e.message || '검색 결과가 없습니다.');
if (status === 204) {
setArticles([]);
} else {
setError('검색 결과가 없습니다.');
setError(null);
return;
}

if (status === 400 && message?.includes('유효한 검색어')) {
setError('유효한 검색어가 없습니다. 의미 있는 단어를 입력하세요.');
setArticles([]);
return;
}

setError('검색 중 오류가 발생했습니다.');
setArticles([]);
} finally {
setLoading(false);
}
Expand Down Expand Up @@ -100,7 +95,7 @@ const SearchPage = () => {
{loading && <LoadingFallback />}
{error && <div className={`py-10 text-center ${errorClassName}`}>{error}</div>}
{!loading && !error && articles.length === 0 && (
<div className="py-10 text-center text-black/60">검색 결과가 없습니다.</div>
<div className="py-10 text-center text-black/60">{EMPTY_RESULT_MESSAGE}</div>
)}
{!loading &&
!error &&
Expand Down
75 changes: 21 additions & 54 deletions src/shared/apis/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,58 @@ import type { AxiosResponse } from 'axios';
import type { ApiRequestOptionsTypes, ApiResponseTypes } from '../types/apiTypes';
import axiosInstance from './axios';

/**
* 기본 API 객체
*
* 사용 예시:
* - GET: api.get<UserTypes[]>('/users')
* - POST: api.post<UserTypes>('/users', userData)
* - PUT: api.put<UserTypes>('/users/1', userData)
* - DELETE: api.delete('/users/1')
*/
function extract<T>(response: AxiosResponse<any>): T {
if (response.status === 204 || response.data == null || response.data === '') {
return undefined as T;
}
const data = response.data as ApiResponseTypes<T>;
if (!data.isSuccess) {
throw new Error(data.message || 'API 요청 실패');
}
return data.result as T;
}

const api = {
/**
* GET 요청
*/
get: async <T = unknown>(url: string, options?: ApiRequestOptionsTypes): Promise<T> => {
const response: AxiosResponse<ApiResponseTypes<T>> = await axiosInstance.get(url, {
const res = await axiosInstance.get(url, {
params: options?.params,
headers: options?.headers,
timeout: options?.timeout,
});
if (!response.data.isSuccess) {
throw new Error(response.data.message || 'API 요청 실패');
}

return response.data.result as T;
return extract<T>(res);
},

/**
* POST 요청
*/
post: async <T = unknown>(url: string, data?: unknown, options?: ApiRequestOptionsTypes): Promise<T> => {
const response: AxiosResponse<ApiResponseTypes<T>> = await axiosInstance.post(url, data, {
const res = await axiosInstance.post(url, data, {
headers: options?.headers,
timeout: options?.timeout,
});
if (!response.data.isSuccess) {
throw new Error(response.data.message || 'API 요청 실패');
}

return response.data.result as T;
return extract<T>(res);
},

/**
* PUT 요청
*/
put: async <T = unknown>(url: string, data?: unknown, options?: ApiRequestOptionsTypes): Promise<T> => {
const response: AxiosResponse<ApiResponseTypes<T>> = await axiosInstance.put(url, data, {
const res = await axiosInstance.put(url, data, {
headers: options?.headers,
timeout: options?.timeout,
});
if (!response.data.isSuccess) {
throw new Error(response.data.message || 'API 요청 실패');
}

return response.data.result as T;
return extract<T>(res);
},

/**
* PATCH 요청
*/
patch: async <T = unknown>(url: string, data?: unknown, options?: ApiRequestOptionsTypes): Promise<T> => {
const response: AxiosResponse<ApiResponseTypes<T>> = await axiosInstance.patch(url, data, {
const res = await axiosInstance.patch(url, data, {
headers: options?.headers,
timeout: options?.timeout,
});
if (!response.data.isSuccess) {
throw new Error(response.data.message || 'API 요청 실패');
}

return response.data.result as T;
return extract<T>(res);
},

/**
* DELETE 요청
*/
delete: async <T = unknown>(url: string, options?: ApiRequestOptionsTypes): Promise<T> => {
const response: AxiosResponse<ApiResponseTypes<T>> = await axiosInstance.delete(url, {
const res = await axiosInstance.delete(url, {
params: options?.params,
headers: options?.headers,
timeout: options?.timeout,
});
if (!response.data.isSuccess) {
throw new Error(response.data.message || 'API 요청 실패');
}

return response.data.result as T;
return extract<T>(res);
},
};

Expand Down
Loading