Browse Source

feat(core): Allow NestJS middleware to be passed in apiOptions

Michael Bromley 4 years ago
parent
commit
44d0b45c42
2 changed files with 19 additions and 8 deletions
  1. 15 5
      packages/core/src/app.module.ts
  2. 4 3
      packages/core/src/config/vendure-config.ts

+ 15 - 5
packages/core/src/app.module.ts

@@ -1,4 +1,11 @@
-import { MiddlewareConsumer, Module, NestModule, OnApplicationShutdown } from '@nestjs/common';
+import {
+    MiddlewareConsumer,
+    Module,
+    NestMiddleware,
+    NestModule,
+    OnApplicationShutdown,
+} from '@nestjs/common';
+import { Type } from '@vendure/common/lib/shared-types';
 import { RequestHandler } from 'express';
 
 import { ApiModule } from './api/api.module';
@@ -10,6 +17,9 @@ import { I18nModule } from './i18n/i18n.module';
 import { I18nService } from './i18n/i18n.service';
 import { PluginModule } from './plugin/plugin.module';
 
+// tslint:disable-next-line:ban-types
+type Middleware = Type<any> | Function;
+
 @Module({
     imports: [ConfigModule, I18nModule, ApiModule, PluginModule.forRoot(), HealthCheckModule],
 })
@@ -19,7 +29,7 @@ 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: RequestHandler; route?: string }> = [
+        const defaultMiddleware: Array<{ handler: Middleware; route?: string }> = [
             { handler: i18nextHandler, route: adminApiPath },
             { handler: i18nextHandler, route: shopApiPath },
         ];
@@ -40,9 +50,9 @@ export class AppModule implements NestModule, OnApplicationShutdown {
      * Groups middleware handlers together in an object with the route as the key.
      */
     private groupMiddlewareByRoute(
-        middlewareArray: Array<{ handler: RequestHandler; route?: string }>,
-    ): { [route: string]: RequestHandler[] } {
-        const result = {} as { [route: string]: RequestHandler[] };
+        middlewareArray: Array<{ handler: Middleware; route?: string }>,
+    ): { [route: string]: Middleware[] } {
+        const result = {} as { [route: string]: Middleware[] };
         for (const middleware of middlewareArray) {
             const route = middleware.route || this.configService.apiOptions.adminApiPath;
             if (!result[route]) {

+ 4 - 3
packages/core/src/config/vendure-config.ts

@@ -1,4 +1,4 @@
-import { DynamicModule, Type } from '@nestjs/common';
+import { DynamicModule, NestMiddleware, Type } from '@nestjs/common';
 import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { PluginDefinition } from 'apollo-server-core';
@@ -124,11 +124,12 @@ export interface ApiOptions {
     cors?: boolean | CorsOptions;
     /**
      * @description
-     * Custom Express middleware for the server.
+     * Custom Express or NestJS middleware for the server.
      *
      * @default []
      */
-    middleware?: Array<{ handler: RequestHandler; route: string }>;
+    // tslint:disable-next-line:ban-types
+    middleware?: Array<{ handler: Type<any> | Function; route: string }>;
     /**
      * @description
      * Custom [ApolloServerPlugins](https://www.apollographql.com/docs/apollo-server/integrations/plugins/) which