@mindik/mailgun-nestjs
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

Mailgun -- generated by @nestjsplus/dyn-schematics


MIT License Built with NestJS Built with @nestjsplus/dyn-schematics


About

The module is a thin wrapper for the Mailgun API.


Installation

npm i @mindik/mailgun-nestjs

Quick Start

Add in your tsconfig.json

{
  "esModuleInterop": true
}

Import the module and pass options to it

// app.app-module.ts
import { AppModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MailgunModule } from '@mindik/mailgun-nestjs';

@AppModule({
  imports: [
    MailgunModule.forRoot({
      /**
       * username: string;
       * key: string;
       * url?: string;
       * public_key?: string;
       * timeout?: number;
       */
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

..or use forRootAsync({ })

// app.app-module.ts
import { AppModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MailgunModule } from '@mindik/mailgun-nestjs';

@AppModule({
  imports: [
    MailgunModule.forRootAsync({
      imports: [/** ConfigModule */],
      /** 
        * useExisting 
        * useFactory 
        * useClass 
      */
      useFactory: async () => (
        {
          /**
           * username: string;
           * key: string;
           * url?: string;
           * public_key?: string;
           * timeout?: number;
           */
        }
      ), 
      inject: [/** ConfigService */]
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Inject Mailgun in your service

// app.service.ts
import { InjectMailgun } from '@mindik/mailgun-nestjs';
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  constructor(
    /*
      or 
      @Inject(MAILGUN_TOKEN) private readonly mail
    */
    @InjectMailgun() private readonly mg
  ) {}

  async test() {
    return this.mg.messages
      .create('sandbox-123.mailgun.org', {
        from: 'Excited User <mailgun@sandbox-123.mailgun.org>',
        to: ['test@example.com'],
        subject: 'Hello',
        text: 'Testing some Mailgun awesomness!',
        html: '<h1>Testing some Mailgun awesomness!</h1>',
      })
      .then((msg) => console.log(msg)) // logs response data
      .catch((err) => console.log(err)); // logs any error
  }
}

You can use a decorator

@InjectMailgun() private readonly mg

that is an alias for a longer entry

@Inject(MAILGUN_TOKEN) private readonly mg

Further use in the application is no different from other modules...

Add the export to the decorator @AppModule()

// app.app-module.ts
import { AppModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MailgunModule } from '@mindik/mailgun-nestjs';

@AppModule({
  imports: [
    MailgunModule.forRoot({
      /**
       * username: string;
       * key: string;
       * url?: string;
       * public_key?: string;
       * timeout?: number;
       */
    })
  ],
  controllers: [AppController],
  providers: [AppService],
  exports: [AppService],
})
export class AppModule {}

Import service in some module

// some.app-module.ts
import { AppModule } from '@nestjs/common';
import { AppService } from 'src/app.service';
import { SomeController } from './some.controller';
import { SomeService } from './some.service';

@AppModule({
  controllers: [SomeController],
  providers: [AppService, SomeService],
})
export class SomeModule {}

Use in your some service

// some.service.ts
import { Injectable } from '@nestjs/common';
import { AppService } from 'src/app.service';

@Injectable()
export class SomeService {
  constructor(private readonly appService: AppService) {}

  async test(): Promise<any> {
    return 'SOME SERVICE ' + (await this.appService.test());
  }
}

Contributing

Any suggestions for improving the project are welcome.

  1. Fork the repository
  2. Create your branch (git checkout -b my-branch)
  3. Commit any changes to your branch
  4. Push your changes to your remote branch
  5. Open a pull request

License

Distributed under the MIT License. See LICENSE for more information.


Acknowledgements

Readme

Keywords

Package Sidebar

Install

npm i @mindik/mailgun-nestjs

Weekly Downloads

99

Version

1.2.0

License

MIT

Unpacked Size

29.7 kB

Total Files

29

Last publish

Collaborators

  • mindik