UNPKG

4.86 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.Rio = undefined;
7
8var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
10var _lodash = require('lodash');
11
12var _lodash2 = _interopRequireDefault(_lodash);
13
14var _schemaConfig = require('./schemaConfig');
15
16var _jsonApiStandardizer = require('./standardizers/json-api-standardizer');
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21
22/**
23 * Adds additional layer over library by providing central place for defining rio behavior
24 * and enabling easier usage by resolving configurations, standardization, denormalization.
25 */
26var Rio = exports.Rio = function () {
27 function Rio() {
28 _classCallCheck(this, Rio);
29
30 this.clear();
31 }
32
33 /**
34 * Register schema configuration. As schema configuration object or
35 * as passing a function that acts as schema resolver.
36 */
37
38
39 _createClass(Rio, [{
40 key: 'registerSchema',
41 value: function registerSchema(config) {
42 if (_lodash2.default.isFunction(config)) {
43 this.schemaResolvers.push(config);
44 } else if (_lodash2.default.isObject(config)) {
45 (0, _schemaConfig.validateSchemaConfig)(config);
46 this.schemaConfigs[config.schema] = config;
47 } else {
48 throw new Error('Schema argument is invalid. Only object of function are allowed.');
49 }
50 }
51
52 /**
53 * Resolve schema by finding a schema configuration object
54 * or by resolving schema with registered schema resolvers.
55 */
56
57 }, {
58 key: 'getSchema',
59 value: function getSchema(schema) {
60 var config = this.schemaConfigs[schema];
61 if (config) {
62 return _lodash2.default.cloneDeep(config);
63 }
64
65 this.schemaResolvers.forEach(function (resolver) {
66 config = resolver(schema);
67 if (config) {
68 (0, _schemaConfig.validateSchemaConfig)(config);
69 return false;
70 }
71 return true;
72 });
73
74 return _lodash2.default.cloneDeep(config);
75 }
76
77 /**
78 * Register source type for data standardization.
79 */
80
81 }, {
82 key: 'registerSourceType',
83 value: function registerSourceType(sourceType, standardizer) {
84 if (!_lodash2.default.isString(sourceType)) {
85 throw new Error('rio.registerSourceType sourceType argument must be string.');
86 }
87 if (_lodash2.default.isEmpty(sourceType)) {
88 throw new Error('rio.registerSourceType sourceType is empty.');
89 }
90 if (!_lodash2.default.isFunction(standardizer)) {
91 throw new Error('rio.registerSourceType standardizer argument must be a function.');
92 }
93
94 this.standardizers[sourceType] = standardizer;
95 }
96
97 /**
98 * Get standardizer function based on source type.
99 */
100
101 }, {
102 key: 'getStandardizer',
103 value: function getStandardizer(sourceType) {
104 if (!_lodash2.default.isString(sourceType)) {
105 throw new Error('rio.getStandardizer sourceType argument must be string.');
106 }
107 if (_lodash2.default.isEmpty(sourceType)) {
108 throw new Error('rio.getStandardizer sourceType is empty.');
109 }
110
111 return this.standardizers[sourceType];
112 }
113
114 /**
115 * Set instance of configured denormalizer used for denormalization with Rio.
116 */
117
118 }, {
119 key: 'setDenormalizer',
120 value: function setDenormalizer(denormalizer) {
121 this.denormalizer = denormalizer;
122 }
123
124 /**
125 * Set instance of configured denormalizer used for denormalization with Rio.
126 */
127
128 }, {
129 key: 'setSchemaPaths',
130 value: function setSchemaPaths(schemaPaths) {
131 this.schemaPaths = schemaPaths;
132 }
133
134 /**
135 * Clears registered collections and resolvers
136 */
137
138 }, {
139 key: 'clear',
140 value: function clear() {
141 this.schemaConfigs = {};
142 this.schemaResolvers = [];
143 this.schemaPaths = {};
144 this.denormalizer = null;
145 this.standardizers = {};
146
147 // Default standardizer for json-api
148 this.registerSourceType(_jsonApiStandardizer.JSON_API_SOURCE, _jsonApiStandardizer.transform);
149 }
150 }]);
151
152 return Rio;
153}();
154
155// Single instance of rio per application
156
157
158var rio = new Rio();
159exports.default = rio;
\No newline at end of file