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/services | |
| parent | 9308a6dce03bd0c616e0ba6fec227473aaa44b33 (diff) | |
chore: fetch posts for rss feed
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/graphql/articles.query.ts | 1 | ||||
| -rw-r--r-- | src/services/graphql/articles.ts | 104 |
2 files changed, 105 insertions, 0 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, + }; +}; |
