Skip to content

Commit

Permalink
feat(email-plugin): Provide an Injector instance to .loadData function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaelbromley committed Oct 1, 2020
1 parent e1fe622 commit e2665a7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/email-plugin/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" <[email protected]>')
Expand Down Expand Up @@ -398,8 +398,8 @@ describe('EmailPlugin', () => {
.setFrom('"test from" <[email protected]>')
.setSubject('Hello, {{ testData }}!')
.setRecipient(() => '[email protected]')
.loadData(async ({ inject }) => {
const service = inject(MockService);
.loadData(async ({ injector }) => {
const service = injector.get(MockService);
return service.someAsyncMethod();
})
.setTemplateVars(event => ({ testData: event.data }));
Expand Down
10 changes: 5 additions & 5 deletions packages/email-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { ModuleRef } from '@nestjs/core';
import { InjectConnection } from '@nestjs/typeorm';
import {
createProxyHandler,
EventBus,
Injector,
JobQueue,
JobQueueService,
Logger,
OnVendureBootstrap,
OnVendureClose,
PluginCommonModule,
RuntimeVendureConfig,
TransactionalConnection,
Type,
VendurePlugin,
WorkerService,
} from '@vendure/core';
import { Connection } from 'typeorm';

import { isDevModeOptions } from './common';
import { EMAIL_PLUGIN_OPTIONS } from './constants';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions packages/email-plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit e2665a7

Please sign in to comment.