@tsed/platform-http 8.0.0 → 8.0.2
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/.barrelsby.json +1 -1
- package/lib/esm/common/index.js +0 -1
- package/lib/esm/common/index.js.map +1 -1
- package/lib/types/common/fn/adapter.d.ts +1 -1
- package/lib/types/common/index.d.ts +0 -1
- package/package.json +19 -17
- package/src/common/builder/PlatformBuilder.spec.ts +331 -0
- package/src/common/builder/PlatformBuilder.ts +426 -0
- package/src/common/builder/__fixtures__/PlatformCustom.ts +39 -0
- package/src/common/config/interfaces/PlatformJsonMapperSettings.ts +11 -0
- package/src/common/config/interfaces/PlatformMulterSettings.ts +21 -0
- package/src/common/config/interfaces/PlatformRouterSettings.ts +11 -0
- package/src/common/config/interfaces/PlatformStaticsSettings.ts +23 -0
- package/src/common/config/services/PlatformConfiguration.spec.ts +181 -0
- package/src/common/config/services/PlatformConfiguration.ts +157 -0
- package/src/common/constants/routerOptions.ts +1 -0
- package/src/common/decorators/PlatformProvider.ts +14 -0
- package/src/common/decorators/multer/multerFileSize.spec.ts +22 -0
- package/src/common/decorators/multer/multerFileSize.ts +32 -0
- package/src/common/decorators/multer/multerOptions.spec.ts +21 -0
- package/src/common/decorators/multer/multerOptions.ts +53 -0
- package/src/common/decorators/multer/multipartFile.spec.ts +259 -0
- package/src/common/decorators/multer/multipartFile.ts +88 -0
- package/src/common/decorators/params/error.spec.ts +15 -0
- package/src/common/decorators/params/error.ts +16 -0
- package/src/common/decorators/params/next.spec.ts +15 -0
- package/src/common/decorators/params/next.ts +31 -0
- package/src/common/decorators/params/request.spec.ts +49 -0
- package/src/common/decorators/params/request.ts +85 -0
- package/src/common/decorators/params/response.spec.ts +36 -0
- package/src/common/decorators/params/response.ts +73 -0
- package/src/common/domain/AnyToPromiseWithCtx.spec.ts +20 -0
- package/src/common/domain/AnyToPromiseWithCtx.ts +30 -0
- package/src/common/domain/EndpointMetadata.ts +1 -0
- package/src/common/domain/PlatformContext.spec.ts +124 -0
- package/src/common/domain/PlatformContext.ts +135 -0
- package/src/common/domain/PlatformRouteDetails.ts +7 -0
- package/src/common/exports.ts +4 -0
- package/src/common/fn/adapter.ts +23 -0
- package/src/common/fn/application.ts +11 -0
- package/src/common/index.ts +62 -0
- package/src/common/interfaces/AfterInit.ts +3 -0
- package/src/common/interfaces/AfterListen.ts +3 -0
- package/src/common/interfaces/AfterRoutesInit.ts +3 -0
- package/src/common/interfaces/BeforeInit.ts +3 -0
- package/src/common/interfaces/BeforeListen.ts +3 -0
- package/src/common/interfaces/BeforeRoutesInit.ts +3 -0
- package/src/common/interfaces/HandlerType.ts +12 -0
- package/src/common/interfaces/IncomingEvent.ts +6 -0
- package/src/common/interfaces/OnReady.ts +3 -0
- package/src/common/interfaces/OnRequest.ts +5 -0
- package/src/common/interfaces/OnResponse.ts +5 -0
- package/src/common/interfaces/OnRoutesInit.ts +3 -0
- package/src/common/interfaces/PlatformRouteOptions.ts +17 -0
- package/src/common/interfaces/ResponseErrorObject.ts +8 -0
- package/src/common/interfaces/Route.ts +12 -0
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.spec.ts +215 -0
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.ts +24 -0
- package/src/common/middlewares/PlatformMulterMiddleware.spec.ts +97 -0
- package/src/common/middlewares/PlatformMulterMiddleware.ts +59 -0
- package/src/common/services/Platform.spec.ts +145 -0
- package/src/common/services/Platform.ts +79 -0
- package/src/common/services/PlatformAdapter.ts +89 -0
- package/src/common/services/PlatformApplication.spec.ts +101 -0
- package/src/common/services/PlatformApplication.ts +54 -0
- package/src/common/services/PlatformHandler.spec.ts +152 -0
- package/src/common/services/PlatformHandler.ts +140 -0
- package/src/common/services/PlatformMiddlewaresChain.spec.ts +96 -0
- package/src/common/services/PlatformMiddlewaresChain.ts +55 -0
- package/src/common/services/PlatformRequest.spec.ts +185 -0
- package/src/common/services/PlatformRequest.ts +181 -0
- package/src/common/services/PlatformResponse.spec.ts +344 -0
- package/src/common/services/PlatformResponse.ts +388 -0
- package/src/common/utils/closeServer.spec.ts +11 -0
- package/src/common/utils/closeServer.ts +7 -0
- package/src/common/utils/createContext.spec.ts +111 -0
- package/src/common/utils/createContext.ts +52 -0
- package/src/common/utils/createHttpServer.spec.ts +74 -0
- package/src/common/utils/createHttpServer.ts +15 -0
- package/src/common/utils/createHttpsServer.spec.ts +73 -0
- package/src/common/utils/createHttpsServer.ts +16 -0
- package/src/common/utils/createInjector.spec.ts +42 -0
- package/src/common/utils/createInjector.ts +55 -0
- package/src/common/utils/createServer.ts +51 -0
- package/src/common/utils/ensureContext.spec.ts +41 -0
- package/src/common/utils/ensureContext.ts +17 -0
- package/src/common/utils/getConfiguration.spec.ts +163 -0
- package/src/common/utils/getConfiguration.ts +34 -0
- package/src/common/utils/getStaticsOptions.spec.ts +27 -0
- package/src/common/utils/getStaticsOptions.ts +24 -0
- package/src/common/utils/listenServer.ts +17 -0
- package/src/common/utils/mapReturnedResponse.spec.ts +35 -0
- package/src/common/utils/mapReturnedResponse.ts +10 -0
- package/src/common/utils/printRoutes.spec.ts +19 -0
- package/src/common/utils/printRoutes.ts +40 -0
- package/src/common/utils/resolveControllers.spec.ts +40 -0
- package/src/common/utils/resolveControllers.ts +54 -0
- package/src/common/utils/setResponseHeaders.spec.ts +84 -0
- package/src/common/utils/setResponseHeaders.ts +31 -0
- package/src/index.ts +2 -0
- package/src/testing/FakeAdapter.ts +69 -0
- package/src/testing/FakeResponse.ts +72 -0
- package/src/testing/PlatformTest.spec.ts +53 -0
- package/src/testing/PlatformTest.ts +157 -0
- package/src/testing/index.ts +5 -0
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
import {type Env, isClass, isFunction, isString, nameOf, Type} from "@tsed/core";
|
|
2
|
+
import {
|
|
3
|
+
colors,
|
|
4
|
+
configuration,
|
|
5
|
+
constant,
|
|
6
|
+
createContainer,
|
|
7
|
+
destroyInjector,
|
|
8
|
+
injector,
|
|
9
|
+
InjectorService,
|
|
10
|
+
logger,
|
|
11
|
+
ProviderOpts,
|
|
12
|
+
ProviderScope,
|
|
13
|
+
setLoggerConfiguration,
|
|
14
|
+
TokenProvider
|
|
15
|
+
} from "@tsed/di";
|
|
16
|
+
import {$asyncAlter, $asyncEmit} from "@tsed/hooks";
|
|
17
|
+
import {getMiddlewaresForHook, PlatformMiddlewareLoadingOptions} from "@tsed/platform-middlewares";
|
|
18
|
+
import {PlatformLayer} from "@tsed/platform-router";
|
|
19
|
+
import type {IncomingMessage, ServerResponse} from "http";
|
|
20
|
+
import Http from "http";
|
|
21
|
+
import Http2 from "http2";
|
|
22
|
+
import type Https from "https";
|
|
23
|
+
|
|
24
|
+
import {PlatformStaticsSettings} from "../config/interfaces/PlatformStaticsSettings.js";
|
|
25
|
+
import {PlatformRouteDetails} from "../domain/PlatformRouteDetails.js";
|
|
26
|
+
import {application} from "../fn/application.js";
|
|
27
|
+
import {Route} from "../interfaces/Route.js";
|
|
28
|
+
import {Platform} from "../services/Platform.js";
|
|
29
|
+
import {PlatformAdapter, PlatformBuilderSettings} from "../services/PlatformAdapter.js";
|
|
30
|
+
import {PlatformApplication} from "../services/PlatformApplication.js";
|
|
31
|
+
import {closeServer} from "../utils/closeServer.js";
|
|
32
|
+
import {createInjector} from "../utils/createInjector.js";
|
|
33
|
+
import {CreateServerReturn} from "../utils/createServer.js";
|
|
34
|
+
import {getConfiguration} from "../utils/getConfiguration.js";
|
|
35
|
+
import {getStaticsOptions} from "../utils/getStaticsOptions.js";
|
|
36
|
+
import {printRoutes} from "../utils/printRoutes.js";
|
|
37
|
+
import {resolveControllers} from "../utils/resolveControllers.js";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @platform
|
|
41
|
+
*/
|
|
42
|
+
export class PlatformBuilder<App = TsED.Application> {
|
|
43
|
+
protected startedAt = new Date();
|
|
44
|
+
protected current = new Date();
|
|
45
|
+
readonly #rootModule: Type<any>;
|
|
46
|
+
#promise: Promise<this>;
|
|
47
|
+
#servers: CreateServerReturn[];
|
|
48
|
+
#listeners: (Http.Server | Https.Server | Http2.Http2Server)[] = [];
|
|
49
|
+
|
|
50
|
+
protected constructor(module: Type, settings: Partial<TsED.Configuration>) {
|
|
51
|
+
this.#rootModule = module;
|
|
52
|
+
|
|
53
|
+
const configuration = getConfiguration(settings, module);
|
|
54
|
+
|
|
55
|
+
createInjector(configuration);
|
|
56
|
+
|
|
57
|
+
this.log(`Loading ${this.name.toUpperCase()} platform adapter...`);
|
|
58
|
+
|
|
59
|
+
this.createHttpServers();
|
|
60
|
+
|
|
61
|
+
this.log("Injector created...");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get name() {
|
|
65
|
+
return this.adapter.NAME;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get rootModule(): any {
|
|
69
|
+
return injector().get(this.#rootModule);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get app(): PlatformApplication<App> {
|
|
73
|
+
return injector().get<PlatformApplication<App>>(PlatformApplication)!;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get platform() {
|
|
77
|
+
return injector().get<Platform>(Platform)!;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get adapter() {
|
|
81
|
+
return injector().get(PlatformAdapter);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Return the settings configured by the decorator @@Configuration@@.
|
|
86
|
+
*
|
|
87
|
+
* ```typescript
|
|
88
|
+
* @Configuration({
|
|
89
|
+
* port: 8000,
|
|
90
|
+
* httpsPort: 8080,
|
|
91
|
+
* mount: {
|
|
92
|
+
* "/rest": "${rootDir}/controllers/**\/*.js"
|
|
93
|
+
* }
|
|
94
|
+
* })
|
|
95
|
+
* export class Server {
|
|
96
|
+
* $onInit(){
|
|
97
|
+
* console.log(this.settings); // {rootDir, port, httpsPort,...}
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @returns {PlatformConfiguration}
|
|
103
|
+
*/
|
|
104
|
+
get settings() {
|
|
105
|
+
return configuration();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get logger() {
|
|
109
|
+
return logger();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get disableBootstrapLog() {
|
|
113
|
+
return constant<boolean>("logger.disableBootstrapLog");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @deprecated use injector() instead of this method.
|
|
118
|
+
*/
|
|
119
|
+
get injector(): InjectorService {
|
|
120
|
+
return injector();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static create<App = TsED.Application>(module: Type<any>, settings: PlatformBuilderSettings<App>) {
|
|
124
|
+
return this.build(module, {
|
|
125
|
+
httpsPort: false,
|
|
126
|
+
httpPort: false,
|
|
127
|
+
...settings
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static build<App = TsED.Application>(module: Type<any>, settings: PlatformBuilderSettings<App>) {
|
|
132
|
+
return new PlatformBuilder(module, settings);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Bootstrap a server application
|
|
137
|
+
* @param module
|
|
138
|
+
* @param settings
|
|
139
|
+
*/
|
|
140
|
+
static bootstrap<App = TsED.Application>(module: Type<any>, settings: PlatformBuilderSettings<App>) {
|
|
141
|
+
return this.build<App>(module, settings).bootstrap();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
callback(): (req: IncomingMessage, res: ServerResponse) => void;
|
|
145
|
+
callback(req: IncomingMessage, res: ServerResponse): void;
|
|
146
|
+
callback(...args: [IncomingMessage?, ServerResponse?]) {
|
|
147
|
+
return (this.adapter.app.callback as any)(...args);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
log(...data: any[]) {
|
|
151
|
+
return !this.disableBootstrapLog && logger().info(...data, this.diff());
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Add classes decorated by @@Controller@@ to components container.
|
|
156
|
+
*
|
|
157
|
+
* ### Example
|
|
158
|
+
*
|
|
159
|
+
* ```typescript
|
|
160
|
+
* @Controller('/ctrl')
|
|
161
|
+
* class MyController{
|
|
162
|
+
* }
|
|
163
|
+
*
|
|
164
|
+
* platform.addControllers('/rest', [MyController])
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* ::: tip
|
|
168
|
+
* If the MyController class isn't decorated, the class will be ignored.
|
|
169
|
+
* :::
|
|
170
|
+
*
|
|
171
|
+
* @param {string} endpoint
|
|
172
|
+
* @param {any[]} controllers
|
|
173
|
+
*/
|
|
174
|
+
public addControllers(endpoint: string, controllers: TokenProvider | TokenProvider[]) {
|
|
175
|
+
[].concat(controllers as never[]).forEach((token: TokenProvider) => {
|
|
176
|
+
configuration().routes.push({token, route: endpoint});
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public async runLifecycle() {
|
|
181
|
+
// init adapter (Express, Koa, etc...)
|
|
182
|
+
await this.adapter.onInit();
|
|
183
|
+
|
|
184
|
+
setLoggerConfiguration();
|
|
185
|
+
|
|
186
|
+
// create the middleware mapping to be executed to the expected hook
|
|
187
|
+
await this.mapTokenMiddlewares();
|
|
188
|
+
|
|
189
|
+
await this.loadInjector();
|
|
190
|
+
|
|
191
|
+
// add the context middleware to the application
|
|
192
|
+
this.log("Mount app context");
|
|
193
|
+
this.adapter.useContext();
|
|
194
|
+
|
|
195
|
+
// init routes (controllers, middlewares, etc...)
|
|
196
|
+
this.log("Load routes");
|
|
197
|
+
await this.adapter.beforeLoadRoutes();
|
|
198
|
+
|
|
199
|
+
if (this.rootModule.$beforeRoutesInit) {
|
|
200
|
+
await this.rootModule.$beforeRoutesInit();
|
|
201
|
+
// remove method to avoid multiple call and preserve hook order
|
|
202
|
+
this.rootModule.$beforeRoutesInit = () => {};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Hooks execution (adding middlewares, controllers, services, etc...)
|
|
206
|
+
await this.loadStatics("$beforeRoutesInit");
|
|
207
|
+
await this.callHook("$beforeRoutesInit");
|
|
208
|
+
|
|
209
|
+
const routes = configuration().get<Route[]>("routes");
|
|
210
|
+
|
|
211
|
+
this.platform.addRoutes(routes);
|
|
212
|
+
|
|
213
|
+
await this.callHook("$onRoutesInit");
|
|
214
|
+
|
|
215
|
+
await this.loadStatics("$afterRoutesInit");
|
|
216
|
+
await this.callHook("$afterRoutesInit");
|
|
217
|
+
|
|
218
|
+
await this.adapter.afterLoadRoutes();
|
|
219
|
+
|
|
220
|
+
// map routers are loaded after all hooks because it contains all added middlewares/controllers in the virtual Ts.ED layers
|
|
221
|
+
// This step will convert all Ts.ED layers to the platform layer (Express or Koa)
|
|
222
|
+
await this.mapRouters();
|
|
223
|
+
|
|
224
|
+
// Server is bootstrapped and ready to listen
|
|
225
|
+
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async loadInjector() {
|
|
230
|
+
this.log("Build providers");
|
|
231
|
+
const settings = configuration();
|
|
232
|
+
|
|
233
|
+
settings.set("routes", settings.get("routes").concat(resolveControllers(settings)));
|
|
234
|
+
|
|
235
|
+
const container = createContainer();
|
|
236
|
+
container.delete(this.#rootModule);
|
|
237
|
+
container.addProvider(this.#rootModule, {
|
|
238
|
+
type: "server:module",
|
|
239
|
+
scope: ProviderScope.SINGLETON
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
await injector().load(container);
|
|
243
|
+
|
|
244
|
+
this.log("Settings and injector loaded...");
|
|
245
|
+
|
|
246
|
+
await this.callHook("$afterInit");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async listen(network = true) {
|
|
250
|
+
if (!this.#promise) {
|
|
251
|
+
await this.bootstrap();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
await this.callHook("$beforeListen");
|
|
255
|
+
|
|
256
|
+
if (network) {
|
|
257
|
+
await this.listenServers();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
await this.callHook("$afterListen");
|
|
261
|
+
|
|
262
|
+
await this.ready();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async stop() {
|
|
266
|
+
await this.callHook("$onDestroy");
|
|
267
|
+
await destroyInjector();
|
|
268
|
+
|
|
269
|
+
this.#listeners.map(closeServer);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
public async ready() {
|
|
273
|
+
const {startedAt} = this;
|
|
274
|
+
|
|
275
|
+
await this.callHook("$onReady");
|
|
276
|
+
|
|
277
|
+
this.log(`Started in ${new Date().getTime() - startedAt.getTime()} ms`);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async callHook(hook: string, ...args: any[]) {
|
|
281
|
+
if (!this.disableBootstrapLog) {
|
|
282
|
+
logger().debug(`\x1B[1mCall hook ${hook}\x1B[22m`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Load middlewares for the given hook
|
|
286
|
+
this.loadMiddlewaresFor(hook);
|
|
287
|
+
|
|
288
|
+
// call hooks added by providers
|
|
289
|
+
await $asyncEmit(hook, args);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
loadStatics(hook: string) {
|
|
293
|
+
const statics = constant<PlatformStaticsSettings>("statics");
|
|
294
|
+
const app = application();
|
|
295
|
+
|
|
296
|
+
getStaticsOptions(statics).forEach(({path, options}) => {
|
|
297
|
+
if (options.hook === hook) {
|
|
298
|
+
app.statics(path, options);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
useProvider(token: Type<any>, settings?: Partial<ProviderOpts>) {
|
|
304
|
+
injector().addProvider(token, settings);
|
|
305
|
+
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
bootstrap(): Promise<this> {
|
|
310
|
+
this.#promise = this.#promise || this.runLifecycle();
|
|
311
|
+
|
|
312
|
+
return this.#promise;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
protected mapRouters() {
|
|
316
|
+
const layers = this.platform.getLayers();
|
|
317
|
+
|
|
318
|
+
this.adapter.mapLayers(layers);
|
|
319
|
+
|
|
320
|
+
const rawBody =
|
|
321
|
+
constant("rawBody") ||
|
|
322
|
+
layers.some(({handlers}) => {
|
|
323
|
+
return handlers.some((handler) => handler.opts?.paramsTypes?.RAW_BODY);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
this.settings.set("rawBody", rawBody);
|
|
327
|
+
|
|
328
|
+
return this.logRoutes(layers.filter((layer) => layer.isProvider()));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
protected diff() {
|
|
332
|
+
const ms = colors.yellow(`+${new Date().getTime() - this.current.getTime()}ms`);
|
|
333
|
+
this.current = new Date();
|
|
334
|
+
return ms;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Load middlewares from configuration for the given hook
|
|
339
|
+
* @param hook
|
|
340
|
+
* @protected
|
|
341
|
+
*/
|
|
342
|
+
protected loadMiddlewaresFor(hook: string): void {
|
|
343
|
+
return getMiddlewaresForHook(hook, this.settings, "$beforeRoutesInit").forEach(({use}) => {
|
|
344
|
+
this.app.use(use);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
protected createHttpServers() {
|
|
349
|
+
this.#servers = this.adapter.getServers();
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
protected async listenServers(): Promise<void> {
|
|
353
|
+
this.#listeners = await Promise.all(this.#servers.map((cb) => cb && cb()));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
protected async logRoutes(layers: PlatformLayer[]) {
|
|
357
|
+
this.log("Routes mounted...");
|
|
358
|
+
|
|
359
|
+
if (!this.settings.get("logger.disableRoutesSummary") && !this.disableBootstrapLog) {
|
|
360
|
+
const routes: PlatformRouteDetails[] = layers.map((layer) => {
|
|
361
|
+
return {
|
|
362
|
+
url: layer.path,
|
|
363
|
+
method: layer.method,
|
|
364
|
+
name: layer.opts.name || `${layer.provider.className}.constructor()`,
|
|
365
|
+
className: layer.opts.className || layer.provider.className,
|
|
366
|
+
methodClassName: layer.opts.methodClassName || ""
|
|
367
|
+
} as PlatformRouteDetails;
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
logger().info(printRoutes(await $asyncAlter("$logRoutes", routes)));
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
protected async mapTokenMiddlewares() {
|
|
375
|
+
let middlewares = constant<PlatformMiddlewareLoadingOptions[]>("middlewares", []);
|
|
376
|
+
const env = constant<Env>("env");
|
|
377
|
+
const defaultHook = "$beforeRoutesInit";
|
|
378
|
+
|
|
379
|
+
const promises = middlewares.map(async (middleware: PlatformMiddlewareLoadingOptions): Promise<PlatformMiddlewareLoadingOptions> => {
|
|
380
|
+
if (isFunction(middleware)) {
|
|
381
|
+
return {
|
|
382
|
+
env,
|
|
383
|
+
hook: defaultHook,
|
|
384
|
+
use: middleware
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (isString(middleware)) {
|
|
389
|
+
middleware = {env, use: middleware, hook: defaultHook};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
let {use, options} = middleware;
|
|
393
|
+
|
|
394
|
+
if (isString(use)) {
|
|
395
|
+
if (["text-parser", "raw-parser", "json-parser", "urlencoded-parser"].includes(use)) {
|
|
396
|
+
use = this.adapter.bodyParser(use.replace("-parser", ""), options);
|
|
397
|
+
} else {
|
|
398
|
+
const mod = await import(use);
|
|
399
|
+
use = (mod.default || mod)(options);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (isClass(use) && ["$beforeInit", "$onInit", "$afterInit"].includes(middleware.hook!)) {
|
|
404
|
+
throw new Error(
|
|
405
|
+
`Ts.ED Middleware "${nameOf(use)}" middleware cannot be added on ${
|
|
406
|
+
middleware.hook
|
|
407
|
+
} hook. Use one of this hooks instead: $beforeRoutesInit, $onRoutesInit, $afterRoutesInit, $beforeListen, $afterListen, $onReady`
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
env,
|
|
413
|
+
hook: defaultHook,
|
|
414
|
+
...middleware,
|
|
415
|
+
use
|
|
416
|
+
};
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
middlewares = await Promise.all(promises);
|
|
420
|
+
|
|
421
|
+
configuration().set(
|
|
422
|
+
"middlewares",
|
|
423
|
+
middlewares.filter((middleware) => middleware.use)
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {FakeAdapter} from "../../../testing/index.js";
|
|
4
|
+
import {PlatformBuilder} from "../PlatformBuilder.js";
|
|
5
|
+
|
|
6
|
+
export class PlatformCustom extends FakeAdapter {
|
|
7
|
+
readonly NAME = "custom";
|
|
8
|
+
readonly providers = [
|
|
9
|
+
{
|
|
10
|
+
token: class Test {}
|
|
11
|
+
}
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
static create(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
|
|
15
|
+
return PlatformBuilder.create<any>(module, {
|
|
16
|
+
...settings,
|
|
17
|
+
adapter: PlatformCustom
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
|
|
22
|
+
return PlatformBuilder.build(module, {
|
|
23
|
+
...settings,
|
|
24
|
+
adapter: PlatformCustom
|
|
25
|
+
}).bootstrap();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
afterLoadRoutes(): Promise<any> {
|
|
29
|
+
return Promise.resolve(undefined);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
beforeLoadRoutes(): Promise<any> {
|
|
33
|
+
return Promise.resolve(undefined);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
useContext(): any {}
|
|
37
|
+
|
|
38
|
+
useRouter(): any {}
|
|
39
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {JsonMapperGlobalOptions} from "@tsed/json-mapper";
|
|
2
|
+
|
|
3
|
+
export interface PlatformJsonMapperSettings extends JsonMapperGlobalOptions {}
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
namespace TsED {
|
|
7
|
+
interface Configuration extends Record<string, any> {
|
|
8
|
+
jsonMapper?: PlatformJsonMapperSettings;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type multer from "multer";
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
namespace TsED {
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
export type MulterFile = Express.Multer.File;
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
export type MulterStorageEngine = multer.StorageEngine;
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
export type MulterOptions = multer.Options;
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
export type MulterField = multer.Field;
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
export type Multer = multer.Multer;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type PlatformMulter = TsED.Multer;
|
|
19
|
+
export type PlatformMulterSettings = TsED.MulterOptions;
|
|
20
|
+
export type PlatformMulterFile = TsED.MulterFile;
|
|
21
|
+
export type PlatformMulterField = TsED.MulterField;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
namespace TsED {
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
export interface StaticsOptions {
|
|
5
|
+
root: string;
|
|
6
|
+
hook?: "$beforeRoutesInit" | "$afterRoutesInit";
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface PlatformStaticsOptions extends TsED.StaticsOptions {
|
|
14
|
+
root: string;
|
|
15
|
+
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type PathOrStaticsOptions = string | PlatformStaticsOptions;
|
|
20
|
+
|
|
21
|
+
export interface PlatformStaticsSettings {
|
|
22
|
+
[endpoint: string]: PathOrStaticsOptions | PathOrStaticsOptions[];
|
|
23
|
+
}
|