npm package: @mindik/mailchimp-nestjs
Description: Dynamic module mailchimp_transactional for nestjs
Author: Anton Starenkov
Latest version: 1.2.4

<h1 align="center"></h1>

<div align="center">
  <a href="https://nestjs.com/" target="_blank">
    <img src="https://nestjs.com/img/logo_text.svg" width="150" alt="Nest Logo" />
  </a>
</div>

<h3 align="center">Mailchimp -- generated by @nestjsplus/dyn-schematics</h3>
<br/>
<div align="center">

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/Mindik/mailchimp-nestjs/blob/main/LICENSE)
[![Built with NestJS](https://img.shields.io/badge/built%20with-NestJs-red.svg)](https://nestjs.com)
[![Built with @nestjsplus/dyn-schematics](https://img.shields.io/badge/Built%20with-%40nestjsplus%2Fdyn--schematics-brightgreen)](https://github.com/nestjsplus/dyn-schematics)

</div>

<br/>

## About

The module is a thin wrapper for the [Mailchimp Transactional API](https://www.npmjs.com/package/@mailchimp/mailchimp_transactional).

<br/>

## Installation

```bash
npm i @mindik/mailchimp-nestjs
```

<br/>

## Quick Start

Import the module and pass your api key to it

```TypeScript
// app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MailchimpModule } from '@mindik/mailchimp-nestjs';

@Module({
  imports: [MailchimpModule.forRoot(/* API_KEY */)],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```
<br/>

..or use forRootAsync({ })

```TypeScript
// app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MailchimpModule } from '@mindik/mailchimp-nestjs';

@Module({
  imports: [
    MailchimpModule.forRootAsync({
      imports: [/* ConfigModule */]
      /* 
        useExisting 
        useFactory 
        useClass 
      */
      useFactory: async () => /* API_KEY */, 
      inject: [/* ConfigService */]
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```
<br/>

Inject Mailchimp in your service


```TypeScript
// app.service.ts
import { InjectMailchimp } from '@mindik/mailchimp-nestjs';
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  constructor(
    /*
      or 
      @Inject(MAILCHIMP_TOKEN) private readonly mail
    */
    @InjectMailchimp() private readonly mail
  ) {}

  // Test connection function ( == this.mail.users.ping())
  async checkMailchimp(): Promise<any> {
    return this.mail.pingPong();
  }
}
```

You can use a decorator

```TypeScript
@InjectMailchimp() private readonly mail
```

 that is an alias for a longer entry
 
```TypeScript
@Inject(MAILCHIMP_TOKEN) private readonly mail
```

<br/>
Further use in the application is no different from other modules...

Add the export to the decorator @Module()

```TypeScript
// app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MailchimpModule } from '@mindik/mailchimp-nestjs';

@Module({
  imports: [MailchimpModule.forRoot(/* API_KEY */)],
  controllers: [AppController],
  providers: [AppService],
  exports: [AppService],
})
export class AppModule {}
```

Import service in some module

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

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

Use in your some service

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

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

  async getPong(): Promise<any> {
    return 'SOME SERVICE ' + (await this.appService.checkMailchimp());
  }
}
```
<br/>

## 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

<br/>

## License

Distributed under the MIT License. See [LICENSE](https://github.com/Mindik/mailchimp-nestjs/blob/main/LICENSE) for more information.

<br/>

## Acknowledgements

 * [nestjs](https://nestjs.com/)

 * [@nestjsplus/dyn-schematics](https://github.com/nestjsplus/dyn-schematics)

 * [@mailchimp/mailchimp_transactional](https://www.npmjs.com/package/@mailchimp/mailchimp_transactional)