Create useNews.ts

This commit is contained in:
Aden Lindsay
2025-02-02 10:07:24 +10:30
committed by GitHub
parent 28bf070ce2
commit 5d8f9d3813

50
composables/useNews.ts Normal file
View File

@ -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
};
};