Repository URL to install this package:
|
Version:
3.12.20 ▾
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { SEARCH_CONTEXT_IDS, SEARCH_METHODS } from '@filerobot/utils/lib/constants';
import { adaptMetadataFilter } from '@filerobot/utils/lib/adaptMetadataFilter';
import { SECTIONS_KEYS_AND_I18N } from '../components/Filters/Filters.constants';
import mapFiltersToBackendFormat from './mapFiltersToBackendFormat';
var prepareSearchUrl = function prepareSearchUrl(_ref, cb) {
var _searchContext$parent, _searchContext$option, _searchContext$option2;
var apiPath = _ref.apiPath,
_ref$query = _ref.query,
query = _ref$query === void 0 ? '' : _ref$query,
offset = _ref.offset,
_ref$format = _ref.format,
format = _ref$format === void 0 ? 'json,regvar:api,select:internal' : _ref$format,
_ref$filters = _ref.filters,
filters = _ref$filters === void 0 ? {} : _ref$filters,
method = _ref.method,
_ref$recursive = _ref.recursive,
recursive = _ref$recursive === void 0 ? false : _ref$recursive,
folder = _ref.folder,
limit = _ref.limit,
_ref$sorting = _ref.sorting,
sorting = _ref$sorting === void 0 ? 'name:asc' : _ref$sorting,
searchSuggestionsFilters = _ref.searchSuggestionsFilters,
searchSuggestionsTags = _ref.searchSuggestionsTags,
_ref$activeCollection = _ref.activeCollection,
activeCollection = _ref$activeCollection === void 0 ? null : _ref$activeCollection,
activeCollectionVirtualFolder = _ref.activeCollectionVirtualFolder,
metadataFields = _ref.metadataFields,
searchContext = _ref.searchContext,
includeMeta = _ref.includeMeta,
debugSearch = _ref.debugSearch;
var queryItems = [];
var searchContextParentValue = searchContext === null || searchContext === void 0 ? void 0 : (_searchContext$parent = searchContext.parent) === null || _searchContext$parent === void 0 ? void 0 : _searchContext$parent.value;
var searchFilters = _objectSpread(_objectSpread(_objectSpread({}, searchContextParentValue === SEARCH_CONTEXT_IDS.LABELS ? {
labels: [searchContext === null || searchContext === void 0 ? void 0 : (_searchContext$option = searchContext.option) === null || _searchContext$option === void 0 ? void 0 : _searchContext$option.sid]
} : {}), searchContextParentValue === SEARCH_CONTEXT_IDS.FOLDERS ? {
filtersFolder: [searchContext === null || searchContext === void 0 ? void 0 : searchContext.option]
} : {}), filters || {});
var formattedFilters = mapFiltersToBackendFormat(searchFilters);
var metadataFilter = (filters[SECTIONS_KEYS_AND_I18N.METADATA] || []).map(function (_ref2) {
var labels = _ref2.labels;
return labels;
});
var newFiltersArray = []; // New way of filtering by &f=...
var hasSearchSuggestionsFilters = (searchSuggestionsFilters || []).length > 0;
var hasSearchSuggestionsTags = (searchSuggestionsTags || []).length > 0;
if (folder && !(searchFilters !== null && searchFilters !== void 0 && searchFilters.filtersFolder) && (!searchContextParentValue || searchContextParentValue === SEARCH_CONTEXT_IDS.ANY)) {
queryItems.push("folder=".concat(folder));
}
if (recursive) {
queryItems.push('recursive=1');
}
if (query || searchSuggestionsFilters || searchSuggestionsTags || metadataFilter !== null && metadataFilter !== void 0 && metadataFilter.length) {
var queryArray = [query].concat(_toConsumableArray((searchSuggestionsFilters || []).map(function (filter) {
var _filter$value;
return "".concat(filter.key, ":\"").concat(((_filter$value = filter.value) === null || _filter$value === void 0 ? void 0 : _filter$value.sys_key) || filter.value, "\"");
})), _toConsumableArray(adaptMetadataFilter(metadataFilter, metadataFields)), _toConsumableArray((searchSuggestionsTags || []).map(function (tag) {
return tag.input;
}))).filter(Boolean);
var queryStr = queryArray.join(' ');
if (queryStr) queryItems.push("q=".concat(queryStr));
}
if (newFiltersArray.length) {
queryItems.push("f=".concat(newFiltersArray.filter(Boolean).join('+')));
}
if (searchContextParentValue === SEARCH_CONTEXT_IDS.MY_FAVORITES) {
newFiltersArray.push('enable_favorited:true');
}
if (typeof offset !== 'undefined') {
queryItems.push("offset=".concat(offset));
}
if (limit) {
queryItems.push("limit=".concat(limit));
}
if (format) {
queryItems.push("format=".concat(format));
}
if (method && method !== SEARCH_METHODS.QUICK) {
queryItems.push("method=".concat(method));
}
if (sorting) {
queryItems.push("sort=".concat(sorting));
}
if ((query || hasSearchSuggestionsFilters || hasSearchSuggestionsTags) && searchContextParentValue === SEARCH_CONTEXT_IDS.COLLECTIONS && searchContext !== null && searchContext !== void 0 && (_searchContext$option2 = searchContext.option) !== null && _searchContext$option2 !== void 0 && _searchContext$option2.uuid) {
queryItems.push("collection_uuid=".concat(searchContext.option.uuid));
} else if (activeCollection !== null && activeCollection !== void 0 && activeCollection.uuid) {
var _activeCollection$dat, _activeCollection$dat2;
queryItems.push("collection_uuid=".concat(activeCollection.uuid));
if (activeCollection !== null && activeCollection !== void 0 && (_activeCollection$dat = activeCollection.data) !== null && _activeCollection$dat !== void 0 && (_activeCollection$dat2 = _activeCollection$dat.groupby) !== null && _activeCollection$dat2 !== void 0 && _activeCollection$dat2.length && activeCollectionVirtualFolder !== null && activeCollectionVirtualFolder !== void 0 && activeCollectionVirtualFolder.filters) {
queryItems.push("f64=".concat(activeCollectionVirtualFolder === null || activeCollectionVirtualFolder === void 0 ? void 0 : activeCollectionVirtualFolder.filters));
}
}
if (formattedFilters.length > 0) {
queryItems = queryItems.concat(formattedFilters);
}
if (includeMeta) queryItems.push('include_meta=1');
if (debugSearch) queryItems.push('debug_search=1');
if (cb) {
cb(queryItems);
}
return "".concat(apiPath).concat(apiPath.includes('?') ? '&' : '?').concat(queryItems.join('&'));
};
export default prepareSearchUrl;