@redocly/portal-plugin-async-api
Version:
Async API plugin for Redocly products
77 lines • 4.89 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as asyncApi from '@asyncapi/parser';
import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';
import path from 'node:path';
import { asyncApiDocsConfigSchema } from './config.js';
const ASYNCAPI_TEMPLATE_ID = 'asyncapi-docs';
const ASYNCAPI_SHARED_DATA_PREFIX = 'asyncapi-docs-';
export default function asyncAPIDocsPlugin() {
const parser = new asyncApi.Parser();
parser.registerSchemaParser(AvroSchemaParser());
return {
id: 'asyncapi',
requiredEntitlements: ['asyncapi'],
redoclyConfigSchema: asyncApiDocsConfigSchema,
loaders: {
asyncapi: (relativePath_1, _a) => __awaiter(this, [relativePath_1, _a], void 0, function* (relativePath, { fs, cache, logger }) {
const absolutePath = path.join(fs.cwd, relativePath);
const { data: yaml } = yield cache.load(relativePath, 'yaml');
if (!yaml.asyncapi)
return undefined;
const { document, diagnostics } = yield asyncApi.fromFile(parser, absolutePath).parse();
diagnostics
.filter((d) => d.severity === 0)
.forEach(({ message, path }) => logger.error(`Cannot parse AsyncAPI schema: ${message} in '${relativePath}:${path.join('.')}'`));
return document;
}),
},
processContent: (actions, context) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const asyncAPITemplateId = actions.createTemplate(ASYNCAPI_TEMPLATE_ID, '@redocly/portal-plugin-async-api/template.js');
const config = yield context.getConfig();
const globalSettings = (config === null || config === void 0 ? void 0 : config.asyncapi) || {};
for (const record of yield context.fs.scan(/(\.ya?ml|\.json)$/)) {
if (yield context.isPathIgnored(record.relativePath))
continue;
const { relativePath } = record;
const sharedDataId = `${ASYNCAPI_SHARED_DATA_PREFIX}${relativePath}`;
try {
const { data: document } = yield context.cache.load(record.realRelativePath, 'asyncapi');
if (!document)
continue;
yield actions.createSharedData(sharedDataId, (_a = document === null || document === void 0 ? void 0 : document.json()) !== null && _a !== void 0 ? _a : {});
actions.addRoute({
fsPath: relativePath,
templateId: asyncAPITemplateId,
getStaticData: buildGetStaticDataFn(globalSettings, context.withPathPrefix),
metadata: Object.assign({ type: 'asyncapi', title: (_d = (_c = (_b = document === null || document === void 0 ? void 0 : document.json()) === null || _b === void 0 ? void 0 : _b.info) === null || _c === void 0 ? void 0 : _c.title) !== null && _d !== void 0 ? _d : 'AsyncAPI', description: (_g = (_f = (_e = document === null || document === void 0 ? void 0 : document.json()) === null || _e === void 0 ? void 0 : _e.info) === null || _f === void 0 ? void 0 : _f.description) !== null && _g !== void 0 ? _g : '' }, ((_k = (_j = (_h = document === null || document === void 0 ? void 0 : document.json()) === null || _h === void 0 ? void 0 : _h.info) === null || _j === void 0 ? void 0 : _j['x-metadata']) !== null && _k !== void 0 ? _k : {})),
sharedData: [{ key: 'AsyncApiDefinition', id: sharedDataId }],
});
}
catch (e) {
console.error(e);
}
}
}),
};
function buildGetStaticDataFn(settings, withPathPrefix) {
return function (route, _context) {
return __awaiter(this, void 0, void 0, function* () {
return {
props: {
settings: Object.assign(Object.assign({}, settings), { baseUrlPath: withPathPrefix(route.slug) }),
},
};
});
};
}
}
//# sourceMappingURL=plugin.js.map