Skip to content

Commit 787be1e

Browse files
committed
feat: Include pagination headers in search
1 parent 93ac27f commit 787be1e

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

src/hooks/useStacSearch.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,41 @@ describe('useStacSearch', () => {
297297
...response.links[0].body
298298
});
299299
});
300+
301+
it('sends pagination header', async () => {
302+
const response = {
303+
links: [{
304+
rel: 'previous',
305+
type: 'application/geo+json',
306+
method: 'POST',
307+
href: 'https://example.com/stac/search',
308+
body: {
309+
limit: 25,
310+
token: 'prev:abc123'
311+
},
312+
headers: {
313+
next: '123abc'
314+
}
315+
}]
316+
};
317+
fetch.mockResponseOnce(JSON.stringify(response));
318+
319+
const { result, waitForNextUpdate } = renderHook(
320+
() => useStacSearch(stacApi)
321+
);
322+
323+
act(() => result.current.setBbox([-0.59, 51.24, 0.30, 51.74]));
324+
act(() => result.current.submit());
325+
await waitForNextUpdate(); // wait to set results
326+
expect(result.current.results).toEqual(response);
327+
expect(result.current.previousPage).toBeDefined();
328+
329+
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
330+
act(() => result.current.previousPage && result.current.previousPage());
331+
await waitForNextUpdate();
332+
333+
expect(result.current.results).toEqual({ data: '12345' });
334+
const postHeader = fetch.mock.calls[1][1]?.headers;
335+
expect(postHeader).toEqual({ 'Content-Type': 'application/json', next: '123abc' });
336+
});
300337
});

src/hooks/useStacSearch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
6767
);
6868

6969
const executeSearch = useCallback(
70-
(payload: SearchPayload) => {
70+
(payload: SearchPayload, headers = {}) => {
7171
setResults(undefined);
7272
setState('LOADING');
7373
setError(undefined);
7474
setNextPageConfig(undefined);
7575
setPreviousPageConfig(undefined);
7676

77-
stacApi.search(payload)
77+
stacApi.search(payload, headers)
7878
.then(response => response.json())
7979
.then(data => {
8080
setResults(data);
@@ -96,7 +96,7 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
9696
...getSearchPayload()
9797
};
9898
}
99-
executeSearch(payload);
99+
executeSearch(payload, link.headers);
100100
}
101101
},
102102
[executeSearch, getSearchPayload]

src/stac-api/index.ts

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

33
type RequestPayload = SearchPayload;
4+
type FetchOptions = {
5+
method: string,
6+
payload: RequestPayload,
7+
headers: GenericObject
8+
}
49

510
class StacApi {
611
baseUrl: string;
@@ -62,13 +67,15 @@ class StacApi {
6267
return Promise.reject(e);
6368
}
6469

65-
fetch(url: string, method: string, payload: RequestPayload): Promise<Response> {
70+
fetch(url: string, options: FetchOptions): Promise<Response> {
71+
const { method, payload, headers } = options;
6672
const { bbox, dateRange, ...restPayload } = payload;
6773

6874
return fetch(url, {
6975
method,
7076
headers: {
71-
'Content-Type': 'application/json'
77+
'Content-Type': 'application/json',
78+
...headers
7279
},
7380
body: JSON.stringify({
7481
...restPayload,
@@ -85,8 +92,8 @@ class StacApi {
8592
});
8693
}
8794

88-
search(payload: SearchPayload): Promise<Response> {
89-
return this.fetch(`${this.baseUrl}/search`, 'POST', payload);
95+
search(payload: SearchPayload, headers = {}): Promise<Response> {
96+
return this.fetch(`${this.baseUrl}/search`, { method: 'POST', payload, headers });
9097
}
9198
}
9299

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Geometry } from 'geojson';
22

3-
type GenericObject = {
3+
export type GenericObject = {
44
[key: string]: any // eslint-disable-line @typescript-eslint/no-explicit-any
55
}
66

0 commit comments

Comments
 (0)