Skip to content

Commit a6495d6

Browse files
committed
fix: ID prev link via rel=previous
1 parent cf7a136 commit a6495d6

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/hooks/useStacSearch.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ describe('useStacSearch', () => {
192192
expect(postPayload).toEqual(response.links[0].body);
193193
});
194194

195-
it('includes nextPage callback', async () => {
195+
it('includes previousPage callback', async () => {
196196
const response = {
197197
links: [{
198198
rel: 'prev',
@@ -225,4 +225,38 @@ describe('useStacSearch', () => {
225225
expect(result.current.results).toEqual({ data: '12345' });
226226
expect(postPayload).toEqual(response.links[0].body);
227227
});
228+
229+
it('includes previousPage callback (previous edition)', async () => {
230+
const response = {
231+
links: [{
232+
rel: 'previous',
233+
type: 'application/geo+json',
234+
method: 'POST',
235+
href: 'https://example.com/stac/search',
236+
body: {
237+
limit: 25,
238+
token: 'prev:abc123'
239+
}
240+
}]
241+
};
242+
fetch.mockResponseOnce(JSON.stringify(response));
243+
244+
const { result, waitForNextUpdate } = renderHook(
245+
() => useStacSearch(stacApi)
246+
);
247+
248+
act(() => result.current.setDateRangeTo('2022-05-17'));
249+
act(() => result.current.submit());
250+
await waitForNextUpdate(); // wait to set results
251+
expect(result.current.results).toEqual(response);
252+
expect(result.current.previousPage).toBeDefined();
253+
254+
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
255+
act(() => result.current.previousPage && result.current.previousPage());
256+
await waitForNextUpdate();
257+
258+
const postPayload = parseRequestPayload(fetch.mock.calls[1][1]);
259+
expect(result.current.results).toEqual({ data: '12345' });
260+
expect(postPayload).toEqual(response.links[0].body);
261+
});
228262
});

src/hooks/useStacSearch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
6969
setNextPage(() => () => loadNewPage(nextPageLink));
7070
}
7171

72-
const previousPageLink = links.find(({ rel }) => rel === 'prev');
72+
// Turns not all STAC APIs implement the spec correctly, some advertise the prev link as previous
73+
const previousPageLink = links.find(({ rel }) => ['prev', 'previous'].includes(rel));
7374
if (previousPageLink) {
7475
setPreviousPage(() => () => loadNewPage(previousPageLink));
7576
}

0 commit comments

Comments
 (0)