@stoplight/elements 9.0.12-beta-0.1 → 9.0.12-beta-0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE +190 -0
- package/dist/README.md +19 -0
- package/dist/__fixtures__/api-descriptions/Instagram.d.ts +1547 -0
- package/dist/__fixtures__/api-descriptions/badgesForSchema.d.ts +1 -0
- package/dist/__fixtures__/api-descriptions/simpleApiWithInternalOperations.d.ts +224 -0
- package/dist/__fixtures__/api-descriptions/simpleApiWithoutDescription.d.ts +212 -0
- package/dist/__fixtures__/api-descriptions/todosApiBundled.d.ts +1 -0
- package/dist/__fixtures__/api-descriptions/zoomApiYaml.d.ts +1 -0
- package/dist/components/API/APIWithResponsiveSidebarLayout.d.ts +25 -0
- package/dist/components/API/APIWithSidebarLayout.d.ts +32 -0
- package/dist/components/API/APIWithStackedLayout.d.ts +29 -0
- package/dist/components/API/utils.d.ts +20 -0
- package/dist/containers/API.d.ts +30 -0
- package/dist/containers/API.spec.d.ts +1 -0
- package/dist/containers/API.stories.d.ts +58 -0
- package/dist/containers/story-helper.d.ts +2 -0
- package/dist/hooks/useExportDocumentProps.d.ts +11 -0
- package/dist/hooks/useExportDocumentProps.spec.d.ts +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.esm.js +657 -0
- package/dist/index.js +681 -0
- package/dist/index.mjs +657 -0
- package/dist/package.json +52 -0
- package/dist/styles.min.css +1 -0
- package/dist/utils/oas/index.d.ts +3 -0
- package/dist/utils/oas/oas2.d.ts +2 -0
- package/dist/utils/oas/oas3.d.ts +2 -0
- package/dist/utils/oas/types.d.ts +33 -0
- package/dist/web-components/components.d.ts +1 -0
- package/dist/web-components/index.d.ts +1 -0
- package/dist/web-components.min.js +2 -0
- package/dist/web-components.min.js.LICENSE.txt +176 -0
- package/package.json +2 -2
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
import { isHttpOperation, isHttpWebhookOperation, isHttpService, resolveUrl, HttpMethodColors, DeprecatedBadge, ParsedDocs, TryItWithRequestSamples, Docs, resolveRelativeLink, ResponsiveSidebarLayout, ElementsOptionsProvider, SidebarLayout, Logo, TableOfContents, PoweredByLink, slugify, withRouter, withStyles, withPersistenceBoundary, withMosaicProvider, withQueryClientProvider, useResponsiveLayout, useParsedValue, useBundleRefsIntoDocument, NonIdealState, InlineRefResolverProvider } from '@stoplight/elements-core';
|
|
2
|
+
import { Box, Flex, Icon, Tabs, TabList, Tab, TabPanels, TabPanel, Heading } from '@stoplight/mosaic';
|
|
3
|
+
import { NodeType } from '@stoplight/types';
|
|
4
|
+
import cn from 'classnames';
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
import defaults from 'lodash/defaults.js';
|
|
7
|
+
import flow from 'lodash/flow.js';
|
|
8
|
+
import { useQuery } from 'react-query';
|
|
9
|
+
import { useLocation, Navigate, Link } from 'react-router-dom';
|
|
10
|
+
import { safeStringify } from '@stoplight/yaml';
|
|
11
|
+
import saver from 'file-saver';
|
|
12
|
+
import { OPERATION_CONFIG, WEBHOOK_CONFIG } from '@stoplight/http-spec/oas';
|
|
13
|
+
import { transformOas2Service, transformOas2Operation } from '@stoplight/http-spec/oas2';
|
|
14
|
+
import { transformOas3Service, transformOas3Operation } from '@stoplight/http-spec/oas3';
|
|
15
|
+
import { encodePointerFragment, pointerToPath } from '@stoplight/json';
|
|
16
|
+
import get from 'lodash/get.js';
|
|
17
|
+
import isObject from 'lodash/isObject.js';
|
|
18
|
+
import last from 'lodash/last.js';
|
|
19
|
+
|
|
20
|
+
function computeTagGroups(serviceNode, nodeType) {
|
|
21
|
+
const groupsByTagId = {};
|
|
22
|
+
const ungrouped = [];
|
|
23
|
+
const lowerCaseServiceTags = serviceNode.tags.map(tn => tn.toLowerCase());
|
|
24
|
+
const groupableNodes = serviceNode.children.filter(n => n.type === nodeType);
|
|
25
|
+
for (const node of groupableNodes) {
|
|
26
|
+
for (const tagName of node.tags) {
|
|
27
|
+
const tagId = tagName.toLowerCase();
|
|
28
|
+
if (groupsByTagId[tagId]) {
|
|
29
|
+
groupsByTagId[tagId].items.push(node);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const serviceTagIndex = lowerCaseServiceTags.findIndex(tn => tn === tagId);
|
|
33
|
+
const serviceTagName = serviceNode.tags[serviceTagIndex];
|
|
34
|
+
groupsByTagId[tagId] = {
|
|
35
|
+
title: serviceTagName || tagName,
|
|
36
|
+
items: [node],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (node.tags.length === 0) {
|
|
41
|
+
ungrouped.push(node);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const orderedTagGroups = Object.entries(groupsByTagId)
|
|
45
|
+
.sort(([g1], [g2]) => {
|
|
46
|
+
const g1LC = g1.toLowerCase();
|
|
47
|
+
const g2LC = g2.toLowerCase();
|
|
48
|
+
const g1Idx = lowerCaseServiceTags.findIndex(tn => tn === g1LC);
|
|
49
|
+
const g2Idx = lowerCaseServiceTags.findIndex(tn => tn === g2LC);
|
|
50
|
+
if (g1Idx < 0 && g2Idx < 0)
|
|
51
|
+
return 0;
|
|
52
|
+
if (g1Idx < 0)
|
|
53
|
+
return 1;
|
|
54
|
+
if (g2Idx < 0)
|
|
55
|
+
return -1;
|
|
56
|
+
return g1Idx - g2Idx;
|
|
57
|
+
})
|
|
58
|
+
.map(([, tagGroup]) => tagGroup);
|
|
59
|
+
return { groups: orderedTagGroups, ungrouped };
|
|
60
|
+
}
|
|
61
|
+
const defaultComputerAPITreeConfig = {
|
|
62
|
+
hideSchemas: false,
|
|
63
|
+
hideInternal: false,
|
|
64
|
+
};
|
|
65
|
+
const computeAPITree = (serviceNode, config = {}) => {
|
|
66
|
+
const mergedConfig = defaults(config, defaultComputerAPITreeConfig);
|
|
67
|
+
const tree = [];
|
|
68
|
+
tree.push({
|
|
69
|
+
id: '/',
|
|
70
|
+
slug: '/',
|
|
71
|
+
title: 'Overview',
|
|
72
|
+
type: 'overview',
|
|
73
|
+
meta: '',
|
|
74
|
+
});
|
|
75
|
+
const hasOperationNodes = serviceNode.children.some(node => node.type === NodeType.HttpOperation);
|
|
76
|
+
if (hasOperationNodes) {
|
|
77
|
+
tree.push({
|
|
78
|
+
title: 'Endpoints',
|
|
79
|
+
});
|
|
80
|
+
const { groups, ungrouped } = computeTagGroups(serviceNode, NodeType.HttpOperation);
|
|
81
|
+
addTagGroupsToTree(groups, ungrouped, tree, NodeType.HttpOperation, mergedConfig.hideInternal);
|
|
82
|
+
}
|
|
83
|
+
const hasWebhookNodes = serviceNode.children.some(node => node.type === NodeType.HttpWebhook);
|
|
84
|
+
if (hasWebhookNodes) {
|
|
85
|
+
tree.push({
|
|
86
|
+
title: 'Webhooks',
|
|
87
|
+
});
|
|
88
|
+
const { groups, ungrouped } = computeTagGroups(serviceNode, NodeType.HttpWebhook);
|
|
89
|
+
addTagGroupsToTree(groups, ungrouped, tree, NodeType.HttpWebhook, mergedConfig.hideInternal);
|
|
90
|
+
}
|
|
91
|
+
let schemaNodes = serviceNode.children.filter(node => node.type === NodeType.Model);
|
|
92
|
+
if (mergedConfig.hideInternal) {
|
|
93
|
+
schemaNodes = schemaNodes.filter(n => !isInternal(n));
|
|
94
|
+
}
|
|
95
|
+
if (!mergedConfig.hideSchemas && schemaNodes.length) {
|
|
96
|
+
tree.push({
|
|
97
|
+
title: 'Schemas',
|
|
98
|
+
});
|
|
99
|
+
const { groups, ungrouped } = computeTagGroups(serviceNode, NodeType.Model);
|
|
100
|
+
addTagGroupsToTree(groups, ungrouped, tree, NodeType.Model, mergedConfig.hideInternal);
|
|
101
|
+
}
|
|
102
|
+
return tree;
|
|
103
|
+
};
|
|
104
|
+
const findFirstNodeSlug = (tree) => {
|
|
105
|
+
for (const item of tree) {
|
|
106
|
+
if ('slug' in item) {
|
|
107
|
+
return item.slug;
|
|
108
|
+
}
|
|
109
|
+
if ('items' in item) {
|
|
110
|
+
const slug = findFirstNodeSlug(item.items);
|
|
111
|
+
if (slug) {
|
|
112
|
+
return slug;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
};
|
|
118
|
+
const isInternal = (node) => {
|
|
119
|
+
const data = node.data;
|
|
120
|
+
if (isHttpOperation(data) || isHttpWebhookOperation(data)) {
|
|
121
|
+
return !!data.internal;
|
|
122
|
+
}
|
|
123
|
+
if (isHttpService(data)) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return !!data['x-internal'];
|
|
127
|
+
};
|
|
128
|
+
const addTagGroupsToTree = (groups, ungrouped, tree, itemsType, hideInternal) => {
|
|
129
|
+
ungrouped.forEach(node => {
|
|
130
|
+
if (hideInternal && isInternal(node)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
tree.push({
|
|
134
|
+
id: node.uri,
|
|
135
|
+
slug: node.uri,
|
|
136
|
+
title: node.name,
|
|
137
|
+
type: node.type,
|
|
138
|
+
meta: isHttpOperation(node.data) || isHttpWebhookOperation(node.data) ? node.data.method : '',
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
groups.forEach(group => {
|
|
142
|
+
const items = group.items.flatMap(node => {
|
|
143
|
+
if (hideInternal && isInternal(node)) {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
id: node.uri,
|
|
148
|
+
slug: node.uri,
|
|
149
|
+
title: node.name,
|
|
150
|
+
type: node.type,
|
|
151
|
+
meta: isHttpOperation(node.data) || isHttpWebhookOperation(node.data) ? node.data.method : '',
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
if (items.length > 0) {
|
|
155
|
+
tree.push({
|
|
156
|
+
title: group.title,
|
|
157
|
+
items,
|
|
158
|
+
itemsType,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
const resolveRelativePath = (currentPath, basePath, outerRouter) => {
|
|
164
|
+
if (!outerRouter || !basePath || basePath === '/') {
|
|
165
|
+
return currentPath;
|
|
166
|
+
}
|
|
167
|
+
const baseUrl = resolveUrl(basePath);
|
|
168
|
+
const currentUrl = resolveUrl(currentPath);
|
|
169
|
+
return baseUrl && currentUrl && baseUrl !== currentUrl ? currentUrl.replace(baseUrl, '') : '/';
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const itemMatchesHash = (hash, item) => {
|
|
173
|
+
if (item.type === NodeType.HttpOperation) {
|
|
174
|
+
return hash.substr(1) === `${item.data.path}-${item.data.method}`;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
return hash.substr(1) === `${item.data.name}-${item.data.method}`;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const TryItContext = React.createContext({
|
|
181
|
+
hideTryIt: false,
|
|
182
|
+
hideTryItPanel: false,
|
|
183
|
+
hideSamples: false,
|
|
184
|
+
tryItCredentialsPolicy: 'omit',
|
|
185
|
+
});
|
|
186
|
+
TryItContext.displayName = 'TryItContext';
|
|
187
|
+
const LocationContext = React.createContext({
|
|
188
|
+
location: {
|
|
189
|
+
hash: '',
|
|
190
|
+
key: '',
|
|
191
|
+
pathname: '',
|
|
192
|
+
search: '',
|
|
193
|
+
state: '',
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
LocationContext.displayName = 'LocationContext';
|
|
197
|
+
const APIWithStackedLayout = ({ serviceNode, hideTryItPanel, hideTryIt, hideSamples, hideExport, hideSecurityInfo, hideServerInfo, exportProps, tryItCredentialsPolicy, tryItCorsProxy, renderExtensionAddon, showPoweredByLink = true, location, }) => {
|
|
198
|
+
const { groups: operationGroups } = computeTagGroups(serviceNode, NodeType.HttpOperation);
|
|
199
|
+
const { groups: webhookGroups } = computeTagGroups(serviceNode, NodeType.HttpWebhook);
|
|
200
|
+
return (React.createElement(LocationContext.Provider, { value: { location } },
|
|
201
|
+
React.createElement(TryItContext.Provider, { value: { hideTryItPanel, hideTryIt, hideSamples, tryItCredentialsPolicy, corsProxy: tryItCorsProxy } },
|
|
202
|
+
React.createElement(Flex, { w: "full", flexDirection: "col", m: "auto", className: "sl-max-w-4xl" },
|
|
203
|
+
React.createElement(Box, { w: "full", borderB: true },
|
|
204
|
+
React.createElement(Docs, { className: "sl-mx-auto", nodeData: serviceNode.data, nodeTitle: serviceNode.name, nodeType: NodeType.HttpService, location: location, layoutOptions: { showPoweredByLink, hideExport, hideSecurityInfo, hideServerInfo }, exportProps: exportProps, tryItCredentialsPolicy: tryItCredentialsPolicy, renderExtensionAddon: renderExtensionAddon })),
|
|
205
|
+
operationGroups.length > 0 && webhookGroups.length > 0 ? React.createElement(Heading, { size: 2 }, "Endpoints") : null,
|
|
206
|
+
operationGroups.map(group => (React.createElement(Group, { key: group.title, group: group }))),
|
|
207
|
+
webhookGroups.length > 0 ? React.createElement(Heading, { size: 2 }, "Webhooks") : null,
|
|
208
|
+
webhookGroups.map(group => (React.createElement(Group, { key: group.title, group: group })))))));
|
|
209
|
+
};
|
|
210
|
+
APIWithStackedLayout.displayName = 'APIWithStackedLayout';
|
|
211
|
+
const Group = React.memo(({ group }) => {
|
|
212
|
+
const [isExpanded, setIsExpanded] = React.useState(false);
|
|
213
|
+
const scrollRef = React.useRef(null);
|
|
214
|
+
const { location: { hash }, } = React.useContext(LocationContext);
|
|
215
|
+
const urlHashMatches = hash.substr(1) === group.title;
|
|
216
|
+
const onClick = React.useCallback(() => setIsExpanded(!isExpanded), [isExpanded]);
|
|
217
|
+
const shouldExpand = React.useMemo(() => {
|
|
218
|
+
return urlHashMatches || group.items.some(item => itemMatchesHash(hash, item));
|
|
219
|
+
}, [group, hash, urlHashMatches]);
|
|
220
|
+
React.useEffect(() => {
|
|
221
|
+
var _a;
|
|
222
|
+
if (shouldExpand) {
|
|
223
|
+
setIsExpanded(true);
|
|
224
|
+
if (urlHashMatches && ((_a = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _a === void 0 ? void 0 : _a.offsetTop)) {
|
|
225
|
+
window.scrollTo(0, scrollRef.current.offsetTop);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}, [shouldExpand, urlHashMatches, group, hash]);
|
|
229
|
+
return (React.createElement(Box, null,
|
|
230
|
+
React.createElement(Flex, { ref: scrollRef, onClick: onClick, mx: "auto", justifyContent: "between", alignItems: "center", borderB: true, px: 2, py: 4, cursor: "pointer", color: { default: 'current', hover: 'muted' } },
|
|
231
|
+
React.createElement(Box, { fontSize: "lg", fontWeight: "medium" }, group.title),
|
|
232
|
+
React.createElement(Icon, { className: "sl-mr-2", icon: isExpanded ? 'chevron-down' : 'chevron-right', size: "sm" })),
|
|
233
|
+
React.createElement(Collapse, { isOpen: isExpanded }, group.items.map(item => {
|
|
234
|
+
return React.createElement(Item, { key: item.uri, item: item });
|
|
235
|
+
}))));
|
|
236
|
+
});
|
|
237
|
+
Group.displayName = 'Group';
|
|
238
|
+
const Item = React.memo(({ item }) => {
|
|
239
|
+
const { location } = React.useContext(LocationContext);
|
|
240
|
+
const { hash } = location;
|
|
241
|
+
const [isExpanded, setIsExpanded] = React.useState(false);
|
|
242
|
+
const scrollRef = React.useRef(null);
|
|
243
|
+
const color = HttpMethodColors[item.data.method] || 'gray';
|
|
244
|
+
const isDeprecated = !!item.data.deprecated;
|
|
245
|
+
const { hideTryIt, hideSamples, hideTryItPanel, tryItCredentialsPolicy, corsProxy } = React.useContext(TryItContext);
|
|
246
|
+
const onClick = React.useCallback(() => setIsExpanded(!isExpanded), [isExpanded]);
|
|
247
|
+
React.useEffect(() => {
|
|
248
|
+
var _a;
|
|
249
|
+
if (itemMatchesHash(hash, item)) {
|
|
250
|
+
setIsExpanded(true);
|
|
251
|
+
if ((_a = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _a === void 0 ? void 0 : _a.offsetTop) {
|
|
252
|
+
window.scrollTo(0, scrollRef.current.offsetTop);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}, [hash, item]);
|
|
256
|
+
return (React.createElement(Box, { ref: scrollRef, w: "full", my: 2, border: true, borderColor: { default: isExpanded ? 'light' : 'transparent', hover: 'light' }, bg: { default: isExpanded ? 'code' : 'transparent', hover: 'code' } },
|
|
257
|
+
React.createElement(Flex, { mx: "auto", alignItems: "center", cursor: "pointer", fontSize: "lg", p: 2, onClick: onClick, color: "current" },
|
|
258
|
+
React.createElement(Box, { w: 24, textTransform: "uppercase", textAlign: "center", fontWeight: "semibold", border: true, rounded: true, px: 2, bg: "canvas", className: cn(`sl-mr-5 sl-text-base`, `sl-text-${color}`, `sl-border-${color}`) }, item.data.method || 'UNKNOWN'),
|
|
259
|
+
React.createElement(Box, { flex: 1, fontWeight: "medium", wordBreak: "all" }, item.type === NodeType.HttpOperation ? item.data.path : item.name),
|
|
260
|
+
isDeprecated && React.createElement(DeprecatedBadge, null)),
|
|
261
|
+
React.createElement(Collapse, { isOpen: isExpanded },
|
|
262
|
+
React.createElement(Box, { flex: 1, p: 2, fontWeight: "medium", mx: "auto", fontSize: "xl" }, item.name),
|
|
263
|
+
hideTryItPanel ? (React.createElement(Box, { as: ParsedDocs, layoutOptions: { noHeading: true, hideTryItPanel: true, hideSamples, hideTryIt }, node: item, p: 4 })) : (React.createElement(Tabs, { appearance: "line" },
|
|
264
|
+
React.createElement(TabList, null,
|
|
265
|
+
React.createElement(Tab, null, "Docs"),
|
|
266
|
+
React.createElement(Tab, null, "TryIt")),
|
|
267
|
+
React.createElement(TabPanels, null,
|
|
268
|
+
React.createElement(TabPanel, null,
|
|
269
|
+
React.createElement(ParsedDocs, { className: "sl-px-4", node: item, location: location, layoutOptions: { noHeading: true, hideTryItPanel: false, hideSamples, hideTryIt } })),
|
|
270
|
+
React.createElement(TabPanel, null,
|
|
271
|
+
React.createElement(TryItWithRequestSamples, { httpOperation: item.data, tryItCredentialsPolicy: tryItCredentialsPolicy, corsProxy: corsProxy, hideSamples: hideSamples, hideTryIt: hideTryIt }))))))));
|
|
272
|
+
});
|
|
273
|
+
Item.displayName = 'Item';
|
|
274
|
+
const Collapse = ({ isOpen, children }) => {
|
|
275
|
+
if (!isOpen)
|
|
276
|
+
return null;
|
|
277
|
+
return React.createElement(Box, null, children);
|
|
278
|
+
};
|
|
279
|
+
Collapse.displayName = 'Collapse';
|
|
280
|
+
|
|
281
|
+
const APIWithResponsiveSidebarLayout = ({ serviceNode, logo, hideTryItPanel, hideTryIt, hideSamples, compact, hideSchemas, hideInternal, hideExport, hideServerInfo, hideSecurityInfo, exportProps, tryItCredentialsPolicy, tryItCorsProxy, renderExtensionAddon, basePath = '/', outerRouter = false, }) => {
|
|
282
|
+
const container = React.useRef(null);
|
|
283
|
+
const tree = React.useMemo(() => computeAPITree(serviceNode, { hideSchemas, hideInternal }), [serviceNode, hideSchemas, hideInternal]);
|
|
284
|
+
const location = useLocation();
|
|
285
|
+
const { pathname: currentPath } = location;
|
|
286
|
+
const relativePath = resolveRelativePath(currentPath, basePath, outerRouter);
|
|
287
|
+
const isRootPath = relativePath === '/';
|
|
288
|
+
const node = isRootPath ? serviceNode : serviceNode.children.find(child => child.uri === relativePath);
|
|
289
|
+
const layoutOptions = React.useMemo(() => ({
|
|
290
|
+
hideTryIt: hideTryIt,
|
|
291
|
+
hideTryItPanel,
|
|
292
|
+
hideSamples,
|
|
293
|
+
hideSecurityInfo: hideSecurityInfo,
|
|
294
|
+
hideServerInfo: hideServerInfo,
|
|
295
|
+
compact: compact,
|
|
296
|
+
hideExport: hideExport || (node === null || node === void 0 ? void 0 : node.type) !== NodeType.HttpService,
|
|
297
|
+
}), [hideTryIt, hideSecurityInfo, hideServerInfo, compact, hideExport, hideTryItPanel, hideSamples, node === null || node === void 0 ? void 0 : node.type]);
|
|
298
|
+
if (!node) {
|
|
299
|
+
const firstSlug = findFirstNodeSlug(tree);
|
|
300
|
+
if (firstSlug) {
|
|
301
|
+
return React.createElement(Navigate, { to: resolveRelativeLink(firstSlug), replace: true });
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (hideInternal && node && isInternal(node)) {
|
|
305
|
+
return React.createElement(Navigate, { to: ".", replace: true });
|
|
306
|
+
}
|
|
307
|
+
const handleTocClick = () => {
|
|
308
|
+
if (container.current) {
|
|
309
|
+
container.current.scrollIntoView();
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
return (React.createElement(ResponsiveSidebarLayout, { onTocClick: handleTocClick, tree: tree, logo: logo !== null && logo !== void 0 ? logo : serviceNode.data.logo, ref: container, name: serviceNode.name }, node && (React.createElement(ElementsOptionsProvider, { renderExtensionAddon: renderExtensionAddon },
|
|
313
|
+
React.createElement(ParsedDocs, { key: relativePath, uri: relativePath, node: node, nodeTitle: node.name, layoutOptions: layoutOptions, location: location, exportProps: exportProps, tryItCredentialsPolicy: tryItCredentialsPolicy, tryItCorsProxy: tryItCorsProxy, renderExtensionAddon: renderExtensionAddon })))));
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const APIWithSidebarLayout = ({ serviceNode, logo, hideTryItPanel, hideTryIt, hideSamples, hideSchemas, hideSecurityInfo, hideServerInfo, hideInternal, hideExport, exportProps, tryItCredentialsPolicy, tryItCorsProxy, renderExtensionAddon, basePath = '/', outerRouter = false, }) => {
|
|
317
|
+
const container = React.useRef(null);
|
|
318
|
+
const tree = React.useMemo(() => computeAPITree(serviceNode, { hideSchemas, hideInternal }), [serviceNode, hideSchemas, hideInternal]);
|
|
319
|
+
const location = useLocation();
|
|
320
|
+
const { pathname: currentPath } = location;
|
|
321
|
+
const relativePath = resolveRelativePath(currentPath, basePath, outerRouter);
|
|
322
|
+
const isRootPath = relativePath === '/';
|
|
323
|
+
const node = isRootPath ? serviceNode : serviceNode.children.find(child => child.uri === relativePath);
|
|
324
|
+
const layoutOptions = React.useMemo(() => ({
|
|
325
|
+
hideTryIt: hideTryIt,
|
|
326
|
+
hideTryItPanel,
|
|
327
|
+
hideSamples,
|
|
328
|
+
hideServerInfo: hideServerInfo,
|
|
329
|
+
hideSecurityInfo: hideSecurityInfo,
|
|
330
|
+
hideExport: hideExport || (node === null || node === void 0 ? void 0 : node.type) !== NodeType.HttpService,
|
|
331
|
+
}), [hideTryIt, hideServerInfo, hideSecurityInfo, hideExport, hideTryItPanel, hideSamples, node === null || node === void 0 ? void 0 : node.type]);
|
|
332
|
+
if (!node) {
|
|
333
|
+
const firstSlug = findFirstNodeSlug(tree);
|
|
334
|
+
if (firstSlug) {
|
|
335
|
+
return React.createElement(Navigate, { to: resolveRelativeLink(firstSlug), replace: true });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (hideInternal && node && isInternal(node)) {
|
|
339
|
+
return React.createElement(Navigate, { to: ".", replace: true });
|
|
340
|
+
}
|
|
341
|
+
const sidebar = (React.createElement(Sidebar, { serviceNode: serviceNode, logo: logo, container: container, pathname: relativePath, tree: tree }));
|
|
342
|
+
return (React.createElement(SidebarLayout, { ref: container, sidebar: sidebar }, node && (React.createElement(ElementsOptionsProvider, { renderExtensionAddon: renderExtensionAddon },
|
|
343
|
+
React.createElement(ParsedDocs, { key: relativePath, uri: relativePath, node: node, nodeTitle: node.name, layoutOptions: layoutOptions, location: location, exportProps: exportProps, tryItCredentialsPolicy: tryItCredentialsPolicy, tryItCorsProxy: tryItCorsProxy, renderExtensionAddon: renderExtensionAddon })))));
|
|
344
|
+
};
|
|
345
|
+
const Sidebar = ({ serviceNode, logo, container, pathname, tree }) => {
|
|
346
|
+
const handleTocClick = () => {
|
|
347
|
+
if (container.current) {
|
|
348
|
+
container.current.scrollIntoView();
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
return (React.createElement(React.Fragment, null,
|
|
352
|
+
React.createElement(Flex, { ml: 4, mb: 5, alignItems: "center" },
|
|
353
|
+
logo ? (React.createElement(Logo, { logo: { url: logo, altText: 'logo' } })) : (serviceNode.data.logo && React.createElement(Logo, { logo: serviceNode.data.logo })),
|
|
354
|
+
React.createElement(Heading, { size: 4 }, serviceNode.name)),
|
|
355
|
+
React.createElement(Flex, { flexGrow: true, flexShrink: true, overflowY: "auto", direction: "col" },
|
|
356
|
+
React.createElement(TableOfContents, { tree: tree, activeId: pathname, Link: Link, onLinkClick: handleTocClick })),
|
|
357
|
+
React.createElement(PoweredByLink, { source: serviceNode.name, pathname: pathname, packageType: "elements" })));
|
|
358
|
+
};
|
|
359
|
+
Sidebar.displayName = 'Sidebar';
|
|
360
|
+
|
|
361
|
+
var NodeTypes;
|
|
362
|
+
(function (NodeTypes) {
|
|
363
|
+
NodeTypes["Paths"] = "paths";
|
|
364
|
+
NodeTypes["Path"] = "path";
|
|
365
|
+
NodeTypes["Operation"] = "operation";
|
|
366
|
+
NodeTypes["Webhooks"] = "webhooks";
|
|
367
|
+
NodeTypes["Webhook"] = "webhook";
|
|
368
|
+
NodeTypes["Components"] = "components";
|
|
369
|
+
NodeTypes["Models"] = "models";
|
|
370
|
+
NodeTypes["Model"] = "model";
|
|
371
|
+
})(NodeTypes || (NodeTypes = {}));
|
|
372
|
+
|
|
373
|
+
const oas2SourceMap = [
|
|
374
|
+
{
|
|
375
|
+
match: 'paths',
|
|
376
|
+
type: NodeTypes.Paths,
|
|
377
|
+
children: [
|
|
378
|
+
{
|
|
379
|
+
notMatch: '^x-',
|
|
380
|
+
type: NodeTypes.Path,
|
|
381
|
+
children: [
|
|
382
|
+
{
|
|
383
|
+
match: 'get|post|put|delete|options|head|patch|trace',
|
|
384
|
+
type: NodeTypes.Operation,
|
|
385
|
+
},
|
|
386
|
+
],
|
|
387
|
+
},
|
|
388
|
+
],
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
match: 'definitions',
|
|
392
|
+
type: NodeTypes.Models,
|
|
393
|
+
children: [
|
|
394
|
+
{
|
|
395
|
+
notMatch: '^x-',
|
|
396
|
+
type: NodeTypes.Model,
|
|
397
|
+
},
|
|
398
|
+
],
|
|
399
|
+
},
|
|
400
|
+
];
|
|
401
|
+
|
|
402
|
+
const oas3SourceMap = [
|
|
403
|
+
{
|
|
404
|
+
match: 'paths',
|
|
405
|
+
type: NodeTypes.Paths,
|
|
406
|
+
children: [
|
|
407
|
+
{
|
|
408
|
+
notMatch: '^x-',
|
|
409
|
+
type: NodeTypes.Path,
|
|
410
|
+
children: [
|
|
411
|
+
{
|
|
412
|
+
match: 'get|post|put|delete|options|head|patch|trace',
|
|
413
|
+
type: NodeTypes.Operation,
|
|
414
|
+
},
|
|
415
|
+
],
|
|
416
|
+
},
|
|
417
|
+
],
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
match: 'webhooks',
|
|
421
|
+
type: NodeTypes.Webhooks,
|
|
422
|
+
children: [
|
|
423
|
+
{
|
|
424
|
+
notMatch: '^x-',
|
|
425
|
+
type: NodeTypes.Webhook,
|
|
426
|
+
children: [
|
|
427
|
+
{
|
|
428
|
+
match: 'get|post|put|delete|options|head|patch|trace',
|
|
429
|
+
type: NodeTypes.Webhook,
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
},
|
|
433
|
+
],
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
match: 'components',
|
|
437
|
+
type: NodeTypes.Components,
|
|
438
|
+
children: [
|
|
439
|
+
{
|
|
440
|
+
match: 'schemas',
|
|
441
|
+
type: NodeTypes.Models,
|
|
442
|
+
children: [
|
|
443
|
+
{
|
|
444
|
+
notMatch: '^x-',
|
|
445
|
+
type: NodeTypes.Model,
|
|
446
|
+
},
|
|
447
|
+
],
|
|
448
|
+
},
|
|
449
|
+
],
|
|
450
|
+
},
|
|
451
|
+
];
|
|
452
|
+
|
|
453
|
+
const isOas2 = (parsed) => isObject(parsed) &&
|
|
454
|
+
'swagger' in parsed &&
|
|
455
|
+
Number.parseInt(String(parsed.swagger)) === 2;
|
|
456
|
+
const isOas3 = (parsed) => isObject(parsed) &&
|
|
457
|
+
'openapi' in parsed &&
|
|
458
|
+
Number.parseFloat(String(parsed.openapi)) >= 3;
|
|
459
|
+
const isOas31 = (parsed) => isObject(parsed) &&
|
|
460
|
+
'openapi' in parsed &&
|
|
461
|
+
Number.parseFloat(String(parsed.openapi)) === 3.1;
|
|
462
|
+
const OAS_MODEL_REGEXP = /((definitions|components)\/?(schemas)?)\//;
|
|
463
|
+
function transformOasToServiceNode(apiDescriptionDocument) {
|
|
464
|
+
if (isOas31(apiDescriptionDocument)) {
|
|
465
|
+
return computeServiceNode(Object.assign(Object.assign({}, apiDescriptionDocument), { jsonSchemaDialect: 'http://json-schema.org/draft-07/schema#' }), oas3SourceMap, transformOas3Service, transformOas3Operation);
|
|
466
|
+
}
|
|
467
|
+
if (isOas3(apiDescriptionDocument)) {
|
|
468
|
+
return computeServiceNode(apiDescriptionDocument, oas3SourceMap, transformOas3Service, transformOas3Operation);
|
|
469
|
+
}
|
|
470
|
+
else if (isOas2(apiDescriptionDocument)) {
|
|
471
|
+
return computeServiceNode(apiDescriptionDocument, oas2SourceMap, transformOas2Service, transformOas2Operation);
|
|
472
|
+
}
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
function computeServiceNode(document, map, transformService, transformOperation) {
|
|
476
|
+
var _a;
|
|
477
|
+
const serviceDocument = transformService({ document });
|
|
478
|
+
const serviceNode = {
|
|
479
|
+
type: NodeType.HttpService,
|
|
480
|
+
uri: '/',
|
|
481
|
+
name: serviceDocument.name,
|
|
482
|
+
data: serviceDocument,
|
|
483
|
+
tags: ((_a = serviceDocument.tags) === null || _a === void 0 ? void 0 : _a.map(tag => tag.name)) || [],
|
|
484
|
+
children: computeChildNodes(document, document, map, transformOperation),
|
|
485
|
+
};
|
|
486
|
+
return serviceNode;
|
|
487
|
+
}
|
|
488
|
+
function computeChildNodes(document, data, map, transformer, parentUri = '') {
|
|
489
|
+
var _a, _b;
|
|
490
|
+
const nodes = [];
|
|
491
|
+
if (!isObject(data))
|
|
492
|
+
return nodes;
|
|
493
|
+
for (const [key, value] of Object.entries(data)) {
|
|
494
|
+
const sanitizedKey = encodePointerFragment(key);
|
|
495
|
+
const match = findMapMatch(sanitizedKey, map);
|
|
496
|
+
if (match) {
|
|
497
|
+
const uri = `${parentUri}/${sanitizedKey}`;
|
|
498
|
+
const jsonPath = pointerToPath(`#${uri}`);
|
|
499
|
+
if (match.type === NodeTypes.Operation && jsonPath.length === 3) {
|
|
500
|
+
const path = String(jsonPath[1]);
|
|
501
|
+
const method = String(jsonPath[2]);
|
|
502
|
+
const operationDocument = transformer({
|
|
503
|
+
document,
|
|
504
|
+
name: path,
|
|
505
|
+
method,
|
|
506
|
+
config: OPERATION_CONFIG,
|
|
507
|
+
});
|
|
508
|
+
let parsedUri;
|
|
509
|
+
const encodedPath = String(encodePointerFragment(path));
|
|
510
|
+
if (operationDocument.iid) {
|
|
511
|
+
parsedUri = `/operations/${operationDocument.iid}`;
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
parsedUri = uri.replace(encodedPath, slugify(path));
|
|
515
|
+
}
|
|
516
|
+
nodes.push({
|
|
517
|
+
type: NodeType.HttpOperation,
|
|
518
|
+
uri: parsedUri,
|
|
519
|
+
data: operationDocument,
|
|
520
|
+
name: operationDocument.summary || operationDocument.iid || operationDocument.path,
|
|
521
|
+
tags: ((_a = operationDocument.tags) === null || _a === void 0 ? void 0 : _a.map(tag => tag.name)) || [],
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
else if (match.type === NodeTypes.Webhook && jsonPath.length === 3) {
|
|
525
|
+
const name = String(jsonPath[1]);
|
|
526
|
+
const method = String(jsonPath[2]);
|
|
527
|
+
const webhookDocument = transformer({
|
|
528
|
+
document,
|
|
529
|
+
name,
|
|
530
|
+
method,
|
|
531
|
+
config: WEBHOOK_CONFIG,
|
|
532
|
+
});
|
|
533
|
+
let parsedUri;
|
|
534
|
+
const encodedPath = String(encodePointerFragment(name));
|
|
535
|
+
if (webhookDocument.iid) {
|
|
536
|
+
parsedUri = `/webhooks/${webhookDocument.iid}`;
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
parsedUri = uri.replace(encodedPath, slugify(name));
|
|
540
|
+
}
|
|
541
|
+
nodes.push({
|
|
542
|
+
type: NodeType.HttpWebhook,
|
|
543
|
+
uri: parsedUri,
|
|
544
|
+
data: webhookDocument,
|
|
545
|
+
name: webhookDocument.summary || webhookDocument.name,
|
|
546
|
+
tags: ((_b = webhookDocument.tags) === null || _b === void 0 ? void 0 : _b.map(tag => tag.name)) || [],
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
else if (match.type === NodeTypes.Model) {
|
|
550
|
+
const schemaDocument = get(document, jsonPath);
|
|
551
|
+
const parsedUri = uri.replace(OAS_MODEL_REGEXP, 'schemas/');
|
|
552
|
+
nodes.push({
|
|
553
|
+
type: NodeType.Model,
|
|
554
|
+
uri: parsedUri,
|
|
555
|
+
data: schemaDocument,
|
|
556
|
+
name: schemaDocument.title || last(uri.split('/')) || '',
|
|
557
|
+
tags: schemaDocument['x-tags'] || [],
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
if (match.children) {
|
|
561
|
+
nodes.push(...computeChildNodes(document, value, match.children, transformer, uri));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return nodes;
|
|
566
|
+
}
|
|
567
|
+
function findMapMatch(key, map) {
|
|
568
|
+
var _a;
|
|
569
|
+
if (typeof key === 'number')
|
|
570
|
+
return;
|
|
571
|
+
for (const entry of map) {
|
|
572
|
+
const escapedKey = key.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
573
|
+
if (!!((_a = entry.match) === null || _a === void 0 ? void 0 : _a.match(escapedKey)) || (entry.notMatch !== void 0 && !entry.notMatch.match(escapedKey))) {
|
|
574
|
+
return entry;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
function isJson(value) {
|
|
579
|
+
try {
|
|
580
|
+
JSON.parse(value);
|
|
581
|
+
}
|
|
582
|
+
catch (e) {
|
|
583
|
+
return false;
|
|
584
|
+
}
|
|
585
|
+
return true;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function useExportDocumentProps({ originalDocument, bundledDocument, }) {
|
|
589
|
+
const isJsonDocument = typeof originalDocument === 'object' || (!!originalDocument && isJson(originalDocument));
|
|
590
|
+
const exportDocument = React.useCallback((document) => {
|
|
591
|
+
const type = isJsonDocument ? 'json' : 'yaml';
|
|
592
|
+
const blob = new Blob([document], {
|
|
593
|
+
type: `application/${type}`,
|
|
594
|
+
});
|
|
595
|
+
saver.saveAs(blob, `document.${type}`);
|
|
596
|
+
}, [isJsonDocument]);
|
|
597
|
+
const exportOriginalDocument = React.useCallback(() => {
|
|
598
|
+
const stringifiedDocument = typeof originalDocument === 'object' ? JSON.stringify(originalDocument, null, 2) : originalDocument || '';
|
|
599
|
+
exportDocument(stringifiedDocument);
|
|
600
|
+
}, [originalDocument, exportDocument]);
|
|
601
|
+
const exportBundledDocument = React.useCallback(() => {
|
|
602
|
+
const stringifiedDocument = isJsonDocument
|
|
603
|
+
? JSON.stringify(bundledDocument, null, 2)
|
|
604
|
+
: safeStringify(bundledDocument);
|
|
605
|
+
exportDocument(stringifiedDocument);
|
|
606
|
+
}, [bundledDocument, isJsonDocument, exportDocument]);
|
|
607
|
+
return {
|
|
608
|
+
original: {
|
|
609
|
+
onPress: exportOriginalDocument,
|
|
610
|
+
},
|
|
611
|
+
bundled: {
|
|
612
|
+
onPress: exportBundledDocument,
|
|
613
|
+
},
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const propsAreWithDocument = (props) => {
|
|
618
|
+
return props.hasOwnProperty('apiDescriptionDocument');
|
|
619
|
+
};
|
|
620
|
+
const APIImpl = props => {
|
|
621
|
+
const { layout = 'sidebar', apiDescriptionUrl = '', logo, hideTryItPanel, hideTryIt, hideSamples, hideSecurityInfo, hideServerInfo, hideSchemas, hideInternal, hideExport, tryItCredentialsPolicy, tryItCorsProxy, maxRefDepth, renderExtensionAddon, basePath, outerRouter = false, } = props;
|
|
622
|
+
const location = useLocation();
|
|
623
|
+
const apiDescriptionDocument = propsAreWithDocument(props) ? props.apiDescriptionDocument : undefined;
|
|
624
|
+
const { isResponsiveLayoutEnabled } = useResponsiveLayout();
|
|
625
|
+
const { data: fetchedDocument, error } = useQuery([apiDescriptionUrl], () => fetch(apiDescriptionUrl).then(res => {
|
|
626
|
+
if (res.ok) {
|
|
627
|
+
return res.text();
|
|
628
|
+
}
|
|
629
|
+
throw new Error(`Unable to load description document, status code: ${res.status}`);
|
|
630
|
+
}), {
|
|
631
|
+
enabled: apiDescriptionUrl !== '' && !apiDescriptionDocument,
|
|
632
|
+
});
|
|
633
|
+
const document = apiDescriptionDocument || fetchedDocument || '';
|
|
634
|
+
const parsedDocument = useParsedValue(document);
|
|
635
|
+
const bundledDocument = useBundleRefsIntoDocument(parsedDocument, { baseUrl: apiDescriptionUrl });
|
|
636
|
+
const serviceNode = React.useMemo(() => transformOasToServiceNode(bundledDocument), [bundledDocument]);
|
|
637
|
+
const exportProps = useExportDocumentProps({ originalDocument: document, bundledDocument });
|
|
638
|
+
if (error) {
|
|
639
|
+
return (React.createElement(Flex, { justify: "center", alignItems: "center", w: "full", minH: "screen" },
|
|
640
|
+
React.createElement(NonIdealState, { title: "Document could not be loaded", description: "The API description document could not be fetched. This could indicate connectivity problems, or issues with the server hosting the spec.", icon: "exclamation-triangle" })));
|
|
641
|
+
}
|
|
642
|
+
if (!bundledDocument) {
|
|
643
|
+
return (React.createElement(Flex, { justify: "center", alignItems: "center", w: "full", minH: "screen", color: "light" },
|
|
644
|
+
React.createElement(Box, { as: Icon, icon: ['fal', 'circle-notch'], size: "3x", spin: true })));
|
|
645
|
+
}
|
|
646
|
+
if (!serviceNode) {
|
|
647
|
+
return (React.createElement(Flex, { justify: "center", alignItems: "center", w: "full", minH: "screen" },
|
|
648
|
+
React.createElement(NonIdealState, { title: "Failed to parse OpenAPI file", description: "Please make sure your OpenAPI file is valid and try again" })));
|
|
649
|
+
}
|
|
650
|
+
return (React.createElement(InlineRefResolverProvider, { document: parsedDocument, maxRefDepth: maxRefDepth },
|
|
651
|
+
layout === 'stacked' && (React.createElement(APIWithStackedLayout, { serviceNode: serviceNode, hideTryIt: hideTryIt, hideSamples: hideSamples, hideTryItPanel: hideTryItPanel, hideSecurityInfo: hideSecurityInfo, hideServerInfo: hideServerInfo, hideExport: hideExport, exportProps: exportProps, tryItCredentialsPolicy: tryItCredentialsPolicy, tryItCorsProxy: tryItCorsProxy, renderExtensionAddon: renderExtensionAddon, location: location })),
|
|
652
|
+
layout === 'sidebar' && (React.createElement(APIWithSidebarLayout, { logo: logo, serviceNode: serviceNode, hideTryItPanel: hideTryItPanel, hideTryIt: hideTryIt, hideSamples: hideSamples, hideSecurityInfo: hideSecurityInfo, hideServerInfo: hideServerInfo, hideSchemas: hideSchemas, hideInternal: hideInternal, hideExport: hideExport, exportProps: exportProps, tryItCredentialsPolicy: tryItCredentialsPolicy, tryItCorsProxy: tryItCorsProxy, renderExtensionAddon: renderExtensionAddon, basePath: basePath, outerRouter: outerRouter })),
|
|
653
|
+
layout === 'responsive' && (React.createElement(APIWithResponsiveSidebarLayout, { logo: logo, serviceNode: serviceNode, hideTryItPanel: hideTryItPanel, hideTryIt: hideTryIt, hideSamples: hideSamples, hideSecurityInfo: hideSecurityInfo, hideServerInfo: hideServerInfo, hideSchemas: hideSchemas, hideInternal: hideInternal, hideExport: hideExport, exportProps: exportProps, tryItCredentialsPolicy: tryItCredentialsPolicy, tryItCorsProxy: tryItCorsProxy, renderExtensionAddon: renderExtensionAddon, compact: isResponsiveLayoutEnabled, basePath: basePath, outerRouter: outerRouter }))));
|
|
654
|
+
};
|
|
655
|
+
const API = flow(withRouter, withStyles, withPersistenceBoundary, withMosaicProvider, withQueryClientProvider)(APIImpl);
|
|
656
|
+
|
|
657
|
+
export { API, APIWithStackedLayout, transformOasToServiceNode, useExportDocumentProps };
|