diff --git a/src/loaders/reading.data.js b/src/loaders/reading.data.js index 77cb5298..7d766da9 100644 --- a/src/loaders/reading.data.js +++ b/src/loaders/reading.data.js @@ -1,8 +1,9 @@ +/// import axios from "axios"; import { loadEnv } from "vitepress"; const env = loadEnv("", process.cwd()); -const books = [ +const bookLogs = [ { name: "모래알만 한 진실이라도", startedAt: "2020-05", @@ -803,7 +804,11 @@ const books = [ }, ]; -const loadBook = async (bookName) => { +/** + * @param {BookLog} bookLog + * @returns {BookLog & (Book | {})} + */ +const loadBook = async (bookLog) => { try { const { data: { @@ -813,14 +818,22 @@ const loadBook = async (bookName) => { method: "get", url: `https://dapi.kakao.com/v3/search/book`, params: { - query: `"${bookName}"`, + query: `"${bookLog.name}"`, + target: "title", }, headers: { Authorization: `KakaoAK ${env.VITE_APP_KAKAO_API_KEY}`, }, }); - return book; + if (!book) { + return bookLog; + } + + return { + ...bookLog, + ...book, + }; } catch (error) { console.error(error); return {}; @@ -828,34 +841,32 @@ const loadBook = async (bookName) => { }; /** - * @param {Object} book - * @param {Array} book.authors - * @param {string} book.thumbnail - * @param {string} book.url + * @param {BookLog & (Book | {})} book + * @returns */ -const parseBook = (book) => { +const normalizeBookInfo = (book) => { + if (!book.title) { + return { + ...book, + authors: "", + thumbnail: "https://placehold.co/120x174", + url: `https://google.com/search?q=${book?.name}`, + }; + } + return { - authors: book?.authors ? book.authors.join(", ") : "", - thumbnail: book?.thumbnail - ? book.thumbnail - : "https://via.placeholder.com/120x174?text=책", - url: book?.url ? book.url : `https://google.com/search?q=${book?.title}`, + ...book, + authors: book.authors.join(", "), }; }; export default { async load() { - const _books = await Promise.all( - books.map(async (book) => { - return { - ...book, - ...parseBook(await loadBook(book.name)), - }; - }) - ); + const bookInfos = await Promise.all(bookLogs.map(loadBook)); + const normalizedBookInfos = bookInfos.map(normalizeBookInfo); return { - books: _books, + books: normalizedBookInfos, }; }, }; diff --git a/src/loaders/typedefs.js b/src/loaders/typedefs.js new file mode 100644 index 00000000..00b23f95 --- /dev/null +++ b/src/loaders/typedefs.js @@ -0,0 +1,46 @@ +/** + * @typedef {Object} BookLog + * @property {string} name + * @property {string} startedAt + * @property {string=} endAt + * @property {number} progressValue + * @property {boolean} inProgress + * @property {Log[]=} logs + */ + +/** + * @typedef {Object} Log + * @property {string} date + * @property {number} progressValue + */ + +//// Kakao Search API 책 검색 응답 + +/** + * @typedef {Object} Meta + * @property {number} total_count 검색된 문서 수 + * @property {number} pageable_count 중복된 문서를 제외하고, 처음부터 요청 페이지까지의 노출 가능 문서 수 + * @property {boolean} is_end 현재 페이지가 마지막 페이지인지 여부 + */ + +/** + * @typedef {Object} Book + * @property {string} title 도서 제목 + * @property {string} contents 도서 소개 + * @property {string} url 도서 상세 URL + * @property {string} isbn ISBN + * @property {string} datetime 도서 출판날짜 + * @property {string[]} authors 도서 저자 리스트 + * @property {string} publisher 도서 출판사 + * @property {string[]} translators 도서 번역자 리스트 + * @property {number} price 도서 정가 + * @property {number} sale_price 도서 판매가 + * @property {string} thumbnail 도서 표지 미리보기 URL + * @property {string} status 도서 판매 상태 정보 + */ + +/** + * @typedef {Object} BookSearchResponse + * @property {Meta} meta 응답 관련 정보 + * @property {Book[]} documents 응답 결과 + */