Skip to content

Commit

Permalink
Merge pull request #12 from NextNC/feature/async-register
Browse files Browse the repository at this point in the history
feat(module): async registration
  • Loading branch information
NextNC authored Jan 30, 2021
2 parents 4ad5ba8 + 4280eaa commit b4720fd
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 25 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<Your Domain>',
API_KEY: '<Your API_KEY>',
HOST: '<Your 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
Expand All @@ -61,7 +83,7 @@ interface EmailOptions {
attachment?;
'recipient-variables'?: {
[email: string]: any;
}
};
}
```

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions src/nestjs-mailgun/configuration.ts
Original file line number Diff line number Diff line change
@@ -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<ModuleMetadata, 'imports'> {
useFactory: (
...args: any[]
) => ConfigurationMailgun | Promise<ConfigurationMailgun>;
inject?: any[];
}
37 changes: 27 additions & 10 deletions src/nestjs-mailgun/mailgun.module.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 || [],
};
}
}
19 changes: 11 additions & 8 deletions src/nestjs-mailgun/services/relay/mailgun.service.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<boolean> {
Expand All @@ -46,8 +50,7 @@ export class MailgunService {
resolve(false);
}
if (err) {
console.log('ERRRP', err);

console.log('ERR', err);
reject(err);
}
});
Expand Down
5 changes: 1 addition & 4 deletions src/nestjs-mailgun/tokens/tokens.ts
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit b4720fd

Please sign in to comment.