@asyncapi/html-template
Version:
HTML template for the AsyncAPI generator.
128 lines (116 loc) • 3.51 kB
JavaScript
;
require('source-map-support/register');
var generatorReactSdk = require('@asyncapi/generator-react-sdk');
var parser = require('@asyncapi/parser');
require('path');
var fs = require('fs');
require('react-dom/server');
require('sync-fetch');
require('@asyncapi/react-component');
var jsxRuntime = require('/home/runner/work/html-template/html-template/node_modules/@asyncapi/generator-react-sdk/node_modules/react/cjs/react-jsx-runtime.production.min.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
function isJsonObject(o) {
return o && typeof o === 'object' && !Array.isArray(o);
}
/**
* Performs a recursive deep merge while assuming only simple JSON types are used.
*/
function mergeInto(from, to) {
for (const key in from) {
if (!Object.hasOwn(from, key)) {
continue;
}
if (isJsonObject(from[key])) {
if (!isJsonObject(to[key])) {
to[key] = {};
}
mergeInto(from[key], to[key]);
} else {
// Override with non-object JSON value
to[key] = from[key];
}
}
}
/**
* Prepares configuration for component.
*/
function prepareConfiguration(params = {}) {
const config = {
show: {
sidebar: true
},
sidebar: {
showOperations: 'byDefault'
}
};
// Apply config override
if (params.config) {
let configOverride;
try {
// Attempt to parse inline stringified JSON
configOverride = JSON.parse(params.config);
} catch (jsonErr) {
// Failed to parse JSON string...
try {
// Attempt to read as JSON file and parse contents
configOverride = JSON.parse(fs__default["default"].readFileSync(params.config, "utf8"));
} catch (err) {
console.error("Failed to parse config override JSON", jsonErr, err);
throw err;
}
}
if (isJsonObject(configOverride)) {
mergeInto(configOverride, config);
}
}
// Apply explicit config properties
if (params.sidebarOrganization === 'byTags') {
config.sidebar.showOperations = 'bySpecTags';
} else if (params.sidebarOrganization === 'byTagsNoRoot') {
config.sidebar.showOperations = 'byOperationsTags';
}
return config;
}
/**
* Stringifies the specification with escaping circular refs
* and annotates that specification is parsed.
*/
function stringifySpec(asyncapi) {
const stringifiedDoc = parser.stringify(asyncapi);
if (stringifiedDoc === undefined) throw new Error("Unable to stringify parsed AsyncAPI document passed by the generator. Please report an issue in https://github.com/asyncapi/html-template repository.");
return stringifiedDoc;
}
/**
* Stringifies prepared configuration for component.
*/
function stringifyConfiguration(params) {
return JSON.stringify(prepareConfiguration(params));
}
function App({
asyncapi,
params = {}
}) {
return `
const schema = ${stringifySpec(asyncapi)};
const config = ${stringifyConfiguration(params)};
const appRoot = document.getElementById('root');
AsyncApiStandalone.render(
{ schema, config, }, appRoot
);
`;
}
function app_js ({
asyncapi,
params = {}
}) {
return /*#__PURE__*/jsxRuntime.jsx(generatorReactSdk.File, {
name: "app.js",
children: /*#__PURE__*/jsxRuntime.jsx(App, {
asyncapi: asyncapi,
params: params
})
});
}
module.exports = app_js;
//# sourceMappingURL=app.js.js.map