Repository navigation
Expand file tree
/
Copy pathSwaggerUI.tsx
More file actions
145 lines (129 loc) · 4.27 KB
/
Copy pathSwaggerUI.tsx
File metadata and controls
145 lines (129 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* eslint-disable @typescript-eslint/naming-convention */
const SwaggerUIReact = dynamic(() => import('swagger-ui-react'), {
loading: () => <ContentLoader/>,
ssr: false,
});
import type { SystemStyleObject } from '@chakra-ui/react';
import { Box, useColorModeValue, useToken } from '@chakra-ui/react';
import dynamic from 'next/dynamic';
import React from 'react';
import config from 'configs/app';
import ContentLoader from 'ui/shared/ContentLoader';
import 'swagger-ui-react/swagger-ui.css';
const feature = config.features.restApiDocs;
const DEFAULT_SERVER = 'blockscout.com/poa/core';
const NeverShowInfoPlugin = () => {
return {
components: {
SchemesContainer: () => null,
ServersContainer: () => null,
InfoContainer: () => null,
},
};
};
const SwaggerUI = () => {
const mainColor = useColorModeValue('blackAlpha.800', 'whiteAlpha.800');
const borderColor = useToken('colors', 'divider');
const mainBgColor = useColorModeValue('blackAlpha.100', 'whiteAlpha.200');
const swaggerStyle: SystemStyleObject = {
'.swagger-ui .scheme-container, .opblock-tag': {
display: 'none',
},
'.swagger-ui': {
color: mainColor,
},
'.swagger-ui .opblock-summary-control:focus': {
outline: 'none',
},
// eslint-disable-next-line max-len
'.swagger-ui .opblock .opblock-summary-path, .swagger-ui .opblock .opblock-summary-description, .swagger-ui div, .swagger-ui p, .swagger-ui h5, .swagger-ui .response-col_links, .swagger-ui h4, .swagger-ui table thead tr th, .swagger-ui table thead tr td, .swagger-ui .parameter__name, .swagger-ui .parameter__type, .swagger-ui .response-col_status, .swagger-ui .tab li, .swagger-ui .opblock .opblock-section-header h4': {
color: 'unset',
},
'.swagger-ui input': {
color: 'blackAlpha.800',
},
'.swagger-ui .opblock .opblock-section-header': {
background: useColorModeValue('whiteAlpha.800', 'blackAlpha.800'),
},
'.swagger-ui .response-col_description__inner p, .swagger-ui .parameters-col_description p': {
margin: 0,
},
'.swagger-ui .wrapper': {
padding: 0,
},
'.swagger-ui .prop-type': {
color: useColorModeValue('blue.600', 'blue.400'),
},
'.swagger-ui .btn.try-out__btn': {
borderColor: useToken('colors', 'link'),
color: useToken('colors', 'link'),
borderRadius: 'sm',
},
'.swagger-ui .btn.try-out__btn:hover': {
boxShadow: 'none',
borderColor: useToken('colors', 'link_hovered'),
color: useToken('colors', 'link_hovered'),
},
'.swagger-ui .btn.try-out__btn.cancel': {
borderColor: useToken('colors', 'error'),
color: useToken('colors', 'error'),
},
'.swagger-ui .btn.try-out__btn.cancel:hover': {
borderColor: useColorModeValue('red.600', 'red.500'),
color: useColorModeValue('red.500', 'red.400'),
},
// MODELS
'.swagger-ui section.models': {
borderColor: borderColor,
},
'.swagger-ui section.models h4': {
color: mainColor,
},
'.swagger-ui section.models .model-container': {
bgColor: mainBgColor,
},
'.swagger-ui .model-title': {
wordBreak: 'break-all',
color: mainColor,
},
'.swagger-ui .model': {
color: mainColor,
},
'.swagger-ui .model-box-control:focus': {
outline: 'none',
},
'.swagger-ui .model-toggle': {
bgColor: useColorModeValue('transparent', 'whiteAlpha.700'),
borderRadius: 'sm',
},
'.swagger-ui .model .property.primitive': {
color: useToken('colors', 'text_secondary'),
wordBreak: 'break-all',
},
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const reqInterceptor = React.useCallback((req: any) => {
if (!req.loadSpec) {
const newUrl = new URL(req.url.replace(DEFAULT_SERVER, config.api.host));
newUrl.protocol = config.api.protocol + ':';
if (config.api.port) {
newUrl.port = config.api.port;
}
req.url = newUrl.toString();
}
return req;
}, []);
if (!feature.isEnabled) {
return null;
}
return (
<Box sx={ swaggerStyle }>
<SwaggerUIReact
url={ feature.specUrl }
plugins={ [ NeverShowInfoPlugin ] }
requestInterceptor={ reqInterceptor }
/>
</Box>
);
};
export default SwaggerUI;