|
|
@@ -1,7 +1,7 @@
|
|
|
import { MiddlewareConsumer, Module, NestModule, OnApplicationShutdown } from '@nestjs/common';
|
|
|
-import { Type } from '@vendure/common/lib/shared-types';
|
|
|
|
|
|
import { ApiModule } from './api/api.module';
|
|
|
+import { Middleware, MiddlewareHandler } from './common';
|
|
|
import { ConfigModule } from './config/config.module';
|
|
|
import { ConfigService } from './config/config.service';
|
|
|
import { Logger } from './config/logger/vendure-logger';
|
|
|
@@ -12,9 +12,6 @@ import { PluginModule } from './plugin/plugin.module';
|
|
|
import { ProcessContextModule } from './process-context/process-context.module';
|
|
|
import { ServiceModule } from './service/service.module';
|
|
|
|
|
|
-// tslint:disable-next-line:ban-types
|
|
|
-type Middleware = Type<any> | Function;
|
|
|
-
|
|
|
@Module({
|
|
|
imports: [
|
|
|
ProcessContextModule,
|
|
|
@@ -32,12 +29,13 @@ export class AppModule implements NestModule, OnApplicationShutdown {
|
|
|
configure(consumer: MiddlewareConsumer) {
|
|
|
const { adminApiPath, shopApiPath, middleware } = this.configService.apiOptions;
|
|
|
const i18nextHandler = this.i18nService.handle();
|
|
|
- const defaultMiddleware: Array<{ handler: Middleware; route?: string }> = [
|
|
|
+ const defaultMiddleware: Middleware[] = [
|
|
|
{ handler: i18nextHandler, route: adminApiPath },
|
|
|
{ handler: i18nextHandler, route: shopApiPath },
|
|
|
];
|
|
|
const allMiddleware = defaultMiddleware.concat(middleware);
|
|
|
- const middlewareByRoute = this.groupMiddlewareByRoute(allMiddleware);
|
|
|
+ const consumableMiddlewares = allMiddleware.filter(mid => !mid.beforeListen);
|
|
|
+ const middlewareByRoute = this.groupMiddlewareByRoute(consumableMiddlewares);
|
|
|
for (const [route, handlers] of Object.entries(middlewareByRoute)) {
|
|
|
consumer.apply(...handlers).forRoutes(route);
|
|
|
}
|
|
|
@@ -52,10 +50,8 @@ export class AppModule implements NestModule, OnApplicationShutdown {
|
|
|
/**
|
|
|
* Groups middleware handlers together in an object with the route as the key.
|
|
|
*/
|
|
|
- private groupMiddlewareByRoute(
|
|
|
- middlewareArray: Array<{ handler: Middleware; route?: string }>,
|
|
|
- ): { [route: string]: Middleware[] } {
|
|
|
- const result = {} as { [route: string]: Middleware[] };
|
|
|
+ private groupMiddlewareByRoute(middlewareArray: Middleware[]): { [route: string]: MiddlewareHandler[] } {
|
|
|
+ const result = {} as { [route: string]: MiddlewareHandler[] };
|
|
|
for (const middleware of middlewareArray) {
|
|
|
const route = middleware.route || this.configService.apiOptions.adminApiPath;
|
|
|
if (!result[route]) {
|