From f111685c5886f3e77edfd3621c98d8ac1b9bcce4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 24 Nov 2023 20:00:08 +0100 Subject: refactor(services, types): reorganize GraphQL fetchers and data types The Typescript mapped types was useful for autocompletion in fetchers but their are harder to maintain. I think it's better to keep each query close to its fetcher to have a better understanding of the fetched data. So I: * colocate queries with their own fetcher * colocate mutations with their own mutator * remove Typescript mapped types for queries and mutations * move data convertors inside graphql services * rename most of data types and fetchers --- src/utils/helpers/server/projects.ts | 38 +++++++++++++----------------------- 1 file changed, 14 insertions(+), 24 deletions(-) (limited to 'src/utils/helpers/server') diff --git a/src/utils/helpers/server/projects.ts b/src/utils/helpers/server/projects.ts index ed73da8..c1a3d10 100644 --- a/src/utils/helpers/server/projects.ts +++ b/src/utils/helpers/server/projects.ts @@ -1,10 +1,6 @@ import { readdirSync } from 'fs'; import path from 'path'; -import { - type MDXProjectMeta, - type ProjectCard, - type ProjectPreview, -} from '../../../types'; +import type { MDXProjectMeta, Project, ProjectPreview } from '../../../types'; /** * Retrieve all the projects filename. @@ -24,9 +20,7 @@ export const getProjectFilenames = (): string[] => { * @param {string} filename - The project filename. * @returns {Promise} */ -export const getProjectData = async ( - filename: string -): Promise => { +export const getProjectData = async (filename: string): Promise => { try { const { meta, @@ -53,7 +47,7 @@ export const getProjectData = async ( }, }, slug: filename, - title: title, + title, }; } catch (err) { console.error(err); @@ -65,28 +59,24 @@ export const getProjectData = async ( * Retrieve all the projects data using filenames. * * @param {string[]} filenames - The filenames without extension. - * @returns {Promise} - An array of projects data. + * @returns {Promise} - An array of projects data. */ export const getProjectsData = async ( filenames: string[] -): Promise => { - return Promise.all( - filenames.map(async (filename) => { - const { id, meta, slug, title } = await getProjectData(filename); - const { cover, dates, tagline, technologies } = meta; - return { id, meta: { cover, dates, tagline, technologies }, slug, title }; - }) - ); -}; +): Promise => + Promise.all(filenames.map(async (filename) => getProjectData(filename))); /** * Method to sort an array of projects by publication date. * - * @param {ProjectCard} a - A single project. - * @param {ProjectCard} b - A single project. + * @param {ProjectPreview} a - A single project. + * @param {ProjectPreview} b - A single project. * @returns The result used by Array.sort() method: 1 || -1 || 0. */ -const sortProjectsByPublicationDate = (a: ProjectCard, b: ProjectCard) => { +const sortProjectsByPublicationDate = ( + a: ProjectPreview, + b: ProjectPreview +) => { if (a.meta.dates.publication < b.meta.dates.publication) return 1; if (a.meta.dates.publication > b.meta.dates.publication) return -1; return 0; @@ -95,9 +85,9 @@ const sortProjectsByPublicationDate = (a: ProjectCard, b: ProjectCard) => { /** * Retrieve all projects in content folder sorted by publication date. * - * @returns {Promise} An array of projects. + * @returns {Promise} An array of projects. */ -export const getProjectsCard = async (): Promise => { +export const getProjectsCard = async (): Promise => { const filenames = getProjectFilenames(); const projects = await getProjectsData(filenames); -- cgit v1.2.3