1- import { apiPaths , createApiPath } from "../api" ;
21import { FetchError } from "@/lib/fetch-error" ;
32import { notFound } from "@/lib/not-found-error" ;
4- import { fetcher } from "../fetch" ;
5- import { useQuery , UseQueryOptions } from "@tanstack/react-query" ;
6- import { ListSecretsParams , SecretsPage } from "@/types/secret" ;
73import { objectToSearchParams } from "@/lib/url" ;
4+ import { ListSecretsParams , SecretsPage } from "@/types/secret" ;
5+ import { apiPaths , createApiPath } from "../api" ;
6+ import { fetcher } from "../fetch" ;
87
98type SecretsOverview = {
109 params : ListSecretsParams ;
1110} ;
1211
13- export function getSecretsQueryKey ( { params } : SecretsOverview ) {
14- return [ "secrets" , params ] ;
15- }
16-
17- export async function fetchAllSecrets ( { params } : SecretsOverview ) {
12+ export async function fetchAllSecrets ( { params } : SecretsOverview ) : Promise < SecretsPage > {
1813 const url = createApiPath ( apiPaths . secrets . all + "?" + objectToSearchParams ( params ) ) ;
1914 const res = await fetcher ( url , {
2015 method : "GET" ,
@@ -26,22 +21,21 @@ export async function fetchAllSecrets({ params }: SecretsOverview) {
2621 if ( res . status === 404 ) notFound ( ) ;
2722
2823 if ( ! res . ok ) {
24+ const errorData : string = await res
25+ . json ( )
26+ . then ( ( data ) => {
27+ if ( Array . isArray ( data . detail ) ) {
28+ return data . detail [ 1 ] ;
29+ }
30+ return data . detail ;
31+ } )
32+ . catch ( ( ) => "Failed to fetch secrets" ) ;
33+
2934 throw new FetchError ( {
30- message : "Error while fetching secrets" ,
3135 status : res . status ,
32- statusText : res . statusText
36+ statusText : res . statusText ,
37+ message : errorData
3338 } ) ;
3439 }
3540 return res . json ( ) ;
3641}
37-
38- export function useAllSecrets (
39- params : SecretsOverview ,
40- options ?: Omit < UseQueryOptions < SecretsPage , FetchError > , "queryKey" | "queryFn" >
41- ) {
42- return useQuery < SecretsPage , FetchError > ( {
43- queryKey : getSecretsQueryKey ( params ) ,
44- queryFn : ( ) => fetchAllSecrets ( params ) ,
45- ...options
46- } ) ;
47- }
0 commit comments