From c7bcb24eea4ae6c756c547784ff2ac1f499ed9e3 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 25 Dec 2023 11:04:57 +0530 Subject: [PATCH] refactor: update emitter parameter type to be EmitterLike --- providers/mail_provider.ts | 3 +-- src/fake_mailer.ts | 4 ++-- src/mail_manager.ts | 6 +++--- src/mailer.ts | 8 ++++---- src/messengers/memory_queue.ts | 6 +++--- src/types.ts | 8 ++++---- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/providers/mail_provider.ts b/providers/mail_provider.ts index bab0cf1..7f9a0da 100644 --- a/providers/mail_provider.ts +++ b/providers/mail_provider.ts @@ -9,7 +9,6 @@ import { configProvider } from '@adonisjs/core' import { RuntimeException } from '@poppinss/utils' -import type { Emitter } from '@adonisjs/core/events' import type { ApplicationService } from '@adonisjs/core/types' import { MailManager, Mailer, Message } from '../index.js' @@ -61,7 +60,7 @@ export default class MailProvider { ) } - return new MailManager(emitter as unknown as Emitter, config) + return new MailManager(emitter, config) }) this.app.container.bind(Mailer, async (resolver) => { diff --git a/src/fake_mailer.ts b/src/fake_mailer.ts index 7dc67b8..22c33fb 100644 --- a/src/fake_mailer.ts +++ b/src/fake_mailer.ts @@ -9,7 +9,7 @@ import string from '@poppinss/utils/string' import { AssertionError } from 'node:assert' -import type { Emitter } from '@adonisjs/core/events' +import type { EmitterLike } from '@adonisjs/core/types/events' import type { SentMessageInfo } from 'nodemailer/lib/json-transport/index.js' import { Mailer } from './mailer.js' @@ -511,7 +511,7 @@ export class FakeMailer extends Mailer implements MailerContract< mails = new MailsCollection() messages = new MessagesCollection() - constructor(name: string, emitter: Emitter, config: MailerConfig) { + constructor(name: string, emitter: EmitterLike, config: MailerConfig) { super(name, new JSONTransport(), emitter, config) super.setMessenger({ queue: async (mail, sendConfig) => { diff --git a/src/mail_manager.ts b/src/mail_manager.ts index 68a5720..28d31ed 100644 --- a/src/mail_manager.ts +++ b/src/mail_manager.ts @@ -8,7 +8,7 @@ */ import { RuntimeException } from '@poppinss/utils' -import type { Emitter } from '@adonisjs/core/events' +import type { EmitterLike } from '@adonisjs/core/types/events' import debug from './debug.js' import { Mailer } from './mailer.js' @@ -28,7 +28,7 @@ import type { * their lifecycle and switch between them. */ export class MailManager> { - #emitter: Emitter + #emitter: EmitterLike /** * Messenger to use on all mailers created @@ -47,7 +47,7 @@ export class MailManager>> = {} constructor( - emitter: Emitter, + emitter: EmitterLike, public config: MailerConfig & { default?: keyof KnownMailers mailers: KnownMailers diff --git a/src/mailer.ts b/src/mailer.ts index b318555..9ce2754 100644 --- a/src/mailer.ts +++ b/src/mailer.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import type { Emitter } from '@adonisjs/core/events' +import type { EmitterLike } from '@adonisjs/core/types/events' import debug from './debug.js' import { Message } from './message.js' @@ -32,7 +32,7 @@ export class Mailer implements MailerCo /** * Reference to AdonisJS application emitter */ - #emitter: Emitter + #emitter: EmitterLike /** * Messenger to use for queuing emails @@ -42,8 +42,8 @@ export class Mailer implements MailerCo constructor( public name: string, public transport: Transport, - emitter: Emitter, - public config: MailerConfig + emitter: EmitterLike, + public config: MailerConfig = {} ) { this.#emitter = emitter this.#messenger = new MemoryQueueMessenger(this, this.#emitter) diff --git a/src/messengers/memory_queue.ts b/src/messengers/memory_queue.ts index 0d35e39..2122ac6 100644 --- a/src/messengers/memory_queue.ts +++ b/src/messengers/memory_queue.ts @@ -8,7 +8,7 @@ */ import fastq, { type done } from 'fastq' -import type { Emitter } from '@adonisjs/core/events' +import type { EmitterLike } from '@adonisjs/core/types/events' import debug from '../debug.js' import type { MailResponse } from '../mail_response.js' @@ -43,7 +43,7 @@ function sendEmail( * emails within memory and send them in the chunks of 10 */ export class MemoryQueueMessenger implements MailerMessenger { - #emitter: Emitter + #emitter: EmitterLike #queue = fastq(this, sendEmail, 10) #jobCompletedCallback?: (error: Error | null, result: MailResponse) => void = ( error @@ -58,7 +58,7 @@ export class MemoryQueueMessenger implements MailerMessenger { constructor( public mailer: MailerContract, - emitter: Emitter + emitter: EmitterLike ) { this.#emitter = emitter } diff --git a/src/types.ts b/src/types.ts index 8eb2c43..28f4ec6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -8,15 +8,15 @@ */ import type { TlsOptions } from 'node:tls' -import { SendMailOptions } from 'nodemailer' +import type { SendMailOptions } from 'nodemailer' import type { SESClientConfig } from '@aws-sdk/client-ses' import type { ConfigProvider } from '@adonisjs/core/types' import type MimeNode from 'nodemailer/lib/mime-node/index.js' -import { BaseMail } from './base_mail.js' import type { Message } from './message.js' -import { MailManager } from './mail_manager.js' -import { MailResponse } from './mail_response.js' +import type { BaseMail } from './base_mail.js' +import type { MailManager } from './mail_manager.js' +import type { MailResponse } from './mail_response.js' /** * Shape of the envelope node after the email has been