From a0d00743cbbdb77b27c1a3d5711407ffed5befac Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sat, 20 Aug 2022 21:39:51 +0200 Subject: refactor(types): move and rename GraphQL types The api file in services was not really readable. So I move the types and I also rewrite a little the fetch function. I also rename most of the type to avoid conflict with preexisting types (like Node) and to keep consistency. --- src/services/graphql/thematics.ts | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) (limited to 'src/services/graphql/thematics.ts') diff --git a/src/services/graphql/thematics.ts b/src/services/graphql/thematics.ts index 4dc69e7..508fc2f 100644 --- a/src/services/graphql/thematics.ts +++ b/src/services/graphql/thematics.ts @@ -1,4 +1,6 @@ import { PageLink, Slug, Thematic } from '@ts/types/app'; +import { GraphQLEdgesInput } from '@ts/types/graphql/generics'; +import { EdgesResponse } from '@ts/types/graphql/queries'; import { RawArticle, RawThematic, @@ -6,8 +8,11 @@ import { TotalItems, } from '@ts/types/raw-data'; import { getImageFromRawData } from '@utils/helpers/images'; -import { getPageLinkFromRawData } from '@utils/helpers/pages'; -import { EdgesResponse, EdgesVars, fetchAPI, getAPIUrl } from './api'; +import { + getPageLinkFromRawData, + sortPageLinksByName, +} from '@utils/helpers/pages'; +import { fetchAPI } from './api'; import { getArticleFromRawData } from './articles'; import { thematicBySlugQuery, @@ -23,7 +28,6 @@ import { */ export const getTotalThematics = async (): Promise => { const response = await fetchAPI({ - api: getAPIUrl(), query: totalThematicsQuery, }); @@ -33,16 +37,16 @@ export const getTotalThematics = async (): Promise => { /** * Retrieve the given number of thematics from API. * - * @param {EdgesVars} props - An object of GraphQL variables. + * @param {GraphQLEdgesInput} props - An object of GraphQL variables. * @returns {Promise>} The thematics data. */ export const getThematicsPreview = async ( - props: EdgesVars + props: GraphQLEdgesInput ): Promise> => { const response = await fetchAPI< RawThematicPreview, typeof thematicsListQuery - >({ api: getAPIUrl(), query: thematicsListQuery, variables: props }); + >({ query: thematicsListQuery, variables: props }); return response.thematics; }; @@ -88,21 +92,8 @@ export const getThematicFromRawData = (data: RawThematic): Thematic => { const uniqueTopics = topics.filter( ({ id }, index) => !topicsIds.includes(id, index + 1) ); - const sortTopicByName = (a: PageLink, b: PageLink) => { - var nameA = a.name.toUpperCase(); // ignore upper and lowercase - var nameB = b.name.toUpperCase(); // ignore upper and lowercase - if (nameA < nameB) { - return -1; - } - if (nameA > nameB) { - return 1; - } - - // names must be equal - return 0; - }; - return uniqueTopics.sort(sortTopicByName); + return uniqueTopics.sort(sortPageLinksByName); }; return { @@ -137,7 +128,6 @@ export const getThematicFromRawData = (data: RawThematic): Thematic => { */ export const getThematicBySlug = async (slug: string): Promise => { const response = await fetchAPI({ - api: getAPIUrl(), query: thematicBySlugQuery, variables: { slug }, }); @@ -153,7 +143,6 @@ export const getThematicBySlug = async (slug: string): Promise => { export const getAllThematicsSlugs = async (): Promise => { const totalThematics = await getTotalThematics(); const response = await fetchAPI({ - api: getAPIUrl(), query: thematicsSlugQuery, variables: { first: totalThematics }, }); -- cgit v1.2.3