2022-11-25 23:54:43 +05:30
|
|
|
import { setAttributes } from '~/lib/utils/dom_utils';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-05-27 22:25:52 +05:30
|
|
|
import {
|
|
|
|
getBaseURL,
|
|
|
|
relativePathToAbsolute,
|
|
|
|
joinPaths,
|
|
|
|
setUrlParams,
|
|
|
|
} from '~/lib/utils/url_utility';
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
const SANDBOX_FRAME_PATH = '/-/sandbox/swagger';
|
|
|
|
|
|
|
|
const getSandboxFrameSrc = () => {
|
|
|
|
const path = joinPaths(gon.relative_url_root || '', SANDBOX_FRAME_PATH);
|
2023-05-27 22:25:52 +05:30
|
|
|
const absoluteUrl = relativePathToAbsolute(path, getBaseURL());
|
|
|
|
if (window.gon?.relative_url_root) {
|
|
|
|
return setUrlParams({ relativeRootPath: window.gon.relative_url_root }, absoluteUrl);
|
|
|
|
}
|
|
|
|
return absoluteUrl;
|
2023-04-23 21:23:45 +05:30
|
|
|
};
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
const createSandbox = () => {
|
|
|
|
const iframeEl = document.createElement('iframe');
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
setAttributes(iframeEl, {
|
2023-04-23 21:23:45 +05:30
|
|
|
src: getSandboxFrameSrc(),
|
2023-03-04 22:38:38 +05:30
|
|
|
sandbox: 'allow-scripts allow-popups allow-forms',
|
2022-11-25 23:54:43 +05:30
|
|
|
frameBorder: 0,
|
|
|
|
width: '100%',
|
|
|
|
// The height will be adjusted dynamically.
|
|
|
|
// Follow-up issue: https://gitlab.com/gitlab-org/gitlab/-/issues/377969
|
|
|
|
height: '1000',
|
|
|
|
});
|
|
|
|
return iframeEl;
|
|
|
|
};
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
export default async (el = document.getElementById('js-openapi-viewer')) => {
|
|
|
|
const wrapperEl = el;
|
2022-11-25 23:54:43 +05:30
|
|
|
const sandboxEl = createSandbox();
|
|
|
|
|
|
|
|
const { data } = await axios.get(wrapperEl.dataset.endpoint);
|
|
|
|
|
|
|
|
wrapperEl.appendChild(sandboxEl);
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
sandboxEl.addEventListener('load', () => {
|
|
|
|
sandboxEl.contentWindow.postMessage(data, '*');
|
|
|
|
});
|
2020-01-01 13:55:28 +05:30
|
|
|
};
|