diff --git a/package.json b/package.json index 63f8b27..e07806c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@developmentseed/stac-react", - "version": "1.0.0-alpha.1", + "version": "1.0.0-alpha.2", "description": "React components and hooks for building STAC-API front-ends", "repository": "git@github.com:developmentseed/stac-react.git", "authors": [ diff --git a/src/hooks/useCollection.ts b/src/hooks/useCollection.ts index d3e2398..f2b3af8 100644 --- a/src/hooks/useCollection.ts +++ b/src/hooks/useCollection.ts @@ -1,15 +1,17 @@ -import { useQuery } from '@tanstack/react-query'; -import type { StacHook, StacRefetchFn } from '../types'; +import { useQuery, type QueryObserverResult } from '@tanstack/react-query'; import type { Collection } from '../types/stac'; import { handleStacResponse } from '../utils/handleStacResponse'; import { generateCollectionQueryKey } from '../utils/queryKeys'; import { useStacApiContext } from '../context/useStacApiContext'; import { ApiError } from '../utils/ApiError'; -interface StacCollectionHook extends StacHook { +type StacCollectionHook = { collection?: Collection; - refetch: StacRefetchFn; -} + isLoading: boolean; + isFetching: boolean; + refetch: () => Promise>; + error: ApiError | null; +}; function useCollection(collectionId: string): StacCollectionHook { const { stacApi } = useStacApiContext(); diff --git a/src/hooks/useCollections.ts b/src/hooks/useCollections.ts index 1ac2961..bc4c2cc 100644 --- a/src/hooks/useCollections.ts +++ b/src/hooks/useCollections.ts @@ -1,15 +1,17 @@ -import { useQuery } from '@tanstack/react-query'; -import type { StacHook, StacRefetchFn } from '../types'; +import { useQuery, type QueryObserverResult } from '@tanstack/react-query'; import type { CollectionsResponse } from '../types/stac'; import { handleStacResponse } from '../utils/handleStacResponse'; import { generateCollectionsQueryKey } from '../utils/queryKeys'; import { useStacApiContext } from '../context/useStacApiContext'; import { ApiError } from '../utils/ApiError'; -interface StacCollectionsHook extends StacHook { +type StacCollectionsHook = { collections?: CollectionsResponse; - refetch: StacRefetchFn; -} + refetch: () => Promise>; + isLoading: boolean; + isFetching: boolean; + error: ApiError | null; +}; function useCollections(): StacCollectionsHook { const { stacApi } = useStacApiContext(); diff --git a/src/hooks/useItem.ts b/src/hooks/useItem.ts index 207c428..a8a7bea 100644 --- a/src/hooks/useItem.ts +++ b/src/hooks/useItem.ts @@ -1,17 +1,19 @@ -import { useQuery } from '@tanstack/react-query'; -import type { StacHook, StacRefetchFn } from '../types'; +import { useQuery, type QueryObserverResult } from '@tanstack/react-query'; import type { Item } from '../types/stac'; import { useStacApiContext } from '../context/useStacApiContext'; import { handleStacResponse } from '../utils/handleStacResponse'; import { generateItemQueryKey } from '../utils/queryKeys'; import { ApiError } from '../utils/ApiError'; -interface StacItemHook extends StacHook { +type ItemHook = { item?: Item; - refetch: StacRefetchFn; -} + isLoading: boolean; + isFetching: boolean; + error: ApiError | null; + refetch: () => Promise>; +}; -function useItem(url: string): StacItemHook { +function useItem(url: string): ItemHook { const { stacApi } = useStacApiContext(); const fetchItem = async (): Promise => { diff --git a/src/hooks/useStacSearch.ts b/src/hooks/useStacSearch.ts index 3a4488a..540b94d 100644 --- a/src/hooks/useStacSearch.ts +++ b/src/hooks/useStacSearch.ts @@ -2,7 +2,6 @@ import { useCallback, useState, useMemo, useEffect } from 'react'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import debounce from '../utils/debounce'; import { generateStacSearchQueryKey } from '../utils/queryKeys'; -import type { StacHook } from '../types'; import { handleStacResponse } from '../utils/handleStacResponse'; import type { Link, @@ -17,7 +16,7 @@ import { ApiError } from '../utils/ApiError'; type PaginationHandler = () => void; -interface StacSearchHook extends StacHook { +type StacSearchHook = { ids?: string[]; setIds: (ids?: string[]) => void; bbox?: Bbox; @@ -34,9 +33,12 @@ interface StacSearchHook extends StacHook { setLimit: (limit: number) => void; submit: () => void; results?: SearchResponse; + isLoading: boolean; + isFetching: boolean; + error: ApiError | null; nextPage: PaginationHandler | undefined; previousPage: PaginationHandler | undefined; -} +}; function useStacSearch(): StacSearchHook { const { stacApi } = useStacApiContext(); @@ -212,7 +214,7 @@ function useStacSearch(): StacSearchHook { results, isLoading, isFetching, - error, + error: error ?? null, sortby, setSortby, limit, diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 1d5daa5..c99b252 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,26 +1,3 @@ -import type { QueryObserverResult } from '@tanstack/react-query'; -import { ApiError } from '../utils/ApiError'; - export type GenericObject = { [key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any }; - -/** - * Base interface for all STAC hooks providing common loading state and error handling. - * All data-fetching hooks (useCollection, useCollections, useItem, useStacSearch) - * extend this interface with their specific data and refetch signatures. - */ -export interface StacHook { - /** True during initial data fetch (no cached data available) */ - isLoading: boolean; - /** True during any fetch operation (including background refetches) */ - isFetching: boolean; - /** Error information if the last request was unsuccessful */ - error: ApiError | null; -} - -/** - * Generic refetch function type for STAC hooks. - * Returns a Promise with the query result including data and error information. - */ -export type StacRefetchFn = () => Promise>;