Skip to content

seyi-adeleke/nest-paystack

Repository files navigation

Coverage Status

Table Of Contents

About

nestjs-paystack implements a paystackModule, which when imported into your nest modules project provides a Paystack client to any class that injects it.

Installation

npm install --save nestjs-paystack

Getting Started

The simplest way to use nestjs-paystack is to import the forRoot function from PaystackModule into the module in which you want to inject the service.

import { Module } from '@nestjs-common';
import { PaystackModule } from 'nestjs-paystack';

@Module({
  imports: [
    PaystackModule.forRoot({
      apiKey: 'sk_xxxxxxxxx',
    }),
  ],
})
export class AppModule {}

You can then inject the Paystack client into your injectable classes by using a custom InjectPaystack decorator

import { Injectable } from '@nestjs/common';
import { InjectPaystack } from 'nestjs-paystack';

@Injectable()
export class AppService {
  public constructor(
  @InjectPaystack() private readonly paystackClient) {}
}

You can also set it up Asynchronously.

import { Module } from '@nestjs-common';
import { PaystackModule } from 'nestjs-paystack';
import { PaystackConfigService } from './PaystackConfigService';
import  { PaystackConfigModule } from './PaystackConfigModule';

@Module({
  imports: [
    PaystackConfigModule,
    PaystackModule.forRootAsync({
      inject: [PaystackConfigService],
      useFactory: (configService: PaystackConfigService) =>
        PaystackConfigService.getPaystackConfig(),
      }),
    }),
  ],
})
export class AppModule {}
// PaystackConfigService.ts
import { Injectable } from '@nestjs/common';
import { PaystackOptions } from 'nestjs-paystack';

@Injectable()
export class ConfigService {
  public getPaystackConfig(): PaystackOptions {
    return {
      apiKey: 'sk_xxxxxxxxxxxxxx',
    };
  }
}
// PaystackConfigModule.ts
import { Global, Module } from '@nestjs/common';
import { PaystackConfigService } from './PaystackConfigService';

@Global()
@Module({
  exports: [PaystackConfigService],
  providers: [PaystackConfigService],
})
export class PaystackConfigModule {}

Usage

This module uses paystack and all methods defined in the documentation are available to the paystackClient. Docs

License

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

Acknowledgements

Copyright © 2020 Seyi Adeleke

About

An npm Package for integrating Paystack with NestJs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published