From 8e6c810d57cd1258a6a7d2b8f400f95afc3f05af Mon Sep 17 00:00:00 2001 From: Luciano Giacchetta Date: Fri, 8 Aug 2025 17:48:45 -0300 Subject: [PATCH] #7 - Added new Type definition and filter by status where we are on dev or prod --- src/libs/posts.ts | 27 ++++++++++++++++++++++----- src/types/codyops-post.ts | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/types/codyops-post.ts diff --git a/src/libs/posts.ts b/src/libs/posts.ts index 5ec8154..0f465e7 100644 --- a/src/libs/posts.ts +++ b/src/libs/posts.ts @@ -1,11 +1,29 @@ import directus from "./directus"; -import { readItems } from "@directus/sdk"; +import { readItems, type Query } from "@directus/sdk"; +import type { Post, DirectusSchema } from "../types/codyops-post"; + +const isDev = import.meta.env.DEV; // Astro's way to check for development mode export const posts = await directus.request( - readItems("codyops_posts", { - fields: ['*'] + readItems>("codyops_posts", { + fields: [ + 'slug', + 'status', + 'sort', + 'user_created', + 'user_updated', + 'date_created', + 'date_updated', + 'title', + 'description', + 'tags', + 'cover_image', + 'content' + ], + filter: { + status: isDev ? { '_neq': 'archived' } : { '_eq': 'published' } } - ) + }) ); export function getAllPosts() { @@ -21,4 +39,3 @@ export function getUniqueTags(posts: any ) { export function filterPostsByTag(posts: any, tag: any) { return posts.filter((post: any) => post.tags.includes(tag)); }; - \ No newline at end of file diff --git a/src/types/codyops-post.ts b/src/types/codyops-post.ts new file mode 100644 index 0000000..8cedeb3 --- /dev/null +++ b/src/types/codyops-post.ts @@ -0,0 +1,18 @@ +export interface Post { + slug: string; + status: string; + sort: number | null; + user_created: string | null; + user_updated: string | null; + date_created: string | null; + date_updated: string | null; + title: string; + description: string; + tags: string[]; + cover_image: string | null; + content: string; +} + +export interface DirectusSchema { + codyops_posts: Post[]; +} -- 2.51.0