nest-redoc
Version:
A NestJS Frontend for your OpenAPI Specs powered by ReDoc
65 lines (64 loc) • 3.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedocExpressAdapter = void 0;
const redoc_utils_1 = require("../redoc.utils");
const redoc_constants_1 = require("../redoc.constants");
const expressBasicAuth = require("express-basic-auth");
class RedocExpressAdapter {
constructor(path, app, document, options, html) {
this.path = path;
this.app = app;
this.document = document;
this.options = options;
this.html = html;
}
setup() {
return new Promise(async (resolve, reject) => {
var _a, _b, _c;
try {
this.app.getHttpAdapter().get(this.url(), async (request, response) => {
if (await this.authenticate(request, response)) {
response.setHeader('Content-Security-Policy', "default-src * 'unsafe-inline' 'unsafe-eval'; script-src * 'unsafe-inline' 'unsafe-eval'; child-src * 'unsafe-inline' 'unsafe-eval' blob:; worker-src * 'unsafe-inline' 'unsafe-eval' blob:; connect-src * 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src *; style-src * 'unsafe-inline';");
response.setHeader('Content-Type', 'text/html');
response.end(this.html);
}
});
(0, redoc_utils_1.sendHttpAsset)(this.app.getHttpAdapter(), this.url(redoc_constants_1.REDOC_URLS.DEFINITIONS), this.document);
(0, redoc_utils_1.sendHttpAsset)(this.app.getHttpAdapter(), this.url(redoc_constants_1.REDOC_URLS.LOGO), (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.logo) === null || _b === void 0 ? void 0 : _b.url);
(0, redoc_utils_1.sendHttpAsset)(this.app.getHttpAdapter(), this.url(redoc_constants_1.REDOC_URLS.FAVICON), (_c = this.options) === null || _c === void 0 ? void 0 : _c.favicon);
resolve();
}
catch (e) {
reject(e);
}
});
}
authenticate(request, response) {
return new Promise(async (resolve) => {
var _a, _b, _c, _d, _e, _f;
if (!((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.auth) === null || _b === void 0 ? void 0 : _b.enabled)) {
return resolve(true);
}
if (!Array.isArray((_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.auth) === null || _d === void 0 ? void 0 : _d.users) || ((_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.auth) === null || _f === void 0 ? void 0 : _f.users.length) <= 0) {
return resolve(true);
}
const users = {};
this.options.auth.users.map(i => users[i.username] = i.password);
const middleware = expressBasicAuth({
users: users,
challenge: true,
unauthorizedResponse: () => resolve(false),
});
middleware(request, response, () => resolve(true));
});
}
get globalPrefix() {
var _a, _b, _c;
return ((_c = (_b = (_a = this.app) === null || _a === void 0 ? void 0 : _a['container']) === null || _b === void 0 ? void 0 : _b['_applicationConfig']) === null || _c === void 0 ? void 0 : _c['globalPrefix']) || null;
}
url(...parts) {
var _a;
return '/' + (0, redoc_utils_1.joinUrl)((!((_a = this.options) === null || _a === void 0 ? void 0 : _a.useGlobalPrefix) && this.globalPrefix) ? null : this.globalPrefix, this.path, ...parts);
}
}
exports.RedocExpressAdapter = RedocExpressAdapter;