@oarepo/invenio-api-vuex
Version:
A set of vuex store modules and route utils to work with Invenio 3 REST API.
1,079 lines (931 loc) • 32.2 kB
JavaScript
/*!
* undefined vundefined
* (c)
*/
import { Action, VuexModule, Mutation, Module } from 'vuex-class-modules';
import axios from 'axios';
import deepmerge from 'deepmerge';
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
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, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (_isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _createSuper(Derived) {
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (_isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
}
function _iterableToArrayLimit(arr, i) {
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
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(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
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 _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 _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
var desc = {};
Object.keys(descriptor).forEach(function (key) {
desc[key] = descriptor[key];
});
desc.enumerable = !!desc.enumerable;
desc.configurable = !!desc.configurable;
if ('value' in desc || desc.initializer) {
desc.writable = true;
}
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
return decorator(target, property, desc) || desc;
}, desc);
if (context && desc.initializer !== void 0) {
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
}
return desc;
}
// eslint-disable-next-line no-unused-vars
var NOT_A_FACET = 'not-a-facet';
var TranslationOptions = Object.freeze({
FACET_CODE_PREFIX: 'prefix-name',
NO_PREFIX: 'no-prefix',
NO_TRANSLATION: 'no-translation',
TRANSLATE: 'no-prefix'
});
function bucketExtractor(_ref) {
var node = _ref.node,
path = _ref.path;
if (node.buckets !== undefined) {
return {
type: 'buckets',
code: path[0].code,
facets: node.buckets.map(function (x) {
return {
label: x.key_as_string !== undefined ? x.key_as_string : x.key,
value: x.key,
count: x.doc_count
};
}),
element: node
};
}
return undefined;
}
function facetFlattener(aggregations, callbacks, collection) {
function* inner(node, path) {
for (var _i = 0, _Object$keys = Object.keys(node); _i < _Object$keys.length; _i++) {
var k = _Object$keys[_i];
var value = node[k];
if (value === Object(value)) {
var valuePath = [{
code: k,
element: value
}].concat(_toConsumableArray(path));
var ret = callbacks.call({
node: value,
path: valuePath,
store: collection
}).filter(function (x) {
return x !== undefined;
});
if (ret.find(function (x) {
return x === NOT_A_FACET;
})) {
// someone says it is not a facet
continue;
}
if (ret.length > 0) {
yield ret[0];
}
yield* inner(value, valuePath);
}
}
}
return Array.from(inner(aggregations, []));
}
function useDefault(defaultTranslate, translate) {
if (translate !== undefined) {
return translate;
}
return defaultTranslate;
}
function facetTranslator(facets, _ref2) {
var facetOptions = _ref2.facetOptions,
i18n = _ref2.i18n;
var translation = facetOptions.translation;
return facets.map(function (facet) {
facet = Object.assign({}, facet);
var translationFacetLabelType = useDefault(translation.defaultTranslateTitles, (translation.translateTitles || {})[facet.code]);
if (translationFacetLabelType !== TranslationOptions.NO_TRANSLATION) {
var prefix = '';
if (translationFacetLabelType !== TranslationOptions.NO_PREFIX) {
prefix = translationFacetLabelType;
}
facet.label = i18n("".concat(prefix).concat(facet.code));
} else {
facet.label = facet.code;
}
var translationFacetValueType = useDefault(translation.defaultTranslateValues, (translation.translateValues || {})[facet.code]);
if (translationFacetValueType === TranslationOptions.NO_TRANSLATION) {
return facet;
}
facet.facets = facet.facets.map(function (value) {
var prefix = '';
if (translationFacetValueType === TranslationOptions.FACET_CODE_PREFIX) {
prefix = "".concat(facet.code, ".");
} else if (translationFacetValueType !== TranslationOptions.NO_PREFIX) {
prefix = translationFacetValueType;
}
return Object.assign({}, value, {
label: i18n("".concat(prefix).concat(value.label))
});
});
return facet;
});
}
var CallbackList = /*#__PURE__*/function () {
function CallbackList() {
var _this$callbacks;
_classCallCheck(this, CallbackList);
this.callbacks = [];
(_this$callbacks = this.callbacks).push.apply(_this$callbacks, arguments);
}
_createClass(CallbackList, [{
key: "addCallback",
value: function addCallback(callback) {
if (Array.isArray(callback)) {
var _this$callbacks2;
(_this$callbacks2 = this.callbacks).push.apply(_this$callbacks2, _toConsumableArray(callback));
} else {
this.callbacks.push(callback);
}
}
}, {
key: "removeCallback",
value: function removeCallback(callback) {
this.callbacks = this.callbacks.map(function (x) {
return x !== callback;
});
}
}, {
key: "call",
value: function call() {
var ret = [];
for (var i = this.callbacks.length - 1; i >= 0; i--) {
var c = this.callbacks[i];
ret.push(c.apply(void 0, arguments));
}
return ret;
}
}, {
key: "chainCall",
value: function chainCall(arg) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
for (var i = this.callbacks.length - 1; i >= 0; i--) {
var c = this.callbacks[i];
arg = c.apply(void 0, [arg].concat(args));
}
return arg;
}
}]);
return CallbackList;
}();
function convertToCallbackList(x) {
if (x === null || x === undefined) {
return new CallbackList();
}
if (x instanceof CallbackList) {
return x;
}
if (!Array.isArray(x)) {
x = [x];
}
return _construct(CallbackList, _toConsumableArray(x));
}
function convertDictToCallbackList(obj) {
if (obj === null || obj === undefined) {
return {};
}
return Object.fromEntries(Object.entries(obj).map(function (x) {
return [x[0], convertToCallbackList(x[1])];
}));
}
var FacetOptions = /*#__PURE__*/function () {
function FacetOptions(_ref) {
var parent = _ref.parent,
facetExtractors = _ref.facetExtractors,
facetPreprocessors = _ref.facetPreprocessors,
defaultTranslateTitles = _ref.defaultTranslateTitles,
defaultTranslateValues = _ref.defaultTranslateValues,
translateTitles = _ref.translateTitles,
translateValues = _ref.translateValues;
_classCallCheck(this, FacetOptions);
this.facetExtractors = new CallbackList(bucketExtractor);
this.facetPreprocessors = new CallbackList(facetTranslator);
this.translation = {
/*
Translate facet titles by default. Can be overriden via translatedTitles
*/
defaultTranslateTitles: TranslationOptions.NO_PREFIX,
/*
Translate facet values by default. Can be overriden via translatedValues
*/
defaultTranslateValues: TranslationOptions.NO_TRANSLATION,
/*
A dictionary from facet name to Union[TranslationOptions, false, str].
not present: use defaultTranslateTitles
NO_TRANSLATION: do not translate the name to title
NO_PREFIX: use facet name as translation key
any other string: use the given string as a translation key
*/
translateTitles: {},
/*
A dictionary from facet name to Union[TranslationOptions, false, str].
not present: use defaultTranslateValues
NO_TRANSLATION: do not translate the facet value's label
NO_PREFIX: use facet's label as translation key
FACET_NAME_PREFIX: use ${facet_name}.${facet.value.label} as translation key
any other string: use ${this_string}.${facet.value.label} as translation key
*/
translateValues: {}
};
if (parent) {
this.assign({
facetExtractors: parent.facetExtractors,
facetPreprocessors: parent.facetPreprocessors,
defaultTranslateTitles: parent.defaultTranslateTitles,
defaultTranslateValues: parent.defaultTranslateValues,
translateTitles: parent.translateTitles,
translateValues: parent.translateValues
});
}
this.assign({
facetExtractors: facetExtractors,
facetPreprocessors: facetPreprocessors,
defaultTranslateTitles: defaultTranslateTitles,
defaultTranslateValues: defaultTranslateValues,
translateTitles: translateTitles,
translateValues: translateValues
});
}
_createClass(FacetOptions, [{
key: "assign",
value: function assign(_ref2) {
var facetExtractors = _ref2.facetExtractors,
facetPreprocessors = _ref2.facetPreprocessors,
defaultTranslateTitles = _ref2.defaultTranslateTitles,
defaultTranslateValues = _ref2.defaultTranslateValues,
translateTitles = _ref2.translateTitles,
translateValues = _ref2.translateValues;
if (facetExtractors !== undefined) {
this.facetExtractors.addCallback(facetExtractors);
}
if (facetPreprocessors !== undefined) {
this.facetPreprocessors.addCallback(facetPreprocessors);
}
if (defaultTranslateTitles !== undefined) {
this.translation.defaultTranslateTitles = defaultTranslateTitles;
}
if (defaultTranslateValues !== undefined) {
this.translation.defaultTranslateValues = defaultTranslateValues;
}
if (translateTitles !== undefined) {
this.translation.translateTitles = Object.assign({}, this.translation.translateTitles, {}, translateTitles);
}
if (translateValues !== undefined) {
this.translation.translateValues = Object.assign({}, this.translation.translateValues, {}, translateValues);
}
}
}]);
return FacetOptions;
}();
var ConfigModule = /*#__PURE__*/function () {
function ConfigModule() {
_classCallCheck(this, ConfigModule);
this.apiURL = null;
this.defaultPageSize = 10;
this.i18n = function (x) {
return x;
};
this.defaultFacetOptions = new FacetOptions({});
this.facetOptions = {};
this.recordPreprocessors = {};
this.defaultRecordPreprocessors = new CallbackList();
this.listRecordPreprocessors = {};
this.defaultListRecordPreprocessors = new CallbackList();
this.usePost = false;
}
_createClass(ConfigModule, [{
key: "collectionURL",
value: function collectionURL(collectionId) {
return "".concat(this.apiURL, "/").concat(collectionId, "/");
}
}, {
key: "recordURL",
value: function recordURL(collectionId, persistentId) {
return "".concat(this.apiURL, "/").concat(collectionId, "/").concat(persistentId, "/");
}
}, {
key: "collectionsURL",
get: function get() {
return "".concat(this.apiURL, "/1.0/oarepo/collections/");
}
}]);
return ConfigModule;
}();
var State = Object.freeze({
INVALID: 0,
LOADING: 1,
LOADED: 2
});
var _class, _temp;
var CollectionListModule = (_class = (_temp = /*#__PURE__*/function (_VuexModule) {
_inherits(CollectionListModule, _VuexModule);
var _super = _createSuper(CollectionListModule);
/** @type {ConfigModule} */
// loading state
function CollectionListModule(config, options) {
var _this;
_classCallCheck(this, CollectionListModule);
_this = _super.call(this, options);
_this.config = null;
_this.collections = [];
_this.state = State.INVALID;
_this.reloadNeeded = false;
_this.config = config;
return _this;
}
_createClass(CollectionListModule, [{
key: "load",
value: async function load() {
this.state = State.LOADING;
var response = await axios.get(this.config.collectionsURL);
this.collections = response.data;
this.state = State.LOADED;
return this.collections;
}
}, {
key: "reload",
value: async function reload() {
return this.load();
}
}, {
key: "loaded",
get: function get() {
return this.state === State.LOADED;
}
}, {
key: "visibleCollections",
get: function get() {
if (this.state !== State.LOADED) {
return [];
}
return this.collections.filter(function (x) {
return !x.hidden;
});
}
}]);
return CollectionListModule;
}(VuexModule), _temp), (_applyDecoratedDescriptor(_class.prototype, "load", [Action], Object.getOwnPropertyDescriptor(_class.prototype, "load"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "reload", [Action], Object.getOwnPropertyDescriptor(_class.prototype, "reload"), _class.prototype)), _class);
var _class$1, _temp$1;
var CollectionModule = (_class$1 = (_temp$1 = /*#__PURE__*/function (_VuexModule) {
_inherits(CollectionModule, _VuexModule);
var _super = _createSuper(CollectionModule);
/** @type {ConfigModule} */
function CollectionModule(config, options) {
var _this;
_classCallCheck(this, CollectionModule);
_this = _super.call(this, options);
_this.config = null;
_this.collectionId = null;
_this.state = State.INVALID;
_this.facets = [];
_this.records = [];
_this.response = null;
_this.queryParams = {};
_this.totalPages = 0;
_this.totalRecords = 0;
_this.reloadNeeded = false;
_this.config = config;
return _this;
}
_createClass(CollectionModule, [{
key: "setSearchResults",
value: function setSearchResults(_ref) {
var _this2 = this;
var aggregations = _ref.aggregations,
records = _ref.records,
total = _ref.total;
this.response = {
aggregations: aggregations,
records: records,
total: total
};
var collectionFacetOptions = this.config.facetOptions[this.collectionId] || this.config.defaultFacetOptions;
this.facets = this.transformFacets(aggregations, collectionFacetOptions);
var collectionListRecordPreprocessors = this.config.listRecordPreprocessors[this.collectionId] || this.config.defaultListRecordPreprocessors;
this.records = records.map(function (x) {
return collectionListRecordPreprocessors.chainCall(x, {
collection: _this2
});
});
var pageSize = this.queryParams.size || this.config.defaultPageSize;
this.totalRecords = total;
this.totalPages = Math.ceil(total / pageSize);
}
}, {
key: "transformFacets",
value: function transformFacets(aggregations, facetOptions) {
var flattenedFacets = facetFlattener(aggregations, facetOptions.facetExtractors, this);
return facetOptions.facetPreprocessors.chainCall(flattenedFacets, {
facetOptions: facetOptions,
collection: this,
i18n: this.config.i18n
});
}
}, {
key: "load",
value: async function load(_ref2) {
var query = _ref2.query,
collectionId = _ref2.collectionId;
this.collectionId = collectionId;
this.queryParams = Object.assign({}, query);
return this.reload();
}
}, {
key: "reload",
value: async function reload() {
// duplicate params
this.state = State.LOADING; // convert to http params
var axiosParams = this.axiosParams;
var response = await axios.get("".concat(this.config.collectionURL(this.collectionId)), {
params: axiosParams
});
var _response$data = response.data,
aggregations = _response$data.aggregations,
hits = _response$data.hits;
this.setSearchResults({
aggregations: aggregations,
records: hits.hits,
total: hits.total
});
this.state = State.LOADED;
return response.data;
}
}, {
key: "transform",
value: function transform() {
// setSearchResults will reload facets so that all their values get translated (if needed)
this.setSearchResults(this.response);
}
}, {
key: "create",
value: async function create(_ref3) {
var metadata = _ref3.metadata,
storeModule = _ref3.storeModule;
var resp = await axios.post(this.config.collectionURL(this.collectionId), metadata, {
headers: {
'Content-Type': 'application/json'
}
});
this.reloadNeeded = true;
if (storeModule !== undefined) {
storeModule.setResponse(resp);
storeModule.recordId = resp.data.metadata.id;
storeModule.collectionId = this.collectionId;
storeModule.state = State.LOADED;
}
return resp.data;
}
}, {
key: "loaded",
get: function get() {
return this.state === State.LOADED;
}
}, {
key: "page",
get: function get() {
return parseInt(this.queryParams.page || 1);
}
}, {
key: "pageSize",
get: function get() {
return parseInt(this.queryParams.size || this.config.defaultPageSize);
}
}, {
key: "axiosParams",
get: function get() {
var axiosParams = new URLSearchParams();
var qp = Object.assign({}, this.queryParams);
if (qp.size === undefined) {
qp.size = this.config.defaultPageSize;
}
Object.entries(this.queryParams).forEach(function (_ref4) {
var _ref5 = _slicedToArray(_ref4, 2),
pkey = _ref5[0],
pvalue = _ref5[1];
if (Array.isArray(pvalue)) {
pvalue.forEach(function (val) {
axiosParams.append(pkey, val);
});
} else {
axiosParams.append(pkey, pvalue);
}
});
return axiosParams;
}
}]);
return CollectionModule;
}(VuexModule), _temp$1), (_applyDecoratedDescriptor(_class$1.prototype, "setSearchResults", [Mutation], Object.getOwnPropertyDescriptor(_class$1.prototype, "setSearchResults"), _class$1.prototype), _applyDecoratedDescriptor(_class$1.prototype, "load", [Action], Object.getOwnPropertyDescriptor(_class$1.prototype, "load"), _class$1.prototype), _applyDecoratedDescriptor(_class$1.prototype, "reload", [Action], Object.getOwnPropertyDescriptor(_class$1.prototype, "reload"), _class$1.prototype), _applyDecoratedDescriptor(_class$1.prototype, "transform", [Action], Object.getOwnPropertyDescriptor(_class$1.prototype, "transform"), _class$1.prototype), _applyDecoratedDescriptor(_class$1.prototype, "create", [Action], Object.getOwnPropertyDescriptor(_class$1.prototype, "create"), _class$1.prototype)), _class$1);
var _class$2, _temp$2;
var RecordModule = (_class$2 = (_temp$2 = /*#__PURE__*/function (_VuexModule) {
_inherits(RecordModule, _VuexModule);
var _super = _createSuper(RecordModule);
/** @type {ConfigModule} */
function RecordModule(config, options) {
var _this;
_classCallCheck(this, RecordModule);
_this = _super.call(this, options);
_this.config = null;
_this.state = State.INVALID;
_this.record = {};
_this.metadata = {};
_this.links = {};
_this.created = null;
_this.updated = null;
_this.collectionId = null;
_this.recordId = null;
_this.reloadNeeded = false;
_this.config = config;
return _this;
}
_createClass(RecordModule, [{
key: "load",
value: async function load(_ref) {
var collectionId = _ref.collectionId,
recordId = _ref.recordId;
this.collectionId = collectionId;
this.recordId = recordId;
return this.reload();
}
}, {
key: "setResponse",
value: function setResponse(response) {
var record = response.data;
var collectionRecordPreprocessors = this.config.recordPreprocessors[this.collectionId] || this.config.defaultRecordPreprocessors;
collectionRecordPreprocessors.call(record, {
record: this
});
this.record = record;
this.metadata = record.metadata;
this.links = record.links;
this.created = new Date(record.created);
this.updated = new Date(record.updated);
}
}, {
key: "reload",
value: async function reload() {
this.state = State.LOADING;
var response = await axios.get(this.recordURL, {
headers: {
Accept: 'application/json'
}
});
this.setResponse(response);
this.state = State.LOADED;
return response.data;
}
}, {
key: "patch",
value: async function patch(data) {
this.state = State.LOADING;
if (!Array.isArray(data)) {
data = [data];
}
var resp;
if (this.config.usePost) {
resp = await axios.post(this.recordURL, data, {
headers: {
'Content-Type': 'application/json-patch+json',
'X-HTTP-Method-Override': 'PATCH'
}
});
} else {
resp = await axios.patch(this.recordURL, data, {
headers: {
'Content-Type': 'application/json-patch+json'
}
});
}
this.setResponse(resp);
this.state = State.LOADED;
return resp.data;
}
}, {
key: "save",
value: async function save() {
this.state = State.LOADING;
var resp = await axios.post(this.recordURL, this.record.metadata, {
headers: {
'Content-Type': 'application/json'
}
});
this.setResponse(resp);
this.state = State.LOADED;
return resp.data;
}
}, {
key: "recordURL",
get: function get() {
return this.config.recordURL(this.collectionId, this.recordId);
}
}, {
key: "loaded",
get: function get() {
return this.state === State.LOADED;
}
}]);
return RecordModule;
}(VuexModule), _temp$2), (_applyDecoratedDescriptor(_class$2.prototype, "load", [Action], Object.getOwnPropertyDescriptor(_class$2.prototype, "load"), _class$2.prototype), _applyDecoratedDescriptor(_class$2.prototype, "setResponse", [Mutation], Object.getOwnPropertyDescriptor(_class$2.prototype, "setResponse"), _class$2.prototype), _applyDecoratedDescriptor(_class$2.prototype, "reload", [Action], Object.getOwnPropertyDescriptor(_class$2.prototype, "reload"), _class$2.prototype), _applyDecoratedDescriptor(_class$2.prototype, "patch", [Action], Object.getOwnPropertyDescriptor(_class$2.prototype, "patch"), _class$2.prototype), _applyDecoratedDescriptor(_class$2.prototype, "save", [Action], Object.getOwnPropertyDescriptor(_class$2.prototype, "save"), _class$2.prototype)), _class$2);
function routerCollectionList(pathParams) {
return deepmerge({
name: 'oarepoCollectionList',
meta: {
preloader: {
key: 'oarepoCollectionList',
store: 'oarepoCollectionList',
action: 'load',
expiration: 3600
}
},
props: true
}, pathParams);
}
function routerCollection(pathParams) {
return deepmerge({
name: 'oarepoCollection',
meta: {
preloader: {
key: 'oarepoCollection',
store: 'oarepoCollection',
action: 'load',
params: {
collectionId: 'collectionId'
},
expiration: 60,
query: true
}
},
props: true
}, pathParams);
}
function routerRecord(pathParams) {
return deepmerge({
name: 'oarepoRecord',
meta: {
preloader: {
key: 'oarepoRecord',
store: 'oarepoRecord',
action: 'load',
params: {
collectionId: 'collectionId',
recordId: 'recordId'
},
expiration: 10
}
},
props: true
}, pathParams);
}
function applyMixins(clz, mixins) {
// inspired by https://github.com/justinfagnani/mixwith.js/blob/master/src/mixwith.js
var app = mixins.reduce(function (c, m) {
return m(c);
}, clz);
return Module({
generateMutationSetters: true
})(app);
}
function facetQuerySynchronization(facets, query) {
return facets.map(function (f) {
query._prop("array:".concat(f.code));
return Object.assign({}, f, {
facets: f.facets.map(function (facet) {
var ret = Object.assign({}, facet);
Object.defineProperty(ret, 'model', {
get: function get() {
return query[f.code].includes(facet.value.toString());
},
set: function set(value) {
if (value) {
query._insert(f.code, facet.value);
} else {
query._remove(f.code, facet.value);
}
}
});
return ret;
})
});
});
}
var index = {
install: function install(Vue) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var defaultOptions = {
apiURL: '/api',
defaultPageSize: 10,
i18n: function i18n(x) {
return x;
},
defaultFacetOptions: new FacetOptions({}),
facetOptions: {},
defaultRecordPreprocessors: new CallbackList(),
recordPreprocessors: {},
defaultListRecordPreprocessors: new CallbackList(),
listRecordPreprocessors: {},
configModule: ConfigModule,
collectionListMixins: [],
collectionMixins: [],
recordMixins: []
};
options = Object.assign({}, defaultOptions, {}, options);
var store = options.store;
if (store === undefined) {
throw new Error('Pass store into the options');
}
var config = new options.configModule();
config.apiURL = options.apiURL;
config.defaultPageSize = options.defaultPageSize;
config.i18n = options.i18n;
config.defaultFacetOptions = options.defaultFacetOptions;
config.facetOptions = options.facetOptions;
config.recordPreprocessors = convertDictToCallbackList(options.recordPreprocessors);
config.defaultRecordPreprocessors = convertToCallbackList(options.defaultRecordPreprocessors);
config.listRecordPreprocessors = convertDictToCallbackList(options.listRecordPreprocessors);
config.defaultListRecordPreprocessors = convertToCallbackList(options.defaultListRecordPreprocessors);
config.usePost = options.usePost;
var collections = new applyMixins(CollectionListModule, options.collectionListMixins)(config, {
store: store,
name: 'oarepoCollectionList'
});
var collection = new applyMixins(CollectionModule, options.collectionMixins)(config, {
store: store,
name: 'oarepoCollection'
});
var record = new applyMixins(RecordModule, options.recordMixins)(config, {
store: store,
name: 'oarepoRecord'
});
if (Vue.prototype.$oarepo === undefined) {
Vue.prototype.$oarepo = {};
}
Object.assign(Vue.prototype.$oarepo, {
config: config,
collections: collections,
collection: collection,
record: record
});
}
};
export default index;
export { CallbackList, CollectionListModule, CollectionModule, ConfigModule, FacetOptions, RecordModule, State, TranslationOptions, facetQuerySynchronization, routerCollection, routerCollectionList, routerRecord };