Skip to content

Commit

Permalink
#14 Refactored to make it readable and as it was intended to be used,…
Browse files Browse the repository at this point in the history
… following the docs.
  • Loading branch information
krckyboy committed Feb 11, 2024
1 parent 0745d10 commit aeb204d
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions next/src/scripts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,45 @@ export const fetchWrapper = async <T>(url: string | URL) => {

export const db = {
getPosts: async () => {
const queryParams = qs.stringify({ sort: 'publishedAt:desc' });
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryParams}`);
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) => {
const queryParams = qs.stringify({
'filters[slug]': slug,
'populate[0]': 'categories'
});
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryParams}`);
const queryParams = {
filters: {
slug: slug
},
populate: ['categories']
};
const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getPostSlugs: async () => {
const queryParams = qs.stringify({ fields: ['slug'] });
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryParams}`);
const queryParams = {
fields: ['slug']
};
const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getFeaturedPosts: async () => {
const queryParams = qs.stringify({
'filters[isFeatured][$eq]': true,
sort: 'publishedAt:desc',
const queryParams = {
filters: {
isFeatured: {
$eq: true
}
},
sort: ['publishedAt:desc'],
fields: ['slug', 'title', 'content', 'publishedAt']
});
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryParams}`);
};
const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getCategories: async () => {
return await fetchWrapper<CategoriesFetchResponse>('/categories');
Expand Down

0 comments on commit aeb204d

Please sign in to comment.