Repository URL to install this package:
|
Version:
3.12.11 ▾
|
var _excluded = ["sorting", "recursive", "filters", "offset", "limit", "format", "noPagination", "noFormatAndSortParams", "noRecursiveParam", "moreQueries"];
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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 RequestError from '@filerobot/utils/lib/RequestError';
import { PERMISSIONS } from '@filerobot/utils/lib/constants';
import checkConnection from '@filerobot/utils/lib/checkConnection';
import { selectUser } from './slices/user.slice';
var Client = /*#__PURE__*/_createClass(function Client(filerobot) {
var _this = this;
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Client);
_defineProperty(this, "getSecretHeaderName", function () {
return 'X-Filerobot-Key';
});
_defineProperty(this, "getSecretKeyValue", function () {
var _selectUser;
return (_selectUser = selectUser(_this.filerobot.getGlobalState())) === null || _selectUser === void 0 ? void 0 : _selectUser.sassKey;
});
/**
* @param {string|string[]} id
*/
_defineProperty(this, "abort", function (id) {
var abortSingleId = function abortSingleId(_id) {
if (_this.xhrs && _this.xhrs[_id]) {
_this.xhrs[_id].abort();
delete _this.xhrs[_id];
}
};
if (typeof id === 'string') {
abortSingleId(id);
} else if (Array.isArray(id)) {
id.forEach(abortSingleId);
}
});
_defineProperty(this, "_generateFetchUrl", function (_ref) {
var apiPath = _ref.apiPath,
_ref$queries = _ref.queries,
queries = _ref$queries === void 0 ? {} : _ref$queries;
var queryItems = [];
Object.keys(queries).forEach(function (key) {
var queryValue = queries[key];
if (Array.isArray(queryValue)) {
queryItems = queryItems.concat(queryValue);
} else if (!['undefined', 'object'].includes(_typeof(queryValue))) {
queryItems.push("".concat(key, "=").concat(encodeURIComponent(queryValue)));
}
});
return "".concat(apiPath).concat(queryItems.length > 0 ? "".concat(apiPath.includes('?') ? '&' : '?').concat(queryItems.join('&')) : '');
});
_defineProperty(this, "_send", function (url) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$method = options.method,
method = _options$method === void 0 ? 'GET' : _options$method,
data = options.data,
_options$headers = options.headers,
headers = _options$headers === void 0 ? {} : _options$headers,
abortSignal = options.abortSignal,
onAbort = options.onAbort,
onProgress = options.onProgress,
onError = options.onError,
avoidErrorMsg = options.avoidErrorMsg,
baseApi = options.baseApi,
_options$responseType = options.responseType,
responseType = _options$responseType === void 0 ? 'json' : _options$responseType,
permissionsToCheck = options.permissionsToCheck,
id = options.id,
_options$requestIdsTo = options.requestIdsToAbort,
requestIdsToAbort = _options$requestIdsTo === void 0 ? [] : _options$requestIdsTo,
_options$enabledAbort = options.enabledAbort,
enabledAbort = _options$enabledAbort === void 0 ? true : _options$enabledAbort,
checkOnlySuccessStatus = options.checkOnlySuccessStatus;
if (permissionsToCheck && permissionsToCheck.length > 0 && !_this.filerobot.checkUserPermissions(permissionsToCheck, true)) {
return Promise.reject(_this._i18n('filerobotInvalidPermissionError'));
}
return new Promise(function (resolve, reject) {
if (enabledAbort && id) {
_this.abort(id);
}
var xhr = new XMLHttpRequest();
if (enabledAbort && Array.isArray(requestIdsToAbort) && requestIdsToAbort.length > 0 && Object.keys(_this.xhrs).length > 0) {
_this.abort(requestIdsToAbort);
}
if (id) {
_this.xhrs[id] = xhr;
}
var finalUrl = (/^(http:|https:)?\/\//.test(url) ? url : "".concat(baseApi || _this._baseEndpoint).concat(url)).replace('#', '%23');
xhr.open(method, finalUrl);
xhr.responseType = responseType;
xhr.setRequestHeader('Content-Type', 'application/json');
Object.keys(headers).map(function (header) {
return xhr.setRequestHeader(header, headers[header]);
});
var hideOrShowError = function hideOrShowError(error) {
if (typeof avoidErrorMsg === 'function') {
if (!avoidErrorMsg(xhr.response)) {
var _xhr$response;
_this.filerobot.hideInfo();
_this.filerobot.info(error, (_xhr$response = xhr.response) === null || _xhr$response === void 0 ? void 0 : _xhr$response.status);
}
return;
}
if (!avoidErrorMsg) {
var _xhr$response2;
_this.filerobot.hideInfo();
_this.filerobot.info(error, (_xhr$response2 = xhr.response) === null || _xhr$response2 === void 0 ? void 0 : _xhr$response2.status);
}
};
xhr.onload = function () {
var _response$status;
var response = xhr.response || {};
var responseContentType = xhr.getResponseHeader('Content-Type');
if (xhr.status >= 200 && xhr.status < 300 && (checkOnlySuccessStatus || responseContentType !== 'application/json' || ['ok', 'success'].includes((_response$status = response.status) === null || _response$status === void 0 ? void 0 : _response$status.toLowerCase()))) {
resolve(response || {});
} else {
var error = new RequestError({
code: xhr.status,
codeLabel: response.code,
message: response.msg || response.message || "Error code ".concat(xhr.status),
details: response.hint || response.msg || response.detail || _this._i18n('explorerServiceUnknownBackendError')
});
if (onError && typeof onError === 'function') {
onError(response);
}
hideOrShowError(error);
reject(error);
}
delete _this.xhrs[id];
};
xhr.onprogress = function (e) {
var isOnline = checkConnection();
if (!isOnline) {
xhr.abort();
var error = new Error(_this.filerobot.i18n('filerobotNoInternetConnectionInfo'));
reject(error);
return;
}
if (typeof onProgress === 'function' && e.lengthComputable) {
onProgress(e.loaded, e.total);
}
};
var isAborted = false;
xhr.onerror = function () {
if (isAborted) {
return;
}
var error = new RequestError({
code: xhr.status,
message: _this._i18n('mutualizedUnknownErrorMessage')
});
hideOrShowError(error);
reject(error);
};
xhr.onabort = function () {
isAborted = true;
if (typeof onAbort === 'function') {
onAbort(xhr, reject);
}
};
xhr.send(JSON.stringify(data));
if (abortSignal) {
abortSignal.addEventListener('abort', function () {
xhr.abort();
}, {
once: true
});
}
});
});
// diff between fetch & get:
// -- fetch => supports filters, sort, pagination, recursive...etc. by receiving the args. and processing/combining them inside the function to prepare the final url.
// -- get => receives the prepared url that's ready to be called to have the results without any further processing.
_defineProperty(this, "fetch", function (path) {
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
sort = _ref2.sorting,
_ref2$recursive = _ref2.recursive,
recursive = _ref2$recursive === void 0 ? false : _ref2$recursive,
_ref2$filters = _ref2.filters,
filters = _ref2$filters === void 0 ? [] : _ref2$filters,
_ref2$offset = _ref2.offset,
offset = _ref2$offset === void 0 ? _this._defaultOffset : _ref2$offset,
_ref2$limit = _ref2.limit,
limit = _ref2$limit === void 0 ? _this._defaultLimit : _ref2$limit,
_ref2$format = _ref2.format,
format = _ref2$format === void 0 ? _this._defaultFormat : _ref2$format,
_ref2$noPagination = _ref2.noPagination,
noPagination = _ref2$noPagination === void 0 ? false : _ref2$noPagination,
_ref2$noFormatAndSort = _ref2.noFormatAndSortParams,
noFormatAndSortParams = _ref2$noFormatAndSort === void 0 ? false : _ref2$noFormatAndSort,
_ref2$noRecursivePara = _ref2.noRecursiveParam,
noRecursiveParam = _ref2$noRecursivePara === void 0 ? true : _ref2$noRecursivePara,
_ref2$moreQueries = _ref2.moreQueries,
moreQueries = _ref2$moreQueries === void 0 ? {} : _ref2$moreQueries,
options = _objectWithoutProperties(_ref2, _excluded);
var params = {
apiPath: path,
queries: _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, moreQueries), !noFormatAndSortParams && {
format: format,
sort: sort
}), !noPagination && {
offset: offset,
limit: limit
}), (filters === null || filters === void 0 ? void 0 : filters.length) > 0 && {
filters: filters
}), (!noRecursiveParam || recursive) && {
recursive: recursive ? 1 : 0
})
};
var url = _this._generateFetchUrl(params);
return _this._send(url, _objectSpread(_objectSpread({}, options), {}, {
permissionToCheck: [PERMISSIONS.LIST_ALL],
headers: _this._combineHeaders(options)
}));
});
_defineProperty(this, "_sendAdminRequest", function (path, options) {
return _this._send(path, _objectSpread(_objectSpread({
method: 'GET'
}, options), {}, {
headers: _objectSpread(_objectSpread({}, options.headers), _this._sessionToken && {
'X-Session-Token': _this._sessionToken
}),
baseApi: "".concat(_this.filerobot.opts.adminApiEndpoint, "/")
}));
});
_defineProperty(this, "_sendShareRequest", function (path, options) {
return _this._send(path, _objectSpread(_objectSpread({
method: 'GET'
}, options), {}, {
baseApi: "".concat(_this.filerobot.opts.shareApiEndpoint, "/").concat(_this._container, "/v4/")
}));
});
_defineProperty(this, "_combineHeaders", function (_ref3) {
var _ref3$noDefaultHeader = _ref3.noDefaultHeaders,
noDefaultHeaders = _ref3$noDefaultHeader === void 0 ? false : _ref3$noDefaultHeader,
_ref3$headers = _ref3.headers,
headers = _ref3$headers === void 0 ? {} : _ref3$headers;
return noDefaultHeaders ? headers : _objectSpread(_objectSpread({}, headers), {}, _defineProperty({}, _this.getSecretHeaderName(), _this.getSecretKeyValue()));
});
_defineProperty(this, "get", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._send(path, _objectSpread(_objectSpread({}, options), {}, {
method: 'GET',
headers: _this._combineHeaders(options)
}));
});
_defineProperty(this, "post", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._send(path, _objectSpread(_objectSpread({}, options), {}, {
data: options.data,
method: 'POST',
headers: _this._combineHeaders(options)
}));
});
_defineProperty(this, "put", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._send(path, _objectSpread(_objectSpread({}, options), {}, {
method: 'PUT',
data: options.data,
headers: _this._combineHeaders(options)
}));
});
_defineProperty(this, "patch", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._send(path, _objectSpread(_objectSpread({}, options), {}, {
method: 'PATCH',
data: options.data,
headers: _this._combineHeaders(options)
}));
});
_defineProperty(this, "delete", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._send(path, _objectSpread(_objectSpread({}, options), {}, {
method: 'DELETE',
data: options.data,
headers: _this._combineHeaders(options)
}));
});
_defineProperty(this, "getAdmin", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._sendAdminRequest(path, options);
});
_defineProperty(this, "postAdmin", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._sendAdminRequest(path, _objectSpread(_objectSpread({}, options), {}, {
method: 'POST',
data: options.data
}));
});
_defineProperty(this, "putAdmin", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._send(path, _objectSpread(_objectSpread({}, options), {}, {
method: 'PUT',
data: options.data
}));
});
_defineProperty(this, "deleteAdmin", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var data = arguments.length > 2 ? arguments[2] : undefined;
return _this._send(path, _objectSpread(_objectSpread({}, options), {}, {
method: 'PUT',
data: data
}));
});
_defineProperty(this, "getShare", function (path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _this._sendShareRequest(path, options);
});
this.filerobot = filerobot;
this.opts = opts;
this._sessionToken = filerobot.opts.sessionToken;
this._projectUuid = filerobot.opts.projectUuid;
this._container = filerobot.opts.container;
this._baseEndpoint = "".concat(filerobot.opts.apiEndpoint, "/").concat(this._container, "/").concat(filerobot.opts.apiEndpointVersion, "/");
this._adminEndpoint = filerobot.opts.adminApiEndpoint;
this._shareEndpoint = filerobot.opts.shareApiEndpoint;
this._i18n = filerobot.i18n;
this._defaultOffset = 0; // almost shouldn't be used as offset should be provided.
this._defaultLimit = 100;
this._defaultFormat = 'json,regvar:api,select:internal';
this.xhrs = {};
});
export default Client;