aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/rss.ts
blob: 95d3b7b030268189b9445d1bff3f5a46e6a2571d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { getArticles, getTotalArticles } from '@services/graphql/articles';
import { Article } from '@ts/types/app';
import { settings } from '@utils/config';
import { Feed } from 'feed';

/**
 * Retrieve the data for all the articles.
 *
 * @returns {Promise<Article[]>} - All the articles.
 */
const getAllArticles = async (): Promise<Article[]> => {
  const totalArticles = await getTotalArticles();
  const { articles } = await getArticles({ first: totalArticles });

  return articles;
};

/**
 * Generate a new feed.
 *
 * @returns {Promise<Feed>} - The feed.
 */
export const generateFeed = async (): Promise<Feed> => {
  const author = {
    name: settings.name,
    email: process.env.APP_AUTHOR_EMAIL,
    link: settings.url,
  };
  const copyright = `${settings.name} CC BY SA ${settings.copyright.startYear} - ${settings.copyright.endYear}`;
  const title = `${settings.name} | ${settings.baseline.fr}`;

  const feed = new Feed({
    author,
    copyright,
    description: process.env.APP_FEED_DESCRIPTION,
    feedLinks: {
      json: `${settings.url}/feed/json`,
      atom: `${settings.url}/feed/atom`,
    },
    generator: 'Feed & NextJS',
    id: settings.url,
    language: settings.locales.defaultLocale,
    link: settings.url,
    title,
  });

  const articles = await getAllArticles();

  articles.forEach((article) => {
    feed.addItem({
      content: article.intro,
      date: new Date(article.meta!.dates.publication),
      description: article.intro,
      id: `${article.id}`,
      link: `${settings.url}/article/${article.slug}`,
      title: article.title,
    });
  });

  return feed;
};