| 14 | } |
| 15 | |
| 16 | class StacApi { |
| 17 | baseUrl: string; |
| 18 | options?: GenericObject; |
| 19 | searchMode = SearchMode.GET; |
| 20 | |
| 21 | constructor(baseUrl: string, searchMode: SearchMode, options?: GenericObject) { |
| 22 | this.baseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl; |
| 23 | this.searchMode = searchMode; |
| 24 | this.options = options; |
| 25 | } |
| 26 | |
| 27 | fixBboxCoordinateOrder(bbox?: Bbox): Bbox | undefined { |
| 28 | if (!bbox) { |
| 29 | return undefined; |
| 30 | } |
| 31 | |
| 32 | const [lonMin, latMin, lonMax, latMax] = bbox; |
| 33 | const sortedBbox: Bbox = [lonMin, latMin, lonMax, latMax]; |
| 34 | |
| 35 | if (lonMin > lonMax) { |
| 36 | sortedBbox[0] = lonMax; |
| 37 | sortedBbox[2] = lonMin; |
| 38 | } |
| 39 | |
| 40 | if (latMin > latMax) { |
| 41 | sortedBbox[1] = latMax; |
| 42 | sortedBbox[3] = latMin; |
| 43 | } |
| 44 | |
| 45 | return sortedBbox; |
| 46 | } |
| 47 | |
| 48 | /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ |
| 49 | makeArrayPayload(arr?: any[]) { |
| 50 | return arr?.length ? arr : undefined; |
| 51 | } |
| 52 | |
| 53 | makeDatetimePayload(dateRange?: DateRange): string | undefined { |
| 54 | if (!dateRange?.from && !dateRange?.to) { |
| 55 | return undefined; |
| 56 | } |
| 57 | |
| 58 | const formatDate = (date: string | undefined, end?: boolean) => { |
| 59 | if (!date) return '..'; |
| 60 | |
| 61 | const timePart = end ? 'T23:59:59Z' : 'T00:00:00Z'; |
| 62 | return date.includes('T') ? date : `${date}${timePart}`; |
| 63 | }; |
| 64 | |
| 65 | return `${formatDate(dateRange?.from)}/${formatDate(dateRange?.to, true)}`; |
| 66 | } |
| 67 | |
| 68 | payloadToQuery({ sortby, ...payload }: SearchPayload): string { |
| 69 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 70 | const queryObj: { [key: string]: any } = {}; |
| 71 | for (const [key, value] of Object.entries(payload)) { |
| 72 | if (!value) continue; |
| 73 |
nothing calls this directly
no outgoing calls
no test coverage detected