import { IHowTo } from './howTo.types'; import { directus } from '@/plugins/axios/'; import { ICategory, IParamsDirectus } from '@/database/baseTypes'; import { POST_STATUS } from '@/utils/constants'; const postPreviewFields = 'title,slug,category.*,preview.*,previewBackgroundColor,readTime,date_created'; async function getPost({ slug, categorySlug, }: IHowTo.GetPostsParams): Promise { const params: IParamsDirectus = { fields: '*.*.*.*', filter: { status: POST_STATUS.published, }, }; if (slug) params.filter = { ...params.filter, slug, }; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: posts } = await directus.get('how-tos', { params, }); const [post] = posts.data; return post; } async function getPostsPreview({ start, categorySlug, limit, }: IHowTo.GetPostsParams): Promise { const params: IParamsDirectus = { fields: postPreviewFields, filter: { status: POST_STATUS.published, }, }; if (start) params.offset = start; if (limit) params.limit = limit; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: posts } = await directus.get('how-tos', { params, }); return posts.data; } async function getCategories(): Promise { const params: IParamsDirectus = { sort: 'name', fields: '*.*', }; const { data: categories } = await directus.get( 'how-to-categories', { params }, ); return categories.data; } export const HowToGateway = { getCategories, getPostsPreview, getPost, };