UNPKG

6.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const logger_service_1 = require("@nestjs/common/services/logger.service");
4const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const application_config_1 = require("./application-config");
7const constants_1 = require("./constants");
8const exceptions_zone_1 = require("./errors/exceptions-zone");
9const load_adapter_1 = require("./helpers/load-adapter");
10const container_1 = require("./injector/container");
11const instance_loader_1 = require("./injector/instance-loader");
12const metadata_scanner_1 = require("./metadata-scanner");
13const nest_application_1 = require("./nest-application");
14const nest_application_context_1 = require("./nest-application-context");
15const scanner_1 = require("./scanner");
16/**
17 * @publicApi
18 */
19class NestFactoryStatic {
20 constructor() {
21 this.logger = new logger_service_1.Logger('NestFactory', true);
22 }
23 async create(module, serverOrOptions, options) {
24 // tslint:disable-next-line:prefer-const
25 let [httpServer, appOptions] = this.isHttpServer(serverOrOptions)
26 ? [serverOrOptions, options]
27 : [this.createHttpAdapter(), serverOrOptions];
28 const applicationConfig = new application_config_1.ApplicationConfig();
29 const container = new container_1.NestContainer(applicationConfig);
30 this.applyLogger(appOptions);
31 await this.initialize(module, container, applicationConfig, httpServer);
32 const instance = new nest_application_1.NestApplication(container, httpServer, applicationConfig, appOptions);
33 const target = this.createNestInstance(instance);
34 return this.createAdapterProxy(target, httpServer);
35 }
36 /**
37 * Creates an instance of NestMicroservice.
38 *
39 * @param module Entry (root) application module class
40 * @param options Optional microservice configuration
41 *
42 * @returns A promise that, when resolved,
43 * contains a reference to the NestMicroservice instance.
44 */
45 async createMicroservice(module, options) {
46 const { NestMicroservice } = load_package_util_1.loadPackage('@nestjs/microservices', 'NestFactory', () => require('@nestjs/microservices'));
47 const applicationConfig = new application_config_1.ApplicationConfig();
48 const container = new container_1.NestContainer(applicationConfig);
49 this.applyLogger(options);
50 await this.initialize(module, container, applicationConfig);
51 return this.createNestInstance(new NestMicroservice(container, options, applicationConfig));
52 }
53 /**
54 * Creates an instance of NestApplicationContext.
55 *
56 * @param module Entry (root) application module class
57 * @param options Optional Nest application configuration
58 *
59 * @returns A promise that, when resolved,
60 * contains a reference to the NestApplicationContext instance.
61 */
62 async createApplicationContext(module, options) {
63 const container = new container_1.NestContainer();
64 this.applyLogger(options);
65 await this.initialize(module, container);
66 const modules = container.getModules().values();
67 const root = modules.next().value;
68 const context = this.createNestInstance(new nest_application_context_1.NestApplicationContext(container, [], root));
69 return context.init();
70 }
71 createNestInstance(instance) {
72 return this.createProxy(instance);
73 }
74 async initialize(module, container, config = new application_config_1.ApplicationConfig(), httpServer = null) {
75 const instanceLoader = new instance_loader_1.InstanceLoader(container);
76 const dependenciesScanner = new scanner_1.DependenciesScanner(container, new metadata_scanner_1.MetadataScanner(), config);
77 container.setHttpAdapter(httpServer);
78 try {
79 this.logger.log(constants_1.MESSAGES.APPLICATION_START);
80 await exceptions_zone_1.ExceptionsZone.asyncRun(async () => {
81 await dependenciesScanner.scan(module);
82 await instanceLoader.createInstancesOfDependencies();
83 dependenciesScanner.applyApplicationProviders();
84 });
85 }
86 catch (e) {
87 process.abort();
88 }
89 }
90 createProxy(target) {
91 const proxy = this.createExceptionProxy();
92 return new Proxy(target, {
93 get: proxy,
94 set: proxy,
95 });
96 }
97 createExceptionProxy() {
98 return (receiver, prop) => {
99 if (!(prop in receiver)) {
100 return;
101 }
102 if (shared_utils_1.isFunction(receiver[prop])) {
103 return this.createExceptionZone(receiver, prop);
104 }
105 return receiver[prop];
106 };
107 }
108 createExceptionZone(receiver, prop) {
109 return (...args) => {
110 let result;
111 exceptions_zone_1.ExceptionsZone.run(() => {
112 result = receiver[prop](...args);
113 });
114 return result;
115 };
116 }
117 applyLogger(options) {
118 if (!options) {
119 return;
120 }
121 !shared_utils_1.isNil(options.logger) && logger_service_1.Logger.overrideLogger(options.logger);
122 }
123 createHttpAdapter(httpServer) {
124 const { ExpressAdapter } = load_adapter_1.loadAdapter('@nestjs/platform-express', 'HTTP', () => require('@nestjs/platform-express'));
125 return new ExpressAdapter(httpServer);
126 }
127 isHttpServer(serverOrOptions) {
128 return !!(serverOrOptions && serverOrOptions.patch);
129 }
130 createAdapterProxy(app, adapter) {
131 return new Proxy(app, {
132 get: (receiver, prop) => {
133 if (!(prop in receiver) && prop in adapter) {
134 return this.createExceptionZone(adapter, prop);
135 }
136 return receiver[prop];
137 },
138 });
139 }
140}
141exports.NestFactoryStatic = NestFactoryStatic;
142/**
143 * Use NestFactory to create an application instance.
144 *
145 * ### Specifying an entry module
146 *
147 * Pass the required *root module* for the application via the module parameter.
148 * By convention, it is usually called `ApplicationModule`. Starting with this
149 * module, Nest assembles the dependency graph and begins the process of
150 * Dependency Injection and instantiates the classes needed to launch your
151 * application.
152 *
153 * @publicApi
154 */
155exports.NestFactory = new NestFactoryStatic();