-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move nuxt content queries to content api composable
- Loading branch information
Showing
6 changed files
with
63 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { BEST_ARTICLES_LIMIT, MOST_VIEWED_ARTICLES_LIMIT } from '~/configs/properties'; | ||
import type { ArticleContent, ArticleListItem } from '~/types/responses'; | ||
|
||
const ARTICLE_LIST_ONLY: Array<keyof ArticleListItem> = ['_path', 'title', 'description', 'image', 'keywords']; | ||
const ARTICLE_SIBLINGS_ONLY: Array<keyof ArticleListItem> = ['_path', 'title']; | ||
const PAGINATION_LIMIT = 10; | ||
|
||
interface PaginationOptions { | ||
skip?: number; | ||
limit?: number; | ||
} | ||
|
||
export default function useContentAPI() { | ||
const fetchArticle = (path: string) => queryContent<ArticleContent>(path).findOne(); | ||
|
||
const fetchArticleSiblings = (path: string, topic?: string) => | ||
queryContent<ArticleContent>() | ||
.only(ARTICLE_SIBLINGS_ONLY) | ||
.where({ _path: { $contains: `/articles/${topic}` } }) | ||
.findSurround(path); | ||
|
||
const fetchArticlesListByTopic = (topic: string) => | ||
queryContent<ArticleListItem>(`articles/${topic}`).only(ARTICLE_LIST_ONLY).find(); | ||
|
||
const fetchMostViewedArticlesList = () => | ||
queryContent<ArticleListItem>('articles').only(ARTICLE_LIST_ONLY).limit(MOST_VIEWED_ARTICLES_LIMIT).find(); // TODO: update | ||
|
||
const fetchBestArticlesList = () => | ||
queryContent<ArticleListItem>('articles').only(ARTICLE_LIST_ONLY).limit(BEST_ARTICLES_LIMIT).find(); // TODO: update | ||
|
||
const fetchPaginalArticlesList = (options = {} as PaginationOptions) => | ||
queryContent<ArticleListItem>('articles') | ||
.only(ARTICLE_LIST_ONLY) | ||
.skip(options.skip ?? 0) | ||
.limit(options.limit || PAGINATION_LIMIT) | ||
.find(); | ||
|
||
return { | ||
fetchArticle, | ||
fetchArticleSiblings, | ||
fetchArticlesListByTopic, | ||
fetchMostViewedArticlesList, | ||
fetchBestArticlesList, | ||
fetchPaginalArticlesList, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters