Skip to content

Commit 6849141

Browse files
committed
refactor: add base StacHook interface for type consistency
- Add StacHook base interface with common properties (isLoading, isFetching, error) - Add StacRefetchFn<T> type for standardized refetch signatures - Update all hooks to extend StacHook interface (StacCollectionHook, StacCollectionsHook, StacItemHook, StacSearchHook)
1 parent 815d89e commit 6849141

File tree

5 files changed

+43
-33
lines changed

5 files changed

+43
-33
lines changed

src/hooks/useCollection.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
2-
import type { ApiErrorType } from '../types';
1+
import { useQuery } from '@tanstack/react-query';
2+
import type { StacHook, StacRefetchFn, ApiErrorType } from '../types';
33
import type { Collection } from '../types/stac';
44
import { handleStacResponse } from '../utils/handleStacResponse';
55
import { generateCollectionQueryKey } from '../utils/queryKeys';
66
import { useStacApiContext } from '../context/useStacApiContext';
77

8-
type StacCollectionHook = {
8+
interface StacCollectionHook extends StacHook {
99
collection?: Collection;
10-
isLoading: boolean;
11-
isFetching: boolean;
12-
refetch: () => Promise<QueryObserverResult<Collection, ApiErrorType>>;
13-
error?: ApiErrorType;
14-
};
10+
refetch: StacRefetchFn<Collection>;
11+
}
1512

1613
function useCollection(collectionId: string): StacCollectionHook {
1714
const { stacApi } = useStacApiContext();

src/hooks/useCollections.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
2-
import { type ApiErrorType } from '../types';
1+
import { useQuery } from '@tanstack/react-query';
2+
import type { StacHook, StacRefetchFn, ApiErrorType } from '../types';
33
import type { CollectionsResponse } from '../types/stac';
44
import { handleStacResponse } from '../utils/handleStacResponse';
55
import { generateCollectionsQueryKey } from '../utils/queryKeys';
66
import { useStacApiContext } from '../context/useStacApiContext';
77

8-
type StacCollectionsHook = {
8+
interface StacCollectionsHook extends StacHook {
99
collections?: CollectionsResponse;
10-
refetch: () => Promise<QueryObserverResult<CollectionsResponse, ApiErrorType>>;
11-
isLoading: boolean;
12-
isFetching: boolean;
13-
error?: ApiErrorType;
14-
};
10+
refetch: StacRefetchFn<CollectionsResponse>;
11+
}
1512

1613
function useCollections(): StacCollectionsHook {
1714
const { stacApi } = useStacApiContext();

src/hooks/useItem.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import { useQuery, type QueryObserverResult } from '@tanstack/react-query';
2-
import { Item } from '../types/stac';
3-
import { type ApiErrorType } from '../types';
1+
import { useQuery } from '@tanstack/react-query';
2+
import type { StacHook, StacRefetchFn, ApiErrorType } from '../types';
3+
import type { Item } from '../types/stac';
44
import { useStacApiContext } from '../context/useStacApiContext';
55
import { handleStacResponse } from '../utils/handleStacResponse';
66
import { generateItemQueryKey } from '../utils/queryKeys';
77

8-
type ItemHook = {
8+
interface StacItemHook extends StacHook {
99
item?: Item;
10-
isLoading: boolean;
11-
isFetching: boolean;
12-
error?: ApiErrorType;
13-
refetch: () => Promise<QueryObserverResult<Item, ApiErrorType>>;
14-
};
10+
refetch: StacRefetchFn<Item>;
11+
}
1512

16-
function useItem(url: string): ItemHook {
13+
function useItem(url: string): StacItemHook {
1714
const { stacApi } = useStacApiContext();
1815

1916
const fetchItem = async (): Promise<Item> => {

src/hooks/useStacSearch.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useCallback, useState, useMemo, useEffect } from 'react';
22
import { useQuery, useQueryClient } from '@tanstack/react-query';
33
import debounce from '../utils/debounce';
44
import { generateStacSearchQueryKey } from '../utils/queryKeys';
5-
import { type ApiErrorType } from '../types';
5+
import type { StacHook, ApiErrorType } from '../types';
66
import { handleStacResponse } from '../utils/handleStacResponse';
77
import type {
88
Link,
@@ -16,7 +16,7 @@ import { useStacApiContext } from '../context/useStacApiContext';
1616

1717
type PaginationHandler = () => void;
1818

19-
type StacSearchHook = {
19+
interface StacSearchHook extends StacHook {
2020
ids?: string[];
2121
setIds: (ids?: string[]) => void;
2222
bbox?: Bbox;
@@ -33,12 +33,9 @@ type StacSearchHook = {
3333
setLimit: (limit: number) => void;
3434
submit: () => void;
3535
results?: SearchResponse;
36-
isLoading: boolean;
37-
isFetching: boolean;
38-
error?: ApiErrorType;
3936
nextPage: PaginationHandler | undefined;
4037
previousPage: PaginationHandler | undefined;
41-
};
38+
}
4239

4340
function useStacSearch(): StacSearchHook {
4441
const { stacApi } = useStacApiContext();
@@ -214,7 +211,7 @@ function useStacSearch(): StacSearchHook {
214211
results,
215212
isLoading,
216213
isFetching,
217-
error: error ?? undefined,
214+
error: error as ApiErrorType,
218215
sortby,
219216
setSortby,
220217
limit,

src/types/index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { QueryObserverResult } from '@tanstack/react-query';
2+
13
export type GenericObject = {
24
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
35
};
@@ -8,3 +10,23 @@ export type ApiErrorType = {
810
statusText: string;
911
url?: string;
1012
};
13+
14+
/**
15+
* Base interface for all STAC hooks providing common loading state and error handling.
16+
* All data-fetching hooks (useCollection, useCollections, useItem, useStacSearch)
17+
* extend this interface with their specific data and refetch signatures.
18+
*/
19+
export interface StacHook {
20+
/** True during initial data fetch (no cached data available) */
21+
isLoading: boolean;
22+
/** True during any fetch operation (including background refetches) */
23+
isFetching: boolean;
24+
/** Error information if the last request was unsuccessful */
25+
error?: ApiErrorType;
26+
}
27+
28+
/**
29+
* Generic refetch function type for STAC hooks.
30+
* Returns a Promise with the query result including data and error information.
31+
*/
32+
export type StacRefetchFn<T> = () => Promise<QueryObserverResult<T, ApiErrorType>>;

0 commit comments

Comments
 (0)