diff --git a/README.md b/README.md index b5ed1cd..5a20a10 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,28 @@ import { MailgunModule } from '@nextnm/nestjs-mailgun'; export class YourModule {} ``` +#### Importing module Async + +```typescript +import { MailgunModule } from '@nextnm/nestjs-mailgun'; +@Module({ + imports: [ + MailgunModule.forAsyncRoot({ + useFactory: async () => { + return { + DOMAIN: '', + API_KEY: '', + HOST: '', // default: 'api.mailgun.net'. Note that if you are using the EU region the host should be set to 'api.eu.mailgun.net' + }; + }, + }), + ], + providers: [], + exports: [], +}) +export class YourModule {} +``` + #### Interfaces ```typescript @@ -61,7 +83,7 @@ interface EmailOptions { attachment?; 'recipient-variables'?: { [email: string]: any; - } + }; } ``` diff --git a/package-lock.json b/package-lock.json index 3166ad3..4922653 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@nextnm/nestjs-mailgun", - "version": "1.0.3", + "version": "1.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 3ed8e87..3512499 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nextnm/nestjs-mailgun", - "version": "1.0.3", + "version": "1.0.4", "description": "NestJS provider for sending emails with mailgun", "author": "Nuno Carvalhão ", "license": "MIT", diff --git a/src/nestjs-mailgun/configuration.ts b/src/nestjs-mailgun/configuration.ts index d9b3a86..1ef0808 100644 --- a/src/nestjs-mailgun/configuration.ts +++ b/src/nestjs-mailgun/configuration.ts @@ -1,6 +1,16 @@ +import { ModuleMetadata } from '@nestjs/common/interfaces'; + export interface ConfigurationMailgun { DOMAIN: string; API_KEY: string; PUBLIC_API_KEY?: string; HOST?: string; } + +export interface ConfigurationMailgunAsync + extends Pick { + useFactory: ( + ...args: any[] + ) => ConfigurationMailgun | Promise; + inject?: any[]; +} diff --git a/src/nestjs-mailgun/mailgun.module.ts b/src/nestjs-mailgun/mailgun.module.ts index 14efc39..f7ff90b 100644 --- a/src/nestjs-mailgun/mailgun.module.ts +++ b/src/nestjs-mailgun/mailgun.module.ts @@ -1,7 +1,10 @@ -import { Module } from '@nestjs/common'; -import { ConfigurationMailgun } from './configuration'; +import { Module, Provider } from '@nestjs/common'; +import { + ConfigurationMailgun, + ConfigurationMailgunAsync, +} from './configuration'; import { MailgunService } from './services/relay/mailgun.service'; -import { API_KEY, DOMAIN, HOST, PUBLIC_API_KEY } from './tokens/tokens'; +import { MAILGUN_CONFIGURATION } from './tokens/tokens'; @Module({}) export class MailgunModule { @@ -12,16 +15,30 @@ export class MailgunModule { // ...controllers, // ], providers: [ - { provide: API_KEY, useValue: config.API_KEY }, - { - provide: PUBLIC_API_KEY, - useValue: config.PUBLIC_API_KEY ? config.PUBLIC_API_KEY : '', - }, - { provide: DOMAIN, useValue: config.DOMAIN }, - { provide: HOST, useValue: config.HOST || 'api.mailgun.net' }, + { provide: MAILGUN_CONFIGURATION, useValue: config }, MailgunService, ], exports: [MailgunService], }; } + public static forAsyncRoot(config: ConfigurationMailgunAsync) { + return { + module: MailgunModule, + // controllers: [ + // ...controllers, + // ], + imports: config.imports || [], + providers: [this.createAsyncProviders(config), MailgunService], + exports: [MailgunService], + }; + } + private static createAsyncProviders( + options: ConfigurationMailgunAsync, + ): Provider { + return { + provide: MAILGUN_CONFIGURATION, + useFactory: options.useFactory, + inject: options.inject || [], + }; + } } diff --git a/src/nestjs-mailgun/services/relay/mailgun.service.ts b/src/nestjs-mailgun/services/relay/mailgun.service.ts index 2155f2c..1ab637e 100644 --- a/src/nestjs-mailgun/services/relay/mailgun.service.ts +++ b/src/nestjs-mailgun/services/relay/mailgun.service.ts @@ -1,6 +1,7 @@ import { Injectable, Inject } from '@nestjs/common'; -import { API_KEY, DOMAIN, HOST, PUBLIC_API_KEY } from '../../tokens/tokens'; +import { MAILGUN_CONFIGURATION } from '../../tokens/tokens'; import * as mailgunConfig from 'mailgun-js'; +import { ConfigurationMailgun } from '../../../nestjs-mailgun/configuration'; export interface EmailOptions { from: string; @@ -18,12 +19,15 @@ export interface EmailOptions { export class MailgunService { private readonly mailgun; constructor( - @Inject(API_KEY) private readonly apiKey, - @Inject(DOMAIN) private readonly domain, - @Inject(HOST) private readonly host, - @Inject(PUBLIC_API_KEY) private readonly publicApiKey, + @Inject(MAILGUN_CONFIGURATION) + private readonly configuration: ConfigurationMailgun, ) { - this.mailgun = mailgunConfig({ apiKey, domain, publicApiKey, host }); + this.mailgun = mailgunConfig({ + apiKey: configuration.API_KEY, + domain: configuration.DOMAIN, + publicApiKey: configuration.PUBLIC_API_KEY, + host: configuration.HOST, + }); } public sendEmail(emailOptions: EmailOptions): Promise { @@ -46,8 +50,7 @@ export class MailgunService { resolve(false); } if (err) { - console.log('ERRRP', err); - + console.log('ERR', err); reject(err); } }); diff --git a/src/nestjs-mailgun/tokens/tokens.ts b/src/nestjs-mailgun/tokens/tokens.ts index f95dbb0..20fc2eb 100644 --- a/src/nestjs-mailgun/tokens/tokens.ts +++ b/src/nestjs-mailgun/tokens/tokens.ts @@ -1,4 +1 @@ -export const API_KEY = 'API_KEY'; -export const PUBLIC_API_KEY = 'PUBLIC_API_KEY'; -export const DOMAIN = 'DOMAIN'; -export const HOST = 'HOST'; +export const MAILGUN_CONFIGURATION = 'MAILGUN_CONFIGURATION';