UNPKG

5.07 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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
9exports.validateSchemaConfig = validateSchemaConfig;
10exports.resolveConfig = resolveConfig;
11exports.buildEndpoint = buildEndpoint;
12exports.resolveSchemaName = resolveSchemaName;
13
14var _lodash = require('lodash');
15
16var _lodash2 = _interopRequireDefault(_lodash);
17
18var _zSchema = require('z-schema');
19
20var _zSchema2 = _interopRequireDefault(_zSchema);
21
22var _urijs = require('urijs');
23
24var _urijs2 = _interopRequireDefault(_urijs);
25
26var _status = require('./status');
27
28var _consts = require('./consts');
29
30var _rio = require('./rio');
31
32var _rio2 = _interopRequireDefault(_rio);
33
34function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
36var validator = new _zSchema2.default();
37var 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 * Validates schema configuration using AJV library and json-schema convention.
78 * Returns useful errors to enable developer easier fixing invalid schema configuration.
79 * @param config
80 */
81function 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 * Encapsulates additional logic around rio.resolveSchema. In case schema argument is object,
92 * function merges argument configuration with registered configuration in rio. Allowing
93 * overriding base configuration.
94 * @param schema
95 * @returns config
96 */
97function 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 * Replace endpoint placeholders '{key}' with corresponding value of key in params dict.
112 * Unused params are resolved into query params as 'key=value' pairs and concatenated to endpoint
113 * @param endpoint
114 * @param params
115 * @param options
116 * @returns {string}
117 */
118function 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
142function 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 // eslint-disable-next-line no-console
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