فهرست منبع

feat(server): Add onClose method to VendurePlugin interface

Michael Bromley 7 سال پیش
والد
کامیت
59aba88341

+ 7 - 0
server/e2e/fixtures/test-plugins.ts

@@ -92,3 +92,10 @@ export class TestPluginWithConfigAndBootstrap implements VendurePlugin {
         this.boostrapWasCalled(configService);
     }
 }
+
+export class TestPluginWithOnClose implements VendurePlugin {
+    constructor(private onCloseCallback: () => void) {}
+    onClose() {
+        this.onCloseCallback();
+    }
+}

+ 8 - 0
server/e2e/plugin.e2e-spec.ts

@@ -8,6 +8,7 @@ import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
 import {
     TestAPIExtensionPlugin,
     TestPluginWithConfigAndBootstrap,
+    TestPluginWithOnClose,
     TestPluginWithProvider,
 } from './fixtures/test-plugins';
 import { TestAdminClient, TestShopClient } from './test-client';
@@ -18,6 +19,7 @@ describe('Plugins', () => {
     const shopClient = new TestShopClient();
     const server = new TestServer();
     const bootstrapMockFn = jest.fn();
+    const onCloseFn = jest.fn();
 
     beforeAll(async () => {
         const token = await server.init(
@@ -30,6 +32,7 @@ describe('Plugins', () => {
                     new TestPluginWithConfigAndBootstrap(bootstrapMockFn),
                     new TestAPIExtensionPlugin(),
                     new TestPluginWithProvider(),
+                    new TestPluginWithOnClose(onCloseFn),
                 ],
             },
         );
@@ -74,4 +77,9 @@ describe('Plugins', () => {
         `);
         expect(result.names).toEqual(['seon', 'linda', 'hong']);
     });
+
+    it('calls onClose method when app is closed', async () => {
+        await server.destroy();
+        expect(onCloseFn).toHaveBeenCalled();
+    });
 });

+ 10 - 2
server/src/app.module.ts

@@ -1,4 +1,4 @@
-import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
+import { MiddlewareConsumer, Module, NestModule, OnModuleDestroy } from '@nestjs/common';
 import cookieSession = require('cookie-session');
 import { RequestHandler } from 'express';
 import { GraphQLDateTime } from 'graphql-iso-date';
@@ -14,7 +14,7 @@ import { I18nService } from './i18n/i18n.service';
 @Module({
     imports: [ConfigModule, I18nModule, ApiModule, EmailModule],
 })
-export class AppModule implements NestModule {
+export class AppModule implements NestModule, OnModuleDestroy {
     constructor(private configService: ConfigService, private i18nService: I18nService) {}
 
     configure(consumer: MiddlewareConsumer) {
@@ -44,6 +44,14 @@ export class AppModule implements NestModule {
         }
     }
 
+    async onModuleDestroy() {
+        for (const plugin of this.configService.plugins) {
+            if (plugin.onClose) {
+                await plugin.onClose();
+            }
+        }
+    }
+
     /**
      * Groups middleware handlers together in an object with the route as the key.
      */

+ 6 - 0
server/src/config/vendure-plugin/vendure-plugin.ts

@@ -66,6 +66,12 @@ export interface VendurePlugin {
      */
     onBootstrap?(inject: InjectorFn): void | Promise<void>;
 
+    /**
+     * @description
+     * This method is called when the app closes. It can be used for any clean-up logic such as stopping servers.
+     */
+    onClose?(): void | Promise<void>;
+
     /**
      * @description
      * The plugin may extend the default Vendure GraphQL shop api by implementing this method and providing extended