Skip to content

Commit

Permalink
Merge pull request #49 from hlxsites/api-docs
Browse files Browse the repository at this point in the history
first version of swagger ui implementation
  • Loading branch information
nc-andreashaller authored Oct 16, 2024
2 parents 4ce7c1f + 2eadd06 commit d21f939
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 380 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tools/importer/helix-importer-ui/*
blocks/mermaid/libs/*
blocks/swaggerui/libs/*
380 changes: 0 additions & 380 deletions apis/example.html

This file was deleted.

20 changes: 20 additions & 0 deletions blocks/swaggerui/libs/swagger-initializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">

// the following lines will be replaced by docker/configurator, when it runs in a docker-container
window.ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});

//</editor-fold>
};
2 changes: 2 additions & 0 deletions blocks/swaggerui/libs/swagger-ui-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions blocks/swaggerui/libs/swagger-ui-bundle.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions blocks/swaggerui/libs/swagger-ui-es-bundle-core.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions blocks/swaggerui/libs/swagger-ui-es-bundle-core.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions blocks/swaggerui/libs/swagger-ui-es-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions blocks/swaggerui/libs/swagger-ui-es-bundle.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions blocks/swaggerui/libs/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions blocks/swaggerui/libs/swagger-ui-standalone-preset.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions blocks/swaggerui/libs/swagger-ui.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions blocks/swaggerui/libs/swagger-ui.css.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions blocks/swaggerui/libs/swagger-ui.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions blocks/swaggerui/libs/swagger-ui.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions blocks/swaggerui/swaggerui.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
raqn-swaggerui .topbar {
display: none;
}
81 changes: 81 additions & 0 deletions blocks/swaggerui/swaggerui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import ComponentBase from '../../scripts/component-base.js';

export default class SwaggerUI extends ComponentBase {

async loadEnvironments() {
const response = await fetch('/apis/environments.json');
return response.json();
}

async loadAPIs() {
const environmentsElement = this.querySelector('.swagger-ui-selection .environments');
const definitionsElement = this.querySelector('.swagger-ui-selection .definitions');
const wrapperElement = this.querySelector('.swagger-ui-wrapper');

const environments = await this.loadEnvironments();
environments.forEach((environment) => {
const option = document.createElement('option');
option.value = environment;
option.textContent = environment;
environmentsElement.appendChild(option);
});

environmentsElement.addEventListener('change', async () => {
const response = await fetch(`/apis/${environmentsElement.value}/index.json`);
const apis = await response.json();
definitionsElement.innerHTML = '';
wrapperElement.innerHTML = '';
apis.forEach((api) => {
const option = document.createElement('option');
option.value = api;
option.textContent = api;
definitionsElement.appendChild(option);
});
definitionsElement.addEventListener('change', () => {
const file = definitionsElement.value;
wrapperElement.innerHTML = '';
window.SwaggerUIBundle({
url: `/apis/${environmentsElement.value}/${file}`,
domNode: wrapperElement,
presets: [window.SwaggerUIBundle.presets.apis, window.SwaggerUIStandalonePreset],
layout: 'StandaloneLayout',
});
});
definitionsElement.dispatchEvent(new Event('change'));
});
environmentsElement.dispatchEvent(new Event('change'));
}

async ready() {
this.innerHTML = `
<div class="swagger-ui-selection">
<select class="environments"></select>
<select class="definitions"></select>
</div>
<div class="swagger-ui-wrapper"></div>`;

const loadCSS = async (href) => new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = href;
link.onload = resolve;
link.onerror = reject;
document.head.append(link);
});
loadCSS('/blocks/swaggerui/libs/swagger-ui.css');
const loadJS = async (src) => new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
await Promise.all([
loadJS('/blocks/swaggerui/libs/swagger-ui-bundle.js'),
loadJS('/blocks/swaggerui/libs/swagger-ui-standalone-preset.js'),
]);

await this.loadAPIs();
}

}
6 changes: 6 additions & 0 deletions scripts/render/component-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export const componentList = {
return node;
},
},
'swaggerui': {
tag: 'raqn-swaggerui',
script: '/blocks/swaggerui/swaggerui',
priority: 0,
},

};

export const injectedComponents = [
Expand Down

0 comments on commit d21f939

Please sign in to comment.