Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { ApiError } from 'next/dist/server/api-utils'
import { Alert, AlertApiRequestQuery } from './types'
export const fetchAlerts = async (
alertQuery: AlertApiRequestQuery,
init?: RequestInit,
): Promise<Alert[] | null> => {
const query = new URLSearchParams({ ...alertQuery }).toString()
let uri = alertQuery.page ? `/api/alerts?${query}` : `/api/alerts`
const response = await fetch(uri, {
...(init ?? {}),
headers: { 'Cache-Control': 'max-age=600' },
})
if (response.ok) return response.json()
if (response.status === 404) return null
throw new ApiError(response.status, response.statusText)
}
export const useAlerts = (
alertQuery: AlertApiRequestQuery,
queryOptions?: Omit<
UseQueryOptions<Alert[] | null, ApiError, Alert[] | null>,
'queryKey' | 'queryFn'
>,
) => {
return useQuery<Alert[] | null, ApiError, Alert[] | null>({
...(queryOptions ?? {}),
queryKey: ['ps:api:alerts', alertQuery],
queryFn: ({ signal }) => fetchAlerts(alertQuery, { signal }),
})
}
|