aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-02 18:36:09 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-02 18:36:09 +0200
commitca921d7536cfe950b5a7d442977bbf900b48faf4 (patch)
tree2e8bb3f4b81414ee881c3d92d9bdfed411c569db
parent9308a6dce03bd0c616e0ba6fec227473aaa44b33 (diff)
chore: fetch posts for rss feed
-rw-r--r--src/services/graphql/articles.query.ts1
-rw-r--r--src/services/graphql/articles.ts104
-rw-r--r--src/ts/types/app.ts86
-rw-r--r--src/ts/types/raw-data.ts103
-rw-r--r--src/utils/helpers/author.ts32
-rw-r--r--src/utils/helpers/dates.ts55
-rw-r--r--src/utils/helpers/images.ts18
-rw-r--r--src/utils/helpers/pages.ts26
-rw-r--r--src/utils/helpers/rss.ts44
9 files changed, 450 insertions, 19 deletions
diff --git a/src/services/graphql/articles.query.ts b/src/services/graphql/articles.query.ts
index e384aba..e62835d 100644
--- a/src/services/graphql/articles.query.ts
+++ b/src/services/graphql/articles.query.ts
@@ -89,6 +89,7 @@ export const articlesQuery = `query Articles($after: String = "", $first: Int =
beforeMore
}
databaseId
+ date
featuredImage {
node {
altText
diff --git a/src/services/graphql/articles.ts b/src/services/graphql/articles.ts
new file mode 100644
index 0000000..e5ce7a5
--- /dev/null
+++ b/src/services/graphql/articles.ts
@@ -0,0 +1,104 @@
+import { Article } from '@ts/types/app';
+import { RawArticle, TotalItems } from '@ts/types/raw-data';
+import { getAuthorFromRawData } from '@utils/helpers/author';
+import { getImageFromRawData } from '@utils/helpers/images';
+import { getPageLinkFromRawData } from '@utils/helpers/pages';
+import { EdgesVars, fetchAPI, getAPIUrl, PageInfo } from './api';
+import { articlesQuery, totalArticlesQuery } from './articles.query';
+
+/**
+ * Retrieve the total number of articles.
+ *
+ * @returns {Promise<number>} - The articles total number.
+ */
+export const getTotalArticles = async (): Promise<number> => {
+ const response = await fetchAPI<TotalItems, typeof totalArticlesQuery>({
+ api: getAPIUrl(),
+ query: totalArticlesQuery,
+ });
+
+ return response.posts.pageInfo.total;
+};
+
+export type GetArticlesReturn = {
+ articles: Article[];
+ pageInfo: PageInfo;
+};
+
+/**
+ * Convert raw data to an Article object.
+ *
+ * @param {RawArticle} data - The page raw data.
+ * @returns {Article} The page data.
+ */
+export const getArticleFromRawData = (data: RawArticle): Article => {
+ const {
+ acfPosts,
+ author,
+ commentCount,
+ contentParts,
+ databaseId,
+ date,
+ featuredImage,
+ info,
+ modified,
+ slug,
+ title,
+ seo,
+ } = data;
+
+ return {
+ content: contentParts.afterMore,
+ id: databaseId,
+ intro: contentParts.beforeMore,
+ meta: {
+ author: getAuthorFromRawData(author.node, 'page'),
+ commentsCount: commentCount || 0,
+ cover: featuredImage?.node
+ ? getImageFromRawData(featuredImage.node)
+ : undefined,
+ dates: {
+ publication: date,
+ update: modified,
+ },
+ readingTime: info.readingTime,
+ seo: {
+ description: seo?.metaDesc || '',
+ title: seo?.title || '',
+ },
+ thematics: acfPosts.postsInThematic?.map((thematic) =>
+ getPageLinkFromRawData(thematic)
+ ),
+ topics: acfPosts.postsInTopic?.map((topic) =>
+ getPageLinkFromRawData(topic)
+ ),
+ wordsCount: info.wordsCount,
+ },
+ slug,
+ title,
+ };
+};
+
+/**
+ * Retrieve the given number of articles from API.
+ *
+ * @param {EdgesVars} obj - An object.
+ * @param {number} obj.first - The number of articles.
+ * @returns {Promise<GetArticlesReturn>} - The articles data.
+ */
+export const getArticles = async ({
+ first,
+}: EdgesVars): Promise<GetArticlesReturn> => {
+ const response = await fetchAPI<RawArticle, typeof articlesQuery>({
+ api: getAPIUrl(),
+ query: articlesQuery,
+ variables: { first },
+ });
+
+ return {
+ articles: response.posts.edges.map((edge) =>
+ getArticleFromRawData(edge.node)
+ ),
+ pageInfo: response.posts.pageInfo,
+ };
+};
diff --git a/src/ts/types/app.ts b/src/ts/types/app.ts
new file mode 100644
index 0000000..b09f3d5
--- /dev/null
+++ b/src/ts/types/app.ts
@@ -0,0 +1,86 @@
+export type AuthorKind = 'page' | 'comment';
+
+export type Author<T extends AuthorKind> = {
+ avatar?: Image;
+ description?: T extends 'page' ? string | undefined : never;
+ name: string;
+ website?: string;
+};
+
+export type CommentMeta = {
+ author: Author<'comment'>;
+ date: string;
+};
+
+export type Comment = {
+ approved: boolean;
+ content: string;
+ id: number;
+ meta: CommentMeta;
+ parentId: number;
+ replies: Comment[];
+};
+
+export type Dates = {
+ publication: string;
+ update: string;
+};
+
+export type Image = {
+ alt: string;
+ height: number;
+ src: string;
+ title?: string;
+ width: number;
+};
+
+export type Repos = {
+ github?: string;
+ gitlab?: string;
+};
+
+export type SEO = {
+ description: string;
+ title: string;
+};
+
+export type PageKind = 'article' | 'project' | 'thematic' | 'topic';
+
+export type Meta<T extends PageKind> = {
+ articles?: T extends 'thematic' | 'topic' ? Article[] : never;
+ author: Author<'page'>;
+ commentsCount?: T extends 'article' ? number : never;
+ cover?: Image;
+ dates: Dates;
+ license?: T extends 'projects' ? string : never;
+ readingTime: number;
+ repos?: T extends 'projects' ? Repos : never;
+ seo: SEO;
+ technologies?: T extends 'projects' ? string[] : never;
+ thematics?: T extends 'article' | 'topic' ? PageLink[] : never;
+ topics?: T extends 'article' | 'thematic' ? PageLink[] : never;
+ website?: T extends 'topic' ? string : never;
+ wordsCount: number;
+};
+
+export type Page<T extends PageKind> = {
+ content: string;
+ id: number;
+ intro: string;
+ meta?: Meta<T>;
+ slug: string;
+ title: string;
+};
+
+export type PageLink = {
+ id: number;
+ name: string;
+ slug: string;
+};
+
+export type Article = Page<'article'>;
+export type ArticleCard = Pick<Article, 'id' | 'slug' | 'title'> &
+ Pick<Meta<'article'>, 'cover' | 'dates'>;
+export type Project = Page<'project'>;
+export type Thematic = Page<'thematic'>;
+export type Topic = Page<'topic'>;
diff --git a/src/ts/types/raw-data.ts b/src/ts/types/raw-data.ts
new file mode 100644
index 0000000..43a2453
--- /dev/null
+++ b/src/ts/types/raw-data.ts
@@ -0,0 +1,103 @@
+/**
+ * Types for raw data coming from GraphQL API.
+ */
+
+import { NodeResponse, PageInfo } from '@services/graphql/api';
+import { AuthorKind } from './app';
+
+export type ACFPosts = {
+ postsInThematic?: RawThematicPreview[];
+ postsInTopic?: RawTopicPreview[];
+};
+
+export type ACFThematics = {
+ postsInThematic: RawArticle[];
+};
+
+export type ACFTopics = {
+ officialWebsite: string;
+ postsInTopic: RawArticle[];
+};
+
+export type ContentParts = {
+ afterMore: string;
+ beforeMore: string;
+};
+
+export type Info = {
+ readingTime: number;
+ wordsCount: number;
+};
+
+export type RawAuthor<T extends AuthorKind> = {
+ description?: T extends 'page' ? string | undefined : never;
+ gravatarUrl?: string;
+ name: string;
+ url?: string;
+};
+
+export type RawComment = {
+ approved: boolean;
+ author: NodeResponse<RawAuthor<'comment'>>;
+ content: string;
+ databaseId: number;
+ date: string;
+ parentDatabaseId: number;
+};
+
+export type RawCover = {
+ altText: string;
+ mediaDetails: {
+ width: number;
+ height: number;
+ };
+ sourceUrl: string;
+ title?: string;
+};
+
+export type RawArticle = RawPage & {
+ acfPosts: ACFPosts;
+ commentCount: number | null;
+};
+
+export type RawArticlePreview = Pick<
+ RawArticle,
+ 'databaseId' | 'date' | 'featuredImage' | 'slug' | 'title'
+>;
+
+export type RawPage = {
+ author: NodeResponse<RawAuthor<'page'>>;
+ contentParts: ContentParts;
+ databaseId: number;
+ date: string;
+ featuredImage: NodeResponse<RawCover> | null;
+ info: Info;
+ modified: string;
+ seo?: RawSEO;
+ slug: string;
+ title: string;
+};
+
+export type RawSEO = {
+ metaDesc: string;
+ title: string;
+};
+
+export type RawThematic = RawPage & {
+ acfThematics: ACFThematics;
+};
+
+export type RawThematicPreview = Pick<
+ RawThematic,
+ 'databaseId' | 'slug' | 'title'
+>;
+
+export type RawTopic = RawPage & {
+ acfTopics: ACFTopics;
+};
+
+export type RawTopicPreview = Pick<RawTopic, 'databaseId' | 'slug' | 'title'>;
+
+export type TotalItems = {
+ pageInfo: Pick<PageInfo, 'total'>;
+};
diff --git a/src/utils/helpers/author.ts b/src/utils/helpers/author.ts
new file mode 100644
index 0000000..cf125fc
--- /dev/null
+++ b/src/utils/helpers/author.ts
@@ -0,0 +1,32 @@
+import { type Author, type AuthorKind } from '@ts/types/app';
+import { type RawAuthor } from '@ts/types/raw-data';
+
+/**
+ * Convert author raw data to regular data.
+ *
+ * @param {RawAuthor<AuthorKind>} data - The author raw data.
+ * @param {AuthorKind} kind - The author kind. Either `page` or `comment`.
+ * @param {number} [avatarSize] - The author avatar size.
+ * @returns {Author<AuthorKind>} The author data.
+ */
+export const getAuthorFromRawData = (
+ data: RawAuthor<typeof kind>,
+ kind: AuthorKind,
+ avatarSize: number = 80
+): Author<typeof kind> => {
+ const { name, description, gravatarUrl, url } = data;
+
+ return {
+ name,
+ avatar: gravatarUrl
+ ? {
+ alt: `${name} avatar`,
+ height: avatarSize,
+ src: gravatarUrl,
+ width: avatarSize,
+ }
+ : undefined,
+ description,
+ website: url,
+ };
+};
diff --git a/src/utils/helpers/dates.ts b/src/utils/helpers/dates.ts
new file mode 100644
index 0000000..fa167a7
--- /dev/null
+++ b/src/utils/helpers/dates.ts
@@ -0,0 +1,55 @@
+import { Dates } from '@ts/types/app';
+import { settings } from '@utils/config';
+
+/**
+ * Format a date based on a locale.
+ *
+ * @param {string} date - The date.
+ * @param {string} [locale] - A locale.
+ * @returns {string} The locale date string.
+ */
+export const getFormattedDate = (
+ date: string,
+ locale: string = settings.locales.defaultLocale
+): string => {
+ const dateOptions: Intl.DateTimeFormatOptions = {
+ day: 'numeric',
+ month: 'long',
+ year: 'numeric',
+ };
+
+ return new Date(date).toLocaleDateString(locale, dateOptions);
+};
+
+/**
+ * Format a time based on a locale.
+ *
+ * @param {string} time - The time.
+ * @param {string} [locale] - A locale.
+ * @returns {string} The locale time string.
+ */
+export const getFormattedTime = (
+ time: string,
+ locale: string = settings.locales.defaultLocale
+): string => {
+ const formattedTime = new Date(time).toLocaleTimeString(locale, {
+ hour: 'numeric',
+ minute: 'numeric',
+ });
+
+ return locale === 'fr' ? formattedTime.replace(':', 'h') : formattedTime;
+};
+
+/**
+ * Retrieve a Dates object.
+ *
+ * @param publication - The publication date.
+ * @param update - The update date.
+ * @returns {Dates} A Dates object.
+ */
+export const getDates = (publication: string, update: string): Dates => {
+ return {
+ publication: getFormattedDate(publication),
+ update: getFormattedDate(update),
+ };
+};
diff --git a/src/utils/helpers/images.ts b/src/utils/helpers/images.ts
new file mode 100644
index 0000000..30bb8be
--- /dev/null
+++ b/src/utils/helpers/images.ts
@@ -0,0 +1,18 @@
+import { Image } from '@ts/types/app';
+import { RawCover } from '@ts/types/raw-data';
+
+/**
+ * Retrieve an Image object from raw data.
+ *
+ * @param image - The cover raw data.
+ * @returns {Image} - An Image object.
+ */
+export const getImageFromRawData = (image: RawCover): Image => {
+ return {
+ alt: image.altText,
+ height: image.mediaDetails.height,
+ src: image.sourceUrl,
+ title: image.title,
+ width: image.mediaDetails.width,
+ };
+};
diff --git a/src/utils/helpers/pages.ts b/src/utils/helpers/pages.ts
new file mode 100644
index 0000000..d757f8c
--- /dev/null
+++ b/src/utils/helpers/pages.ts
@@ -0,0 +1,26 @@
+import { type PageLink } from '@ts/types/app';
+import {
+ type RawThematicPreview,
+ type RawTopicPreview,
+} from '@ts/types/raw-data';
+
+/**
+ * Convert raw data to a Link object.
+ *
+ * @param data - An object.
+ * @param {number} data.databaseId - The data id.
+ * @param {string} data.slug - The data slug.
+ * @param {string} data.title - The data name.
+ * @returns {PageLink} The link data (id, slug and title).
+ */
+export const getPageLinkFromRawData = (
+ data: RawThematicPreview | RawTopicPreview
+): PageLink => {
+ const { databaseId, slug, title } = data;
+
+ return {
+ id: databaseId,
+ name: title,
+ slug,
+ };
+};
diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts
index 10a8e77..95d3b7b 100644
--- a/src/utils/helpers/rss.ts
+++ b/src/utils/helpers/rss.ts
@@ -1,20 +1,26 @@
-import { getPostsTotal, getPublishedPosts } from '@services/graphql/queries';
-import { ArticlePreview } from '@ts/types/articles';
-import { PostsList } from '@ts/types/blog';
+import { getArticles, getTotalArticles } from '@services/graphql/articles';
+import { Article } from '@ts/types/app';
import { settings } from '@utils/config';
import { Feed } from 'feed';
-const getAllPosts = async (): Promise<ArticlePreview[]> => {
- const totalPosts = await getPostsTotal();
- const posts: ArticlePreview[] = [];
+/**
+ * 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 });
- const postsList: PostsList = await getPublishedPosts({ first: totalPosts });
- posts.push(...postsList.posts);
-
- return posts;
+ return articles;
};
-export const generateFeed = async () => {
+/**
+ * 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,
@@ -38,16 +44,16 @@ export const generateFeed = async () => {
title,
});
- const posts = await getAllPosts();
+ const articles = await getAllArticles();
- posts.forEach((post) => {
+ articles.forEach((article) => {
feed.addItem({
- content: post.intro,
- date: new Date(post.dates.publication),
- description: post.intro,
- id: post.id,
- link: `${settings.url}/article/${post.slug}`,
- title: post.title,
+ 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,
});
});