Skip to content

Commit bf297e3

Browse files
committed
feat: Add ID filter (resolves #21)
1 parent a914f22 commit bf297e3

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

src/hooks/useStacSearch.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ function parseRequestPayload(mockApiCall?: RequestInit) {
1313
describe('useStacSearch — API supports POST', () => {
1414
beforeEach(() => fetch.resetMocks());
1515

16+
it('includes IDs in search', async () => {
17+
fetch
18+
.mockResponseOnce(
19+
JSON.stringify({ links: [{ rel: 'search', method: 'POST' }] }),
20+
{ url: 'https://fake-stac-api.net' }
21+
)
22+
.mockResponseOnce(JSON.stringify({ data: '12345' }));
23+
24+
const { result, waitForNextUpdate } = renderHook(
25+
() => useStacSearch(),
26+
{ wrapper }
27+
);
28+
await waitForNextUpdate();
29+
30+
act(() => result.current.setIds(['collection_1', 'collection_2']));
31+
act(() => result.current.submit());
32+
33+
await waitForNextUpdate();
34+
35+
const postPayload = parseRequestPayload(fetch.mock.calls[1][1]);
36+
expect(postPayload).toEqual({ ids: ['collection_1', 'collection_2'], limit: 25 });
37+
expect(result.current.results).toEqual({ data: '12345' });
38+
});
39+
1640
it('includes Bbox in search', async () => {
1741
fetch
1842
.mockResponseOnce(

src/hooks/useStacSearch.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { useStacApiContext } from '../context';
1414
type PaginationHandler = () => void;
1515

1616
type StacSearchHook = {
17+
ids?: string[]
18+
setIds: (ids: string[]) => void
1719
bbox?: Bbox
1820
setBbox: (bbox: Bbox) => void
1921
collections?: CollectionIdList
@@ -33,6 +35,7 @@ type StacSearchHook = {
3335
function useStacSearch(): StacSearchHook {
3436
const { stacApi } = useStacApiContext();
3537
const [ results, setResults ] = useState<SearchResponse>();
38+
const [ ids, setIds ] = useState<string[]>();
3639
const [ bbox, setBbox ] = useState<Bbox>();
3740
const [ collections, setCollections ] = useState<CollectionIdList>();
3841
const [ dateRangeFrom, setDateRangeFrom ] = useState<string>('');
@@ -47,6 +50,7 @@ function useStacSearch(): StacSearchHook {
4750
setResults(undefined);
4851
setBbox(undefined);
4952
setCollections(undefined);
53+
setIds(undefined);
5054
setDateRangeFrom('');
5155
setDateRangeTo('');
5256
};
@@ -71,11 +75,12 @@ function useStacSearch(): StacSearchHook {
7175
*/
7276
const getSearchPayload = useCallback(
7377
() => ({
78+
ids,
7479
bbox,
7580
collections,
7681
dateRange: { from: dateRangeFrom, to: dateRangeTo }
7782
}),
78-
[ bbox, collections, dateRangeFrom, dateRangeTo ]
83+
[ ids, bbox, collections, dateRangeFrom, dateRangeTo ]
7984
);
8085

8186
/**
@@ -161,6 +166,8 @@ function useStacSearch(): StacSearchHook {
161166

162167
return {
163168
submit,
169+
ids,
170+
setIds,
164171
bbox,
165172
setBbox,
166173
collections,

src/stac-api/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ApiError, GenericObject } from '../types';
2-
import type { Bbox, SearchPayload, DateRange, CollectionIdList } from '../types/stac';
2+
import type { Bbox, SearchPayload, DateRange } from '../types/stac';
33

44
type RequestPayload = SearchPayload;
55
type FetchOptions = {
@@ -45,8 +45,8 @@ class StacApi {
4545
return sortedBbox;
4646
}
4747

48-
makeCollectionsPayload(collections?: CollectionIdList) {
49-
return collections?.length ? collections : undefined;
48+
makeArrayPayload(arr?: any[]) { /* eslint-disable-line @typescript-eslint/no-explicit-any */
49+
return arr?.length ? arr : undefined;
5050
}
5151

5252
makeDatetimePayload(dateRange?: DateRange): string | undefined {
@@ -116,10 +116,11 @@ class StacApi {
116116
}
117117

118118
search(payload: SearchPayload, headers = {}): Promise<Response> {
119-
const { bbox, dateRange, collections, ...restPayload } = payload;
119+
const { ids, bbox, dateRange, collections, ...restPayload } = payload;
120120
const requestPayload = {
121121
...restPayload,
122-
collections: this.makeCollectionsPayload(collections),
122+
ids: this.makeArrayPayload(ids),
123+
collections: this.makeArrayPayload(collections),
123124
bbox: this.fixBboxCoordinateOrder(bbox),
124125
datetime: this.makeDatetimePayload(dateRange),
125126
limit: 25

src/types/stac.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import type { Geometry } from 'geojson';
22
import type { GenericObject } from '.';
33

44
export type Bbox = [number, number, number, number];
5+
export type IdList = string[];
56
export type CollectionIdList = string[];
67
export type DateRange = {
78
from?: string,
89
to?: string
910
}
1011

1112
export type SearchPayload = {
13+
ids?: IdList,
1214
bbox?: Bbox,
1315
collections?: CollectionIdList,
1416
dateRange?: DateRange,

0 commit comments

Comments
 (0)