aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/server/projects.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-24 20:00:08 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-27 14:47:51 +0100
commitf111685c5886f3e77edfd3621c98d8ac1b9bcce4 (patch)
tree62a541fe3afeb64bf745443706fbfb02e96c5230 /src/utils/helpers/server/projects.ts
parentbee515641cb144be9a855ff2cac258d2fedab21d (diff)
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
Diffstat (limited to 'src/utils/helpers/server/projects.ts')
-rw-r--r--src/utils/helpers/server/projects.ts38
1 files changed, 14 insertions, 24 deletions
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<ProjectPreview>}
*/
-export const getProjectData = async (
- filename: string
-): Promise<ProjectPreview> => {
+export const getProjectData = async (filename: string): Promise<Project> => {
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<ProjectCard[]>} - An array of projects data.
+ * @returns {Promise<ProjectPreview[]>} - An array of projects data.
*/
export const getProjectsData = async (
filenames: string[]
-): Promise<ProjectCard[]> => {
- 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<ProjectPreview[]> =>
+ 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<ProjectCard[]>} An array of projects.
+ * @returns {Promise<ProjectPreview[]>} An array of projects.
*/
-export const getProjectsCard = async (): Promise<ProjectCard[]> => {
+export const getProjectsCard = async (): Promise<ProjectPreview[]> => {
const filenames = getProjectFilenames();
const projects = await getProjectsData(filenames);