diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-28 15:09:04 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-28 16:21:57 +0100 |
| commit | ab81df7f3d317281a05caec18e2cfd89dc26bc7a (patch) | |
| tree | e7c8fbdd238ebf514aa00eec98434f50ed69be73 | |
| parent | ff79add1d59375817a10331a35458cca3c419cfe (diff) | |
refactor(services): avoid nested fetchers
If we use fetchPostsCount inside fetchAllPostsSlugs, we might have
problems to test fetchAllPostsSlugs failure. So it is better to let
the consumer pass the posts count as an argument. The same applies
to thematics and topics.
| -rw-r--r-- | src/pages/article/[slug].tsx | 4 | ||||
| -rw-r--r-- | src/pages/sujet/[slug].tsx | 3 | ||||
| -rw-r--r-- | src/pages/thematique/[slug].tsx | 3 | ||||
| -rw-r--r-- | src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts | 7 | ||||
| -rw-r--r-- | src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts | 9 | ||||
| -rw-r--r-- | src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts | 7 |
6 files changed, 18 insertions, 15 deletions
diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx index f228ff0..04ae617 100644 --- a/src/pages/article/[slug].tsx +++ b/src/pages/article/[slug].tsx @@ -26,6 +26,7 @@ import { fetchAllPostsSlugs, fetchCommentsList, fetchPost, + fetchPostsCount, } from '../../services/graphql'; import styles from '../../styles/pages/article.module.scss'; import type { Article, NextPageWithLayout, SingleComment } from '../../types'; @@ -333,7 +334,8 @@ export const getStaticProps: GetStaticProps<ArticlePageProps> = async ({ }; export const getStaticPaths: GetStaticPaths = async () => { - const slugs = await fetchAllPostsSlugs(); + const postsCount = await fetchPostsCount(); + const slugs = await fetchAllPostsSlugs(postsCount); const paths = slugs.map((slug) => { return { params: { slug } }; }); diff --git a/src/pages/sujet/[slug].tsx b/src/pages/sujet/[slug].tsx index aed7ea9..c63906f 100644 --- a/src/pages/sujet/[slug].tsx +++ b/src/pages/sujet/[slug].tsx @@ -231,7 +231,8 @@ export const getStaticProps: GetStaticProps<TopicPageProps> = async ({ }; export const getStaticPaths: GetStaticPaths = async () => { - const slugs = await fetchAllTopicsSlugs(); + const topicsCount = await fetchTopicsCount(); + const slugs = await fetchAllTopicsSlugs(topicsCount); const paths = slugs.map((slug) => { return { params: { slug } }; }); diff --git a/src/pages/thematique/[slug].tsx b/src/pages/thematique/[slug].tsx index a44c98b..f8c3404 100644 --- a/src/pages/thematique/[slug].tsx +++ b/src/pages/thematique/[slug].tsx @@ -216,7 +216,8 @@ export const getStaticProps: GetStaticProps<ThematicPageProps> = async ({ }; export const getStaticPaths: GetStaticPaths = async () => { - const slugs = await fetchAllThematicsSlugs(); + const thematicsCount = await fetchThematicsCount(); + const slugs = await fetchAllThematicsSlugs(thematicsCount); const paths = slugs.map((slug) => { return { params: { slug } }; }); diff --git a/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts index 28f2bbf..25cc782 100644 --- a/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts +++ b/src/services/graphql/fetchers/posts/fetch-all-posts-slugs.ts @@ -1,6 +1,5 @@ import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -import { fetchPostsCount } from './fetch-posts-count'; type PostsSlugsResponse = { posts: Nullable<GraphQLNodes<SlugNode>>; @@ -17,14 +16,14 @@ const postsSlugsQuery = `query PostsSlugs($first: Int) { /** * Retrieve the WordPress posts slugs. * + * @param {number} count - The number of posts slugs to retrieve. * @returns {Promise<string[]>} The posts slugs. */ -export const fetchAllPostsSlugs = async (): Promise<string[]> => { - const postsCount = await fetchPostsCount(); +export const fetchAllPostsSlugs = async (count: number): Promise<string[]> => { const response = await fetchGraphQL<PostsSlugsResponse>({ query: postsSlugsQuery, url: getGraphQLUrl(), - variables: { first: postsCount }, + variables: { first: count }, }); if (!response.posts) diff --git a/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts index 739c009..c44bb6d 100644 --- a/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts +++ b/src/services/graphql/fetchers/thematics/fetch-all-thematics-slugs.ts @@ -1,6 +1,5 @@ import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -import { fetchThematicsCount } from './fetch-thematics-count'; type ThematicsSlugsResponse = { thematics: Nullable<GraphQLNodes<SlugNode>>; @@ -17,14 +16,16 @@ const thematicsSlugsQuery = `query ThematicsSlugs($first: Int) { /** * Retrieve the WordPress thematics slugs. * + * @param {number} count - The number of thematics slugs to retrieve. * @returns {Promise<string[]>} The thematics slugs. */ -export const fetchAllThematicsSlugs = async (): Promise<string[]> => { - const thematicsCount = await fetchThematicsCount(); +export const fetchAllThematicsSlugs = async ( + count: number +): Promise<string[]> => { const response = await fetchGraphQL<ThematicsSlugsResponse>({ query: thematicsSlugsQuery, url: getGraphQLUrl(), - variables: { first: thematicsCount }, + variables: { first: count }, }); if (!response.thematics) diff --git a/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts index eab4a7c..1df0039 100644 --- a/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts +++ b/src/services/graphql/fetchers/topics/fetch-all-topics-slugs.ts @@ -1,6 +1,5 @@ import type { GraphQLNodes, Nullable, SlugNode } from '../../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../../utils/helpers'; -import { fetchTopicsCount } from './fetch-topics-count'; type TopicsSlugsResponse = { topics: Nullable<GraphQLNodes<SlugNode>>; @@ -17,14 +16,14 @@ const topicsSlugsQuery = `query TopicsSlugs($first: Int) { /** * Retrieve the WordPress topics slugs. * + * @param {number} count - The number of topics slugs to retrieve. * @returns {Promise<string[]>} The topics slugs. */ -export const fetchAllTopicsSlugs = async (): Promise<string[]> => { - const topicsCount = await fetchTopicsCount(); +export const fetchAllTopicsSlugs = async (count: number): Promise<string[]> => { const response = await fetchGraphQL<TopicsSlugsResponse>({ query: topicsSlugsQuery, url: getGraphQLUrl(), - variables: { first: topicsCount }, + variables: { first: count }, }); if (!response.topics) |
