@nxarch/nest-nguniversal
Version:
A NestJS library to serve your NestJS Angular Universal project
66 lines (65 loc) • 3.02 kB
JavaScript
;
var AngularUniversalModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AngularUniversalModule = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const fs_1 = require("fs");
const path_1 = require("path");
require("reflect-metadata");
const angular_universal_constants_1 = require("./angular-universal.constants");
const angular_universal_providers_1 = require("./angular-universal.providers");
const in_memory_cache_storage_1 = require("./cache/in-memory-cache.storage");
const setup_bs_reload_watcher_1 = require("./utils/setup-bs-reload-watcher");
let AngularUniversalModule = AngularUniversalModule_1 = class AngularUniversalModule {
constructor(ngOptions, httpAdapterHost) {
this.ngOptions = ngOptions;
this.httpAdapterHost = httpAdapterHost;
}
static forRoot(options) {
var _a;
const indexHtml = (0, fs_1.existsSync)((0, path_1.join)(options.viewsPath, 'index.original.html')) ? 'index.original.html' : 'index';
if (process.env.NXARCH_SERVER_AUTO_SYNC === 'true')
(0, setup_bs_reload_watcher_1.setupBsReloadWatcher)();
options = Object.assign({ templatePath: indexHtml, rootStaticPath: '*.*', renderPath: '*' }, options);
return {
module: AngularUniversalModule_1,
providers: [
{
provide: angular_universal_constants_1.ANGULAR_UNIVERSAL_OPTIONS,
useValue: options,
},
Object.assign({ provide: angular_universal_constants_1.ANGULAR_UNIVERSAL_CACHE }, (((_a = options === null || options === void 0 ? void 0 : options.cache) === null || _a === void 0 ? void 0 : _a.storage) || { useValue: new in_memory_cache_storage_1.InMemoryCacheStorage() })),
],
};
}
onModuleInit() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.ngOptions.useCustomRenderEndpoint)
return;
if (!this.httpAdapterHost) {
return;
}
const httpAdapter = this.httpAdapterHost.httpAdapter;
if (!httpAdapter) {
return;
}
const app = httpAdapter.getInstance();
app.get(this.ngOptions.renderPath, (req, res) => {
res.render(this.ngOptions.templatePath, {
req,
res,
});
});
});
}
};
AngularUniversalModule = AngularUniversalModule_1 = tslib_1.__decorate([
(0, common_1.Module)({
providers: [...angular_universal_providers_1.angularUniversalProviders],
}),
tslib_1.__param(0, (0, common_1.Inject)(angular_universal_constants_1.ANGULAR_UNIVERSAL_OPTIONS)),
tslib_1.__metadata("design:paramtypes", [Object, core_1.HttpAdapterHost])
], AngularUniversalModule);
exports.AngularUniversalModule = AngularUniversalModule;