-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoJSONSchema.js
146 lines (128 loc) · 3.74 KB
/
toJSONSchema.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const toJSONSchema = (() => {
const types = {
object: 'object',
array: 'array',
string: 'string',
boolean: 'boolean',
number: 'number',
null: 'null',
};
const immutableTypes = {
map: '@@__IMMUTABLE_MAP__@@',
list: '@@__IMMUTABLE_LIST__@@',
set: '@@__IMMUTABLE_SET__@@',
};
function traverse(stateNode) {
const nodeIsImmutable = isImmutable(stateNode);
const nodeValue = nodeIsImmutable
? stateNode.toJS()
: stateNode;
const schema = {
type: getType(nodeValue),
isImmutable: nodeIsImmutable,
};
if (schema.isImmutable) {
schema.immutableType = getImmutableType(stateNode);
}
switch (schema.type) {
case types.array:
const item = nodeIsImmutable ? stateNode.get(0) : nodeValue[0];
schema.items = traverse(item);
break;
case types.object:
const entries = nodeIsImmutable
? [...stateNode.entries()]
: Object.entries(nodeValue);
schema.properties = entries.reduce((props, [key, val]) => {
return {
...props,
[key]: traverse(val),
};
}, {});
break;
}
return schema;
}
function isImmutable(node) {
if (!node) {
return false;
}
return Object.values(immutableTypes).some(t => !!node[t]);
}
function getImmutableType(node) {
const type = !!node['@@__IMMUTABLE_ORDERED__@@'] ? 'ordered_' : '';
const typeEntries = Object.entries(immutableTypes);
for (let i = 0; i < typeEntries.length; i++) {
const [key, value] = typeEntries[i];
if (node[value]) {
return type + key;
}
}
return type;
}
function getType(node) {
const primitiveType = typeof node;
if (primitiveType === 'object') {
return getObjectType(node);
}
if (primitiveType === 'undefined') {
return types.null;
}
return primitiveType;
}
function getObjectType(node) {
if (node === null) {
return types.null;
}
if (Array.isArray(node)) {
return types.array;
}
return types.object;
}
function download(name, content) {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(content));
element.setAttribute('download', `${name}.json`);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function getDefaultStoreState() {
if (elementHasStore(window.$r)) {
return window.$r.props.store.getState();
}
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
if (devTools && devTools.reactDevtoolsAgent) {
const elements = [...devTools.reactDevtoolsAgent.elementData.values()];
for (let i = 0; i < elements.length; i++) {
const currentElement = elements[i];
if (elementHasStore(currentElement)) {
return currentElement.props.store.getState();
}
}
}
return null;
}
function elementHasStore(element) {
return (
element &&
element.props &&
element.props.store &&
element.props.store.getState
);
}
return (appName, stateObject = getDefaultStoreState()) => {
if (!stateObject || typeof stateObject !== 'object') {
console.error('State Object must be typeof object.');
return null;
}
const schema = {
'$schema': 'http://json-schema.org/draft-06/schema#',
title: appName,
...traverse(stateObject),
};
download(appName, JSON.stringify(schema, undefined, 2));
return schema;
};
})();