Skip to content
Draft
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
47 changes: 47 additions & 0 deletions src/brapi_methods/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @param {String} [behavior="fork"] Behavior of the node
* @return {BrAPI_Behavior_Node}
*/

/*
export function search(entity,params,behavior){
var param_map = this.map(function(d){
return typeof params === "function" ? params(d) : params;
Expand All @@ -26,6 +28,51 @@ export function search(entity,params,behavior){
return get_params;
})
};
*/

export function search(entity, params, behavior) {
// Map over the parameters
var param_map = this.map(function (d) {
return typeof params === "function" ? params(d) : params;
});

// Perform the POST request
var search_responses = param_map.search_POST(entity, function (p) {
var pageless_params = Object.assign({}, p);
delete pageless_params.page;
delete pageless_params.pageRange;
// delete pageless_params.pageSize;
return pageless_params;
});

// Process the POST responses
return search_responses.flatMap(function (response, index) {
if (response.searchResultsDbId || response.searchResultDbId) {
// Asynchronous search: proceed with GET request
var get_params = {};
get_params.searchResultsDbId =
response.searchResultsDbId || response.searchResultDbId;
var originalParams = param_map.value[index];
if (originalParams.page !== undefined) get_params.page = originalParams.page;
if (originalParams.pageRange !== undefined)
get_params.pageRange = originalParams.pageRange;
if (originalParams.pageSize !== undefined)
get_params.pageSize = originalParams.pageSize;

// Make the GET request
return this.search_GET(entity, function () {
return get_params;
});
} else if (response.data) {
// Synchronous search: return the data directly
return this.constructor.of(response);
} else {
// Handle unexpected responses
throw new Error("Invalid search response: " + JSON.stringify(response));
}
}.bind(this));
}


/** `POST /search/{entity}`
* @alias BrAPINode.prototype.search_POST
Expand Down