Skip to content

Commit

Permalink
Fix sortby format for search POST radiantearth#402 (radiantearth#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliayun23 authored and silvester-pari committed May 29, 2024
1 parent f5c521c commit 556168a
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ export default class Utils {
}).join('/');
}

static formatSortbyForPOST(value) {
// POST search requires sortby to be an array of objects containing a property name and sort direction.
// See spec here: https://api.stacspec.org/v1.0.0-rc.1/item-search/#tag/Item-Search/operation/postItemSearch
// This function converts the property name to the desired format.
const sortby = {
field: '',
direction: 'asc'
};

// Check if the value starts with a minus sign ("-")
if (value.startsWith('-')) {
// sort by descending order
sortby.field = value.substring(1);
sortby.direction = 'desc';
} else {
//sort by ascending order
sortby.field = value;
}

// Put the object in an array
return [sortby];
}

static getPaginationLinks(data) {
let pages = {};
if (Utils.isObject(data)) {
Expand Down Expand Up @@ -284,7 +307,10 @@ export default class Utils {
continue;
}

if (key === 'datetime') {
if (key === 'sortby') {
value = Utils.formatSortbyForPOST(value);
}
else if (key === 'datetime') {
value = Utils.formatDatetimeQuery(value);
}
else if (key === 'filters') {
Expand Down

0 comments on commit 556168a

Please sign in to comment.