@jpmorganchase/elemental 6.0.2 → 7.1.1

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