Skip to content

Commit

Permalink
fix api.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
anshgoyalevil committed Mar 4, 2024
1 parent 959d6b9 commit 15ebef4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
12 changes: 12 additions & 0 deletions types/post.ts
Original file line number Diff line number Diff line change
@@ -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'];
35 changes: 15 additions & 20 deletions utils/api.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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
}
}

/**
Expand All @@ -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
);
}
23 changes: 0 additions & 23 deletions utils/types.d.ts

This file was deleted.

0 comments on commit 15ebef4

Please sign in to comment.