From 15ebef40eef1c0e249bd0eec5bab8f3210076443 Mon Sep 17 00:00:00 2001 From: Ansh Goyal Date: Mon, 4 Mar 2024 11:44:57 +0530 Subject: [PATCH] fix api.ts --- types/post.ts | 12 ++++++++++++ utils/api.ts | 35 +++++++++++++++-------------------- utils/types.d.ts | 23 ----------------------- 3 files changed, 27 insertions(+), 43 deletions(-) delete mode 100644 utils/types.d.ts diff --git a/types/post.ts b/types/post.ts index d4ce703d30d..146c90383fb 100644 --- a/types/post.ts +++ b/types/post.ts @@ -1,4 +1,16 @@ import type posts from '../config/posts.json'; export type IPosts = typeof posts; + export type IDocs = IPosts['docs']; +export type IDoc = IDocs[number]; + +export type IBlog = IPosts['blog']; +export type IBlogPost = IBlog[number]; + +export type IAbout = IPosts['about']; +export type IAboutPost = IAbout[number]; + +export type IPost = IDoc & IBlogPost & IAboutPost; + +export type IDocsTree = IPosts['docsTree']; \ No newline at end of file diff --git a/utils/api.ts b/utils/api.ts index e28f7f45853..d7a35af5073 100644 --- a/utils/api.ts +++ b/utils/api.ts @@ -1,11 +1,11 @@ import posts from '../config/posts.json'; -import { IPost, Posts } from './types'; +import type { IPost, IPosts } from '../types/post'; /** * Retrieves all posts. * @returns {Object} All posts. */ -export function getAllPosts(): object { +export function getAllPosts(): IPosts { return posts; } @@ -18,24 +18,19 @@ export function getAllPosts(): object { export function getPostBySlug( slug: string, type: string = '' -): IPost | undefined { - if (type) - return (posts as any)[type as keyof Posts].find( - (post: IPost) => post.slug === slug && !post.isSection - ); +) { + if(type) + return (posts as any)[type].find((post: IPost) => post.slug === slug && !post.isSection ) else { - let item: IPost | undefined; - Object.entries(posts).forEach(([key, value]: [string, any]) => { - let content; - if (key !== 'docsTree') { - content = (posts as any)[key as keyof Posts].find( - (post: IPost) => post.slug === slug && !post.isSection - ); - if (content) item = content; - } - }); - return item; - } + let item; + Object.entries(posts).forEach(([key, value]) => { + let content + if(key!== 'docsTree') + content = (posts as any)[key].find((post: IPost) => post.slug === slug && !post.isSection) + if(content) item = content + }) + return item + } } /** @@ -49,6 +44,6 @@ export function getDocBySlug( slug: string ): object | undefined { return structuredPosts.find( - (post: IPost) => post.slug === slug && !post.isSection + (post) => post.slug === slug && !post.isSection ); } diff --git a/utils/types.d.ts b/utils/types.d.ts deleted file mode 100644 index 4b064f9c07a..00000000000 --- a/utils/types.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -export interface IPost { - title: string; - isSection: boolean; - weight: number; - toc: { - content: string; - slug: string; - lvl: number; - i: number; - seen: number; - }[]; - readingTime: number; - excerpt: string; - sectionSlug: string; - sectionWeight: number; - id: string; - isIndex: boolean; - slug: string; -} - -export interface Posts { - [key: string]: IPost[]; -}