app.module.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. MiddlewareConsumer,
  3. Module,
  4. NestModule,
  5. OnApplicationBootstrap,
  6. OnApplicationShutdown,
  7. OnModuleInit,
  8. } from '@nestjs/common';
  9. import { ModuleRef } from '@nestjs/core';
  10. import cookieSession = require('cookie-session');
  11. import { RequestHandler } from 'express';
  12. import { ApiModule } from './api/api.module';
  13. import { ConfigModule } from './config/config.module';
  14. import { ConfigService } from './config/config.service';
  15. import { Logger } from './config/logger/vendure-logger';
  16. import { I18nModule } from './i18n/i18n.module';
  17. import { I18nService } from './i18n/i18n.service';
  18. import { PluginModule } from './plugin/plugin.module';
  19. import { ProcessContextModule } from './process-context/process-context.module';
  20. @Module({
  21. imports: [ConfigModule, I18nModule, ApiModule, PluginModule.forRoot(), ProcessContextModule.forRoot()],
  22. })
  23. export class AppModule implements NestModule, OnApplicationBootstrap, OnApplicationShutdown {
  24. constructor(
  25. private configService: ConfigService,
  26. private i18nService: I18nService,
  27. private moduleRef: ModuleRef,
  28. ) {}
  29. async onApplicationBootstrap() {
  30. const { jobQueueStrategy } = this.configService.jobQueueOptions;
  31. if (typeof jobQueueStrategy.init === 'function') {
  32. await jobQueueStrategy.init(this.moduleRef);
  33. }
  34. }
  35. configure(consumer: MiddlewareConsumer) {
  36. const { adminApiPath, shopApiPath } = this.configService;
  37. const i18nextHandler = this.i18nService.handle();
  38. const defaultMiddleware: Array<{ handler: RequestHandler; route?: string }> = [
  39. { handler: i18nextHandler, route: adminApiPath },
  40. { handler: i18nextHandler, route: shopApiPath },
  41. ];
  42. if (this.configService.authOptions.tokenMethod === 'cookie') {
  43. const cookieHandler = cookieSession({
  44. name: 'session',
  45. secret: this.configService.authOptions.sessionSecret,
  46. httpOnly: true,
  47. });
  48. defaultMiddleware.push({ handler: cookieHandler, route: adminApiPath });
  49. defaultMiddleware.push({ handler: cookieHandler, route: shopApiPath });
  50. }
  51. const allMiddleware = defaultMiddleware.concat(this.configService.middleware);
  52. const middlewareByRoute = this.groupMiddlewareByRoute(allMiddleware);
  53. for (const [route, handlers] of Object.entries(middlewareByRoute)) {
  54. consumer.apply(...handlers).forRoutes(route);
  55. }
  56. }
  57. async onApplicationShutdown(signal?: string) {
  58. const { jobQueueStrategy } = this.configService.jobQueueOptions;
  59. if (typeof jobQueueStrategy.destroy === 'function') {
  60. await jobQueueStrategy.destroy();
  61. }
  62. if (signal) {
  63. Logger.info('Received shutdown signal:' + signal);
  64. }
  65. }
  66. /**
  67. * Groups middleware handlers together in an object with the route as the key.
  68. */
  69. private groupMiddlewareByRoute(
  70. middlewareArray: Array<{ handler: RequestHandler; route?: string }>,
  71. ): { [route: string]: RequestHandler[] } {
  72. const result = {} as { [route: string]: RequestHandler[] };
  73. for (const middleware of middlewareArray) {
  74. const route = middleware.route || this.configService.adminApiPath;
  75. if (!result[route]) {
  76. result[route] = [];
  77. }
  78. result[route].push(middleware.handler);
  79. }
  80. return result;
  81. }
  82. }