Skip to content

Commit

Permalink
✨ 책 타입 추가, 코드 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
ktseo41 committed Oct 9, 2024
1 parent af3ffe8 commit a9a5b61
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/loaders/reading.data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// <reference path="typedefs.js" />
import axios from "axios";
import { loadEnv } from "vitepress";
const env = loadEnv("", process.cwd());

const books = [
const bookLogs = [
{
name: "모래알만 한 진실이라도",
startedAt: "2020-05",
Expand Down Expand Up @@ -803,7 +804,11 @@ const books = [
},
];

const loadBook = async (bookName) => {
/**
* @param {BookLog} bookLog
* @returns {BookLog & (Book | {})}
*/
const loadBook = async (bookLog) => {
try {
const {
data: {
Expand All @@ -813,49 +818,55 @@ 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 {};
}
};

/**
* @param {Object} book
* @param {Array<string>} 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,
};
},
};
46 changes: 46 additions & 0 deletions src/loaders/typedefs.js
Original file line number Diff line number Diff line change
@@ -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 응답 결과
*/

0 comments on commit a9a5b61

Please sign in to comment.