diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-02 18:36:09 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-02 18:36:09 +0200 |
| commit | ca921d7536cfe950b5a7d442977bbf900b48faf4 (patch) | |
| tree | 2e8bb3f4b81414ee881c3d92d9bdfed411c569db /src/utils | |
| parent | 9308a6dce03bd0c616e0ba6fec227473aaa44b33 (diff) | |
chore: fetch posts for rss feed
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/helpers/author.ts | 32 | ||||
| -rw-r--r-- | src/utils/helpers/dates.ts | 55 | ||||
| -rw-r--r-- | src/utils/helpers/images.ts | 18 | ||||
| -rw-r--r-- | src/utils/helpers/pages.ts | 26 | ||||
| -rw-r--r-- | src/utils/helpers/rss.ts | 44 |
5 files changed, 156 insertions, 19 deletions
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, }); }); |
