ソースを参照

chore(email-plugin): Refactor folder structure

Michael Bromley 1 年間 前
コミット
20a6ea6b3d
26 ファイル変更112 行追加112 行削除
  1. 1 1
      packages/core/e2e/database-transactions.e2e-spec.ts
  2. 1 1
      packages/core/e2e/error-handler-strategy.e2e-spec.ts
  3. 1 1
      packages/core/src/api/common/request-context.ts
  4. 1 1
      packages/core/src/api/decorators/allow.decorator.ts
  5. 1 1
      packages/core/src/app.module.ts
  6. 2 2
      packages/core/src/event-bus/event-bus.spec.ts
  7. 1 1
      packages/core/src/job-queue/job.ts
  8. 1 1
      packages/core/src/service/helpers/fulfillment-state-machine/fulfillment-state.ts
  9. 1 1
      packages/core/src/service/helpers/refund-state-machine/refund-state.ts
  10. 8 7
      packages/email-plugin/index.ts
  11. 1 1
      packages/email-plugin/src/dev-mailbox.ts
  12. 5 8
      packages/email-plugin/src/email-processor.ts
  13. 1 1
      packages/email-plugin/src/event-listener.ts
  14. 1 1
      packages/email-plugin/src/generator/email-generator.ts
  15. 2 8
      packages/email-plugin/src/generator/handlebars-mjml-generator.ts
  16. 0 0
      packages/email-plugin/src/generator/noop-email-generator.ts
  17. 2 2
      packages/email-plugin/src/handler/default-email-handlers.ts
  18. 5 5
      packages/email-plugin/src/handler/event-handler.ts
  19. 0 0
      packages/email-plugin/src/handler/mock-events.ts
  20. 3 3
      packages/email-plugin/src/plugin.spec.ts
  21. 15 15
      packages/email-plugin/src/plugin.ts
  22. 2 2
      packages/email-plugin/src/sender/email-sender.ts
  23. 4 3
      packages/email-plugin/src/sender/nodemailer-email-sender.ts
  24. 3 1
      packages/email-plugin/src/template-loader/file-based-template-loader.ts
  25. 45 0
      packages/email-plugin/src/template-loader/template-loader.ts
  26. 5 45
      packages/email-plugin/src/types.ts

+ 1 - 1
packages/core/e2e/database-transactions.e2e-spec.ts

@@ -232,7 +232,7 @@ describe('Transaction infrastructure', () => {
             expect(e.message).toContain('Failed!');
         }
 
-        // Wait a bit to see an events in handlers
+        // Wait a bit to see an events in handler
         await new Promise(resolve => setTimeout(resolve, 100));
 
         expect(TransactionTestPlugin.callHandler).not.toHaveBeenCalled();

+ 1 - 1
packages/core/e2e/error-handler-strategy.e2e-spec.ts

@@ -99,7 +99,7 @@ describe('ErrorHandlerStrategy', () => {
         await server.destroy();
     });
 
-    it('no error handlers have initially been called', async () => {
+    it('no error handler have initially been called', async () => {
         expect(TestErrorHandlerStrategy.serverErrorSpy).toHaveBeenCalledTimes(0);
         expect(TestErrorHandlerStrategy.workerErrorSpy).toHaveBeenCalledTimes(0);
     });

+ 1 - 1
packages/core/src/api/common/request-context.ts

@@ -26,7 +26,7 @@ export type SerializedRequestContext = {
  * required at various points of the stack.
  *
  * It is a good practice to inject the RequestContext (using the {@link Ctx} decorator) into
- * _all_ resolvers & REST handlers, and then pass it through to the service layer.
+ * _all_ resolvers & REST handler, and then pass it through to the service layer.
  *
  * This allows the service layer to access information about the current user, the active language,
  * the active Channel, and so on. In addition, the {@link TransactionalConnection} relies on the

+ 1 - 1
packages/core/src/api/decorators/allow.decorator.ts

@@ -10,7 +10,7 @@ export const PERMISSIONS_METADATA_KEY = '__permissions__';
  *
  * In a GraphQL context, it can be applied to top-level queries and mutations as well as field resolvers.
  *
- * For REST controllers, it can be applied to route handlers.
+ * For REST controllers, it can be applied to route handler.
  *
  * ## Allow and Sessions
  * The `@Allow()` decorator is closely linked to the way Vendure manages sessions. For any operation or route that is decorated

+ 1 - 1
packages/core/src/app.module.ts

@@ -50,7 +50,7 @@ export class AppModule implements NestModule, OnApplicationShutdown {
     }
 
     /**
-     * Groups middleware handlers together in an object with the route as the key.
+     * Groups middleware handler together in an object with the route as the key.
      */
     private groupMiddlewareByRoute(middlewareArray: Middleware[]): { [route: string]: MiddlewareHandler[] } {
         const result = {} as { [route: string]: MiddlewareHandler[] };

+ 2 - 2
packages/core/src/event-bus/event-bus.spec.ts

@@ -54,7 +54,7 @@ describe('EventBus', () => {
             expect(handler).toHaveBeenCalledWith(event3);
         });
 
-        it('multiple handlers are called', async () => {
+        it('multiple handler are called', async () => {
             const handler1 = vi.fn();
             const handler2 = vi.fn();
             const handler3 = vi.fn();
@@ -156,7 +156,7 @@ describe('EventBus', () => {
             expect(handler).toHaveBeenCalledWith(event3);
         });
 
-        it('multiple handlers are called', async () => {
+        it('multiple handler are called', async () => {
             const handler1 = vi.fn();
             const handler2 = vi.fn();
             const handler3 = vi.fn();

+ 1 - 1
packages/core/src/job-queue/job.ts

@@ -206,7 +206,7 @@ export class Job<T extends JobData<T> = any> {
 
     /**
      * @description
-     * Used to register event handlers for job events
+     * Used to register event handler for job events
      */
     on(eventType: JobEventType, listener: JobEventListener<T>) {
         this.eventListeners[eventType].push(listener);

+ 1 - 1
packages/core/src/service/helpers/fulfillment-state-machine/fulfillment-state.ts

@@ -35,7 +35,7 @@ export type FulfillmentState =
 
 /**
  * @description
- * The data which is passed to the state transition handlers of the FulfillmentStateMachine.
+ * The data which is passed to the state transition handler of the FulfillmentStateMachine.
  *
  * @docsCategory fulfillment
  */

+ 1 - 1
packages/core/src/service/helpers/refund-state-machine/refund-state.ts

@@ -26,7 +26,7 @@ export const refundStateTransitions: Transitions<RefundState> = {
 
 /**
  * @description
- * The data which is passed to the state transition handlers of the RefundStateMachine.
+ * The data which is passed to the state transition handler of the RefundStateMachine.
  *
  * @docsCategory payment
  */

+ 8 - 7
packages/email-plugin/index.ts

@@ -1,11 +1,12 @@
-export * from './src/default-email-handlers';
-export * from './src/nodemailer-email-sender';
-export * from './src/event-handler';
+export * from './src/handler/default-email-handlers';
+export * from './src/sender/nodemailer-email-sender';
+export * from './src/handler/event-handler';
 export * from './src/event-listener';
-export * from './src/handlebars-mjml-generator';
-export * from './src/noop-email-generator';
+export * from './src/generator/handlebars-mjml-generator';
+export * from './src/generator/noop-email-generator';
 export * from './src/plugin';
 export * from './src/template-loader';
 export * from './src/types';
-export * from './src/email-generator';
-export * from './src/email-sender';
+export * from './src/generator/email-generator';
+export * from './src/sender/email-sender';
+export { TemplateLoader } from './src/template-loader/template-loader';

+ 1 - 1
packages/email-plugin/src/dev-mailbox.ts

@@ -4,7 +4,7 @@ import { Request, Router } from 'express';
 import fs from 'fs-extra';
 import path from 'path';
 
-import { EmailEventHandler } from './event-handler';
+import { EmailEventHandler } from './handler/event-handler';
 import { EmailPluginDevModeOptions, EventWithContext } from './types';
 
 /**

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

@@ -1,23 +1,20 @@
 import { Inject, Injectable } from '@nestjs/common';
 import { ModuleRef } from '@nestjs/core';
-import { Ctx, Injector, InternalServerError, Logger, RequestContext } from '@vendure/core';
+import { Injector, Logger, RequestContext } from '@vendure/core';
 import fs from 'fs-extra';
 
 import { deserializeAttachments } from './attachment-utils';
 import { isDevModeOptions, resolveTransportSettings } from './common';
 import { EMAIL_PLUGIN_OPTIONS, loggerCtx } from './constants';
-import { EmailGenerator } from './email-generator';
-import { EmailSender } from './email-sender';
-import { HandlebarsMjmlGenerator } from './handlebars-mjml-generator';
-import { NodemailerEmailSender } from './nodemailer-email-sender';
-import { FileBasedTemplateLoader } from './template-loader';
+import { EmailGenerator } from './generator/email-generator';
+import { HandlebarsMjmlGenerator } from './generator/handlebars-mjml-generator';
+import { EmailSender } from './sender/email-sender';
+import { NodemailerEmailSender } from './sender/nodemailer-email-sender';
 import {
     EmailDetails,
-    EmailPluginOptions,
     EmailTransportOptions,
     InitializedEmailPluginOptions,
     IntermediateEmailDetails,
-    TemplateLoader,
 } from './types';
 
 /**

+ 1 - 1
packages/email-plugin/src/event-listener.ts

@@ -1,6 +1,6 @@
 import { Type } from '@vendure/common/lib/shared-types';
 
-import { EmailEventHandler } from './event-handler';
+import { EmailEventHandler } from './handler/event-handler';
 import { EventWithContext } from './types';
 
 /**

+ 1 - 1
packages/email-plugin/src/email-generator.ts → packages/email-plugin/src/generator/email-generator.ts

@@ -1,6 +1,6 @@
 import { InjectableStrategy, VendureEvent } from '@vendure/core';
 
-import { EmailDetails, EmailPluginOptions } from './types';
+import { EmailDetails, EmailPluginOptions } from '../types';
 
 /**
  * @description

+ 2 - 8
packages/email-plugin/src/handlebars-mjml-generator.ts → packages/email-plugin/src/generator/handlebars-mjml-generator.ts

@@ -1,16 +1,10 @@
 import dateFormat from 'dateformat';
-import fs from 'fs-extra';
 import Handlebars from 'handlebars';
 import mjml2html from 'mjml';
-import path from 'path';
+
+import { InitializedEmailPluginOptions } from '../types';
 
 import { EmailGenerator } from './email-generator';
-import {
-    EmailPluginDevModeOptions,
-    EmailPluginOptions,
-    InitializedEmailPluginOptions,
-    Partial,
-} from './types';
 
 /**
  * @description

+ 0 - 0
packages/email-plugin/src/noop-email-generator.ts → packages/email-plugin/src/generator/noop-email-generator.ts


+ 2 - 2
packages/email-plugin/src/default-email-handlers.ts → packages/email-plugin/src/handler/default-email-handlers.ts

@@ -1,7 +1,6 @@
 /* eslint-disable @typescript-eslint/no-non-null-assertion */
 import {
     AccountRegistrationEvent,
-    AssetStorageStrategy,
     ConfigService,
     EntityHydrator,
     IdentifierChangeRequestEvent,
@@ -15,8 +14,9 @@ import {
 } from '@vendure/core';
 import { Request } from 'express';
 
+import { EmailEventListener } from '../event-listener';
+
 import { EmailEventHandler } from './event-handler';
-import { EmailEventListener } from './event-listener';
 import {
     mockAccountRegistrationEvent,
     mockEmailAddressChangeEvent,

+ 5 - 5
packages/email-plugin/src/event-handler.ts → packages/email-plugin/src/handler/event-handler.ts

@@ -2,9 +2,9 @@ import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { Type } from '@vendure/common/lib/shared-types';
 import { Injector, Logger } from '@vendure/core';
 
-import { serializeAttachments } from './attachment-utils';
-import { loggerCtx } from './constants';
-import { EmailEventListener } from './event-listener';
+import { serializeAttachments } from '../attachment-utils';
+import { loggerCtx } from '../constants';
+import { EmailEventListener } from '../event-listener';
 import {
     EmailAttachment,
     EmailTemplateConfig,
@@ -15,7 +15,7 @@ import {
     SetAttachmentsFn,
     SetOptionalAddressFieldsFn,
     SetTemplateVarsFn,
-} from './types';
+} from '../types';
 
 /**
  * @description
@@ -118,7 +118,7 @@ import {
  *   // Add an instance of the plugin to the plugins array
  *   plugins: [
  *     EmailPlugin.init({
- *       handlers: [...defaultEmailHandlers, quoteRequestedHandler],
+ *       handler: [...defaultEmailHandlers, quoteRequestedHandler],
  *       templatePath: path.join(__dirname, 'vendure/email/templates'),
  *       // ... etc
  *     }),

+ 0 - 0
packages/email-plugin/src/mock-events.ts → packages/email-plugin/src/handler/mock-events.ts


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

@@ -22,10 +22,10 @@ import path from 'path';
 import { Readable } from 'stream';
 import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
 
-import { orderConfirmationHandler } from './default-email-handlers';
+import { orderConfirmationHandler } from './handler/default-email-handlers';
 import { EmailProcessor } from './email-processor';
-import { EmailSender } from './email-sender';
-import { EmailEventHandler } from './event-handler';
+import { EmailSender } from './sender/email-sender';
+import { EmailEventHandler } from './handler/event-handler';
 import { EmailEventListener } from './event-listener';
 import { EmailPlugin } from './plugin';
 import { EmailDetails, EmailPluginOptions, EmailTransportOptions } from './types';

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

@@ -26,7 +26,7 @@ import { isDevModeOptions, resolveTransportSettings } from './common';
 import { EMAIL_PLUGIN_OPTIONS, loggerCtx } from './constants';
 import { DevMailbox } from './dev-mailbox';
 import { EmailProcessor } from './email-processor';
-import { EmailEventHandler, EmailEventHandlerWithAsyncData } from './event-handler';
+import { EmailEventHandler, EmailEventHandlerWithAsyncData } from './handler/event-handler';
 import { FileBasedTemplateLoader } from './template-loader';
 import {
     EmailPluginDevModeOptions,
@@ -46,13 +46,13 @@ import {
  * Vendure has an internal events system (see {@link EventBus}) that allows plugins to subscribe to events. The EmailPlugin is configured with {@link EmailEventHandler}s
  * that listen for a specific event and when it is published, the handler defines which template to use to generate the resulting email.
  *
- * The plugin comes with a set of default handlers for the following events:
+ * The plugin comes with a set of default handler for the following events:
  * - Order confirmation
  * - New customer email address verification
  * - Password reset request
  * - Email address change request
  *
- * You can also create your own handlers and register them with the plugin - see the {@link EmailEventHandler} docs for more details.
+ * You can also create your own handler and register them with the plugin - see the {@link EmailEventHandler} docs for more details.
  *
  * ## Installation
  *
@@ -70,7 +70,7 @@ import {
  *   // Add an instance of the plugin to the plugins array
  *   plugins: [
  *     EmailPlugin.init({
- *       handlers: defaultEmailHandlers,
+ *       handler: defaultEmailHandlers,
  *       templatePath: path.join(__dirname, 'static/email/templates'),
  *       transport: {
  *         type: 'smtp',
@@ -135,21 +135,21 @@ import {
  * * `formatMoney`: Formats an amount of money (which are always stored as integers in Vendure) as a decimal, e.g. `123` => `1.23`
  * * `formatDate`: Formats a Date value with the [dateformat](https://www.npmjs.com/package/dateformat) package.
  *
- * ## Extending the default email handlers
+ * ## Extending the default email handler
  *
- * The `defaultEmailHandlers` array defines the default handlers such as for handling new account registration, order confirmation, password reset
+ * The `defaultEmailHandlers` array defines the default handler such as for handling new account registration, order confirmation, password reset
  * etc. These defaults can be extended by adding custom templates for languages other than the default, or even completely new types of emails
  * which respond to any of the available [VendureEvents](/reference/typescript-api/events/).
  *
- * A good way to learn how to create your own email handlers is to take a look at the
- * [source code of the default handlers](https://github.com/vendure-ecommerce/vendure/blob/master/packages/email-plugin/src/default-email-handlers.ts).
- * New handlers are defined in exactly the same way.
+ * A good way to learn how to create your own email handler is to take a look at the
+ * [source code of the default handler](https://github.com/vendure-ecommerce/vendure/blob/master/packages/email-plugin/src/default-email-handlers.ts).
+ * New handler are defined in exactly the same way.
  *
- * It is also possible to modify the default handlers:
+ * It is also possible to modify the default handler:
  *
  * ```ts
  * // Rather than importing `defaultEmailHandlers`, you can
- * // import the handlers individually
+ * // import the handler individually
  * import {
  *   orderConfirmationHandler,
  *   emailVerificationHandler,
@@ -176,10 +176,10 @@ import {
  *     customer: event.data.customer,
  *   }));
  *
- * // Then you pass the handlers to the EmailPlugin init method
+ * // Then you pass the handler to the EmailPlugin init method
  * // individually
  * EmailPlugin.init({
- *   handlers: [
+ *   handler: [
  *     myOrderConfirmationHandler,
  *     myPasswordResetHandler,
  *     emailVerificationHandler,
@@ -203,7 +203,7 @@ import {
  * const config: VendureConfig = {
  *   plugins: [
  *     EmailPlugin.init({
- *       handlers: defaultEmailHandlers,
+ *       handler: defaultEmailHandlers,
  *       templatePath: path.join(__dirname, 'static/email/templates'),
  *       transport: (injector, ctx) => {
  *         if (ctx) {
@@ -231,7 +231,7 @@ import {
  * EmailPlugin.init({
  *   devMode: true,
  *   route: 'mailbox',
- *   handlers: defaultEmailHandlers,
+ *   handler: defaultEmailHandlers,
  *   templatePath: path.join(__dirname, 'vendure/email/templates'),
  *   outputPath: path.join(__dirname, 'test-emails'),
  * })

+ 2 - 2
packages/email-plugin/src/email-sender.ts → packages/email-plugin/src/sender/email-sender.ts

@@ -1,6 +1,6 @@
 import { InjectableStrategy } from '@vendure/core';
 
-import { EmailDetails, EmailTransportOptions } from './types';
+import { EmailDetails, EmailTransportOptions } from '../types';
 
 /**
  * @description
@@ -30,7 +30,7 @@ import { EmailDetails, EmailTransportOptions } from './types';
  *   // ...
  *   plugins: [
  *     EmailPlugin.init({
- *        // ... template, handlers config omitted
+ *        // ... template, handler config omitted
  *       transport: { type: 'none' },
  *        emailSender: new SendgridEmailSender(),
  *     }),

+ 4 - 3
packages/email-plugin/src/nodemailer-email-sender.ts → packages/email-plugin/src/sender/nodemailer-email-sender.ts

@@ -9,15 +9,16 @@ import path from 'path';
 import { Stream } from 'stream';
 import { format } from 'util';
 
-import { loggerCtx } from './constants';
-import { EmailSender } from './email-sender';
+import { loggerCtx } from '../constants';
 import {
     EmailDetails,
     EmailTransportOptions,
     SendmailTransportOptions,
     SESTransportOptions,
     SMTPTransportOptions,
-} from './types';
+} from '../types';
+
+import { EmailSender } from './email-sender';
 
 export type StreamTransportInfo = {
     envelope: {

+ 3 - 1
packages/email-plugin/src/template-loader.ts → packages/email-plugin/src/template-loader/file-based-template-loader.ts

@@ -2,7 +2,9 @@ import { Injector, RequestContext } from '@vendure/core';
 import fs from 'fs/promises';
 import path from 'path';
 
-import { LoadTemplateInput, Partial, TemplateLoader } from './types';
+import { LoadTemplateInput, Partial } from '../types';
+
+import { TemplateLoader } from './template-loader';
 
 /**
  * @description

+ 45 - 0
packages/email-plugin/src/template-loader/template-loader.ts

@@ -0,0 +1,45 @@
+import { Injector, RequestContext } from '@vendure/core';
+
+import { LoadTemplateInput, Partial } from '../types';
+
+/**
+ * @description
+ * Loads email templates based on the given request context, type and template name
+ * and return the template as a string.
+ *
+ * @example
+ * ```ts
+ * import { EmailPlugin, TemplateLoader } from '\@vendure/email-plugin';
+ *
+ * class MyTemplateLoader implements TemplateLoader {
+ *      loadTemplate(injector, ctx, { type, templateName }){
+ *          return myCustomTemplateFunction(ctx);
+ *      }
+ * }
+ *
+ * // In vendure-config.ts:
+ * ...
+ * EmailPlugin.init({
+ *     templateLoader: new MyTemplateLoader()
+ *     ...
+ * })
+ * ```
+ *
+ * @docsCategory core plugins/EmailPlugin
+ * @docsPage TemplateLoader
+ * @docsWeight 0
+ */
+export interface TemplateLoader {
+    /**
+     * @description
+     * Load template and return it's content as a string
+     */
+    loadTemplate(injector: Injector, ctx: RequestContext, input: LoadTemplateInput): Promise<string>;
+
+    /**
+     * @description
+     * Load partials and return their contents.
+     * This method is only called during initialization, i.e. during server startup.
+     */
+    loadPartials?(): Promise<Partial[]>;
+}

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

@@ -4,10 +4,11 @@ import { Injector, RequestContext, SerializedRequestContext, VendureEvent } from
 import { Attachment } from 'nodemailer/lib/mailer';
 import SESTransport from 'nodemailer/lib/ses-transport';
 import SMTPTransport from 'nodemailer/lib/smtp-transport';
+import { EmailEventHandler } from './handler/event-handler';
 
-import { EmailGenerator } from './email-generator';
-import { EmailSender } from './email-sender';
-import { EmailEventHandler } from './event-handler';
+import { EmailGenerator } from './generator/email-generator';
+import { EmailSender } from './sender/email-sender';
+import { TemplateLoader } from './template-loader/template-loader';
 
 /**
  * @description
@@ -178,7 +179,7 @@ export interface SMTPTransportOptions extends SMTPTransport.Options {
  *   // Add an instance of the plugin to the plugins array
  *   plugins: [
  *     EmailPlugin.init({
- *       handlers: defaultEmailHandlers,
+ *       handler: defaultEmailHandlers,
  *       templatePath: path.join(__dirname, 'static/email/templates'),
  *       transport: {
  *         type: 'ses',
@@ -362,47 +363,6 @@ export interface Partial {
     content: string;
 }
 
-/**
- * @description
- * Loads email templates based on the given request context, type and template name
- * and return the template as a string.
- *
- * @example
- * ```ts
- * import { EmailPlugin, TemplateLoader } from '\@vendure/email-plugin';
- *
- * class MyTemplateLoader implements TemplateLoader {
- *      loadTemplate(injector, ctx, { type, templateName }){
- *          return myCustomTemplateFunction(ctx);
- *      }
- * }
- *
- * // In vendure-config.ts:
- * ...
- * EmailPlugin.init({
- *     templateLoader: new MyTemplateLoader()
- *     ...
- * })
- * ```
- *
- * @docsCategory core plugins/EmailPlugin
- * @docsPage TemplateLoader
- * @docsWeight 0
- */
-export interface TemplateLoader {
-    /**
-     * @description
-     * Load template and return it's content as a string
-     */
-    loadTemplate(injector: Injector, ctx: RequestContext, input: LoadTemplateInput): Promise<string>;
-    /**
-     * @description
-     * Load partials and return their contents.
-     * This method is only called during initialization, i.e. during server startup.
-     */
-    loadPartials?(): Promise<Partial[]>;
-}
-
 /**
  * @description
  * A function used to define template variables available to email templates.