summaryrefslogtreecommitdiffstats
path: root/src/ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2021-12-20 00:15:20 +0100
committerArmand Philippot <git@armandphilippot.com>2021-12-20 00:15:20 +0100
commitfa6adedc42e9c6ec39cc30df16b54900c220b094 (patch)
tree6bb498beadaa382245cecb86ce56931580313c6f /src/ts
parent2ff898626c5c0abc6b8195224067b992403e313b (diff)
refactor: rewrite types and services
I was repeating myself a lot in services. So I rewrited the different functions to improve readability and I extracted some formatting functions to put them in utils. I also rewrited/reorganized some types to keep consistent names.
Diffstat (limited to 'src/ts')
-rw-r--r--src/ts/types/app.ts75
-rw-r--r--src/ts/types/articles.ts90
-rw-r--r--src/ts/types/blog.ts48
-rw-r--r--src/ts/types/comments.ts38
-rw-r--r--src/ts/types/contact.ts16
-rw-r--r--src/ts/types/cover.ts2
-rw-r--r--src/ts/types/homepage.ts15
-rw-r--r--src/ts/types/pages.ts16
-rw-r--r--src/ts/types/pagination.ts4
-rw-r--r--src/ts/types/taxonomies.ts114
10 files changed, 224 insertions, 194 deletions
diff --git a/src/ts/types/app.ts b/src/ts/types/app.ts
index 488fe6e..ba28416 100644
--- a/src/ts/types/app.ts
+++ b/src/ts/types/app.ts
@@ -1,6 +1,22 @@
import { NextPage } from 'next';
import { AppProps } from 'next/app';
import { ReactElement, ReactNode } from 'react';
+import { PostBy } from './articles';
+import { AllPostsSlug, RawPostsList } from './blog';
+import { CommentData, CreateComment } from './comments';
+import { ContactData, SendEmail } from './contact';
+import { HomePageBy } from './homepage';
+import { PageBy } from './pages';
+import {
+ AllSubjectsSlug,
+ AllThematicsSlug,
+ SubjectBy,
+ ThematicBy,
+} from './taxonomies';
+
+//==============================================================================
+// Next
+//==============================================================================
export type NextPageWithLayout<P = {}> = NextPage<P> & {
getLayout?: (page: ReactElement) => ReactNode;
@@ -9,3 +25,62 @@ export type NextPageWithLayout<P = {}> = NextPage<P> & {
export type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};
+
+//==============================================================================
+// API
+//==============================================================================
+
+export type VariablesType<T> = T extends
+ | PageBy
+ | PostBy
+ | SubjectBy
+ | ThematicBy
+ ? Slug
+ : T extends RawPostsList
+ ? CursorPagination
+ : T extends CreateComment
+ ? CommentData
+ : T extends SendEmail
+ ? ContactData
+ : null;
+
+export type RequestType =
+ | AllPostsSlug
+ | AllSubjectsSlug
+ | AllThematicsSlug
+ | CreateComment
+ | HomePageBy
+ | PageBy
+ | PostBy
+ | SubjectBy
+ | ThematicBy
+ | RawPostsList
+ | SendEmail;
+
+//==============================================================================
+// Globals
+//==============================================================================
+
+export type ContentParts = {
+ afterMore: string;
+ beforeMore: string;
+};
+
+export type CursorPagination = {
+ first: number;
+ after: string;
+};
+
+export type Dates = {
+ publication: string;
+ update: string;
+};
+
+export type PageInfo = {
+ endCursor: string;
+ hasNextPage: boolean;
+};
+
+export type Slug = {
+ slug: string;
+};
diff --git a/src/ts/types/articles.ts b/src/ts/types/articles.ts
index afaa3e3..e6a40ef 100644
--- a/src/ts/types/articles.ts
+++ b/src/ts/types/articles.ts
@@ -1,80 +1,68 @@
-import { Comment, CommentsResponse } from './comments';
-import { Cover, CoverResponse } from './cover';
+import { ContentParts, Dates } from './app';
+import { Comment, CommentsNode } from './comments';
+import { Cover, RawCover } from './cover';
import { SEO } from './seo';
import { SubjectPreview, ThematicPreview } from './taxonomies';
-export type ArticleDates = {
- publication: string;
- update: string;
-};
-
export type ArticleAuthor = {
firstName: string;
lastName: string;
name: string;
};
-export type ArticlePreviewResponse = {
- acfPosts: {
- postsInSubject: SubjectPreview[] | null;
- postsInThematic: ThematicPreview[] | null;
- };
- commentCount: number | null;
- contentParts: {
- beforeMore: string;
- };
- databaseId: number;
- date: string;
- featuredImage: CoverResponse;
- id: string;
- modified: string;
- slug: string;
- title: string;
+export type ACFPosts = {
+ postsInSubject: SubjectPreview[] | null;
+ postsInThematic: ThematicPreview[] | null;
};
-export type ArticlePreview = {
+export type Article = {
+ author: ArticleAuthor;
commentCount: number | null;
+ comments: Comment[];
content: string;
databaseId: number;
- date: ArticleDates;
- featuredImage: Cover | null;
+ dates: Dates;
id: string;
- slug: string;
+ intro: string;
+ seo: SEO;
subjects: SubjectPreview[] | [];
thematics: ThematicPreview[] | [];
title: string;
};
-export type ArticleResponse = ArticlePreviewResponse & {
- author: {
- node: ArticleAuthor;
- };
- comments: CommentsResponse;
- contentParts: {
- afterMore: string;
- };
- seo: SEO;
+export type RawArticle = Pick<
+ Article,
+ 'commentCount' | 'databaseId' | 'id' | 'seo' | 'title'
+> & {
+ acfPosts: ACFPosts;
+ author: { node: ArticleAuthor };
+ comments: CommentsNode;
+ contentParts: ContentParts;
+ date: string;
+ modified: string;
};
-export type Article = ArticlePreview & {
- author: ArticleAuthor;
- comments: Comment[];
- intro: string;
- seo: SEO;
-};
+export type ArticlePreview = Pick<
+ Article,
+ 'commentCount' | 'dates' | 'id' | 'intro' | 'thematics' | 'title'
+> & { featuredImage: Cover; slug: string };
-export type PostByResponse = {
- postBy: ArticleResponse;
+export type RawArticlePreview = Pick<
+ Article,
+ 'commentCount' | 'id' | 'title'
+> & {
+ acfPosts: Pick<ACFPosts, 'postsInThematic'>;
+ contentParts: Pick<ContentParts, 'beforeMore'>;
+ date: string;
+ featuredImage: RawCover;
+ modified: string;
+ slug: string;
};
-export type FetchPostByReturn = (slug: string) => Promise<PostByResponse>;
-
-export type GetPostByReturn = (slug: string) => Promise<Article>;
+export type PostBy = {
+ postBy: RawArticle;
+};
export type ArticleProps = {
post: Article;
};
-
-export type ArticleSlug = {
- slug: string;
-};
diff --git a/src/ts/types/blog.ts b/src/ts/types/blog.ts
index 32fa9b8..7325ddf 100644
--- a/src/ts/types/blog.ts
+++ b/src/ts/types/blog.ts
@@ -1,47 +1,29 @@
-import {
- ArticlePreview,
- ArticlePreviewResponse,
- ArticleSlug,
-} from './articles';
-import { PageInfo } from './pagination';
-
-export type PostsListEdge = {
- cursor: string;
- node: ArticlePreviewResponse;
-};
-
-export type PostsListResponse = {
- posts: {
- edges: PostsListEdge[];
- pageInfo: PageInfo;
- };
-};
+import { PageInfo, Slug } from './app';
+import { ArticlePreview, RawArticlePreview } from './articles';
export type PostsList = {
posts: ArticlePreview[];
pageInfo: PageInfo;
};
-export type FetchPostsListReturn = (
- first?: number,
- after?: string
-) => Promise<PostsListResponse>;
-
-type PostsListProps = {
- first?: number;
- after?: string;
+export type PostsListEdges = {
+ cursor: string;
+ node: RawArticlePreview;
};
-export type GetPostsListReturn = (props: PostsListProps) => Promise<PostsList>;
-
-export type BlogPageProps = {
- fallback: PostsList;
+export type RawPostsList = {
+ posts: {
+ edges: PostsListEdges[];
+ pageInfo: PageInfo;
+ };
};
-export type AllPostsSlugResponse = {
+export type AllPostsSlug = {
posts: {
- nodes: ArticleSlug[];
+ nodes: Slug[];
};
};
-export type FetchAllPostsSlugReturn = () => Promise<ArticleSlug[]>;
+export type BlogPageProps = {
+ fallback: PostsList;
+};
diff --git a/src/ts/types/comments.ts b/src/ts/types/comments.ts
index a1bb120..d5c0052 100644
--- a/src/ts/types/comments.ts
+++ b/src/ts/types/comments.ts
@@ -1,10 +1,14 @@
+//==============================================================================
+// Comments query
+//==============================================================================
+
export type CommentAuthor = {
gravatarUrl: string;
name: string;
url: string;
};
-export type CommentAuthorResponse = {
+export type RawCommentAuthor = {
node: CommentAuthor;
};
@@ -19,14 +23,28 @@ export type Comment = {
replies: Comment[];
};
-export type RawComment = Omit<Comment, 'author'> & {
- author: CommentAuthorResponse;
+export type RawComment = Omit<Comment, 'author' | 'replies'> & {
+ author: RawCommentAuthor;
};
-export type CommentsResponse = {
+export type CommentsNode = {
nodes: RawComment[];
};
+//==============================================================================
+// Comment mutations
+//==============================================================================
+
+export type CommentData = {
+ author: string;
+ authorEmail: string;
+ authorUrl: string;
+ content: string;
+ parent: number;
+ commentOn: number;
+ mutationId: string;
+};
+
export type CreatedComment = {
clientMutationId: string;
success: boolean;
@@ -35,16 +53,6 @@ export type CreatedComment = {
};
};
-export type CreatedCommentResponse = {
+export type CreateComment = {
createComment: CreatedComment;
};
-
-export type CreatedCommentReturn = (
- author: string,
- authorEmail: string,
- authorUrl: string,
- content: string,
- parent: number,
- commentOn: number,
- mutationId: string
-) => Promise<CreatedComment>;
diff --git a/src/ts/types/contact.ts b/src/ts/types/contact.ts
index c0f23e0..ef6847a 100644
--- a/src/ts/types/contact.ts
+++ b/src/ts/types/contact.ts
@@ -1,3 +1,10 @@
+export type ContactData = {
+ body: string;
+ mutationId: string;
+ replyTo: string;
+ subject: string;
+};
+
export type SentEmail = {
clientMutationId: string;
message: string;
@@ -7,13 +14,6 @@ export type SentEmail = {
to: string;
};
-export type SentEmailResponse = {
+export type SendEmail = {
sendEmail: SentEmail;
};
-
-export type SendMailReturn = (
- subject: string,
- body: string,
- replyTo: string,
- mutationId: string
-) => Promise<SentEmail>;
diff --git a/src/ts/types/cover.ts b/src/ts/types/cover.ts
index 2a565ef..4df898e 100644
--- a/src/ts/types/cover.ts
+++ b/src/ts/types/cover.ts
@@ -4,6 +4,6 @@ export type Cover = {
title: string;
} | null;
-export type CoverResponse = {
+export type RawCover = {
node: Cover;
} | null;
diff --git a/src/ts/types/homepage.ts b/src/ts/types/homepage.ts
index 404aa38..8ff2ccb 100644
--- a/src/ts/types/homepage.ts
+++ b/src/ts/types/homepage.ts
@@ -1,19 +1,12 @@
-export type fetchHomePageReturn = () => Promise<HomePageResponse>;
-
-export type HomePageResponse = {
- nodeByUri: {
- id: string;
- content: string;
- };
-};
-
-export type getHomePageReturn = () => Promise<HomePage>;
-
export type HomePage = {
id: string;
content: string;
};
+export type HomePageBy = {
+ nodeByUri: HomePage;
+};
+
export type HomePageProps = {
data: HomePage;
};
diff --git a/src/ts/types/pages.ts b/src/ts/types/pages.ts
index 4b38ff4..93ff62e 100644
--- a/src/ts/types/pages.ts
+++ b/src/ts/types/pages.ts
@@ -1,29 +1,23 @@
+import { ContentParts, Dates } from './app';
+
export type Page = {
content: string;
- date: string;
+ dates: Dates;
intro: string;
- modified: string;
title: string;
};
export type RawPage = {
- contentParts: {
- afterMore: string;
- beforeMore: string;
- };
+ contentParts: ContentParts;
date: string;
modified: string;
title: string;
};
-export type PageResponse = {
+export type PageBy = {
pageBy: RawPage;
};
-export type FetchPageByUriReturn = (uri: string) => Promise<RawPage>;
-
-export type GetPageReturn = () => Promise<Page>;
-
export type PageProps = {
page: Page;
};
diff --git a/src/ts/types/pagination.ts b/src/ts/types/pagination.ts
deleted file mode 100644
index 45830d9..0000000
--- a/src/ts/types/pagination.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-export type PageInfo = {
- endCursor: string;
- hasNextPage: boolean;
-};
diff --git a/src/ts/types/taxonomies.ts b/src/ts/types/taxonomies.ts
index a8c372c..3945934 100644
--- a/src/ts/types/taxonomies.ts
+++ b/src/ts/types/taxonomies.ts
@@ -1,96 +1,90 @@
-import { ArticlePreview, ArticlePreviewResponse } from './articles';
-import { Cover, CoverResponse } from './cover';
+import { ContentParts, Dates, Slug } from './app';
+import { ArticlePreview, RawArticlePreview } from './articles';
+import { Cover, RawCover } from './cover';
-type TaxonomyPreview = {
+//==============================================================================
+// Taxonomies base
+//==============================================================================
+
+type Taxonomy = {
+ content: string;
databaseId: number;
+ dates: Dates;
id: string;
- slug: string;
+ intro: string;
+ posts: ArticlePreview[];
title: string;
};
-export type Taxonomy = TaxonomyPreview & {
- content: string;
- date: string;
- intro: string;
- modified: string;
- posts: ArticlePreview[];
+type TaxonomyPreview = Pick<Taxonomy, 'databaseId' | 'id' | 'title'> & {
+ slug: string;
};
-export type SubjectPreview = TaxonomyPreview & {
+//==============================================================================
+// Subjects
+//==============================================================================
+
+export type Subject = Taxonomy & {
featuredImage: Cover;
+ officialWebsite: string;
};
-export type ThematicPreview = TaxonomyPreview;
+export type SubjectPreview = TaxonomyPreview & {
+ featuredImage: Cover;
+};
-export type ThematicResponse = TaxonomyPreview & {
- acfThematics: {
- postsInThematic: ArticlePreviewResponse[];
- };
- contentParts: {
- afterMore: string;
- beforeMore: string;
+export type RawSubject = SubjectPreview & {
+ acfSubjects: {
+ officialWebsite: string;
+ postsInSubject: RawArticlePreview[];
};
+ contentParts: ContentParts;
date: string;
+ featuredImage: RawCover;
modified: string;
};
-export type ThematicProps = {
- thematic: Taxonomy;
-};
-
-export type AllTaxonomiesSlug = {
- slug: string;
+export type SubjectBy = {
+ subjectBy: RawSubject;
};
-export type AllThematicsSlugResponse = {
- thematics: {
- nodes: AllTaxonomiesSlug[];
+export type AllSubjectsSlug = {
+ subjects: {
+ nodes: Slug[];
};
};
-export type ThematicByResponse = {
- thematicBy: ThematicResponse;
-};
+//==============================================================================
+// Thematics
+//==============================================================================
-export type FetchThematicByReturn = (
- slug: string
-) => Promise<ThematicByResponse>;
+export type Thematic = Taxonomy;
-export type GetTaxonomyByReturn = (slug: string) => Promise<Taxonomy>;
-
-export type FetchAllTaxonomiesSlugReturn = () => Promise<AllTaxonomiesSlug[]>;
-
-export type Subject = Taxonomy & {
- featuredImage: Cover;
- officialWebsite: string;
-};
+export type ThematicPreview = TaxonomyPreview;
-export type SubjectResponse = SubjectPreview & {
- acfSubjects: {
- postsInSubject: ArticlePreviewResponse[];
- };
- contentParts: {
- afterMore: string;
- beforeMore: string;
+export type RawThematic = TaxonomyPreview & {
+ acfThematics: {
+ postsInThematic: RawArticlePreview[];
};
+ contentParts: ContentParts;
date: string;
- featuredImage: CoverResponse;
modified: string;
- officialWebsite: string;
};
-export type SubjectProps = {
- subject: Subject;
+export type ThematicBy = {
+ thematicBy: RawThematic;
};
-export type SubjectByResponse = {
- subjectBy: SubjectResponse;
+export type AllThematicsSlug = {
+ thematics: {
+ nodes: Slug[];
+ };
};
-export type FetchSubjectByReturn = (slug: string) => Promise<SubjectByResponse>;
+export type SubjectProps = {
+ subject: Subject;
+};
-export type AllSubjectsSlugResponse = {
- subjects: {
- nodes: AllTaxonomiesSlug[];
- };
+export type ThematicProps = {
+ thematic: Thematic;
};