Skip to content

Commit c48d752

Browse files
committed
feat: Add limit to set page size in search (resolves #22)
1 parent 69227eb commit c48d752

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ Option | Type | Description
273273
`setDateRangeTo(toDate)` | `function` | Callback to set `dateRangeto`. `toDate` must be ISO representation of a date, ie. `2022-05-18`, or `undefined` to reset.
274274
`sortby` | `array` | Specifies the order of results. Array of `{ field: string, direction: 'asc' | 'desc' }`
275275
`setSortby(sort)` | `function` | Callback to set `sortby`. `sort` must be an array of `{ field: string, direction: 'asc' | 'desc' }`, or `undefined` to reset.
276+
`limit` | `number` | The number of results returned per result page.
277+
`setLimit(limit)` | `function` | Callback to set `limit`. `limit` must be a `number`, or `undefined` to reset.
276278
`results` | `object` | The result of the last search query; a [GeoJSON `FeatureCollection` with additional members](https://github.com/radiantearth/stac-api-spec/blob/v1.0.0-rc.2/fragments/itemcollection/README.md). `undefined` if the search request has not been submitted, or if there was an error.
277279
`state` | `string` | The status of the request. `"IDLE"` before and after the request is sent or received. `"LOADING"` when the request is in progress.
278280
`error` | [`Error`](#error) | Error information if the last request was unsuccessful. `undefined` if the last request was successful.

src/hooks/useStacSearch.test.ts

+51-5
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,30 @@ describe('useStacSearch — API supports POST', () => {
552552
expect(result.current.results).toEqual({ data: '12345' });
553553
});
554554

555+
it('includes limit in search', async () => {
556+
fetch
557+
.mockResponseOnce(
558+
JSON.stringify({ links: [{ rel: 'search', method: 'POST' }] }),
559+
{ url: 'https://fake-stac-api.net' }
560+
)
561+
.mockResponseOnce(JSON.stringify({ data: '12345' }));
562+
563+
const { result, waitForNextUpdate } = renderHook(
564+
() => useStacSearch(),
565+
{ wrapper }
566+
);
567+
await waitForNextUpdate();
568+
569+
act(() => result.current.setLimit(50));
570+
act(() => result.current.submit());
571+
572+
await waitForNextUpdate();
573+
574+
const postPayload = parseRequestPayload(fetch.mock.calls[1][1]);
575+
expect(postPayload).toEqual({ limit: 50 });
576+
expect(result.current.results).toEqual({ data: '12345' });
577+
});
578+
555579
// it('should reset state with each new StacApi instance', async () => {
556580
// const bbox: Bbox = [-0.59, 51.24, 0.30, 51.74];
557581
// fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
@@ -597,7 +621,7 @@ describe('useStacSearch — API supports GET', () => {
597621
act(() => result.current.submit());
598622
await waitForNextUpdate();
599623

600-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?bbox=-0.59%2C51.24%2C0.3%2C51.74&limit=25');
624+
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?limit=25&bbox=-0.59%2C51.24%2C0.3%2C51.74');
601625
expect(result.current.results).toEqual({ data: '12345' });
602626
});
603627

@@ -619,7 +643,7 @@ describe('useStacSearch — API supports GET', () => {
619643
act(() => result.current.submit());
620644
await waitForNextUpdate();
621645

622-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?collections=wildfire%2Csurface_temp&limit=25');
646+
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?limit=25&collections=wildfire%2Csurface_temp');
623647
expect(result.current.results).toEqual({ data: '12345' });
624648
});
625649

@@ -642,7 +666,7 @@ describe('useStacSearch — API supports GET', () => {
642666
act(() => result.current.submit());
643667
await waitForNextUpdate();
644668

645-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?datetime=2022-01-17%2F2022-05-17&limit=25');
669+
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?limit=25&datetime=2022-01-17%2F2022-05-17');
646670
expect(result.current.results).toEqual({ data: '12345' });
647671
});
648672

@@ -664,7 +688,7 @@ describe('useStacSearch — API supports GET', () => {
664688
act(() => result.current.submit());
665689
await waitForNextUpdate();
666690

667-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?datetime=2022-01-17%2F..&limit=25');
691+
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?limit=25&datetime=2022-01-17%2F..');
668692
expect(result.current.results).toEqual({ data: '12345' });
669693
});
670694

@@ -686,7 +710,7 @@ describe('useStacSearch — API supports GET', () => {
686710
act(() => result.current.submit());
687711
await waitForNextUpdate();
688712

689-
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?datetime=..%2F2022-05-17&limit=25');
713+
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?limit=25&datetime=..%2F2022-05-17');
690714
expect(result.current.results).toEqual({ data: '12345' });
691715
});
692716

@@ -714,4 +738,26 @@ describe('useStacSearch — API supports GET', () => {
714738
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?limit=25&sortby=%2Bid%2C-properties.cloud');
715739
expect(result.current.results).toEqual({ data: '12345' });
716740
});
741+
742+
it('includes limit', async () => {
743+
fetch
744+
.mockResponseOnce(
745+
JSON.stringify({ links: [] }),
746+
{ url: 'https://fake-stac-api.net' }
747+
)
748+
.mockResponseOnce(JSON.stringify({ data: '12345' }));
749+
750+
const { result, waitForNextUpdate } = renderHook(
751+
() => useStacSearch(),
752+
{ wrapper }
753+
);
754+
await waitForNextUpdate();
755+
756+
act(() => result.current.setLimit(50));
757+
act(() => result.current.submit());
758+
await waitForNextUpdate();
759+
760+
expect(fetch.mock.calls[1][0]).toEqual('https://fake-stac-api.net/search?limit=50');
761+
expect(result.current.results).toEqual({ data: '12345' });
762+
});
717763
});

src/hooks/useStacSearch.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type StacSearchHook = {
2525
setDateRangeFrom: (date: string) => void
2626
dateRangeTo?: string
2727
setDateRangeTo: (date: string) => void
28+
limit?: number;
29+
setLimit: (limit: number) => void;
2830
sortby?: Sortby[]
2931
setSortby: (sort: Sortby[]) => void
3032
submit: () => void
@@ -43,6 +45,7 @@ function useStacSearch(): StacSearchHook {
4345
const [ collections, setCollections ] = useState<CollectionIdList>();
4446
const [ dateRangeFrom, setDateRangeFrom ] = useState<string>('');
4547
const [ dateRangeTo, setDateRangeTo ] = useState<string>('');
48+
const [ limit, setLimit ] = useState<number>(25);
4649
const [ sortby, setSortby ] = useState<Sortby[]>();
4750
const [ state, setState ] = useState<LoadingState>('IDLE');
4851
const [ error, setError ] = useState<ApiError>();
@@ -58,6 +61,7 @@ function useStacSearch(): StacSearchHook {
5861
setDateRangeFrom('');
5962
setDateRangeTo('');
6063
setSortby(undefined);
64+
setLimit(25);
6165
};
6266

6367
/**
@@ -84,9 +88,10 @@ function useStacSearch(): StacSearchHook {
8488
bbox,
8589
collections,
8690
dateRange: { from: dateRangeFrom, to: dateRangeTo },
87-
sortby
91+
sortby,
92+
limit
8893
}),
89-
[ ids, bbox, collections, dateRangeFrom, dateRangeTo, sortby ]
94+
[ ids, bbox, collections, dateRangeFrom, dateRangeTo, sortby, limit ]
9095
);
9196

9297
/**
@@ -187,6 +192,8 @@ function useStacSearch(): StacSearchHook {
187192
error,
188193
sortby,
189194
setSortby,
195+
limit,
196+
setLimit,
190197
nextPage: nextPageConfig ? nextPageFn : undefined,
191198
previousPage: previousPageConfig ? previousPageFn : undefined
192199
};

src/stac-api/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ class StacApi {
130130
collections: this.makeArrayPayload(collections),
131131
bbox: this.fixBboxCoordinateOrder(bbox),
132132
datetime: this.makeDatetimePayload(dateRange),
133-
limit: 25
134133
};
135134

136135
if (this.searchMode === 'POST') {

0 commit comments

Comments
 (0)