Browse Source

feat(email-plugin): Provide an Injector instance to .loadData function

BREAKING CHANGE: If you are using the `.loadData()` method of an EmailEventHandler, the callback signature has changed to provide an instance of the Injector class, rather than an `inject()` function.
Michael Bromley 5 years ago
parent
commit
e2665a7a16

+ 4 - 4
packages/email-plugin/src/plugin.spec.ts

@@ -367,8 +367,8 @@ describe('EmailPlugin', () => {
         it('loads async data', async () => {
             const handler = new EmailEventListener('test')
                 .on(MockEvent)
-                .loadData(async ({ inject }) => {
-                    const service = inject(MockService);
+                .loadData(async ({ injector }) => {
+                    const service = injector.get(MockService);
                     return service.someAsyncMethod();
                 })
                 .setFrom('"test from" <noreply@test.com>')
@@ -398,8 +398,8 @@ describe('EmailPlugin', () => {
                 .setFrom('"test from" <noreply@test.com>')
                 .setSubject('Hello, {{ testData }}!')
                 .setRecipient(() => 'test@test.com')
-                .loadData(async ({ inject }) => {
-                    const service = inject(MockService);
+                .loadData(async ({ injector }) => {
+                    const service = injector.get(MockService);
                     return service.someAsyncMethod();
                 })
                 .setTemplateVars(event => ({ testData: event.data }));

+ 5 - 5
packages/email-plugin/src/plugin.ts

@@ -1,8 +1,8 @@
 import { ModuleRef } from '@nestjs/core';
-import { InjectConnection } from '@nestjs/typeorm';
 import {
     createProxyHandler,
     EventBus,
+    Injector,
     JobQueue,
     JobQueueService,
     Logger,
@@ -10,11 +10,11 @@ import {
     OnVendureClose,
     PluginCommonModule,
     RuntimeVendureConfig,
+    TransactionalConnection,
     Type,
     VendurePlugin,
     WorkerService,
 } from '@vendure/core';
-import { Connection } from 'typeorm';
 
 import { isDevModeOptions } from './common';
 import { EMAIL_PLUGIN_OPTIONS } from './constants';
@@ -154,7 +154,7 @@ export class EmailPlugin implements OnVendureBootstrap, OnVendureClose {
     /** @internal */
     constructor(
         private eventBus: EventBus,
-        @InjectConnection() private connection: Connection,
+        private connection: TransactionalConnection,
         private moduleRef: ModuleRef,
         private workerService: WorkerService,
         private jobQueueService: JobQueueService,
@@ -234,10 +234,10 @@ export class EmailPlugin implements OnVendureBootstrap, OnVendureClose {
         const { type } = handler;
         try {
             if (handler instanceof EmailEventHandlerWithAsyncData) {
+                const injector = new Injector(this.moduleRef);
                 (event as EventWithAsyncData<EventWithContext, any>).data = await handler._loadDataFn({
                     event,
-                    connection: this.connection,
-                    inject: t => this.moduleRef.get(t, { strict: false }),
+                    injector,
                 });
             }
             const result = await handler.handle(event as any, EmailPlugin.options.globalTemplateVars);

+ 2 - 4
packages/email-plugin/src/types.ts

@@ -1,6 +1,5 @@
 import { Omit } from '@vendure/common/lib/omit';
-import { RequestContext, Type, VendureEvent, WorkerMessage } from '@vendure/core';
-import { Connection } from 'typeorm';
+import { Injector, RequestContext, VendureEvent, WorkerMessage } from '@vendure/core';
 
 import { EmailEventHandler } from './event-handler';
 
@@ -291,8 +290,7 @@ export interface EmailGenerator<T extends string = any, E extends VendureEvent =
  */
 export type LoadDataFn<Event extends EventWithContext, R> = (context: {
     event: Event;
-    connection: Connection;
-    inject: <T>(type: Type<T>) => T;
+    injector: Injector;
 }) => Promise<R>;
 
 export type IntermediateEmailDetails = {