app.module.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { MiddlewareConsumer, Module, NestModule, OnModuleDestroy } from '@nestjs/common';
  2. import cookieSession = require('cookie-session');
  3. import { RequestHandler } from 'express';
  4. import { GraphQLDateTime } from 'graphql-iso-date';
  5. import { ApiModule } from './api/api.module';
  6. import { ConfigModule } from './config/config.module';
  7. import { ConfigService } from './config/config.service';
  8. import { EmailModule } from './email/email.module';
  9. import { validateCustomFieldsConfig } from './entity/custom-entity-fields';
  10. import { I18nModule } from './i18n/i18n.module';
  11. import { I18nService } from './i18n/i18n.service';
  12. @Module({
  13. imports: [ConfigModule, I18nModule, ApiModule, EmailModule],
  14. })
  15. export class AppModule implements NestModule, OnModuleDestroy {
  16. constructor(private configService: ConfigService, private i18nService: I18nService) {}
  17. configure(consumer: MiddlewareConsumer) {
  18. const { adminApiPath, shopApiPath } = this.configService;
  19. // tslint:disable-next-line:no-floating-promises
  20. validateCustomFieldsConfig(this.configService.customFields);
  21. const i18nextHandler = this.i18nService.handle();
  22. const defaultMiddleware: Array<{ handler: RequestHandler; route?: string }> = [
  23. { handler: i18nextHandler, route: adminApiPath },
  24. { handler: i18nextHandler, route: shopApiPath },
  25. ];
  26. if (this.configService.authOptions.tokenMethod === 'cookie') {
  27. const cookieHandler = cookieSession({
  28. name: 'session',
  29. secret: this.configService.authOptions.sessionSecret,
  30. httpOnly: true,
  31. });
  32. defaultMiddleware.push({ handler: cookieHandler, route: adminApiPath });
  33. defaultMiddleware.push({ handler: cookieHandler, route: shopApiPath });
  34. }
  35. const allMiddleware = defaultMiddleware.concat(this.configService.middleware);
  36. const middlewareByRoute = this.groupMiddlewareByRoute(allMiddleware);
  37. for (const [route, handlers] of Object.entries(middlewareByRoute)) {
  38. consumer.apply(...handlers).forRoutes(route);
  39. }
  40. }
  41. async onModuleDestroy() {
  42. for (const plugin of this.configService.plugins) {
  43. if (plugin.onClose) {
  44. await plugin.onClose();
  45. }
  46. }
  47. }
  48. /**
  49. * Groups middleware handlers together in an object with the route as the key.
  50. */
  51. private groupMiddlewareByRoute(
  52. middlewareArray: Array<{ handler: RequestHandler; route?: string }>,
  53. ): { [route: string]: RequestHandler[] } {
  54. const result = {} as { [route: string]: RequestHandler[] };
  55. for (const middleware of middlewareArray) {
  56. const route = middleware.route || this.configService.adminApiPath;
  57. if (!result[route]) {
  58. result[route] = [];
  59. }
  60. result[route].push(middleware.handler);
  61. }
  62. return result;
  63. }
  64. }