@@ -3,6 +3,7 @@ import StacApi, { SearchMode } from '../stac-api';
33import { Link } from '../types/stac' ;
44import { GenericObject } from '../types' ;
55import { generateStacApiQueryKey } from '../utils/queryKeys' ;
6+ import { handleStacResponse } from '../utils/handleStacResponse' ;
67
78type StacApiHook = {
89 stacApi ?: StacApi ;
@@ -14,29 +15,18 @@ function useStacApi(url: string, options?: GenericObject): StacApiHook {
1415 const { data, isSuccess, isLoading, isError } = useQuery ( {
1516 queryKey : generateStacApiQueryKey ( url , options ) ,
1617 queryFn : async ( ) => {
17- let searchMode = SearchMode . GET ;
1818 const response = await fetch ( url , {
1919 headers : {
20- 'Content-Type' : 'application/json' ,
2120 ...options ?. headers ,
2221 } ,
2322 } ) ;
24- const baseUrl = response . url ;
25- let json ;
26- try {
27- json = await response . json ( ) ;
28- } catch ( error ) {
29- throw new Error (
30- `Invalid JSON response from STAC API: ${ error instanceof Error ? error . message : String ( error ) } `
31- ) ;
32- }
33- const doesPost = json . links ?. find (
23+ const stacData = await handleStacResponse < { links ?: Link [ ] } > ( response ) ;
24+
25+ const doesPost = stacData . links ?. find (
3426 ( { rel, method } : Link ) => rel === 'search' && method === 'POST'
3527 ) ;
36- if ( doesPost ) {
37- searchMode = SearchMode . POST ;
38- }
39- return new StacApi ( baseUrl , searchMode , options ) ;
28+
29+ return new StacApi ( response . url , doesPost ? SearchMode . POST : SearchMode . GET , options ) ;
4030 } ,
4131 staleTime : Infinity ,
4232 } ) ;
0 commit comments