Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newsData api 에서 뉴스 가져오기 #9

Open
pol-dev-shinroo opened this issue Apr 7, 2023 · 0 comments
Open

newsData api 에서 뉴스 가져오기 #9

pol-dev-shinroo opened this issue Apr 7, 2023 · 0 comments

Comments

@pol-dev-shinroo
Copy link
Contributor

import axios from "axios";
import convertKorean from "@/utils/convertUtf";

interface NewsApi {
  country?: string;
  category?: string;
  language?: string;
  page?: string;
}

const useNewsApi = async ({
  country,
  category,
  language,
  page,
}: NewsApi = {}) => {
  try {
    let url = `https://newsdata.io/api/1/news?apikey=${process.env.NEWS_DATA_API_KEY}`;
    if (country) url += `&country=${country}`;
    if (category) url += `&category=${category}`;
    if (language) url += `&language=${language}`;
    if (page) url += `&page=${page}`;

    const response = await axios({
      method: "get",
      url,
    });

    const { data } = response;

    if (data.results.length !== 0) {
      const convertedData = data.results.map((newsItem: any) => {
        if (newsItem.content !== null) {
          const convertedContent = convertKorean(newsItem.content);
          return { ...newsItem, content: convertedContent };
        } else {
          return { ...newsItem };
        }
      });
      return { ...data, results: convertedData };
    } else {
      return data;
    }
  } catch (error) {
    return error;
  }
};

export default useNewsApi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant