aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/rss.ts
blob: d9c3b1ea5926b2ec50637c3d808591a939efea76 (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
62
63
64
65
66
67
68
69
70
71
import { Feed } from 'feed';
import {
  getArticleFromRawData,
  getArticles,
  getTotalArticles,
} from '../../services/graphql';
import type { Article } from '../../types';
import { CONFIG } from '../config';
import { ROUTES } from '../constants';

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

  rawArticles.edges.forEach(async (edge) => {
    articles.push(await getArticleFromRawData(edge.node));
  });

  return articles;
};

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

  const feed = new Feed({
    author,
    copyright,
    description: process.env.APP_FEED_DESCRIPTION,
    feedLinks: {
      json: `${CONFIG.url}${ROUTES.RSS}/json`,
      atom: `${CONFIG.url}${ROUTES.RSS}/atom`,
    },
    generator: 'Feed & NextJS',
    id: CONFIG.url,
    language: CONFIG.locales.defaultLocale,
    link: CONFIG.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: `${CONFIG.url}${ROUTES.ARTICLE}/${article.slug}`,
      title: article.title,
    });
  });

  return feed;
};