This commit is contained in:
7
api/endpoints/getPosts.ts
Normal file
7
api/endpoints/getPosts.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { httpClient } from '../httpClient'
|
||||
|
||||
export const getPosts = (search: string) =>
|
||||
httpClient<Posts>('/api/posts', {
|
||||
method: 'GET',
|
||||
query: { search },
|
||||
})
|
||||
4
api/endpoints/getUsers.ts
Normal file
4
api/endpoints/getUsers.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { httpClient } from '../httpClient'
|
||||
|
||||
export const getUsers = () =>
|
||||
httpClient<Users>('/api/users')
|
||||
2
api/endpoints/index.ts
Normal file
2
api/endpoints/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './getPosts'
|
||||
export * from './getUsers'
|
||||
21
api/httpClient.ts
Normal file
21
api/httpClient.ts
Normal 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
2
api/queries/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './useGetPosts'
|
||||
export * from './useGetUsers'
|
||||
10
api/queries/useGetPosts.ts
Normal file
10
api/queries/useGetPosts.ts
Normal 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,
|
||||
})
|
||||
}
|
||||
10
api/queries/useGetUsers.ts
Normal file
10
api/queries/useGetUsers.ts
Normal 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,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user