generated from fastify/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make it possible to passthrough functions to the swagger-initializer.…
…js (#37) * make it possible to passthrough functions to the swagger-initializer.js * Apply suggestions from code review Co-authored-by: Frazer Smith <[email protected]> * add unit tests, add Map and Set support * improve typings and fix readme.md * Update README.md * Update README.md Co-authored-by: Frazer Smith <[email protected]> * add comments ot initOAuth * simplify swaggerInitializer * Update lib/routes.js Co-authored-by: Manuel Spigolon <[email protected]> * unnamed symbols should also be serializable * improve documentation regarding uiConfig * add more tests * rename file --------- Co-authored-by: Frazer Smith <[email protected]> Co-authored-by: Manuel Spigolon <[email protected]>
- Loading branch information
1 parent
96d715e
commit e2daa82
Showing
10 changed files
with
741 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
function serialize (value) { | ||
switch (typeof value) { | ||
case 'bigint': | ||
return value.toString() + 'n' | ||
case 'boolean': | ||
return value ? 'true' : 'false' | ||
case 'function': | ||
return value.toString() | ||
case 'number': | ||
return '' + value | ||
case 'object': | ||
if (value === null) { | ||
return 'null' | ||
} else if (Array.isArray(value)) { | ||
return serializeArray(value) | ||
} else if (value instanceof RegExp) { | ||
return `/${value.source}/${value.flags}` | ||
} else if (value instanceof Date) { | ||
return `new Date(${value.getTime()})` | ||
} else if (value instanceof Set) { | ||
return `new Set(${serializeArray(Array.from(value))})` | ||
} else if (value instanceof Map) { | ||
return `new Map(${serializeArray(Array.from(value))})` | ||
} else { | ||
return serializeObject(value) | ||
} | ||
case 'string': | ||
return JSON.stringify(value) | ||
case 'symbol': | ||
return serializeSymbol(value) | ||
case 'undefined': | ||
return 'undefined' | ||
} | ||
} | ||
const symbolRE = /Symbol\((.+)\)/ | ||
function serializeSymbol (value) { | ||
return symbolRE.test(value.toString()) | ||
? `Symbol("${value.toString().match(symbolRE)[1]}")` | ||
: 'Symbol()' | ||
} | ||
|
||
function serializeArray (value) { | ||
let result = '[' | ||
const il = value.length | ||
const last = il - 1 | ||
for (let i = 0; i < il; ++i) { | ||
result += serialize(value[i]) | ||
i !== last && (result += ',') | ||
} | ||
return result + ']' | ||
} | ||
|
||
function serializeObject (value) { | ||
let result = '{' | ||
const keys = Object.keys(value) | ||
let i = 0 | ||
const il = keys.length | ||
const last = il - 1 | ||
for (; i < il; ++i) { | ||
const key = keys[i] | ||
result += `"${key}":${serialize(value[key])}` | ||
i !== last && (result += ',') | ||
} | ||
return result + '}' | ||
} | ||
|
||
module.exports = serialize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict' | ||
|
||
const serialize = require('./serialize') | ||
|
||
function swaggerInitializer (opts) { | ||
return `window.onload = function () { | ||
function resolveUrl(url) { | ||
const anchor = document.createElement('a') | ||
anchor.href = url | ||
return anchor.href | ||
} | ||
const config = ${serialize(opts.uiConfig)} | ||
const resConfig = Object.assign({}, { | ||
dom_id: '#swagger-ui', | ||
deepLinking: true, | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIStandalonePreset | ||
], | ||
plugins: [ | ||
SwaggerUIBundle.plugins.DownloadUrl | ||
], | ||
layout: "StandaloneLayout" | ||
}, config, { | ||
url: resolveUrl('./json').replace('static/json', 'json'), | ||
oauth2RedirectUrl: resolveUrl('./oauth2-redirect.html') | ||
}); | ||
const ui = SwaggerUIBundle(resConfig) | ||
window.ui = ui | ||
ui.initOAuth(${serialize(opts.initOAuth)}) | ||
}` | ||
} | ||
|
||
module.exports = swaggerInitializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.