npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-campay

v1.0.3

Published

NestJS module for Campay. Campay is a Mobile Money solutions provider in Cameroon

Downloads

3

Readme


Install

npm i nestjs-campay

Example

Firstly, import module with CampayModule.forRoot(...) or CampayModule.forRootAsync(...) only once in root module (check out module configuration docs below):

import { CampayModule } from 'nestjs-campay';

@Module({
  imports: [CampayModule.forRoot({
    permanentAccessToken: 'my-secret-api-key',
    isProduction: true
  })],
})
class AppModule {}

Secondly, inject the Campay service into your service to perform operations:

import { CampayService } from 'nestjs-campay';

export class MyService {
  constructor(private readonly campayService: CampayService) {}
  collect() {
    const res = await this.campayService.collect({
      amount: 5000,
      from: '2376xxxxxx',
      description: 'Paying goods',
      external_reference: 'xxx-xxx',
    });
  }
}

This module functions as a wrapper for the campay HTTP API. It inherits all constraints imposed by the campay HTTP API. For instance, in development mode (specified by the isProduction property during module registration), limitations such as a maximum transfer amount of 100XAF apply.

Configuration params

The following interface is used for the configuration:

interface Params {
  /**
   * Optional parameter
   * The campay permanentAccessToken.
   */
  permanentAccessToken?: string;

  /**
   * Optional parameter for setting the application username. Should be set along with the password.
   * 
   * Internally the username & password pair will be used to fetch an short-lived access token for performing calls to the Campay API
   */
  username?: string;

  /**
   * Optional parameter for setting the application password. Should be set along with the username
   */
  password?: string;

  /**
   * Optional parameter to set the module in production mode. Internally the correct campay base url will be determined based on this configuration.
   * 
   * Default: false
   * 
   */
  isProduction?: boolean;

  /**
   * Optional parameter to set the number of retries for refreshing the access token.
   * Only needed if username and password are also provided.
   * 
   * The refreshing could fail for various reasons as connection issues, temporary service outage, etc...
   * **Note: **The refreshing will not be retried in case of wrong credentials.
   * 
   * Default: 2
   * 
   */
  nbRefreshTokenRetries?: number;
}

When the permanentAccessToken is specified, authentication with the Campay API will utilize it. Otherwise, both the username and password options must be provided. Failure to provide either will result in an error being thrown during startup.

Synchronous configuration

Use CampayModule.forRoot method with argument of Params interface:

import { CampayModule } from 'nestjs-campay';

@Module({
  imports: [
    CampayModule.forRoot({
      username: 'xxx-xxx',
      password: 'super-secret-password',
    })
  ],
  ...
})
class MyModule {}

Asynchronous configuration

With CampayModule.forRootAsync you can, for example, import your ConfigModule and inject ConfigService to use it in useFactory method.

useFactory should return object with Params interface.

Here's an example:

import { CampayModule } from 'nestjs-campay';

@Injectable()
class ConfigService {
  public readonly permanentAccessToken = 'campay-api-key-xxx-xxx';
  public readonly isProduction = true;
}

@Module({
  providers: [ConfigService],
  exports: [ConfigService]
})
class ConfigModule {}

@Module({
  imports: [
    CampayModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        await somePromise();
        return {
          permanentAccessToken: config.permanentAccessToken,
          isProduction: config.isProduction,
          nbRefreshTokenRetries: 4
        };
      }
    })
  ],
  ...
})
class MyModule {}

Mocking axios instance for testing

This package exposes a AXIOS_INSTANCE_TOKEN token that can inject a custom axios implementation of type AxiosInstance (from axios package). Using this token, you can provide a mock implementation of the axios instance using any of the standard custom provider techniques, including useClass, useValue and useFactory.

  const module: TestingModule = await Test.createTestingModule({
    providers: [
      ...,
      {
        provide: AXIOS_INSTANCE_TOKEN,
        useValue: axiosMock,
      },
    ],
  }).compile();