1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getCollection = getCollection;
|
7 |
|
8 | var _lodash = require('lodash');
|
9 |
|
10 | var _lodash2 = _interopRequireDefault(_lodash);
|
11 |
|
12 | var _rio = require('../rio');
|
13 |
|
14 | var _rio2 = _interopRequireDefault(_rio);
|
15 |
|
16 | var _ReduxApiStateDenormalizer = require('./ReduxApiStateDenormalizer');
|
17 |
|
18 | var _schemaConfig = require('../schemaConfig');
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 | var emptyArray = [];
|
23 | Object.freeze(emptyArray);
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function getCollection(collection, state) {
|
34 | var schema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
35 |
|
36 | if (!collection) {
|
37 |
|
38 |
|
39 | return emptyArray;
|
40 | }
|
41 |
|
42 | if (!_lodash2.default.isArray(collection)) {
|
43 | throw new Error('Collection argument needs to be array.');
|
44 | }
|
45 | if (!_lodash2.default.isObject(state)) {
|
46 | throw new Error('State argument is invalid, should be an object.');
|
47 | }
|
48 |
|
49 | var resolvedSchema = (0, _schemaConfig.resolveSchemaName)(collection, schema);
|
50 |
|
51 | var schemaPaths = _rio2.default.schemaPaths;
|
52 | if (!schemaPaths[resolvedSchema]) {
|
53 | throw new Error('Storage for resolved schema ' + resolvedSchema + ' doesn\'t exists in state.');
|
54 | }
|
55 | var storageMap = (0, _ReduxApiStateDenormalizer.createSchemasMap)(state, schemaPaths);
|
56 |
|
57 | return _rio2.default.denormalizer.denormalizeCollection(collection, storageMap, resolvedSchema);
|
58 | } |
\ | No newline at end of file |