-
Notifications
You must be signed in to change notification settings - Fork 3
/
definitionReducer.js
119 lines (104 loc) · 3.22 KB
/
definitionReducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import _ from 'lodash';
import { SwaggerLoadingStatus, ActionType } from '../constants/constants';
import { resolveAllOf } from '../helpers/allOfResolver';
const initialState = {
store: {},
tags: [],
entrypoints: [],
swaggerLoadingStatus: SwaggerLoadingStatus.INITIAL,
};
function extractTags(definedTags, entrypoints) {
return _.uniqBy(
[
...definedTags,
..._.flatMap(entrypoints, entrypoint =>
_.map(entrypoint.operation.tags, tag => ({
name: tag,
description: '',
}))),
],
'name');
}
function extractEntrypoints(pathsObject) {
return _.flatMap(pathsObject, (pathObject, path) =>
_.map(pathObject, (operationObject, method) => ({
path, method, operation: operationObject,
})));
}
function defineSchema(schema, definitions) {
if (!schema) {
return schema;
}
if (schema.$ref) {
return defineSchema(definitions(schema.$ref), definitions);
}
switch (schema.type) {
case 'object':
return {
...schema,
properties: _.mapValues(schema.properties, property => defineSchema(property, definitions)),
};
case 'array':
return {
...schema,
items: defineSchema(schema.items, definitions),
};
default:
return schema;
}
}
function defineEntrypoints(entrypoints, getDefinition) {
return entrypoints.map(entrypoint => ({
...entrypoint,
operation: {
...entrypoint.operation,
parameters: _.map(entrypoint.operation.parameters, param => {
if (param.$ref) {
return getDefinition(param.$ref);
}
return { ...param, schema: defineSchema(param.schema, getDefinition) };
}),
responses: _.mapValues(entrypoint.operation.responses, responseObject => ({
...responseObject,
schema: defineSchema(responseObject.schema, getDefinition),
})),
},
}));
}
function getDefinition(swaggerDefinitions) {
return (key) => {
if (key.startsWith('#/definitions/')) {
const definitionKey = _.replace(key, '#/definitions/', '');
return swaggerDefinitions[definitionKey];
}
return undefined;
};
}
export default function definitionReducer(state = initialState, action) {
const newState = { ...state };
switch (action.type) {
case ActionType.FETCH_DEFINITION_SUCCESS:
const swaggerDefinition = action.definition;
const swaggerDefinitions = resolveAllOf(swaggerDefinition.definitions);
const swaggerPaths = _.get(swaggerDefinition, 'paths', {});
const swaggerTags = _.get(swaggerDefinition, 'tags', []);
const swaggerEntrypoints = extractEntrypoints(swaggerPaths);
const entrypoints = defineEntrypoints(
swaggerEntrypoints,
getDefinition(swaggerDefinitions)
);
const tags = extractTags(swaggerTags, entrypoints);
newState.store = swaggerDefinition;
newState.entrypoints = entrypoints;
newState.tags = tags;
newState.swaggerLoadingStatus = SwaggerLoadingStatus.LOADING_COMPLETED;
break;
case ActionType.FETCH_DEFINITION_FAILURE:
newState.swaggerLoadingStatus = SwaggerLoadingStatus.LOADING_FAILED;
newState.swaggerLoadingError = action.error;
break;
default:
break;
}
return newState;
}