debian-mirror-gitlab/app/assets/javascripts/lib/swagger.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { SwaggerUIBundle } from 'swagger-ui-dist';
import { safeLoad } from 'js-yaml';
import { isObject } from '~/lib/utils/type_utility';
2023-05-27 22:25:52 +05:30
import { getParameterByName } from '~/lib/utils/url_utility';
import { resetServiceWorkersPublicPath } from '~/lib/utils/webpack';
const resetWebpackPublicPath = () => {
window.gon = { relative_url_root: getParameterByName('relativeRootPath') };
resetServiceWorkersPublicPath();
};
2022-11-25 23:54:43 +05:30
const renderSwaggerUI = (value) => {
/* SwaggerUIBundle accepts openapi definition
* in only JSON format, so we convert the YAML
* config to JSON if it's not JSON value
*/
let spec = value;
if (!isObject(spec)) {
spec = safeLoad(spec, { json: true });
}
2023-05-27 22:25:52 +05:30
resetWebpackPublicPath();
2022-11-25 23:54:43 +05:30
Promise.all([import(/* webpackChunkName: 'openapi' */ 'swagger-ui-dist/swagger-ui.css')])
.then(() => {
SwaggerUIBundle({
spec,
dom_id: '#swagger-ui',
deepLinking: true,
displayOperationId: true,
});
})
.catch((error) => {
throw error;
});
};
const addInitHook = () => {
window.addEventListener(
'message',
(event) => {
if (event.origin !== window.location.origin) {
return;
}
renderSwaggerUI(event.data);
},
false,
);
};
addInitHook();
export default {};