diff --git a/src/utils.js b/src/utils.js index 1ee4daf68..327dad417 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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)) { @@ -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') {