nest-redoc
Version:
A NestJS Frontend for your OpenAPI Specs powered by ReDoc
106 lines (105 loc) • 6.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedocModule = void 0;
const redoc_options_schema_1 = require("./schema/redoc-options.schema");
const redoc_utils_1 = require("./redoc.utils");
const redoc_constants_1 = require("./redoc.constants");
const redoc_fastify_adapter_1 = require("./adapters/redoc-fastify.adapter");
const swagger_1 = require("@nestjs/swagger");
const redoc_express_adapter_1 = require("./adapters/redoc-express.adapter");
const express_handlebars_1 = require("express-handlebars");
const main_handlebars_1 = require("./views/main.handlebars");
const scripts_handlebars_1 = require("./views/scripts.handlebars");
const styles_handlebars_1 = require("./views/styles.handlebars");
class RedocModule {
static setup(path, app, document, options) {
return new Promise(async (resolve, reject) => {
var _a;
try {
const redocPath = (0, redoc_utils_1.normalizeUrl)(path);
const redocOptions = await this.validateOptions(options || {}, document);
const redocDocument = this.buildDocument(redocPath, document, redocOptions);
const redocTemplate = await this.renderHandlebars(redocPath, redocOptions);
const httpAdapter = app.getHttpAdapter();
switch ((_a = httpAdapter === null || httpAdapter === void 0 ? void 0 : httpAdapter.constructor) === null || _a === void 0 ? void 0 : _a.name) {
case 'FastifyAdapter': return resolve(await (new redoc_fastify_adapter_1.RedocFastifyAdapter(redocPath, app, redocDocument, redocOptions, redocTemplate).setup()));
default: return resolve(await (new redoc_express_adapter_1.RedocExpressAdapter(redocPath, app, redocDocument, redocOptions, redocTemplate).setup()));
}
}
catch (e) {
reject(e);
}
});
}
static createDocumentBuilder() {
return new swagger_1.DocumentBuilder();
}
static createDocument(app, config, options) {
return swagger_1.SwaggerModule.createDocument(app, config, options);
}
static buildDocument(path, document, options) {
var _a, _b, _c, _d, _e;
return Object.assign(document || {}, {
info: Object.assign((document === null || document === void 0 ? void 0 : document.info) || {}, {
'x-logo': {
url: (0, redoc_utils_1.isString)((_a = options === null || options === void 0 ? void 0 : options.logo) === null || _a === void 0 ? void 0 : _a.url) ? options.logo.url : (0, redoc_utils_1.isBuffer)((_b = options === null || options === void 0 ? void 0 : options.logo) === null || _b === void 0 ? void 0 : _b.url) ? (0, redoc_utils_1.joinUrl)(path, redoc_constants_1.REDOC_URLS.LOGO) : null,
backgroundColor: (_c = options === null || options === void 0 ? void 0 : options.logo) === null || _c === void 0 ? void 0 : _c.backgroundColor,
altText: (_d = options === null || options === void 0 ? void 0 : options.logo) === null || _d === void 0 ? void 0 : _d.altText,
href: ((_e = options === null || options === void 0 ? void 0 : options.logo) === null || _e === void 0 ? void 0 : _e.href) || `/${path}`,
},
}),
'x-tagGroups': (options === null || options === void 0 ? void 0 : options.tagGroups) || [],
});
}
static validateOptions(options, document) {
return new Promise(async (resolve, reject) => {
try {
resolve(await (0, redoc_options_schema_1.RedocOptionsSchema)(document).validateAsync(options));
}
catch (e) {
reject(new TypeError(e.message));
}
});
}
static renderHandlebars(path, options) {
return new Promise(async (resolve, reject) => {
try {
const renderData = {
data: {
title: options === null || options === void 0 ? void 0 : options.title,
urls: {
logo: (0, redoc_utils_1.joinUrl)(path, redoc_constants_1.REDOC_URLS.LOGO),
favicon: (0, redoc_utils_1.joinUrl)(path, redoc_constants_1.REDOC_URLS.FAVICON),
definitions: (0, redoc_utils_1.joinUrl)(path, redoc_constants_1.REDOC_URLS.DEFINITIONS),
},
favicon: (0, redoc_utils_1.isString)(options === null || options === void 0 ? void 0 : options.favicon) || (0, redoc_utils_1.isBuffer)(options === null || options === void 0 ? void 0 : options.favicon),
redocVersion: options === null || options === void 0 ? void 0 : options.redocVersion,
options: Object.assign(options || {}, {
expandResponses: Array.isArray(options === null || options === void 0 ? void 0 : options.expandResponses) ? options.expandResponses.join(',') : options === null || options === void 0 ? void 0 : options.expandResponses,
}),
theme: (options === null || options === void 0 ? void 0 : options.theme) || {}
}
};
renderData.data.optionsB64Json = Buffer.from(JSON.stringify(Object.assign(Object.assign({}, renderData.data.options), renderData.data.theme))).toString('base64');
renderData.data.components = {
styles: this.compileHandlebarTemplate(styles_handlebars_1.HANDLEBARS_STYLES)(renderData),
scripts: this.compileHandlebarTemplate(scripts_handlebars_1.HANDLEBARS_SCRIPTS)(renderData),
};
(0, redoc_utils_1.deleteKey)(renderData.data, [
'logo', 'tagGroups', 'auth',
]);
resolve(this.compileHandlebarTemplate(main_handlebars_1.HANDLEBARS_MAIN)(renderData));
}
catch (e) {
reject(e);
}
});
}
static compileHandlebarTemplate(templateString) {
return (0, express_handlebars_1.create)().handlebars.compile(templateString, {
noEscape: true,
preventIndent: false,
});
}
}
exports.RedocModule = RedocModule;