Skip to content

Commit 56a4b8e

Browse files
committed
refactor: Make request options for fetch function optional
1 parent 787be1e commit 56a4b8e

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/hooks/useStacSearch.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import type {
88
CollectionIdList,
99
ApiError,
1010
SearchPayload,
11-
LinkBody
11+
LinkBody,
12+
LoadingState
1213
} from '../types';
1314

1415
type SearchResponse = {
@@ -17,8 +18,6 @@ type SearchResponse = {
1718
links: Link[]
1819
}
1920

20-
type LoadingState = 'IDLE' | 'LOADING';
21-
2221
type PaginationHandler = () => void;
2322

2423
type StacSearchHook = {

src/stac-api/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { Bbox, SearchPayload, DateRange, ApiError, GenericObject } from '..
22

33
type RequestPayload = SearchPayload;
44
type FetchOptions = {
5-
method: string,
6-
payload: RequestPayload,
7-
headers: GenericObject
5+
method?: string,
6+
payload?: RequestPayload,
7+
headers?: GenericObject
88
}
99

1010
class StacApi {
@@ -67,22 +67,16 @@ class StacApi {
6767
return Promise.reject(e);
6868
}
6969

70-
fetch(url: string, options: FetchOptions): Promise<Response> {
71-
const { method, payload, headers } = options;
72-
const { bbox, dateRange, ...restPayload } = payload;
70+
fetch(url: string, options: Partial<FetchOptions> = {}): Promise<Response> {
71+
const { method = 'GET', payload = {}, headers = {} } = options;
7372

7473
return fetch(url, {
7574
method,
7675
headers: {
7776
'Content-Type': 'application/json',
7877
...headers
7978
},
80-
body: JSON.stringify({
81-
...restPayload,
82-
bbox: this.fixBboxCoordinateOrder(bbox),
83-
datetime: this.makeDatetimePayload(dateRange),
84-
limit: 25
85-
})
79+
body: JSON.stringify(payload)
8680
}).then(async (response) => {
8781
if (response.ok) {
8882
return response;
@@ -93,7 +87,17 @@ class StacApi {
9387
}
9488

9589
search(payload: SearchPayload, headers = {}): Promise<Response> {
96-
return this.fetch(`${this.baseUrl}/search`, { method: 'POST', payload, headers });
90+
const { bbox, dateRange, ...restPayload } = payload;
91+
const requestPayload = {
92+
...restPayload,
93+
bbox: this.fixBboxCoordinateOrder(bbox),
94+
datetime: this.makeDatetimePayload(dateRange),
95+
limit: 25
96+
};
97+
return this.fetch(
98+
`${this.baseUrl}/search`,
99+
{ method: 'POST', payload: requestPayload, headers }
100+
);
97101
}
98102
}
99103

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ export type ApiError = {
5757
status: number,
5858
statusText: string
5959
}
60+
61+
export type LoadingState = 'IDLE' | 'LOADING';

0 commit comments

Comments
 (0)