@expressots/adapter-express 1.8.2 → 3.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/CHANGELOG.md +150 -139
- package/lib/cjs/adapter-express/application-express.base.js +2 -12
- package/lib/cjs/adapter-express/application-express.js +132 -52
- package/lib/cjs/adapter-express/application-express.types.js +0 -20
- package/lib/cjs/adapter-express/express-utils/base-middleware.js +2 -2
- package/lib/cjs/adapter-express/express-utils/constants.js +1 -3
- package/lib/cjs/adapter-express/express-utils/decorators.js +26 -12
- package/lib/cjs/adapter-express/express-utils/http-status-middleware.js +10 -5
- package/lib/cjs/adapter-express/express-utils/utils.js +3 -0
- package/lib/cjs/adapter-express/index.js +3 -1
- package/lib/cjs/adapter-express/render/constants.js +40 -0
- package/lib/cjs/adapter-express/render/engine.js +52 -11
- package/lib/cjs/adapter-express/render/index.js +0 -3
- package/lib/cjs/di/di.interfaces.js +10 -0
- package/lib/cjs/types/adapter-express/application-express.base.d.ts +17 -0
- package/lib/cjs/types/adapter-express/application-express.d.ts +58 -23
- package/lib/cjs/types/adapter-express/application-express.types.d.ts +1 -41
- package/lib/cjs/types/adapter-express/express-utils/base-middleware.d.ts +1 -1
- package/lib/cjs/types/adapter-express/express-utils/constants.d.ts +1 -1
- package/lib/cjs/types/adapter-express/express-utils/decorators.d.ts +18 -4
- package/lib/cjs/types/adapter-express/express-utils/http-status-middleware.d.ts +2 -0
- package/lib/cjs/types/adapter-express/express-utils/interfaces.d.ts +1 -1
- package/lib/cjs/types/adapter-express/express-utils/inversify-express-server.d.ts +1 -1
- package/lib/cjs/types/adapter-express/express-utils/utils.d.ts +1 -1
- package/lib/cjs/types/adapter-express/index.d.ts +1 -2
- package/lib/cjs/types/adapter-express/render/constants.d.ts +26 -0
- package/lib/cjs/types/adapter-express/render/engine.d.ts +14 -22
- package/lib/cjs/types/adapter-express/render/index.d.ts +4 -4
- package/lib/cjs/types/di/di.interfaces.d.ts +289 -0
- package/lib/package.json +11 -16
- package/package.json +11 -16
- package/lib/cjs/adapter-express/application-express.interface.js +0 -2
- package/lib/cjs/adapter-express/render/ejs/ejs.config.js +0 -37
- package/lib/cjs/adapter-express/render/ejs/ejs.types.js +0 -3
- package/lib/cjs/adapter-express/render/handlebars/hbs.config.js +0 -38
- package/lib/cjs/adapter-express/render/pug/pug.config.js +0 -25
- package/lib/cjs/types/adapter-express/application-express.interface.d.ts +0 -20
- package/lib/cjs/types/adapter-express/render/ejs/ejs.config.d.ts +0 -21
- package/lib/cjs/types/adapter-express/render/ejs/ejs.types.d.ts +0 -169
- package/lib/cjs/types/adapter-express/render/handlebars/hbs.config.d.ts +0 -20
- package/lib/cjs/types/adapter-express/render/pug/pug.config.d.ts +0 -17
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
7
24
|
};
|
|
8
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
27
|
};
|
|
11
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
29
|
exports.AppExpress = void 0;
|
|
30
|
+
const fs_1 = __importDefault(require("fs"));
|
|
31
|
+
const process_1 = __importStar(require("process"));
|
|
13
32
|
const core_1 = require("@expressots/core");
|
|
14
|
-
const
|
|
15
|
-
const process_1 = __importDefault(require("process"));
|
|
33
|
+
const shared_1 = require("@expressots/shared");
|
|
16
34
|
const application_express_base_1 = require("./application-express.base");
|
|
17
|
-
const application_express_types_1 = require("./application-express.types");
|
|
18
35
|
const http_status_middleware_1 = require("./express-utils/http-status-middleware");
|
|
19
36
|
const inversify_express_server_1 = require("./express-utils/inversify-express-server");
|
|
20
|
-
const ejs_config_1 = require("./render/ejs/ejs.config");
|
|
21
37
|
const engine_1 = require("./render/engine");
|
|
22
|
-
const hbs_config_1 = require("./render/handlebars/hbs.config");
|
|
23
|
-
const pug_config_1 = require("./render/pug/pug.config");
|
|
24
38
|
/**
|
|
25
39
|
* The AppExpress class provides methods for configuring and running an Express application.
|
|
26
40
|
* @class AppExpress
|
|
@@ -32,14 +46,17 @@ const pug_config_1 = require("./render/pug/pug.config");
|
|
|
32
46
|
* @method setEngine - Configures the application's view engine based on the provided configuration options.
|
|
33
47
|
* @method isDevelopment - Verifies if the current environment is development.
|
|
34
48
|
*/
|
|
35
|
-
|
|
49
|
+
class AppExpress extends application_express_base_1.ApplicationBase {
|
|
36
50
|
constructor() {
|
|
37
|
-
super(
|
|
51
|
+
super();
|
|
38
52
|
this.logger = new core_1.Logger();
|
|
53
|
+
this.console = new core_1.Console();
|
|
39
54
|
this.globalPrefix = "/";
|
|
40
55
|
this.middlewares = [];
|
|
41
56
|
this.renderOptions = {};
|
|
57
|
+
this.globalConfiguration();
|
|
42
58
|
}
|
|
59
|
+
globalConfiguration() { }
|
|
43
60
|
configureServices() { }
|
|
44
61
|
postServerInitialization() { }
|
|
45
62
|
serverShutdown() { }
|
|
@@ -51,12 +68,42 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
51
68
|
process_1.default.exit(0);
|
|
52
69
|
}
|
|
53
70
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @param
|
|
71
|
+
* Initialize the InversifyJS container with the provided modules and options.
|
|
72
|
+
* @param appModules - An array of application modules to be loaded into the container.
|
|
73
|
+
* @param containerOptions - Container global configuration options.
|
|
74
|
+
* @option skipBaseClassChecks - Skip the base class checks for the container.
|
|
75
|
+
* @option autoBindInjectable - Automatically bind the injectable classes.
|
|
76
|
+
* @option defaultScope - The default scope to use for bindings.
|
|
77
|
+
*
|
|
78
|
+
* @returns The configured AppContainer instance.
|
|
79
|
+
* @public API
|
|
80
|
+
*/
|
|
81
|
+
configContainer(appModules, containerOptions) {
|
|
82
|
+
this.appContainer = new core_1.AppContainer(containerOptions ? containerOptions : {});
|
|
83
|
+
if (!appModules) {
|
|
84
|
+
this.logger.error("No modules provided for container configuration", "adapter-express");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
this.appContainer.create(appModules);
|
|
88
|
+
this.providerManager = new core_1.ProviderManager(this.appContainer.Container);
|
|
89
|
+
this.middlewareManager = new core_1.Middleware();
|
|
90
|
+
return this.appContainer;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Get the ProviderManager instance.
|
|
94
|
+
* @returns The ProviderManager instance.
|
|
95
|
+
* @public API
|
|
56
96
|
*/
|
|
57
|
-
|
|
58
|
-
this.
|
|
59
|
-
|
|
97
|
+
get Provider() {
|
|
98
|
+
return this.providerManager;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get the Middleware instance.
|
|
102
|
+
* @returns The Middleware instance.
|
|
103
|
+
* @public API
|
|
104
|
+
*/
|
|
105
|
+
get Middleware() {
|
|
106
|
+
return this.middlewareManager;
|
|
60
107
|
}
|
|
61
108
|
/**
|
|
62
109
|
* Configures the Express application with the provided middleware entries.
|
|
@@ -98,22 +145,25 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
98
145
|
* @returns The configured Application instance.
|
|
99
146
|
*/
|
|
100
147
|
async init() {
|
|
148
|
+
if (!this.appContainer) {
|
|
149
|
+
this.logger.error("No container provided for application configuration", "adapter-express");
|
|
150
|
+
(0, process_1.exit)(1);
|
|
151
|
+
}
|
|
101
152
|
await this.configureServices();
|
|
102
|
-
const
|
|
103
|
-
const sortedMiddlewarePipeline = middleware.getMiddlewarePipeline();
|
|
153
|
+
const sortedMiddlewarePipeline = this.Middleware.getMiddlewarePipeline();
|
|
104
154
|
const pipeline = sortedMiddlewarePipeline.map((entry) => entry.middleware);
|
|
105
155
|
this.middlewares.push(...pipeline);
|
|
106
156
|
/* Apply the status code to the response */
|
|
107
|
-
this.middlewares.unshift(new http_status_middleware_1.HttpStatusCodeMiddleware());
|
|
108
|
-
const expressServer = new inversify_express_server_1.InversifyExpressServer(this.
|
|
157
|
+
this.middlewares.unshift(new http_status_middleware_1.HttpStatusCodeMiddleware(this.globalPrefix));
|
|
158
|
+
const expressServer = new inversify_express_server_1.InversifyExpressServer(this.appContainer.Container, null, {
|
|
109
159
|
rootPath: this.globalPrefix,
|
|
110
160
|
});
|
|
111
161
|
expressServer.setConfig((app) => {
|
|
112
162
|
this.configureMiddleware(app, this.middlewares);
|
|
113
163
|
});
|
|
114
164
|
expressServer.setErrorConfig((app) => {
|
|
115
|
-
if (
|
|
116
|
-
app.use(
|
|
165
|
+
if (this.Middleware.getErrorHandler()) {
|
|
166
|
+
app.use(this.Middleware.getErrorHandler());
|
|
117
167
|
}
|
|
118
168
|
});
|
|
119
169
|
this.app = expressServer.build();
|
|
@@ -122,17 +172,17 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
122
172
|
/**
|
|
123
173
|
* Start listening on the given port and environment.
|
|
124
174
|
* @param port - The port number to listen on.
|
|
125
|
-
* @param
|
|
126
|
-
* @
|
|
175
|
+
* @param appInfo - Optional message to display the app name and version.
|
|
176
|
+
* @public API
|
|
127
177
|
*/
|
|
128
|
-
async listen(port,
|
|
178
|
+
async listen(port, appInfo) {
|
|
129
179
|
await this.init();
|
|
130
180
|
await this.configEngine();
|
|
131
|
-
this.
|
|
132
|
-
this.
|
|
133
|
-
this.
|
|
181
|
+
this.environment = this.environment || "development";
|
|
182
|
+
this.app.set("env", this.environment);
|
|
183
|
+
this.port = typeof port === "string" ? parseInt(port, 10) : port || 3000;
|
|
134
184
|
this.app.listen(this.port, () => {
|
|
135
|
-
this.console.messageServer(this.port, this.environment,
|
|
185
|
+
this.console.messageServer(this.port, this.environment, appInfo);
|
|
136
186
|
["SIGTERM", "SIGHUP", "SIGBREAK", "SIGQUIT", "SIGINT"].forEach((signal) => {
|
|
137
187
|
process_1.default.on(signal, this.handleExit.bind(this));
|
|
138
188
|
});
|
|
@@ -141,12 +191,9 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
141
191
|
}
|
|
142
192
|
/**
|
|
143
193
|
* Sets the global route prefix for the application.
|
|
144
|
-
*
|
|
145
|
-
* @public
|
|
146
194
|
* @method setGlobalRoutePrefix
|
|
147
|
-
*
|
|
148
195
|
* @param {string} prefix - The prefix to use for all routes.
|
|
149
|
-
*
|
|
196
|
+
* @public API
|
|
150
197
|
*/
|
|
151
198
|
setGlobalRoutePrefix(prefix) {
|
|
152
199
|
this.globalPrefix = prefix;
|
|
@@ -157,14 +204,14 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
157
204
|
async configEngine() {
|
|
158
205
|
if (this.renderOptions.engine) {
|
|
159
206
|
switch (this.renderOptions.engine) {
|
|
160
|
-
case
|
|
161
|
-
await (0,
|
|
207
|
+
case shared_1.RenderEngine.Engine.HBS:
|
|
208
|
+
await (0, engine_1.setEngineHandlebars)(this.app, this.renderOptions.options);
|
|
162
209
|
break;
|
|
163
|
-
case
|
|
164
|
-
await (0,
|
|
210
|
+
case shared_1.RenderEngine.Engine.EJS:
|
|
211
|
+
await (0, engine_1.setEngineEjs)(this.app, this.renderOptions.options);
|
|
165
212
|
break;
|
|
166
|
-
case
|
|
167
|
-
await (0,
|
|
213
|
+
case shared_1.RenderEngine.Engine.PUG:
|
|
214
|
+
await (0, engine_1.setEnginePug)(this.app, this.renderOptions.options);
|
|
168
215
|
break;
|
|
169
216
|
default:
|
|
170
217
|
throw new Error("Unsupported engine type!");
|
|
@@ -173,13 +220,12 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
173
220
|
}
|
|
174
221
|
/**
|
|
175
222
|
* Configures the application's view engine based on the provided configuration options.
|
|
176
|
-
*
|
|
177
|
-
* @public
|
|
178
223
|
* @method setEngine
|
|
179
224
|
* @template T - A generic type extending from RenderTemplateOptions.
|
|
180
225
|
*
|
|
181
226
|
* @param {Engine} engine - The view engine to set
|
|
182
227
|
* @param {EngineOptions} [options] - The configuration options for the view engine
|
|
228
|
+
* @public API
|
|
183
229
|
*/
|
|
184
230
|
async setEngine(engine, options) {
|
|
185
231
|
try {
|
|
@@ -196,21 +242,58 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
196
242
|
}
|
|
197
243
|
/**
|
|
198
244
|
* Verifies if the current environment is development.
|
|
199
|
-
*
|
|
200
245
|
* @returns A boolean value indicating whether the current environment is development or not.
|
|
246
|
+
* @public API
|
|
201
247
|
*/
|
|
202
248
|
isDevelopment() {
|
|
203
249
|
if (this.app) {
|
|
204
|
-
return this.app.get("env") ===
|
|
250
|
+
return this.app.get("env") === "development";
|
|
205
251
|
}
|
|
206
|
-
this.
|
|
207
|
-
.get(core_1.Logger)
|
|
208
|
-
.error("isDevelopment() method must be called on `PostServerInitialization`", "application");
|
|
252
|
+
this.appContainer.Container.get(core_1.Logger).error("isDevelopment() method must be called on `PostServerInitialization`", "application");
|
|
209
253
|
return false;
|
|
210
254
|
}
|
|
255
|
+
/**
|
|
256
|
+
* Load environment variables from the specified file based on the environment configuration.
|
|
257
|
+
* @param environment - The environment to load configuration for.
|
|
258
|
+
* @param options - The options to use for loading the environment configuration.
|
|
259
|
+
* @option env - The environment configuration options.
|
|
260
|
+
* @example
|
|
261
|
+
* ```typescript
|
|
262
|
+
* {
|
|
263
|
+
env: {
|
|
264
|
+
development: ".env.development",
|
|
265
|
+
production: ".env.production"
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
* ```
|
|
269
|
+
* @public API
|
|
270
|
+
*/
|
|
271
|
+
initEnvironment(environment, options) {
|
|
272
|
+
this.environment = environment;
|
|
273
|
+
if (options === undefined) {
|
|
274
|
+
(0, shared_1.config)({ path: ".env" });
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
if (!options.env[environment]) {
|
|
278
|
+
this.logger.error(`Environment configuration for [${environment}] does not exist.`, "adapter-express");
|
|
279
|
+
process_1.default.exit(1);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
const envFileName = options.env[environment];
|
|
283
|
+
if (!fs_1.default.existsSync(envFileName)) {
|
|
284
|
+
this.logger.error(`Environment file [${envFileName}] does not exist.`, "adapter-express");
|
|
285
|
+
process_1.default.exit(1);
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
(0, shared_1.config)({ path: envFileName });
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
211
293
|
/**
|
|
212
294
|
* Get the underlying HTTP server. (default: Express.js)
|
|
213
295
|
* @returns The underlying HTTP server after initialization.
|
|
296
|
+
* @public API
|
|
214
297
|
*/
|
|
215
298
|
async getHttpServer() {
|
|
216
299
|
if (!this.app) {
|
|
@@ -219,8 +302,5 @@ let AppExpress = class AppExpress extends application_express_base_1.Application
|
|
|
219
302
|
}
|
|
220
303
|
return this.app;
|
|
221
304
|
}
|
|
222
|
-
}
|
|
305
|
+
}
|
|
223
306
|
exports.AppExpress = AppExpress;
|
|
224
|
-
exports.AppExpress = AppExpress = __decorate([
|
|
225
|
-
(0, inversify_binding_decorators_1.provide)(AppExpress)
|
|
226
|
-
], AppExpress);
|
|
@@ -1,22 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ServerEnvironment = exports.ExpressoMiddleware = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Abstract class for creating custom Expresso middleware.
|
|
6
|
-
* Custom middleware classes should extend this class and implement the use method.
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
class ExpressoMiddleware {
|
|
10
|
-
get name() {
|
|
11
|
-
return this.constructor.name;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
exports.ExpressoMiddleware = ExpressoMiddleware;
|
|
15
|
-
/**
|
|
16
|
-
* Enum representing possible server environments.
|
|
17
|
-
*/
|
|
18
|
-
var ServerEnvironment;
|
|
19
|
-
(function (ServerEnvironment) {
|
|
20
|
-
ServerEnvironment["Development"] = "development";
|
|
21
|
-
ServerEnvironment["Production"] = "production";
|
|
22
|
-
})(ServerEnvironment || (exports.ServerEnvironment = ServerEnvironment = {}));
|
|
@@ -7,7 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
};
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.BaseMiddleware = void 0;
|
|
10
|
-
const
|
|
10
|
+
const core_1 = require("@expressots/core");
|
|
11
11
|
let BaseMiddleware = class BaseMiddleware {
|
|
12
12
|
bind(serviceIdentifier) {
|
|
13
13
|
return this.httpContext.container.bind(serviceIdentifier);
|
|
@@ -15,5 +15,5 @@ let BaseMiddleware = class BaseMiddleware {
|
|
|
15
15
|
};
|
|
16
16
|
exports.BaseMiddleware = BaseMiddleware;
|
|
17
17
|
exports.BaseMiddleware = BaseMiddleware = __decorate([
|
|
18
|
-
(0,
|
|
18
|
+
(0, core_1.injectable)()
|
|
19
19
|
], BaseMiddleware);
|
|
@@ -46,7 +46,5 @@ var HTTP_VERBS_ENUM;
|
|
|
46
46
|
})(HTTP_VERBS_ENUM || (exports.HTTP_VERBS_ENUM = HTTP_VERBS_ENUM = {}));
|
|
47
47
|
const DUPLICATED_CONTROLLER_NAME = (name) => `Two controllers cannot have the same name: ${name}`;
|
|
48
48
|
exports.DUPLICATED_CONTROLLER_NAME = DUPLICATED_CONTROLLER_NAME;
|
|
49
|
-
exports.NO_CONTROLLERS_FOUND = "No
|
|
50
|
-
"have been found! Please ensure that you have register" +
|
|
51
|
-
"at least one Controller.";
|
|
49
|
+
exports.NO_CONTROLLERS_FOUND = "No controller found! Please ensure that you have register at least one Controller.";
|
|
52
50
|
exports.DEFAULT_ROUTING_ROOT_PATH = "/";
|
|
@@ -16,15 +16,16 @@ exports.Render = Render;
|
|
|
16
16
|
exports.getRenderMetadata = getRenderMetadata;
|
|
17
17
|
exports.FileUpload = FileUpload;
|
|
18
18
|
require("reflect-metadata");
|
|
19
|
-
const
|
|
19
|
+
const core_1 = require("@expressots/core");
|
|
20
20
|
const constants_1 = require("./constants");
|
|
21
21
|
const resolver_multer_1 = require("./resolver-multer");
|
|
22
|
-
const
|
|
23
|
-
exports.injectHttpContext = (0,
|
|
22
|
+
const core_2 = require("@expressots/core");
|
|
23
|
+
exports.injectHttpContext = (0, core_1.inject)(constants_1.TYPE.HttpContext);
|
|
24
24
|
/**
|
|
25
25
|
* Controller decorator to define a new controller
|
|
26
26
|
* @param path route path
|
|
27
27
|
* @param middleware array of middleware to be applied to all routes in the controller
|
|
28
|
+
* @public API
|
|
28
29
|
*/
|
|
29
30
|
function controller(path, ...middleware) {
|
|
30
31
|
return (target) => {
|
|
@@ -46,7 +47,7 @@ function controller(path, ...middleware) {
|
|
|
46
47
|
Reflect.defineMetadata(constants_1.HTTP_CODE_METADATA.httpCode, statusCodePathMapping, Reflect);
|
|
47
48
|
Reflect.deleteMetadata(constants_1.HTTP_CODE_METADATA.statusCode, Reflect);
|
|
48
49
|
Reflect.deleteMetadata(constants_1.HTTP_CODE_METADATA.path, Reflect);
|
|
49
|
-
(0,
|
|
50
|
+
(0, core_1.decorate)((0, core_1.injectable)(), target);
|
|
50
51
|
Reflect.defineMetadata(constants_1.METADATA_KEY.controller, currentMetadata, target);
|
|
51
52
|
const previousMetadata = Reflect.getMetadata(constants_1.METADATA_KEY.controller, Reflect) || [];
|
|
52
53
|
const newMetadata = [currentMetadata, ...previousMetadata];
|
|
@@ -57,14 +58,14 @@ function controller(path, ...middleware) {
|
|
|
57
58
|
* Http decorator to define the status code for a route
|
|
58
59
|
* @param code
|
|
59
60
|
* @returns MethodDecorator
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
61
|
+
* @example ```typescript
|
|
62
62
|
* @Http(200)
|
|
63
63
|
* @Get("/")
|
|
64
64
|
* hello() {
|
|
65
65
|
* return "Hello World";
|
|
66
66
|
* }
|
|
67
67
|
* ```
|
|
68
|
+
* @public API
|
|
68
69
|
*/
|
|
69
70
|
function Http(code) {
|
|
70
71
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
|
|
@@ -84,6 +85,7 @@ function Http(code) {
|
|
|
84
85
|
* Decorator to allow accept all HTTP methods
|
|
85
86
|
* @param path route path, wildcard
|
|
86
87
|
* @param middleware array of middleware to be applied to all routes defined in path logic
|
|
88
|
+
* @public API
|
|
87
89
|
*/
|
|
88
90
|
function All(path, ...middleware) {
|
|
89
91
|
return Method("all", path, ...middleware);
|
|
@@ -92,6 +94,7 @@ function All(path, ...middleware) {
|
|
|
92
94
|
* Decorator to allow GET HTTP method
|
|
93
95
|
* @param path route path
|
|
94
96
|
* @param middleware array of middleware to be applied to the route
|
|
97
|
+
* @public API
|
|
95
98
|
*/
|
|
96
99
|
function Get(path, ...middleware) {
|
|
97
100
|
return enhancedHttpMethod("get", path, ...middleware);
|
|
@@ -100,6 +103,7 @@ function Get(path, ...middleware) {
|
|
|
100
103
|
* Decorator to allow POST HTTP method
|
|
101
104
|
* @param path route path
|
|
102
105
|
* @param middleware array of middleware to be applied to the route
|
|
106
|
+
* @public API
|
|
103
107
|
*/
|
|
104
108
|
function Post(path, ...middleware) {
|
|
105
109
|
return Method("post", path, ...middleware);
|
|
@@ -108,6 +112,7 @@ function Post(path, ...middleware) {
|
|
|
108
112
|
* Decorator to allow PUT HTTP method
|
|
109
113
|
* @param path route path
|
|
110
114
|
* @param middleware array of middleware to be applied to the route
|
|
115
|
+
* @public API
|
|
111
116
|
*/
|
|
112
117
|
function Put(path, ...middleware) {
|
|
113
118
|
return enhancedHttpMethod("put", path, ...middleware);
|
|
@@ -116,6 +121,7 @@ function Put(path, ...middleware) {
|
|
|
116
121
|
* Decorator to allow PATCH HTTP method
|
|
117
122
|
* @param path route path
|
|
118
123
|
* @param middleware array of middleware to be applied to the route
|
|
124
|
+
* @public API
|
|
119
125
|
*/
|
|
120
126
|
function Patch(path, ...middleware) {
|
|
121
127
|
return enhancedHttpMethod("patch", path, ...middleware);
|
|
@@ -124,6 +130,7 @@ function Patch(path, ...middleware) {
|
|
|
124
130
|
* Decorator to allow HEAD HTTP method
|
|
125
131
|
* @param path route path
|
|
126
132
|
* @param middleware array of middleware to be applied to the route
|
|
133
|
+
* @public API
|
|
127
134
|
*/
|
|
128
135
|
function Head(path, ...middleware) {
|
|
129
136
|
return Method("head", path, ...middleware);
|
|
@@ -132,6 +139,7 @@ function Head(path, ...middleware) {
|
|
|
132
139
|
* Decorator to allow DELETE HTTP method
|
|
133
140
|
* @param path route path
|
|
134
141
|
* @param middleware array of middleware to be applied to the route
|
|
142
|
+
* @public API
|
|
135
143
|
*/
|
|
136
144
|
function Delete(path, ...middleware) {
|
|
137
145
|
return enhancedHttpMethod("delete", path, ...middleware);
|
|
@@ -191,6 +199,7 @@ function enhancedHttpMethod(method, path, ...middleware) {
|
|
|
191
199
|
* @param method custom HTTP method
|
|
192
200
|
* @param path route path
|
|
193
201
|
* @param middleware array of middleware to be applied to the route
|
|
202
|
+
* @public API
|
|
194
203
|
*/
|
|
195
204
|
function Method(method, path, ...middleware) {
|
|
196
205
|
return (target, key) => {
|
|
@@ -281,6 +290,9 @@ function paramDecoratorFactory(parameterType) {
|
|
|
281
290
|
/**
|
|
282
291
|
* Parameter decorator to inject the request object
|
|
283
292
|
* @returns ParameterDecorator
|
|
293
|
+
* @param type - The type of parameter to inject
|
|
294
|
+
* @param parameterName - The name of the parameter to inject
|
|
295
|
+
* @public API
|
|
284
296
|
*/
|
|
285
297
|
function params(type, parameterName) {
|
|
286
298
|
return (target, methodName, index) => {
|
|
@@ -310,7 +322,8 @@ function params(type, parameterName) {
|
|
|
310
322
|
* Render decorator to define the template and default data for a route
|
|
311
323
|
* @param template The template to render
|
|
312
324
|
* @param defaultData The default data to pass to the template
|
|
313
|
-
* @returns
|
|
325
|
+
* @returns MethodDecorator
|
|
326
|
+
* @public API
|
|
314
327
|
*/
|
|
315
328
|
function Render(template, defaultData) {
|
|
316
329
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -357,6 +370,7 @@ function isResponse(obj) {
|
|
|
357
370
|
* @param multerOptions
|
|
358
371
|
* @default { none: true }
|
|
359
372
|
* @returns MethodDecorator
|
|
373
|
+
* @public API
|
|
360
374
|
*/
|
|
361
375
|
function FileUpload(options, multerOptions) {
|
|
362
376
|
const multer = (0, resolver_multer_1.packageResolver)("multer");
|
|
@@ -398,7 +412,7 @@ function FileUpload(options, multerOptions) {
|
|
|
398
412
|
* @returns
|
|
399
413
|
*/
|
|
400
414
|
function inferMulterMethod(options) {
|
|
401
|
-
const report = new
|
|
415
|
+
const report = new core_2.Report();
|
|
402
416
|
if (options.none)
|
|
403
417
|
return "none";
|
|
404
418
|
if (options.any)
|
|
@@ -409,17 +423,17 @@ function inferMulterMethod(options) {
|
|
|
409
423
|
return "array";
|
|
410
424
|
if (options.fieldName)
|
|
411
425
|
return "single";
|
|
412
|
-
throw report.error("Invalid options provided for FileUpload.",
|
|
426
|
+
throw report.error("Invalid options provided for FileUpload.", core_2.StatusCode.InternalServerError, "multer-decorator");
|
|
413
427
|
}
|
|
414
428
|
/**
|
|
415
429
|
* Get the multer middleware based on the method
|
|
416
430
|
* @param upload
|
|
417
431
|
* @param options
|
|
418
432
|
* @param method
|
|
419
|
-
* @returns
|
|
433
|
+
* @returns RequestHandler
|
|
420
434
|
*/
|
|
421
435
|
function getMulterMiddleware(upload, options, method) {
|
|
422
|
-
const report = new
|
|
436
|
+
const report = new core_2.Report();
|
|
423
437
|
switch (method) {
|
|
424
438
|
case "single":
|
|
425
439
|
return upload.single(options.fieldName);
|
|
@@ -437,6 +451,6 @@ function getMulterMiddleware(upload, options, method) {
|
|
|
437
451
|
case "any":
|
|
438
452
|
return upload.any();
|
|
439
453
|
default:
|
|
440
|
-
throw report.error(`Unsupported Multer method: ${method}`,
|
|
454
|
+
throw report.error(`Unsupported Multer method: ${method}`, core_2.StatusCode.InternalServerError, "multer-decorator");
|
|
441
455
|
}
|
|
442
456
|
}
|
|
@@ -9,14 +9,19 @@ const constants_1 = require("./constants");
|
|
|
9
9
|
* @returns express.RequestHandler
|
|
10
10
|
*/
|
|
11
11
|
class HttpStatusCodeMiddleware extends core_1.ExpressoMiddleware {
|
|
12
|
+
constructor(globalPrefix = "/") {
|
|
13
|
+
super();
|
|
14
|
+
this.globalPrefix = globalPrefix;
|
|
15
|
+
}
|
|
12
16
|
use(req, res, next) {
|
|
13
17
|
const statusCodeMapping = Reflect.getMetadata(constants_1.HTTP_CODE_METADATA.httpCode, Reflect);
|
|
14
|
-
let path = req.path
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
path = "/";
|
|
18
|
+
let path = req.path;
|
|
19
|
+
if (this.globalPrefix !== "/" && path.startsWith(this.globalPrefix)) {
|
|
20
|
+
path = path.slice(this.globalPrefix.length);
|
|
18
21
|
}
|
|
19
|
-
path =
|
|
22
|
+
path = path.endsWith("/") ? path.slice(0, -1) : path;
|
|
23
|
+
const formattedMethod = req.method.toLowerCase();
|
|
24
|
+
path = `${path || "/"}/-${formattedMethod}`;
|
|
20
25
|
const statusCode = statusCodeMapping[path];
|
|
21
26
|
if (statusCode) {
|
|
22
27
|
res.status(statusCode);
|
|
@@ -7,12 +7,15 @@ exports.getControllerMethodMetadata = getControllerMethodMetadata;
|
|
|
7
7
|
exports.getControllerParameterMetadata = getControllerParameterMetadata;
|
|
8
8
|
exports.cleanUpMetadata = cleanUpMetadata;
|
|
9
9
|
exports.instanceOfIHttpActionResult = instanceOfIHttpActionResult;
|
|
10
|
+
const core_1 = require("@expressots/core");
|
|
10
11
|
const constants_1 = require("./constants");
|
|
11
12
|
function getControllersFromContainer(container, forceControllers) {
|
|
12
13
|
if (container.isBound(constants_1.TYPE.Controller)) {
|
|
13
14
|
return container.getAll(constants_1.TYPE.Controller);
|
|
14
15
|
}
|
|
15
16
|
if (forceControllers) {
|
|
17
|
+
const logger = new core_1.Logger();
|
|
18
|
+
logger.error(constants_1.NO_CONTROLLERS_FOUND, "adapter-express");
|
|
16
19
|
throw new Error(constants_1.NO_CONTROLLERS_FOUND);
|
|
17
20
|
}
|
|
18
21
|
else {
|
|
@@ -14,8 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.AppExpress = void 0;
|
|
17
|
+
exports.RenderEngine = exports.AppExpress = void 0;
|
|
18
18
|
__exportStar(require("./express-utils"), exports);
|
|
19
19
|
var application_express_1 = require("./application-express");
|
|
20
20
|
Object.defineProperty(exports, "AppExpress", { enumerable: true, get: function () { return application_express_1.AppExpress; } });
|
|
21
|
+
var shared_1 = require("@expressots/shared");
|
|
22
|
+
Object.defineProperty(exports, "RenderEngine", { enumerable: true, get: function () { return shared_1.RenderEngine; } });
|
|
21
23
|
__exportStar(require("./render"), exports);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PUG_DEFAULTS = exports.DEFAULT_PARTIALS_DIR = exports.HANDLEBARS_DEFAULTS = exports.EJS_DEFAULTS = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
/**
|
|
6
|
+
* Ejs defaults
|
|
7
|
+
* @type {EjsOptions}
|
|
8
|
+
* @constant
|
|
9
|
+
* @default
|
|
10
|
+
*/
|
|
11
|
+
exports.EJS_DEFAULTS = {
|
|
12
|
+
viewsDir: (0, path_1.join)(process.cwd(), "views"),
|
|
13
|
+
viewEngine: "ejs",
|
|
14
|
+
serverOptions: {},
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Handlebars defaults
|
|
18
|
+
* @type {HandlebarsOptions}
|
|
19
|
+
* @constant
|
|
20
|
+
* @default
|
|
21
|
+
*/
|
|
22
|
+
exports.HANDLEBARS_DEFAULTS = {
|
|
23
|
+
viewEngine: "hbs",
|
|
24
|
+
viewsDir: (0, path_1.join)(process.cwd(), "views"),
|
|
25
|
+
partialsDir: (0, path_1.join)(process.cwd(), "views/partials"),
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Default partials directory
|
|
29
|
+
*/
|
|
30
|
+
exports.DEFAULT_PARTIALS_DIR = (0, path_1.join)(process.cwd(), "views/partials");
|
|
31
|
+
/**
|
|
32
|
+
* Pug defaults
|
|
33
|
+
* @type {PugOptions}
|
|
34
|
+
* @constant
|
|
35
|
+
* @default
|
|
36
|
+
*/
|
|
37
|
+
exports.PUG_DEFAULTS = {
|
|
38
|
+
viewEngine: "pug",
|
|
39
|
+
viewsDir: (0, path_1.join)(process.cwd(), "views"),
|
|
40
|
+
};
|