UNPKG

nestjs-mvc-tools

Version:

NestJS MVC Tools is a small set of tools designed to help you get started more easily with traditional web development approaches in NestJS.

80 lines (79 loc) 4.12 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var NestMvcModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestMvcModule = void 0; const common_1 = require("@nestjs/common"); const nest_mvc_csrf_middleware_1 = require("./middlewares/nest-mvc-csrf.middleware"); const nest_mvc_flash_middleware_1 = require("./middlewares/nest-mvc-flash.middleware"); const nest_mvc_view_middleware_1 = require("./middlewares/nest-mvc-view.middleware"); const edge_js_service_1 = require("./services/edge-js.service"); const nest_mvc_csrf_service_1 = require("./services/nest-mvc-csrf.service"); const nest_mvc_logger_service_1 = require("./services/nest-mvc-logger.service"); const nest_mvc_options_service_1 = require("./services/nest-mvc-options.service"); const vite_asset_path_helper_factory_service_1 = require("./services/vite-asset-path-helper-factory.service"); /** * NestJS MVC module providing server-side rendering capabilities. * Integrates Edge.js templating, Vite asset pipeline, and CSRF protection. */ let NestMvcModule = NestMvcModule_1 = class NestMvcModule { configure(consumer) { consumer .apply(nest_mvc_view_middleware_1.NestMvcViewMiddleware, // Middleware that creates renderer for each request nest_mvc_flash_middleware_1.NestMvcFlashMiddleware, // Middleware that provides flash messages to renderer state nest_mvc_csrf_middleware_1.NestMvcCsrfMiddleware // Middleware that generates and validates CSRF tokens ) .forRoutes("*"); } /** * Registers the module synchronously with static configuration. * @param options - Static configuration options for the module * @returns Dynamic module configuration */ static forRoot(options) { // Create raw options provider const nestMvcOptionsProvider = { provide: "NEST_MVC_OPTIONS", useValue: options, }; const providers = [ // Additional options provider nestMvcOptionsProvider, // Service that provides options so each internal class can use only the options it needs nest_mvc_options_service_1.NestMvcOptionsService, // Service that provides EdgeJs library for use in NestJS edge_js_service_1.EdgeJsService, // Service that provides helper functions for easy Vite asset path management vite_asset_path_helper_factory_service_1.ViteAssetPathHelperFactoryService, // Service that provides CSRF token generation and validation nest_mvc_csrf_service_1.NestMvcCsrfService, // NestMvc dedicated logger nest_mvc_logger_service_1.NestMvcLoggerService, ]; // Create globals factory provider if globalsFactory and globalsInjects are provided const viewOptions = options?.view; if (viewOptions?.globalsFactory && viewOptions?.globalsInjects) { const globalsFactoryProvider = { provide: edge_js_service_1.GLOBALS_FACTORY_PROVIDER_TOKEN, useFactory: viewOptions.globalsFactory, inject: viewOptions.globalsInjects, }; providers.push(globalsFactoryProvider); } return { global: true, module: NestMvcModule_1, providers, exports: [edge_js_service_1.EdgeJsService] }; } }; exports.NestMvcModule = NestMvcModule; exports.NestMvcModule = NestMvcModule = NestMvcModule_1 = __decorate([ (0, common_1.Module)({}) ], NestMvcModule);