Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sortby format for search POST #406

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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