This commit is contained in:
alsaze
2025-11-17 18:40:04 +03:00
parent 98af218a35
commit 98cbe03fdf
30 changed files with 27121 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
import { httpClient } from '../httpClient'
export const getPosts = (search: string) =>
httpClient<Posts>('/api/posts', {
method: 'GET',
query: { search },
})

View File

@@ -0,0 +1,4 @@
import { httpClient } from '../httpClient'
export const getUsers = () =>
httpClient<Users>('/api/users')

2
api/endpoints/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from './getPosts'
export * from './getUsers'

21
api/httpClient.ts Normal file
View File

@@ -0,0 +1,21 @@
import { useToast } from '#imports'
const showToast = (description: string) => {
if (!import.meta.client)
return
const toast = useToast()
toast.add({
title: 'Uh oh! Something went wrong.',
description,
})
}
export const httpClient = $fetch.create({
onResponseError({ request, response }) {
const message = response?._data?.statusMessage ?? 'Something went wrong'
showToast(message)
},
})

2
api/queries/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from './useGetPosts'
export * from './useGetUsers'

View File

@@ -0,0 +1,10 @@
import { useQuery } from '@tanstack/vue-query'
import { getPosts } from '~/api/endpoints'
export const useGetPosts = (search: MaybeRef<string>) => {
return useQuery<Posts>({
queryKey: ['get-posts', search],
queryFn: () => getPosts(unref(search)),
staleTime: Infinity,
})
}

View File

@@ -0,0 +1,10 @@
import { useQuery } from '@tanstack/vue-query'
import { getUsers } from '~/api/endpoints'
export const useGetUsers = () => {
return useQuery<Users>({
queryKey: ['get-users'],
queryFn: getUsers,
staleTime: Infinity,
})
}