@tsed/platform-express 7.0.0-beta.2 → 7.0.0-beta.20
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/coverage.json +6 -0
- package/jest.config.js +9 -0
- package/lib/cjs/components/PlatformExpress.js +126 -66
- package/lib/cjs/components/PlatformExpress.js.map +1 -1
- package/lib/cjs/exports.js +7 -0
- package/lib/cjs/exports.js.map +1 -0
- package/lib/cjs/index.js +9 -12
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/PlatformExpressSettings.js.map +1 -1
- package/lib/cjs/interfaces/interfaces.js +3 -0
- package/lib/cjs/interfaces/interfaces.js.map +1 -0
- package/lib/cjs/middlewares/staticsMiddleware.js +1 -1
- package/lib/cjs/middlewares/staticsMiddleware.js.map +1 -1
- package/lib/cjs/package.json +3 -0
- package/lib/esm/components/PlatformExpress.js +126 -48
- package/lib/esm/components/PlatformExpress.js.map +1 -1
- package/lib/esm/exports.js +5 -0
- package/lib/esm/exports.js.map +1 -0
- package/lib/esm/index.js +9 -12
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/PlatformExpressSettings.js.map +1 -1
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/types/components/PlatformExpress.d.ts +40 -22
- package/lib/types/exports.d.ts +1 -0
- package/lib/types/index.d.ts +8 -3
- package/lib/types/interfaces/PlatformExpressSettings.d.ts +15 -1
- package/lib/types/interfaces/{index.d.ts → interfaces.d.ts} +6 -4
- package/package.json +64 -26
- package/readme.md +3 -4
- package/tsconfig.compile.esm.json +1 -12
- package/lib/cjs/decorators/caseSensitive.js +0 -31
- package/lib/cjs/decorators/caseSensitive.js.map +0 -1
- package/lib/cjs/decorators/index.js +0 -8
- package/lib/cjs/decorators/index.js.map +0 -1
- package/lib/cjs/decorators/mergeParams.js +0 -31
- package/lib/cjs/decorators/mergeParams.js.map +0 -1
- package/lib/cjs/decorators/routerSettings.js +0 -32
- package/lib/cjs/decorators/routerSettings.js.map +0 -1
- package/lib/cjs/decorators/strict.js +0 -31
- package/lib/cjs/decorators/strict.js.map +0 -1
- package/lib/cjs/interfaces/index.js +0 -6
- package/lib/cjs/interfaces/index.js.map +0 -1
- package/lib/cjs/middlewares/rawBodyMiddleware.js +0 -27
- package/lib/cjs/middlewares/rawBodyMiddleware.js.map +0 -1
- package/lib/cjs/services/PlatformExpressApplication.js +0 -32
- package/lib/cjs/services/PlatformExpressApplication.js.map +0 -1
- package/lib/cjs/services/PlatformExpressHandler.js +0 -31
- package/lib/cjs/services/PlatformExpressHandler.js.map +0 -1
- package/lib/cjs/services/PlatformExpressRequest.js +0 -12
- package/lib/cjs/services/PlatformExpressRequest.js.map +0 -1
- package/lib/cjs/services/PlatformExpressResponse.js +0 -12
- package/lib/cjs/services/PlatformExpressResponse.js.map +0 -1
- package/lib/cjs/services/PlatformExpressRouter.js +0 -34
- package/lib/cjs/services/PlatformExpressRouter.js.map +0 -1
- package/lib/cjs/services/index.js +0 -9
- package/lib/cjs/services/index.js.map +0 -1
- package/lib/esm/decorators/caseSensitive.js +0 -27
- package/lib/esm/decorators/caseSensitive.js.map +0 -1
- package/lib/esm/decorators/index.js +0 -5
- package/lib/esm/decorators/index.js.map +0 -1
- package/lib/esm/decorators/mergeParams.js +0 -27
- package/lib/esm/decorators/mergeParams.js.map +0 -1
- package/lib/esm/decorators/routerSettings.js +0 -28
- package/lib/esm/decorators/routerSettings.js.map +0 -1
- package/lib/esm/decorators/strict.js +0 -27
- package/lib/esm/decorators/strict.js.map +0 -1
- package/lib/esm/interfaces/index.js +0 -3
- package/lib/esm/interfaces/index.js.map +0 -1
- package/lib/esm/middlewares/rawBodyMiddleware.js +0 -22
- package/lib/esm/middlewares/rawBodyMiddleware.js.map +0 -1
- package/lib/esm/services/PlatformExpressApplication.js +0 -29
- package/lib/esm/services/PlatformExpressApplication.js.map +0 -1
- package/lib/esm/services/PlatformExpressHandler.js +0 -27
- package/lib/esm/services/PlatformExpressHandler.js.map +0 -1
- package/lib/esm/services/PlatformExpressRequest.js +0 -8
- package/lib/esm/services/PlatformExpressRequest.js.map +0 -1
- package/lib/esm/services/PlatformExpressResponse.js +0 -8
- package/lib/esm/services/PlatformExpressResponse.js.map +0 -1
- package/lib/esm/services/PlatformExpressRouter.js +0 -31
- package/lib/esm/services/PlatformExpressRouter.js.map +0 -1
- package/lib/esm/services/index.js +0 -6
- package/lib/esm/services/index.js.map +0 -1
- package/lib/types/decorators/caseSensitive.d.ts +0 -23
- package/lib/types/decorators/index.d.ts +0 -4
- package/lib/types/decorators/mergeParams.d.ts +0 -23
- package/lib/types/decorators/routerSettings.d.ts +0 -24
- package/lib/types/decorators/strict.d.ts +0 -23
- package/lib/types/middlewares/rawBodyMiddleware.d.ts +0 -4
- package/lib/types/services/PlatformExpressApplication.d.ts +0 -24
- package/lib/types/services/PlatformExpressHandler.d.ts +0 -9
- package/lib/types/services/PlatformExpressRequest.d.ts +0 -22
- package/lib/types/services/PlatformExpressResponse.d.ts +0 -14
- package/lib/types/services/PlatformExpressRouter.d.ts +0 -18
- package/lib/types/services/index.d.ts +0 -5
package/coverage.json
ADDED
package/jest.config.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "platform-express"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: require("./coverage.json")
|
|
8
|
+
}
|
|
9
|
+
};
|
|
@@ -1,58 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
3
|
exports.PlatformExpress = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
23
5
|
const common_1 = require("@tsed/common");
|
|
24
6
|
const core_1 = require("@tsed/core");
|
|
25
|
-
const
|
|
26
|
-
const
|
|
7
|
+
const express_1 = tslib_1.__importDefault(require("express"));
|
|
8
|
+
const util_1 = require("util");
|
|
9
|
+
const staticsMiddleware_1 = require("../middlewares/staticsMiddleware");
|
|
27
10
|
/**
|
|
28
11
|
* @platform
|
|
29
12
|
* @express
|
|
30
13
|
*/
|
|
31
14
|
class PlatformExpress {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
provide: common_1.PlatformRouter,
|
|
41
|
-
useClass: services_1.PlatformExpressRouter
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
provide: common_1.PlatformHandler,
|
|
45
|
-
useClass: services_1.PlatformExpressHandler
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
provide: common_1.PlatformResponse,
|
|
49
|
-
useClass: services_1.PlatformExpressResponse
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
provide: common_1.PlatformRequest,
|
|
53
|
-
useClass: services_1.PlatformExpressRequest
|
|
54
|
-
}
|
|
55
|
-
];
|
|
15
|
+
injector;
|
|
16
|
+
providers = [];
|
|
17
|
+
#multer;
|
|
18
|
+
constructor(injector) {
|
|
19
|
+
this.injector = injector;
|
|
20
|
+
Promise.resolve().then(() => tslib_1.__importStar(require("multer"))).then(({ default: multer }) => (this.#multer = multer));
|
|
56
21
|
}
|
|
57
22
|
/**
|
|
58
23
|
* Create new serverless application. In this mode, the component scan are disabled.
|
|
@@ -76,48 +41,143 @@ class PlatformExpress {
|
|
|
76
41
|
adapter: PlatformExpress
|
|
77
42
|
});
|
|
78
43
|
}
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
44
|
+
onInit() {
|
|
45
|
+
const middlewares = this.injector.settings.get("middlewares", []);
|
|
46
|
+
this.injector.settings.set("middlewares", middlewares.filter((middleware) => {
|
|
47
|
+
const name = (0, core_1.nameOf)(middleware);
|
|
48
|
+
if (["textParser", "jsonParser", "rawParser", "urlencodedParser"].includes(name)) {
|
|
49
|
+
this.injector.settings.set(`express.bodyParser.${name.replace("Parser", "")}`, () => middleware);
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}));
|
|
85
54
|
}
|
|
86
55
|
async beforeLoadRoutes() {
|
|
87
|
-
const
|
|
56
|
+
const injector = this.injector;
|
|
57
|
+
const app = this.injector.get(common_1.PlatformApplication);
|
|
88
58
|
// disable x-powered-by header
|
|
89
59
|
injector.settings.get("env") === core_1.Env.PROD && app.getApp().disable("x-powered-by");
|
|
90
60
|
await this.configureViewsEngine();
|
|
91
61
|
}
|
|
92
62
|
async afterLoadRoutes() {
|
|
93
|
-
const
|
|
63
|
+
const app = this.injector.get(common_1.PlatformApplication);
|
|
64
|
+
const platformExceptions = this.injector.get(common_1.PlatformExceptions);
|
|
94
65
|
// NOT FOUND
|
|
95
66
|
app.use((req, res, next) => {
|
|
96
|
-
|
|
97
|
-
|
|
67
|
+
const { $ctx } = req;
|
|
68
|
+
!$ctx.isDone() && platformExceptions?.resourceNotFound(req.$ctx);
|
|
98
69
|
});
|
|
99
70
|
// EXCEPTION FILTERS
|
|
100
71
|
app.use((err, req, res, next) => {
|
|
101
|
-
|
|
102
|
-
|
|
72
|
+
const { $ctx } = req;
|
|
73
|
+
!$ctx.isDone() && platformExceptions?.catch(err, $ctx);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
mapLayers(layers) {
|
|
77
|
+
const app = this.getPlatformApplication();
|
|
78
|
+
const rawApp = app.getApp();
|
|
79
|
+
layers.forEach((layer) => {
|
|
80
|
+
switch (layer.method) {
|
|
81
|
+
case "statics":
|
|
82
|
+
rawApp.use(layer.path, this.statics(layer.path, layer.opts));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
rawApp[layer.method](...layer.getArgs());
|
|
103
86
|
});
|
|
104
87
|
}
|
|
88
|
+
mapHandler(handler, metadata) {
|
|
89
|
+
switch (metadata.type) {
|
|
90
|
+
case common_1.PlatformHandlerType.RAW_FN:
|
|
91
|
+
case common_1.PlatformHandlerType.RAW_ERR_FN:
|
|
92
|
+
return handler;
|
|
93
|
+
case common_1.PlatformHandlerType.ERR_MIDDLEWARE:
|
|
94
|
+
return async (error, req, res, next) => {
|
|
95
|
+
return (0, common_1.runInContext)(req.$ctx, () => {
|
|
96
|
+
const { $ctx } = req;
|
|
97
|
+
$ctx.next = next;
|
|
98
|
+
$ctx.error = error;
|
|
99
|
+
return handler($ctx);
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
default:
|
|
103
|
+
return (req, res, next) => {
|
|
104
|
+
return (0, common_1.runInContext)(req.$ctx, () => {
|
|
105
|
+
req.$ctx.next = next;
|
|
106
|
+
handler(req.$ctx);
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
105
111
|
useContext() {
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
app.
|
|
110
|
-
await invoke({ request, response });
|
|
111
|
-
|
|
112
|
+
const app = this.getPlatformApplication();
|
|
113
|
+
const invoke = (0, common_1.createContext)(this.injector);
|
|
114
|
+
this.injector.logger.debug("Mount app context");
|
|
115
|
+
app.use(async (request, response, next) => {
|
|
116
|
+
const $ctx = await invoke({ request, response });
|
|
117
|
+
await $ctx.start();
|
|
118
|
+
$ctx.response.getRes().on("finish", () => $ctx.finish());
|
|
119
|
+
return (0, common_1.runInContext)($ctx, next);
|
|
112
120
|
});
|
|
113
121
|
return this;
|
|
114
122
|
}
|
|
123
|
+
app() {
|
|
124
|
+
const app = this.injector.settings.get("express.app") || (0, express_1.default)();
|
|
125
|
+
return {
|
|
126
|
+
app,
|
|
127
|
+
callback: () => app
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
multipart(options) {
|
|
131
|
+
const m = this.#multer(options);
|
|
132
|
+
const makePromise = (multer, name) => {
|
|
133
|
+
// istanbul ignore next
|
|
134
|
+
if (!multer[name])
|
|
135
|
+
return;
|
|
136
|
+
const fn = multer[name];
|
|
137
|
+
multer[name] = function apply(...args) {
|
|
138
|
+
const middleware = Reflect.apply(fn, this, args);
|
|
139
|
+
return (req, res) => (0, util_1.promisify)(middleware)(req, res);
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
makePromise(m, "any");
|
|
143
|
+
makePromise(m, "array");
|
|
144
|
+
makePromise(m, "fields");
|
|
145
|
+
makePromise(m, "none");
|
|
146
|
+
makePromise(m, "single");
|
|
147
|
+
return m;
|
|
148
|
+
}
|
|
149
|
+
statics(endpoint, options) {
|
|
150
|
+
const { root, ...props } = options;
|
|
151
|
+
return (0, staticsMiddleware_1.staticsMiddleware)(root, props);
|
|
152
|
+
}
|
|
153
|
+
bodyParser(type, additionalOptions = {}) {
|
|
154
|
+
const opts = this.injector.settings.get(`express.bodyParser.${type}`);
|
|
155
|
+
let parser = express_1.default[type];
|
|
156
|
+
let options = {};
|
|
157
|
+
if ((0, core_1.isFunction)(opts)) {
|
|
158
|
+
parser = opts;
|
|
159
|
+
options = {};
|
|
160
|
+
}
|
|
161
|
+
switch (type) {
|
|
162
|
+
case "urlencoded":
|
|
163
|
+
options.extended = true;
|
|
164
|
+
break;
|
|
165
|
+
case "raw":
|
|
166
|
+
options.type = () => true;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
return parser({ ...options, ...additionalOptions });
|
|
170
|
+
}
|
|
171
|
+
getPlatformApplication() {
|
|
172
|
+
return this.injector.get(common_1.PlatformApplication);
|
|
173
|
+
}
|
|
115
174
|
async configureViewsEngine() {
|
|
116
|
-
const
|
|
175
|
+
const injector = this.injector;
|
|
176
|
+
const app = this.injector.get(common_1.PlatformApplication);
|
|
117
177
|
try {
|
|
118
|
-
const { exists, disabled } = settings.get("views") || {};
|
|
178
|
+
const { exists, disabled } = this.injector.settings.get("views") || {};
|
|
119
179
|
if (exists && !disabled) {
|
|
120
|
-
const { PlatformViews } = await Promise.resolve().then(() => __importStar(require("@tsed/platform-views")));
|
|
180
|
+
const { PlatformViews } = await Promise.resolve().then(() => tslib_1.__importStar(require("@tsed/platform-views")));
|
|
121
181
|
const platformViews = injector.get(PlatformViews);
|
|
122
182
|
const express = app.getApp();
|
|
123
183
|
platformViews.getEngines().forEach(({ extension, engine }) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformExpress.js","sourceRoot":"","sources":["../../../src/components/PlatformExpress.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,yCAUsB;AACtB,qCAAqC;AACrC,wEAAmE;AACnE,0CAMqB;AAErB;;;GAGG;AACH,MAAa,eAAe;IAwB1B,YAAoB,QAA8D;QAA9D,aAAQ,GAAR,QAAQ,CAAsD;QAvBzE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AAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,KAAK,CAAC,oBAAoB;QAChC,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAChD,IAAI;YACF,MAAM,EAAC,MAAM,EAAE,QAAQ,EAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;YAEvD,IAAI,MAAM,IAAI,CAAC,QAAQ,EAAE;gBACvB,MAAM,EAAC,aAAa,EAAC,GAAG,wDAAa,sBAAsB,GAAC,CAAC;gBAC7D,MAAM,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAgB,aAAa,CAAE,CAAC;gBAClE,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;gBAE7B,aAAa,CAAC,UAAU,EAAE,CAAC,OAAO,CAAC,CAAC,EAAC,SAAS,EAAE,MAAM,EAAC,EAAE,EAAE;oBACzD,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;gBAC3C,CAAC,CAAC,CAAC;gBAEH,aAAa,CAAC,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;gBACjF,aAAa,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,IAAI,CAAC,CAAC;aAChE;SACF;QAAC,OAAO,KAAK,EAAE;YACd,uBAAuB;YACvB,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC;gBACnB,KAAK,EAAE,sBAAsB;gBAC7B,OAAO,EAAE,oEAAoE;gBAC7E,KAAK;aACN,CAAC,CAAC;SACJ;IACH,CAAC;CACF;AA3HD,0CA2HC","sourcesContent":["import Express from \"express\";\nimport type {PlatformViews} from \"@tsed/platform-views\";\nimport {\n createContext,\n PlatformAdapter,\n PlatformApplication,\n PlatformBuilder,\n PlatformExceptions,\n PlatformHandler,\n PlatformRequest,\n PlatformResponse,\n PlatformRouter\n} from \"@tsed/common\";\nimport {Env, Type} from \"@tsed/core\";\nimport {rawBodyMiddleware} from \"../middlewares/rawBodyMiddleware\";\nimport {\n PlatformExpressApplication,\n PlatformExpressHandler,\n PlatformExpressRequest,\n PlatformExpressResponse,\n PlatformExpressRouter\n} from \"../services\";\n\n/**\n * @platform\n * @express\n */\nexport class PlatformExpress implements PlatformAdapter<Express.Application, Express.Router> {\n readonly providers = [\n {\n provide: PlatformApplication,\n useClass: PlatformExpressApplication\n },\n {\n provide: PlatformRouter,\n useClass: PlatformExpressRouter\n },\n {\n provide: PlatformHandler,\n useClass: PlatformExpressHandler\n },\n {\n provide: PlatformResponse,\n useClass: PlatformExpressResponse\n },\n {\n provide: PlatformRequest,\n useClass: PlatformExpressRequest\n }\n ];\n\n constructor(private platform: PlatformBuilder<Express.Application, Express.Router>) {}\n\n /**\n * Create new serverless application. In this mode, the component scan are disabled.\n * @param module\n * @param settings\n */\n static create(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.create<Express.Application, Express.Router>(module, {\n ...settings,\n adapter: PlatformExpress\n });\n }\n\n /**\n * Bootstrap a server application\n * @param module\n * @param settings\n */\n static async bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.bootstrap<Express.Application, Express.Router>(module, {\n ...settings,\n adapter: PlatformExpress\n });\n }\n\n useRouter(): this {\n const {logger, app} = this.platform;\n\n logger.debug(\"Mount app router\");\n app.getApp().use(rawBodyMiddleware);\n app.getApp().use(app.getRouter());\n\n return this;\n }\n\n async beforeLoadRoutes() {\n const {injector, app} = this.platform;\n // disable x-powered-by header\n injector.settings.get(\"env\") === Env.PROD && app.getApp().disable(\"x-powered-by\");\n\n await this.configureViewsEngine();\n }\n\n async afterLoadRoutes() {\n const {injector, app} = this.platform;\n // NOT FOUND\n app.use((req: any, res: any, next: any) => {\n !res.headersSent && injector.get<PlatformExceptions>(PlatformExceptions)?.resourceNotFound(req.$ctx);\n });\n\n // EXCEPTION FILTERS\n app.use((err: any, req: any, res: any, next: any) => {\n !res.headersSent && injector.get<PlatformExceptions>(PlatformExceptions)?.catch(err, req.$ctx);\n });\n }\n\n useContext(): this {\n const {logger, app, injector} = this.platform;\n\n logger.debug(\"Mount app context\");\n\n const invoke = createContext(injector);\n\n app.getApp().use(async (request: any, response: any, next: any) => {\n await invoke({request, response});\n\n return next();\n });\n\n return this;\n }\n\n private async configureViewsEngine() {\n const {settings, injector, app} = this.platform;\n try {\n const {exists, disabled} = settings.get(\"views\") || {};\n\n if (exists && !disabled) {\n const {PlatformViews} = await import(\"@tsed/platform-views\");\n const platformViews = injector.get<PlatformViews>(PlatformViews)!;\n const express = app.getApp();\n\n platformViews.getEngines().forEach(({extension, engine}) => {\n express.engine(extension, engine.render);\n });\n\n platformViews.viewEngine && express.set(\"view engine\", platformViews.viewEngine);\n platformViews.root && express.set(\"views\", platformViews.root);\n }\n } catch (error) {\n // istanbul ignore next\n injector.logger.warn({\n event: \"PLATFORM_VIEWS_ERROR\",\n message: \"Unable to configure the PlatformViews service on your environment.\",\n error\n });\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformExpress.js","sourceRoot":"","sources":["../../../src/components/PlatformExpress.ts"],"names":[],"mappings":";;;;AAAA,yCAasB;AACtB,qCAAyD;AAIzD,8DAA8B;AAE9B,+BAA+B;AAE/B,wEAAmE;AAwBnE;;;GAGG;AACH,MAAa,eAAe;IAIJ;IAHb,SAAS,GAAG,EAAE,CAAC;IACxB,OAAO,CAAgB;IAEvB,YAAsB,QAAyB;QAAzB,aAAQ,GAAR,QAAQ,CAAiB;QAC7C,0DAAO,QAAQ,IAAE,IAAI,CAAC,CAAC,EAAC,OAAO,EAAE,MAAM,EAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;IACxE,CAAC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AK;aACN,CAAC,CAAC;SACJ;IACH,CAAC;CACF;AArOD,0CAqOC","sourcesContent":["import {\n createContext,\n InjectorService,\n PlatformAdapter,\n PlatformApplication,\n PlatformBuilder,\n PlatformContext,\n PlatformExceptions,\n PlatformHandlerType,\n PlatformMulter,\n PlatformMulterSettings,\n PlatformStaticsOptions,\n runInContext\n} from \"@tsed/common\";\nimport {Env, isFunction, nameOf, Type} from \"@tsed/core\";\nimport {PlatformHandlerMetadata, PlatformLayer} from \"@tsed/platform-router\";\nimport type {PlatformViews} from \"@tsed/platform-views\";\nimport {OptionsJson, OptionsText, OptionsUrlencoded} from \"body-parser\";\nimport Express from \"express\";\nimport type multer from \"multer\";\nimport {promisify} from \"util\";\nimport {PlatformExpressStaticsOptions} from \"../interfaces/PlatformExpressStaticsOptions\";\nimport {staticsMiddleware} from \"../middlewares/staticsMiddleware\";\n\ndeclare module \"express\" {\n export interface Request {\n id: string;\n $ctx: PlatformContext;\n }\n}\n\ndeclare global {\n namespace TsED {\n // export interface Router extends Express.Router {}\n\n export interface Application extends Express.Application {}\n\n export interface StaticsOptions extends PlatformExpressStaticsOptions {}\n\n export interface Request extends Express.Request {\n id: string;\n $ctx: PlatformContext;\n }\n }\n}\n\n/**\n * @platform\n * @express\n */\nexport class PlatformExpress implements PlatformAdapter<Express.Application> {\n readonly providers = [];\n #multer: typeof multer;\n\n constructor(protected injector: InjectorService) {\n import(\"multer\").then(({default: multer}) => (this.#multer = multer));\n }\n\n /**\n * Create new serverless application. In this mode, the component scan are disabled.\n * @param module\n * @param settings\n */\n static create(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.create<Express.Application>(module, {\n ...settings,\n adapter: PlatformExpress\n });\n }\n\n /**\n * Bootstrap a server application\n * @param module\n * @param settings\n */\n static async bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.bootstrap<Express.Application>(module, {\n ...settings,\n adapter: PlatformExpress\n });\n }\n\n onInit() {\n const middlewares = this.injector.settings.get(\"middlewares\", []);\n\n this.injector.settings.set(\n \"middlewares\",\n middlewares.filter((middleware) => {\n const name = nameOf(middleware);\n if ([\"textParser\", \"jsonParser\", \"rawParser\", \"urlencodedParser\"].includes(name)) {\n this.injector.settings.set(`express.bodyParser.${name.replace(\"Parser\", \"\")}`, () => middleware);\n return false;\n }\n\n return true;\n })\n );\n }\n\n async beforeLoadRoutes() {\n const injector = this.injector;\n const app = this.injector.get<PlatformApplication<Express.Application>>(PlatformApplication)!;\n\n // disable x-powered-by header\n injector.settings.get(\"env\") === Env.PROD && app.getApp().disable(\"x-powered-by\");\n\n await this.configureViewsEngine();\n }\n\n async afterLoadRoutes() {\n const app = this.injector.get<PlatformApplication<Express.Application>>(PlatformApplication)!;\n const platformExceptions = this.injector.get<PlatformExceptions>(PlatformExceptions)!;\n\n // NOT FOUND\n app.use((req: any, res: any, next: any) => {\n const {$ctx} = req;\n !$ctx.isDone() && platformExceptions?.resourceNotFound(req.$ctx);\n });\n\n // EXCEPTION FILTERS\n app.use((err: any, req: any, res: any, next: any) => {\n const {$ctx} = req;\n !$ctx.isDone() && platformExceptions?.catch(err, $ctx);\n });\n }\n\n mapLayers(layers: PlatformLayer[]) {\n const app = this.getPlatformApplication();\n const rawApp: any = app.getApp();\n\n layers.forEach((layer) => {\n switch (layer.method) {\n case \"statics\":\n rawApp.use(layer.path, this.statics(layer.path as string, layer.opts as any));\n return;\n }\n\n rawApp[layer.method](...layer.getArgs());\n });\n }\n\n mapHandler(handler: Function, metadata: PlatformHandlerMetadata) {\n switch (metadata.type) {\n case PlatformHandlerType.RAW_FN:\n case PlatformHandlerType.RAW_ERR_FN:\n return handler;\n case PlatformHandlerType.ERR_MIDDLEWARE:\n return async (error: unknown, req: any, res: any, next: any) => {\n return runInContext(req.$ctx, () => {\n const {$ctx} = req;\n\n $ctx.next = next;\n $ctx.error = error;\n\n return handler($ctx);\n });\n };\n default:\n return (req: any, res: any, next: any) => {\n return runInContext(req.$ctx, () => {\n req.$ctx.next = next;\n handler(req.$ctx);\n });\n };\n }\n }\n\n useContext(): this {\n const app = this.getPlatformApplication();\n const invoke = createContext(this.injector);\n\n this.injector.logger.debug(\"Mount app context\");\n\n app.use(async (request: any, response: any, next: any) => {\n const $ctx = await invoke({request, response});\n await $ctx.start();\n\n $ctx.response.getRes().on(\"finish\", () => $ctx.finish());\n\n return runInContext($ctx, next);\n });\n\n return this;\n }\n\n app() {\n const app = this.injector.settings.get(\"express.app\") || Express();\n\n return {\n app,\n callback: () => app\n };\n }\n\n multipart(options: PlatformMulterSettings): PlatformMulter {\n const m = this.#multer(options);\n\n const makePromise = (multer: any, name: string) => {\n // istanbul ignore next\n if (!multer[name]) return;\n\n const fn = multer[name];\n\n multer[name] = function apply(...args: any[]) {\n const middleware = Reflect.apply(fn, this, args);\n\n return (req: any, res: any) => promisify(middleware)(req, res);\n };\n };\n\n makePromise(m, \"any\");\n makePromise(m, \"array\");\n makePromise(m, \"fields\");\n makePromise(m, \"none\");\n makePromise(m, \"single\");\n\n return m;\n }\n\n statics(endpoint: string, options: PlatformStaticsOptions) {\n const {root, ...props} = options;\n\n return staticsMiddleware(root, props);\n }\n\n bodyParser(type: \"json\" | \"raw\" | \"text\" | \"urlencoded\", additionalOptions: any = {}): any {\n const opts = this.injector.settings.get(`express.bodyParser.${type}`);\n let parser: any = Express[type];\n let options: OptionsJson & OptionsText & OptionsUrlencoded = {};\n\n if (isFunction(opts)) {\n parser = opts;\n options = {};\n }\n\n switch (type) {\n case \"urlencoded\":\n options.extended = true;\n break;\n case \"raw\":\n options.type = () => true;\n break;\n }\n\n return parser({...options, ...additionalOptions});\n }\n\n private getPlatformApplication() {\n return this.injector.get<PlatformApplication<Express.Application>>(PlatformApplication)!;\n }\n\n private async configureViewsEngine() {\n const injector = this.injector;\n const app = this.injector.get<PlatformApplication<Express.Application>>(PlatformApplication)!;\n\n try {\n const {exists, disabled} = this.injector.settings.get(\"views\") || {};\n\n if (exists && !disabled) {\n const {PlatformViews} = await import(\"@tsed/platform-views\");\n const platformViews = injector.get<PlatformViews>(PlatformViews)!;\n const express = app.getApp();\n\n platformViews.getEngines().forEach(({extension, engine}) => {\n express.engine(extension, engine.render);\n });\n\n platformViews.viewEngine && express.set(\"view engine\", platformViews.viewEngine);\n platformViews.root && express.set(\"views\", platformViews.root);\n }\n } catch (error) {\n // istanbul ignore next\n injector.logger.warn({\n event: \"PLATFORM_VIEWS_ERROR\",\n message: \"Unable to configure the PlatformViews service on your environment.\",\n error\n });\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@tsed/common");
|
|
4
|
+
const PlatformExpress_1 = require("./components/PlatformExpress");
|
|
5
|
+
common_1.PlatformTest.adapter = PlatformExpress_1.PlatformExpress;
|
|
6
|
+
common_1.PlatformBuilder.adapter = PlatformExpress_1.PlatformExpress;
|
|
7
|
+
//# sourceMappingURL=exports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exports.js","sourceRoot":"","sources":["../../src/exports.ts"],"names":[],"mappings":";;AAAA,yCAA2D;AAC3D,kEAA6D;AAE7D,qBAAY,CAAC,OAAO,GAAG,iCAAsB,CAAC;AAC9C,wBAAe,CAAC,OAAO,GAAG,iCAAsB,CAAC","sourcesContent":["import {PlatformBuilder, PlatformTest} from \"@tsed/common\";\nimport {PlatformExpress} from \"./components/PlatformExpress\";\n\nPlatformTest.adapter = PlatformExpress as any;\nPlatformBuilder.adapter = PlatformExpress as any;\n"]}
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file Automatically generated by barrelsby.
|
|
4
|
+
*/
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
const tslib_1 = require("tslib");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// decorators
|
|
11
|
-
(0, tslib_1.__exportStar)(require("./decorators"), exports);
|
|
12
|
-
// services
|
|
13
|
-
(0, tslib_1.__exportStar)(require("./services"), exports);
|
|
14
|
-
// components
|
|
15
|
-
(0, tslib_1.__exportStar)(require("./components/PlatformExpress"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./exports"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./components/PlatformExpress"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./interfaces/PlatformExpressSettings"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./interfaces/PlatformExpressStaticsOptions"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./interfaces/interfaces"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./middlewares/staticsMiddleware"), exports);
|
|
16
13
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,oDAA0B;AAC1B,uEAA6C;AAC7C,+EAAqD;AACrD,qFAA2D;AAC3D,kEAAwC;AACxC,0EAAgD","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./exports\";\nexport * from \"./components/PlatformExpress\";\nexport * from \"./interfaces/PlatformExpressSettings\";\nexport * from \"./interfaces/PlatformExpressStaticsOptions\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./middlewares/staticsMiddleware\";\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformExpressSettings.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformExpressSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {RouterOptions} from \"express\";\n\nexport interface PlatformExpressSettings {\n /**\n * Global configuration for the Express.Router. See express [documentation](http://expressjs.com/en/api.html#express.router).\n */\n router?: RouterOptions;\n\n app?: Express.Application;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformExpressSettings.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformExpressSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {RouterOptions} from \"express\";\nimport type {OptionsJson, OptionsText, Options, OptionsUrlencoded} from \"body-parser\";\nimport type {NextHandleFunction} from \"connect\";\n\nexport interface PlatformExpressSettings {\n /**\n * Global configuration for the Express.Router. See express [documentation](http://expressjs.com/en/api.html#express.router).\n */\n router?: RouterOptions;\n /**\n * Express application\n */\n app?: Express.Application;\n\n /**\n * body parser configuration\n */\n bodyParser?: {\n json?: ((opts?: OptionsJson) => NextHandleFunction) | OptionsJson;\n text?: ((opts?: OptionsText) => NextHandleFunction) | OptionsText;\n raw?: ((opts?: Options) => NextHandleFunction) | Options;\n urlencoded?: ((opts?: OptionsUrlencoded) => NextHandleFunction) | OptionsUrlencoded;\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import {NextFunction as ENext, Request as EReq, Response as ERes} from \"express\";\nimport {PlatformExpressSettings} from \"./PlatformExpressSettings\";\n\ndeclare global {\n namespace TsED {\n export interface Configuration {\n /**\n * Configuration related to Express platform application.\n */\n express: PlatformExpressSettings;\n }\n\n export interface NextFunction extends ENext {}\n\n export interface Response extends ERes {}\n export interface Request extends EReq {}\n }\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.staticsMiddleware = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const express_1 =
|
|
5
|
+
const express_1 = tslib_1.__importDefault(require("express"));
|
|
6
6
|
/**
|
|
7
7
|
* @ignore
|
|
8
8
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"staticsMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/staticsMiddleware.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"staticsMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/staticsMiddleware.ts"],"names":[],"mappings":";;;;AAAA,8DAA8B;AAE9B;;GAEG;AACH,SAAgB,iBAAiB,CAAC,SAAiB,EAAE,UAAe,EAAE;IACpE,MAAM,UAAU,GAAG,iBAAO,CAAC,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAEtD,OAAO,CAAC,OAAY,EAAE,QAAa,EAAE,IAAS,EAAE,EAAE;QAChD,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE;YACzB,UAAU,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;SACrC;aAAM;YACL,IAAI,EAAE,CAAC;SACR;IACH,CAAC,CAAC;AACJ,CAAC;AAVD,8CAUC","sourcesContent":["import Express from \"express\";\n\n/**\n * @ignore\n */\nexport function staticsMiddleware(directory: string, options: any = {}) {\n const middleware = Express.static(directory, options);\n\n return (request: any, response: any, next: any) => {\n if (!response.headersSent) {\n middleware(request, response, next);\n } else {\n next();\n }\n };\n}\n"]}
|
|
@@ -1,36 +1,19 @@
|
|
|
1
|
-
import { createContext, PlatformApplication, PlatformBuilder, PlatformExceptions,
|
|
2
|
-
import { Env } from "@tsed/core";
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
1
|
+
import { createContext, PlatformApplication, PlatformBuilder, PlatformExceptions, PlatformHandlerType, runInContext } from "@tsed/common";
|
|
2
|
+
import { Env, isFunction, nameOf } from "@tsed/core";
|
|
3
|
+
import Express from "express";
|
|
4
|
+
import { promisify } from "util";
|
|
5
|
+
import { staticsMiddleware } from "../middlewares/staticsMiddleware.js";
|
|
5
6
|
/**
|
|
6
7
|
* @platform
|
|
7
8
|
* @express
|
|
8
9
|
*/
|
|
9
10
|
export class PlatformExpress {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
provide: PlatformRouter,
|
|
19
|
-
useClass: PlatformExpressRouter
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
provide: PlatformHandler,
|
|
23
|
-
useClass: PlatformExpressHandler
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
provide: PlatformResponse,
|
|
27
|
-
useClass: PlatformExpressResponse
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
provide: PlatformRequest,
|
|
31
|
-
useClass: PlatformExpressRequest
|
|
32
|
-
}
|
|
33
|
-
];
|
|
11
|
+
injector;
|
|
12
|
+
providers = [];
|
|
13
|
+
#multer;
|
|
14
|
+
constructor(injector) {
|
|
15
|
+
this.injector = injector;
|
|
16
|
+
import("multer").then(({ default: multer }) => (this.#multer = multer));
|
|
34
17
|
}
|
|
35
18
|
/**
|
|
36
19
|
* Create new serverless application. In this mode, the component scan are disabled.
|
|
@@ -54,46 +37,141 @@ export class PlatformExpress {
|
|
|
54
37
|
adapter: PlatformExpress
|
|
55
38
|
});
|
|
56
39
|
}
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
40
|
+
onInit() {
|
|
41
|
+
const middlewares = this.injector.settings.get("middlewares", []);
|
|
42
|
+
this.injector.settings.set("middlewares", middlewares.filter((middleware) => {
|
|
43
|
+
const name = nameOf(middleware);
|
|
44
|
+
if (["textParser", "jsonParser", "rawParser", "urlencodedParser"].includes(name)) {
|
|
45
|
+
this.injector.settings.set(`express.bodyParser.${name.replace("Parser", "")}`, () => middleware);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}));
|
|
63
50
|
}
|
|
64
51
|
async beforeLoadRoutes() {
|
|
65
|
-
const
|
|
52
|
+
const injector = this.injector;
|
|
53
|
+
const app = this.injector.get(PlatformApplication);
|
|
66
54
|
// disable x-powered-by header
|
|
67
55
|
injector.settings.get("env") === Env.PROD && app.getApp().disable("x-powered-by");
|
|
68
56
|
await this.configureViewsEngine();
|
|
69
57
|
}
|
|
70
58
|
async afterLoadRoutes() {
|
|
71
|
-
const
|
|
59
|
+
const app = this.injector.get(PlatformApplication);
|
|
60
|
+
const platformExceptions = this.injector.get(PlatformExceptions);
|
|
72
61
|
// NOT FOUND
|
|
73
62
|
app.use((req, res, next) => {
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
const { $ctx } = req;
|
|
64
|
+
!$ctx.isDone() && platformExceptions?.resourceNotFound(req.$ctx);
|
|
76
65
|
});
|
|
77
66
|
// EXCEPTION FILTERS
|
|
78
67
|
app.use((err, req, res, next) => {
|
|
79
|
-
|
|
80
|
-
|
|
68
|
+
const { $ctx } = req;
|
|
69
|
+
!$ctx.isDone() && platformExceptions?.catch(err, $ctx);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
mapLayers(layers) {
|
|
73
|
+
const app = this.getPlatformApplication();
|
|
74
|
+
const rawApp = app.getApp();
|
|
75
|
+
layers.forEach((layer) => {
|
|
76
|
+
switch (layer.method) {
|
|
77
|
+
case "statics":
|
|
78
|
+
rawApp.use(layer.path, this.statics(layer.path, layer.opts));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
rawApp[layer.method](...layer.getArgs());
|
|
81
82
|
});
|
|
82
83
|
}
|
|
84
|
+
mapHandler(handler, metadata) {
|
|
85
|
+
switch (metadata.type) {
|
|
86
|
+
case PlatformHandlerType.RAW_FN:
|
|
87
|
+
case PlatformHandlerType.RAW_ERR_FN:
|
|
88
|
+
return handler;
|
|
89
|
+
case PlatformHandlerType.ERR_MIDDLEWARE:
|
|
90
|
+
return async (error, req, res, next) => {
|
|
91
|
+
return runInContext(req.$ctx, () => {
|
|
92
|
+
const { $ctx } = req;
|
|
93
|
+
$ctx.next = next;
|
|
94
|
+
$ctx.error = error;
|
|
95
|
+
return handler($ctx);
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
default:
|
|
99
|
+
return (req, res, next) => {
|
|
100
|
+
return runInContext(req.$ctx, () => {
|
|
101
|
+
req.$ctx.next = next;
|
|
102
|
+
handler(req.$ctx);
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
83
107
|
useContext() {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
app.
|
|
88
|
-
await invoke({ request, response });
|
|
89
|
-
|
|
108
|
+
const app = this.getPlatformApplication();
|
|
109
|
+
const invoke = createContext(this.injector);
|
|
110
|
+
this.injector.logger.debug("Mount app context");
|
|
111
|
+
app.use(async (request, response, next) => {
|
|
112
|
+
const $ctx = await invoke({ request, response });
|
|
113
|
+
await $ctx.start();
|
|
114
|
+
$ctx.response.getRes().on("finish", () => $ctx.finish());
|
|
115
|
+
return runInContext($ctx, next);
|
|
90
116
|
});
|
|
91
117
|
return this;
|
|
92
118
|
}
|
|
119
|
+
app() {
|
|
120
|
+
const app = this.injector.settings.get("express.app") || Express();
|
|
121
|
+
return {
|
|
122
|
+
app,
|
|
123
|
+
callback: () => app
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
multipart(options) {
|
|
127
|
+
const m = this.#multer(options);
|
|
128
|
+
const makePromise = (multer, name) => {
|
|
129
|
+
// istanbul ignore next
|
|
130
|
+
if (!multer[name])
|
|
131
|
+
return;
|
|
132
|
+
const fn = multer[name];
|
|
133
|
+
multer[name] = function apply(...args) {
|
|
134
|
+
const middleware = Reflect.apply(fn, this, args);
|
|
135
|
+
return (req, res) => promisify(middleware)(req, res);
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
makePromise(m, "any");
|
|
139
|
+
makePromise(m, "array");
|
|
140
|
+
makePromise(m, "fields");
|
|
141
|
+
makePromise(m, "none");
|
|
142
|
+
makePromise(m, "single");
|
|
143
|
+
return m;
|
|
144
|
+
}
|
|
145
|
+
statics(endpoint, options) {
|
|
146
|
+
const { root, ...props } = options;
|
|
147
|
+
return staticsMiddleware(root, props);
|
|
148
|
+
}
|
|
149
|
+
bodyParser(type, additionalOptions = {}) {
|
|
150
|
+
const opts = this.injector.settings.get(`express.bodyParser.${type}`);
|
|
151
|
+
let parser = Express[type];
|
|
152
|
+
let options = {};
|
|
153
|
+
if (isFunction(opts)) {
|
|
154
|
+
parser = opts;
|
|
155
|
+
options = {};
|
|
156
|
+
}
|
|
157
|
+
switch (type) {
|
|
158
|
+
case "urlencoded":
|
|
159
|
+
options.extended = true;
|
|
160
|
+
break;
|
|
161
|
+
case "raw":
|
|
162
|
+
options.type = () => true;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
return parser({ ...options, ...additionalOptions });
|
|
166
|
+
}
|
|
167
|
+
getPlatformApplication() {
|
|
168
|
+
return this.injector.get(PlatformApplication);
|
|
169
|
+
}
|
|
93
170
|
async configureViewsEngine() {
|
|
94
|
-
const
|
|
171
|
+
const injector = this.injector;
|
|
172
|
+
const app = this.injector.get(PlatformApplication);
|
|
95
173
|
try {
|
|
96
|
-
const { exists, disabled } = settings.get("views") || {};
|
|
174
|
+
const { exists, disabled } = this.injector.settings.get("views") || {};
|
|
97
175
|
if (exists && !disabled) {
|
|
98
176
|
const { PlatformViews } = await import("@tsed/platform-views");
|
|
99
177
|
const platformViews = injector.get(PlatformViews);
|