From 5d8f9d38133e136a04ba3bb6b1b26ae04067fe99 Mon Sep 17 00:00:00 2001 From: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com> Date: Sun, 2 Feb 2025 10:07:24 +1030 Subject: [PATCH] Create useNews.ts --- composables/useNews.ts | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 composables/useNews.ts diff --git a/composables/useNews.ts b/composables/useNews.ts new file mode 100644 index 0000000..f5f2093 --- /dev/null +++ b/composables/useNews.ts @@ -0,0 +1,50 @@ +export const useNews = () => { + const getAll = async (options?: { + limit?: number; + skip?: number; + orderBy?: 'asc' | 'desc'; + tags?: string[]; + search?: string; + }) => { + const query = new URLSearchParams(); + + if (options?.limit) query.set('limit', options.limit.toString()); + if (options?.skip) query.set('skip', options.skip.toString()); + if (options?.orderBy) query.set('order', options.orderBy); + if (options?.tags?.length) query.set('tags', options.tags.join(',')); + if (options?.search) query.set('search', options.search); + + return await useFetch(`/api/v1/news?${query.toString()}`); + }; + + const getById = async (id: string) => { + return await useFetch(`/api/v1/news/${id}`); + }; + + const create = async (article: { + title: string; + excerpt: string; + content: string; + image?: string; + tags: string[]; + authorId: string; + }) => { + return await $fetch('/api/v1/news', { + method: 'POST', + body: article + }); + }; + + const remove = async (id: string) => { + return await $fetch(`/api/v1/news/${id}`, { + method: 'DELETE' + }); + }; + + return { + getAll, + getById, + create, + remove + }; +};