source: node_modules/swagger-ui-express/index.js@ 65b6638

main
Last change on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[d24f17c]1'use strict'
2
3var express = require('express')
4var getAbsoluteSwaggerFsPath = require('swagger-ui-dist/absolute-path')
5var favIconHtml = '<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />' +
6 '<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />'
7var swaggerInit = ''
8
9function trimQuery(q) {
10 return q && q.split('?')[0]
11}
12
13var htmlTplString = `
14<!-- HTML for static distribution bundle build -->
15<!DOCTYPE html>
16<html lang="en">
17<head>
18 <meta charset="UTF-8">
19 <% robotsMetaString %>
20 <title><% title %></title>
21 <link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
22 <% favIconString %>
23 <style>
24 html
25 {
26 box-sizing: border-box;
27 overflow: -moz-scrollbars-vertical;
28 overflow-y: scroll;
29 }
30 *,
31 *:before,
32 *:after
33 {
34 box-sizing: inherit;
35 }
36
37 body {
38 margin:0;
39 background: #fafafa;
40 }
41 </style>
42</head>
43
44<body>
45
46<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0">
47 <defs>
48 <symbol viewBox="0 0 20 20" id="unlocked">
49 <path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
50 </symbol>
51
52 <symbol viewBox="0 0 20 20" id="locked">
53 <path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8zM12 8H8V5.199C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8z"/>
54 </symbol>
55
56 <symbol viewBox="0 0 20 20" id="close">
57 <path d="M14.348 14.849c-.469.469-1.229.469-1.697 0L10 11.819l-2.651 3.029c-.469.469-1.229.469-1.697 0-.469-.469-.469-1.229 0-1.697l2.758-3.15-2.759-3.152c-.469-.469-.469-1.228 0-1.697.469-.469 1.228-.469 1.697 0L10 8.183l2.651-3.031c.469-.469 1.228-.469 1.697 0 .469.469.469 1.229 0 1.697l-2.758 3.152 2.758 3.15c.469.469.469 1.229 0 1.698z"/>
58 </symbol>
59
60 <symbol viewBox="0 0 20 20" id="large-arrow">
61 <path d="M13.25 10L6.109 2.58c-.268-.27-.268-.707 0-.979.268-.27.701-.27.969 0l7.83 7.908c.268.271.268.709 0 .979l-7.83 7.908c-.268.271-.701.27-.969 0-.268-.269-.268-.707 0-.979L13.25 10z"/>
62 </symbol>
63
64 <symbol viewBox="0 0 20 20" id="large-arrow-down">
65 <path d="M17.418 6.109c.272-.268.709-.268.979 0s.271.701 0 .969l-7.908 7.83c-.27.268-.707.268-.979 0l-7.908-7.83c-.27-.268-.27-.701 0-.969.271-.268.709-.268.979 0L10 13.25l7.418-7.141z"/>
66 </symbol>
67
68
69 <symbol viewBox="0 0 24 24" id="jump-to">
70 <path d="M19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z"/>
71 </symbol>
72
73 <symbol viewBox="0 0 24 24" id="expand">
74 <path d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"/>
75 </symbol>
76
77 </defs>
78</svg>
79
80<div id="swagger-ui"></div>
81
82<script src="./swagger-ui-bundle.js"> </script>
83<script src="./swagger-ui-standalone-preset.js"> </script>
84<script src="./swagger-ui-init.js"> </script>
85<% customJs %>
86<% customJsStr %>
87<% customCssUrl %>
88<style>
89 <% customCss %>
90</style>
91</body>
92
93</html>
94`
95
96var jsTplString = `
97window.onload = function() {
98 // Build a system
99 var url = window.location.search.match(/url=([^&]+)/);
100 if (url && url.length > 1) {
101 url = decodeURIComponent(url[1]);
102 } else {
103 url = window.location.origin;
104 }
105 <% swaggerOptions %>
106 url = options.swaggerUrl || url
107 var urls = options.swaggerUrls
108 var customOptions = options.customOptions
109 var spec1 = options.swaggerDoc
110 var swaggerOptions = {
111 spec: spec1,
112 url: url,
113 urls: urls,
114 dom_id: '#swagger-ui',
115 deepLinking: true,
116 presets: [
117 SwaggerUIBundle.presets.apis,
118 SwaggerUIStandalonePreset
119 ],
120 plugins: [
121 SwaggerUIBundle.plugins.DownloadUrl
122 ],
123 layout: "StandaloneLayout"
124 }
125 for (var attrname in customOptions) {
126 swaggerOptions[attrname] = customOptions[attrname];
127 }
128 var ui = SwaggerUIBundle(swaggerOptions)
129
130 if (customOptions.oauth) {
131 ui.initOAuth(customOptions.oauth)
132 }
133
134 if (customOptions.preauthorizeApiKey) {
135 const key = customOptions.preauthorizeApiKey.authDefinitionKey;
136 const value = customOptions.preauthorizeApiKey.apiKeyValue;
137 if (!!key && !!value) {
138 const pid = setInterval(() => {
139 const authorized = ui.preauthorizeApiKey(key, value);
140 if(!!authorized) clearInterval(pid);
141 }, 500)
142
143 }
144 }
145
146 if (customOptions.authAction) {
147 ui.authActions.authorize(customOptions.authAction)
148 }
149
150 window.ui = ui
151}
152`
153
154function toExternalScriptTag(url) {
155 return `<script src='${url}'></script>`
156}
157
158function toInlineScriptTag(jsCode) {
159 return `<script>${jsCode}</script>`
160}
161
162function toExternalStylesheetTag(url) {
163 return `<link href='${url}' rel='stylesheet'>`
164}
165
166function toTags(customCode, toScript) {
167 if (typeof customCode === 'string') {
168 return toScript(customCode)
169 } else if (Array.isArray(customCode)) {
170 return customCode.map(toScript).join('\n')
171 } else {
172 return ''
173 }
174}
175
176var generateHTML = function (swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle, _htmlTplString, _jsTplString) {
177 var isExplorer
178 var customJs
179 var customJsStr
180 var swaggerUrls
181 var customCssUrl
182 var customRobots
183 if (opts && typeof opts === 'object') {
184 options = opts.swaggerOptions
185 customCss = opts.customCss
186 customJs = opts.customJs
187 customJsStr = opts.customJsStr
188 customfavIcon = opts.customfavIcon
189 customRobots = opts.customRobots
190 swaggerUrl = opts.swaggerUrl
191 swaggerUrls = opts.swaggerUrls
192 isExplorer = opts.explorer || !!swaggerUrls
193 customSiteTitle = opts.customSiteTitle
194 customCssUrl = opts.customCssUrl
195 } else {
196 //support legacy params based function
197 isExplorer = opts
198 }
199 options = options || {}
200 var explorerString = isExplorer ? '' : '.swagger-ui .topbar .download-url-wrapper { display: none }'
201 customCss = explorerString + ' ' + customCss || explorerString
202 customfavIcon = customfavIcon || false
203 customSiteTitle = customSiteTitle || 'Swagger UI'
204 _htmlTplString = _htmlTplString || htmlTplString
205 _jsTplString = _jsTplString || jsTplString
206
207 var robotsMetaString = customRobots ? '<meta name="robots" content="' + customRobots + '" />' : ''
208 var favIconString = customfavIcon ? '<link rel="icon" href="' + customfavIcon + '" />' : favIconHtml
209 var htmlWithCustomCss = _htmlTplString.toString().replace('<% customCss %>', customCss)
210 var htmlWithCustomRobots = htmlWithCustomCss.replace('<% robotsMetaString %>', robotsMetaString)
211 var htmlWithFavIcon = htmlWithCustomRobots.replace('<% favIconString %>', favIconString)
212 var htmlWithCustomJsUrl = htmlWithFavIcon.replace('<% customJs %>', toTags(customJs, toExternalScriptTag))
213 var htmlWithCustomJs = htmlWithCustomJsUrl.replace('<% customJsStr %>', toTags(customJsStr, toInlineScriptTag))
214 var htmlWithCustomCssUrl = htmlWithCustomJs.replace('<% customCssUrl %>', toTags(customCssUrl, toExternalStylesheetTag))
215
216 var initOptions = {
217 swaggerDoc: swaggerDoc || undefined,
218 customOptions: options,
219 swaggerUrl: swaggerUrl || undefined,
220 swaggerUrls: swaggerUrls || undefined
221 }
222
223 swaggerInit = _jsTplString.toString().replace('<% swaggerOptions %>', stringify(initOptions))
224 return htmlWithCustomCssUrl.replace('<% title %>', customSiteTitle)
225}
226
227var setup = function (swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle) {
228 var html = generateHTML(swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle, htmlTplString, jsTplString)
229 return function (req, res) {
230 if (req.swaggerDoc) {
231 var reqHtml = generateHTML(req.swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle, htmlTplString, jsTplString)
232 res.send(reqHtml)
233 } else {
234 res.send(html)
235 }
236 }
237}
238
239function swaggerInitFn(req, res, next) {
240 if (trimQuery(req.url).endsWith('/package.json')) {
241 res.sendStatus(404)
242 } else if (trimQuery(req.url).endsWith('/swagger-ui-init.js')) {
243 res.set('Content-Type', 'application/javascript')
244 res.send(swaggerInit)
245 } else {
246 next()
247 }
248}
249
250var swaggerInitFunction = function (swaggerDoc, opts) {
251 var swaggerInitFile = jsTplString.toString().replace('<% swaggerOptions %>', stringify(opts))
252 return function (req, res, next) {
253 if (trimQuery(req.url).endsWith('/package.json')) {
254 res.sendStatus(404)
255 } else if (trimQuery(req.url).endsWith('/swagger-ui-init.js')) {
256 if (req.swaggerDoc) {
257 opts.swaggerDoc = req.swaggerDoc
258 swaggerInitFile = jsTplString.toString().replace('<% swaggerOptions %>', stringify(opts))
259 }
260 res.set('Content-Type', 'application/javascript')
261 res.send(swaggerInitFile)
262 } else {
263 next()
264 }
265 }
266}
267
268var swaggerAssetMiddleware = options => {
269 var opts = options || {}
270 opts.index = false
271 return express.static(getAbsoluteSwaggerFsPath(), opts)
272}
273
274var serveFiles = function (swaggerDoc, opts) {
275 opts = opts || {}
276 var initOptions = {
277 swaggerDoc: swaggerDoc || undefined,
278 customOptions: opts.swaggerOptions || {},
279 swaggerUrl: opts.swaggerUrl || {},
280 swaggerUrls: opts.swaggerUrls || undefined
281 }
282 var swaggerInitWithOpts = swaggerInitFunction(swaggerDoc, initOptions)
283 return [swaggerInitWithOpts, swaggerAssetMiddleware()]
284}
285
286var serve = [swaggerInitFn, swaggerAssetMiddleware()]
287var serveWithOptions = options => [swaggerInitFn, swaggerAssetMiddleware(options)]
288
289var stringify = function (obj, prop) {
290 var placeholder = '____FUNCTIONPLACEHOLDER____'
291 var fns = []
292 var json = JSON.stringify(obj, function (key, value) {
293 if (typeof value === 'function') {
294 fns.push(value)
295 return placeholder
296 }
297 return value
298 }, 2)
299 json = json.replace(new RegExp('"' + placeholder + '"', 'g'), function (_) {
300 return fns.shift()
301 })
302 return 'var options = ' + json + ';'
303}
304
305module.exports = {
306 setup: setup,
307 serve: serve,
308 serveWithOptions: serveWithOptions,
309 generateHTML: generateHTML,
310 serveFiles: serveFiles
311}
Note: See TracBrowser for help on using the repository browser.