1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.Rio = undefined;
|
7 |
|
8 | var _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 |
|
10 | var _lodash = require('lodash');
|
11 |
|
12 | var _lodash2 = _interopRequireDefault(_lodash);
|
13 |
|
14 | var _schemaConfig = require('./schemaConfig');
|
15 |
|
16 | var _jsonApiStandardizer = require('./standardizers/json-api-standardizer');
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var Rio = exports.Rio = function () {
|
27 | function Rio() {
|
28 | _classCallCheck(this, Rio);
|
29 |
|
30 | this.clear();
|
31 | }
|
32 |
|
33 | |
34 |
|
35 |
|
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 |
|
54 |
|
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 |
|
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 |
|
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 |
|
116 |
|
117 |
|
118 | }, {
|
119 | key: 'setDenormalizer',
|
120 | value: function setDenormalizer(denormalizer) {
|
121 | this.denormalizer = denormalizer;
|
122 | }
|
123 |
|
124 | |
125 |
|
126 |
|
127 |
|
128 | }, {
|
129 | key: 'setSchemaPaths',
|
130 | value: function setSchemaPaths(schemaPaths) {
|
131 | this.schemaPaths = schemaPaths;
|
132 | }
|
133 |
|
134 | |
135 |
|
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 |
|
148 | this.registerSourceType(_jsonApiStandardizer.JSON_API_SOURCE, _jsonApiStandardizer.transform);
|
149 | }
|
150 | }]);
|
151 |
|
152 | return Rio;
|
153 | }();
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | var rio = new Rio();
|
159 | exports.default = rio; |
\ | No newline at end of file |