1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
8 |
|
9 | exports.validateSchemaConfig = validateSchemaConfig;
|
10 | exports.resolveConfig = resolveConfig;
|
11 | exports.buildEndpoint = buildEndpoint;
|
12 | exports.resolveSchemaName = resolveSchemaName;
|
13 |
|
14 | var _lodash = require('lodash');
|
15 |
|
16 | var _lodash2 = _interopRequireDefault(_lodash);
|
17 |
|
18 | var _zSchema = require('z-schema');
|
19 |
|
20 | var _zSchema2 = _interopRequireDefault(_zSchema);
|
21 |
|
22 | var _urijs = require('urijs');
|
23 |
|
24 | var _urijs2 = _interopRequireDefault(_urijs);
|
25 |
|
26 | var _status = require('./status');
|
27 |
|
28 | var _consts = require('./consts');
|
29 |
|
30 | var _rio = require('./rio');
|
31 |
|
32 | var _rio2 = _interopRequireDefault(_rio);
|
33 |
|
34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
35 |
|
36 | var validator = new _zSchema2.default();
|
37 | var schemaDefinition = {
|
38 | type: 'object',
|
39 | properties: {
|
40 | schema: {
|
41 | type: 'string'
|
42 | },
|
43 | request: {
|
44 | type: 'object',
|
45 | properties: {
|
46 | endpoint: {
|
47 | type: 'string'
|
48 | },
|
49 | headers: {
|
50 | type: 'object'
|
51 | },
|
52 | method: {
|
53 | type: 'string'
|
54 | },
|
55 | types: {
|
56 | type: 'array'
|
57 | },
|
58 | resourceType: {
|
59 | type: 'string'
|
60 | },
|
61 | body: {
|
62 | type: ['object', 'string']
|
63 | }
|
64 | },
|
65 | additionalProperties: false,
|
66 | required: ['endpoint', 'headers']
|
67 | },
|
68 | actions: {
|
69 | type: 'object'
|
70 | }
|
71 | },
|
72 | additionalProperties: false,
|
73 | required: ['schema', 'request']
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function validateSchemaConfig(config) {
|
82 | var validResult = validator.validate(config, schemaDefinition);
|
83 | if (!validResult) {
|
84 | var validationErrorsMsg = JSON.stringify(validator.getLastErrors());
|
85 | var configMsg = JSON.stringify(config);
|
86 | throw new Error('Schema configuration is invalid. Error: ' + validationErrorsMsg + '.' + (' Invalid schema config: ' + configMsg));
|
87 | }
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function resolveConfig(schema) {
|
98 | if (_lodash2.default.isString(schema)) {
|
99 | return _rio2.default.getSchema(schema);
|
100 | } else if (_lodash2.default.isObject(schema)) {
|
101 | var argConfig = schema;
|
102 | var rioConfig = _rio2.default.getSchema(argConfig.schema);
|
103 | var resolvedSchema = _lodash2.default.merge(rioConfig, argConfig);
|
104 | validateSchemaConfig(resolvedSchema);
|
105 | return resolvedSchema;
|
106 | }
|
107 | return undefined;
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function buildEndpoint(endpoint) {
|
119 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
120 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
121 |
|
122 | var isEndpointResolved = options[_consts.RESOLVED_ENDPOINT];
|
123 | if (isEndpointResolved) {
|
124 | return endpoint;
|
125 | }
|
126 |
|
127 | var usedParams = [];
|
128 | var paramEndpoint = endpoint.replace(/{(\w+)}/g, function (match, key) {
|
129 | usedParams.push(key);
|
130 | return _lodash2.default.get(params, [key], '');
|
131 | });
|
132 |
|
133 | var unusedQueryParams = _lodash2.default.omit(params, usedParams);
|
134 |
|
135 | var paramEndpointUri = new _urijs2.default(paramEndpoint);
|
136 |
|
137 | var resolvedQueryParams = _extends({}, paramEndpointUri.query(true), unusedQueryParams);
|
138 |
|
139 | return paramEndpointUri.query(resolvedQueryParams).toString();
|
140 | }
|
141 |
|
142 | function resolveSchemaName(reference, schema) {
|
143 | var referenceSchema = _lodash2.default.get((0, _status.getStatus)(reference), 'schema');
|
144 |
|
145 | var isReferenceSchemaValid = !_lodash2.default.isEmpty(referenceSchema) && _lodash2.default.isString(referenceSchema);
|
146 | var isArgumentSchemaValid = !_lodash2.default.isEmpty(schema) && _lodash2.default.isString(schema);
|
147 |
|
148 | if (isReferenceSchemaValid && isArgumentSchemaValid) {
|
149 |
|
150 | console.warn('getCollection or getOne gets both reference schema (' + referenceSchema + ')' + (' and argument schema (' + schema + '). Reference schema has priority') + ' over schema argument.');
|
151 | }
|
152 | if (!isReferenceSchemaValid && !isArgumentSchemaValid) {
|
153 | throw new Error('Missing schema name in getCollection or getOne function. Schema needs to' + ' be defined in reference or as argument.');
|
154 | }
|
155 |
|
156 | if (isReferenceSchemaValid) {
|
157 | return referenceSchema;
|
158 | }
|
159 | return schema;
|
160 | } |
\ | No newline at end of file |