1 | import React from "react";
|
---|
2 | import PropTypes from "prop-types";
|
---|
3 | import SwaggerUIConstructor from "#swagger-ui";
|
---|
4 | class SwaggerUI extends React.Component {
|
---|
5 | constructor(props) {
|
---|
6 | super(props);
|
---|
7 | this.SwaggerUIComponent = null;
|
---|
8 | this.system = null;
|
---|
9 | }
|
---|
10 | componentDidMount() {
|
---|
11 | const ui = SwaggerUIConstructor({
|
---|
12 | plugins: this.props.plugins,
|
---|
13 | spec: this.props.spec,
|
---|
14 | url: this.props.url,
|
---|
15 | layout: this.props.layout,
|
---|
16 | defaultModelsExpandDepth: this.props.defaultModelsExpandDepth,
|
---|
17 | defaultModelRendering: this.props.defaultModelRendering,
|
---|
18 | presets: [SwaggerUIConstructor.presets.apis, ...this.props.presets],
|
---|
19 | requestInterceptor: this.props.requestInterceptor,
|
---|
20 | responseInterceptor: this.props.responseInterceptor,
|
---|
21 | onComplete: this.onComplete,
|
---|
22 | docExpansion: this.props.docExpansion,
|
---|
23 | supportedSubmitMethods: this.props.supportedSubmitMethods,
|
---|
24 | queryConfigEnabled: this.props.queryConfigEnabled,
|
---|
25 | defaultModelExpandDepth: this.props.defaultModelExpandDepth,
|
---|
26 | displayOperationId: this.props.displayOperationId,
|
---|
27 | tryItOutEnabled: this.props.tryItOutEnabled,
|
---|
28 | displayRequestDuration: this.props.displayRequestDuration,
|
---|
29 | requestSnippetsEnabled: this.props.requestSnippetsEnabled,
|
---|
30 | requestSnippets: this.props.requestSnippets,
|
---|
31 | showMutatedRequest: this.props.showMutatedRequest,
|
---|
32 | deepLinking: this.props.deepLinking,
|
---|
33 | showExtensions: this.props.showExtensions,
|
---|
34 | showCommonExtensions: this.props.showCommonExtensions,
|
---|
35 | filter: this.props.filter,
|
---|
36 | persistAuthorization: this.props.persistAuthorization,
|
---|
37 | withCredentials: this.props.withCredentials,
|
---|
38 | ...(typeof this.props.oauth2RedirectUrl === "string" ? {
|
---|
39 | oauth2RedirectUrl: this.props.oauth2RedirectUrl
|
---|
40 | } : {})
|
---|
41 | });
|
---|
42 | this.system = ui;
|
---|
43 | this.SwaggerUIComponent = ui.getComponent("App", "root");
|
---|
44 | this.forceUpdate();
|
---|
45 | }
|
---|
46 | render() {
|
---|
47 | return this.SwaggerUIComponent ? /*#__PURE__*/React.createElement(this.SwaggerUIComponent, null) : null;
|
---|
48 | }
|
---|
49 | componentDidUpdate(prevProps) {
|
---|
50 | const prevStateUrl = this.system.specSelectors.url();
|
---|
51 | if (this.props.url !== prevStateUrl || this.props.url !== prevProps.url) {
|
---|
52 | // flush current content
|
---|
53 | this.system.specActions.updateSpec("");
|
---|
54 | if (this.props.url) {
|
---|
55 | // update the internal URL
|
---|
56 | this.system.specActions.updateUrl(this.props.url);
|
---|
57 | // trigger remote definition fetch
|
---|
58 | this.system.specActions.download(this.props.url);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | const prevStateSpec = this.system.specSelectors.specStr();
|
---|
62 | if (this.props.spec && (this.props.spec !== prevStateSpec || this.props.spec !== prevProps.spec)) {
|
---|
63 | if (typeof this.props.spec === "object") {
|
---|
64 | this.system.specActions.updateSpec(JSON.stringify(this.props.spec));
|
---|
65 | } else {
|
---|
66 | this.system.specActions.updateSpec(this.props.spec);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | onComplete = () => {
|
---|
71 | if (typeof this.props.onComplete === "function") {
|
---|
72 | return this.props.onComplete(this.system);
|
---|
73 | }
|
---|
74 | };
|
---|
75 | }
|
---|
76 | SwaggerUI.defaultProps = {
|
---|
77 | spec: "",
|
---|
78 | url: "",
|
---|
79 | layout: "BaseLayout",
|
---|
80 | requestInterceptor: req => req,
|
---|
81 | responseInterceptor: res => res,
|
---|
82 | supportedSubmitMethods: ["get", "put", "post", "delete", "options", "head", "patch", "trace"],
|
---|
83 | queryConfigEnabled: false,
|
---|
84 | plugins: [],
|
---|
85 | displayOperationId: false,
|
---|
86 | showMutatedRequest: true,
|
---|
87 | docExpansion: "list",
|
---|
88 | defaultModelExpandDepth: 1,
|
---|
89 | defaultModelsExpandDepth: 1,
|
---|
90 | defaultModelRendering: "example",
|
---|
91 | presets: [],
|
---|
92 | deepLinking: false,
|
---|
93 | showExtensions: false,
|
---|
94 | showCommonExtensions: false,
|
---|
95 | filter: null,
|
---|
96 | requestSnippetsEnabled: false,
|
---|
97 | requestSnippets: {
|
---|
98 | generators: {
|
---|
99 | "curl_bash": {
|
---|
100 | title: "cURL (bash)",
|
---|
101 | syntax: "bash"
|
---|
102 | },
|
---|
103 | "curl_powershell": {
|
---|
104 | title: "cURL (PowerShell)",
|
---|
105 | syntax: "powershell"
|
---|
106 | },
|
---|
107 | "curl_cmd": {
|
---|
108 | title: "cURL (CMD)",
|
---|
109 | syntax: "bash"
|
---|
110 | }
|
---|
111 | },
|
---|
112 | defaultExpanded: true,
|
---|
113 | languages: null // e.g. only show curl bash = ["curl_bash"]
|
---|
114 | },
|
---|
115 | tryItOutEnabled: false,
|
---|
116 | displayRequestDuration: false,
|
---|
117 | withCredentials: undefined,
|
---|
118 | persistAuthorization: false,
|
---|
119 | oauth2RedirectUrl: undefined
|
---|
120 | };
|
---|
121 | SwaggerUI.System = SwaggerUIConstructor.System;
|
---|
122 | SwaggerUI.presets = SwaggerUIConstructor.presets;
|
---|
123 | SwaggerUI.plugins = SwaggerUIConstructor.plugins;
|
---|
124 | export default SwaggerUI;
|
---|
125 |
|
---|