Skip to content

Commit

Permalink
Merge pull request #26 from krckyboy/#14
Browse files Browse the repository at this point in the history
  • Loading branch information
krckyboy authored Feb 11, 2024
2 parents 6c85fdc + 7ae93ff commit b9bffb6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
33 changes: 21 additions & 12 deletions next/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
},
"dependencies": {
"@types/nprogress": "^0.2.3",
"@types/qs": "^6.9.11",
"date-fns": "^3.2.0",
"next": "14.0.4",
"nprogress": "^0.2.0",
"path": "^0.12.7",
"qs": "^6.11.2",
"react": "^18",
"react-dom": "^18",
"react-markdown": "^9.0.1",
Expand Down
46 changes: 39 additions & 7 deletions next/src/scripts/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CategoriesFetchResponse, PostsFetchResponse } from '@/components/blog-post-item/types';
import qs from 'qs';

export const fetchWrapper = async <T>(url: string | URL) => {
try {
Expand All @@ -21,22 +22,53 @@ export const fetchWrapper = async <T>(url: string | URL) => {
}
};

const sortByPublishedAt = 'sort=publishedAt:desc';

export const db = {
getPosts: async () => {
return await fetchWrapper<PostsFetchResponse>(`/posts?${sortByPublishedAt}`);
const queryParams = {
sort: ['publishedAt:desc'],
pagination: {
pageSize: 10,
page: 1
}
};

const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getPostBySlug: async (slug: string) => {
return await fetchWrapper<PostsFetchResponse>(`/posts/?&filters[slug]=${slug}&populate[0]=categories`);
const queryParams = {
filters: {
slug: slug
},
populate: ['categories']
};

const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getPostSlugs: async () => {
return await fetchWrapper<PostsFetchResponse>('/posts?fields[0]=slug');
const queryParams = {
fields: ['slug']
};

const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getFeaturedPosts: async () => {
return await fetchWrapper<PostsFetchResponse>(`/posts?&filters[isFeatured][$eq]=true&${sortByPublishedAt}&fields[0]=slug&fields[1]=title&fields[2]=content&fields[3]=publishedAt`);
const queryParams = {
filters: {
isFeatured: {
$eq: true
}
},
sort: ['publishedAt:desc'],
fields: ['slug', 'title', 'content', 'publishedAt']
};

const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getCategories: async () => {
return await fetchWrapper<CategoriesFetchResponse>('/categories');
}
};
};

0 comments on commit b9bffb6

Please sign in to comment.