From cd87342f74e3854af75b60ab6f768c91fd765e69 Mon Sep 17 00:00:00 2001 From: Julia Yun Date: Tue, 6 Feb 2024 09:53:02 +1100 Subject: [PATCH] Fix sortby format for search POST #402 --- src/utils.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 1ee4daf6..327dad41 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') {