GithubHelp home page GithubHelp logo

lkaric / nestjs-twilio Goto Github PK

View Code? Open in Web Editor NEW
41.0 1.0 10.0 643 KB

Injectable Twilio client for Nestjs.

Home Page: https://www.npmjs.com/package/nestjs-twilio

License: MIT License

Shell 2.98% JavaScript 28.66% TypeScript 68.35%
nestjs nodejs twilio twilio-api typescript hacktoberfest

nestjs-twilio's Introduction

nestjs-twilio

Injectable Twilio client for Nestjs.

npm version miniziped size tree shaking MIT licensed

Implementing the TwilioModule from this package you gain access to Twilio client through dependency injection with minimal setup.

Instalation

$ npm install --save nestjs-twilio
$ yarn add nestjs-twilio

Getting Started

To use Twilio client we need to register module for example in app.module.ts

import { TwilioModule } from 'nestjs-twilio';

@Module({
  imports: [
    TwilioModule.forRoot({
      accountSid: process.env.TWILIO_ACCOUNT_SID,
      authToken: process.env.TWILIO_AUTH_TOKEN,
    }),
  ],
})
export class AppModule {}

If you are using the @nestjs/config package from nest, you can use the ConfigModule using the registerAsync() function to inject your environment variables like this in your custom module:

import { TwilioModule } from 'nestjs-twilio';

@Module({
  imports: [
    TwilioModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (cfg: ConfigService) => ({
        accountSid: cfg.get('TWILIO_ACCOUNT_SID'),
        authToken: cfg.get('TWILIO_AUTH_TOKEN'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

Example usage in service.

import { InjectTwilio, TwilioService } from 'nestjs-twilio';

@Injectable()
export class AppService {
  public constructor(private readonly twilioService: TwilioService) {}

  async sendSMS() {
    return this.twilioService.client.messages.create({
      body: 'SMS Body, sent to the phone!',
      from: TWILIO_PHONE_NUMBER,
      to: TARGET_PHONE_NUMBER,
    });
  }
}

For full Client API see Twilio Node SDK reference here

Testing

Example of testing can be found here.

nestjs-twilio's People

Contributors

dependabot[bot] avatar djedlajn avatar jacopopatroclo avatar lkaric avatar luluhoc avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nestjs-twilio's Issues

Configurable module import dependency resolution error

Bug Report

Describe the Bug

Thanks for the package! I'm getting the following error when attempting to import TwilioModule into another module using the service:

[Nest] 66091  - 2022-11-18, 11:13:07 p.m.   ERROR [ExceptionHandler] Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] at index [0] is available in the TwilioModule context.

Potential solutions:
- If CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] is a provider, is it part of the current TwilioModule?
- If CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] is exported from a separate @Module, is that module imported within TwilioModule?
  @Module({
    imports: [ /* the Module containing CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] */ ]
  })

Error: Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument CONFIGURABLE_MODULE_OPTIONS[fbec59cd-15f8-4bb2-804d-1987cd0a8ef6] at index [0] is available in the TwilioModule context.

How to Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Add TwilioModule.forRootAsync... as defined in app.module.ts
  2. Add TwilioService to the constructor of a service
  3. Import TwilioModule in the *.module.ts for that service

Expected Behavior

No dependency injection error

Error

See above.

Your Environment

  • OS: Mac
  • Version of nestjs-twilio: 3.1.0
  • Version of NestJS - 9.2.0

Error reproducing steps

Please explain how did your error ocurr, you can also leave gists, repos or any kind of codebase.

Additional Information

Any other information about the problem here.

Twilio itself should be a peer dependency

Feature Request

I want to be able to select a different version for Twilio client

Describe the Feature

I want to be able to select a different version of Twilio node client instead of the specific one that this wrapper enforce.

Describe the Solution you'd like

Add Twilio as a peer dependency instead of a direct dependency in package.json, maybe with something like this.

In package.json

....

"peerDependencies": {
    "twilio": "*"
  }

....

The star in there is because this package do not use some specific feature of twilio and should not enforce some specific version.

Additional Information

I use this article as a reference for when to use peer or not.

See for example TypeOrm Nest integration. In this case this package enforce a specific version for TypeOrm.

I will follow up this issue with a pull request if you guys don't have the time to look into it.

Nest 9.X support?

Bug Report

Describe the Bug

This package cannot be installed on NestJS 9 projects

How to Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. try to install this package on a NestJS 9 project.

Expected Behavior

Tell me what should happen.

Error

Error resulted by the potential bug.

Your Environment

  • OS: [e.g. macOS, Windows]
  • Version of nestjs-twilio: [e.g. v1.0.0]

Error reproducing steps

Please explain how did your error ocurr, you can also leave gists, repos or any kind of codebase.

Additional Information

Any other information about the problem here.

Is there way to disable or postpone client authorization?

On my project, I have an urgent need to delay authorization or even disable client but load the module. Perhaps there is a way to inherit from NestTwilioModule and somehow make delayed authorization? The matter is that from start I do not have accountSid and authToken to authorize.

Configurable module import dependency resolution error

Bug Report

Describe the Bug

[Nest] 21260 - 20/07/2023, 9:57:29 pm ERROR [ExceptionHandler] Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] at index [0] is available in the TwilioModule context.

Potential solutions:

  • Is TwilioModule a valid NestJS module?
  • If CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] is a provider, is it part of the current TwilioModule?
  • If CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] is exported from a separate @module, is that module imported within TwilioModule?
    @module({
    imports: [ /* the Module containing CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] */ ]
    })

Error: Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] at index [0] is available in the TwilioModule context.

Potential solutions:

  • Is TwilioModule a valid NestJS module?
  • If CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] is a provider, is it part of the current TwilioModule?
  • If CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] is exported from a separate @module, is that module imported within TwilioModule?
    @module({
    imports: [ /* the Module containing CONFIGURABLE_MODULE_OPTIONS[fabf635f2b63dc62a1ece] */ ]
    })

How to Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Add TwilioModule.forRootAsync... as defined in app.module.ts
  2. Add TwilioService to the constructor of a service
  3. Import TwilioModule in the *.module.ts for that service

Expected Behavior

No dependency injection error

Error

Error resulted by the potential bug.

Your Environment

  • OS: Windows
  • Version of nestjs-twilio: v3.2.0

Error reproducing steps

Please explain how did your error ocurr, you can also leave gists, repos or any kind of codebase.

Additional Information

Any other information about the problem here.

Constructor injection gives the following error

'TwilioClient' refers to a value, but is being used as a type here. Did you mean 'typeof TwilioClient'?ts(2749)
Parameter 'client' of constructor from exported class has or is using private name 'TwilioClient'.ts(4063)

Ubuntu 20.10
Nestjs

  constructor(
    @InjectTwilio() 
    private readonly client: TwilioClient
  ) {}

testing

How to testing with this module? I am using like this:

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [
        CacheModule.register(),
        TwilioModule.forRoot({
          accountSid: process.env.TEST_ACCOUNT_SID,
          authToken: process.env.TEST_VERIFICATION_SID,
        }),
      ],
      providers: [
        AuthService,
        AuthResolver,
        AuthConfig,
        CookieService,
        JwtStrategy,
        UserService,
        {
          provide: getModelToken('UserDto'),
          useValue: fakeUsersModel,
        },
      ],
    }).compile();

    service = module.get<AuthService>(AuthService);
  });

cant resolve dependency tree

Bug Report

NestJS Info

image

Describe the Bug

installing this npm causes an error

npm i nestjs-twilio 

and give me this
image

Expected Behavior

it should install properly or without an error.

Workaround

you can install the npm by this,

npm i nestjs-twilio  --legacy-peer-deeps

Ill update this ticket if I find something meaningful.

Property 'forRoot' does not exist on type 'TwilioModule'

Bug Report

Describe the Bug

Hi! I am trying to integrate the library in the current version v3.1.0 but on registering the module exactly as recommended in the getting started documentation i receive the following error Property 'forRootAsync' does not exist on type 'typeof TwilioModule'

How to Reproduce

Just follow the library documentation with the register module example:

Screen Shot 2022-10-14 at 13 44 49

My Environment

  • OS: macOS
  • Version of nestjs-twilio: v3.1.0

does not work with sub-module

Bug Report

AppModule.ts

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { NotificationsModule } from './notifications/notifications.module';

@Module({
  imports: [ConfigModule.forRoot(), NotificationsModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

NotificationsModule.ts

import { Module } from '@nestjs/common';
import { NotificationsService } from './notifications.service';
import { NotificationsController } from './notifications.controller';
import { TwilioModule } from 'nestjs-twilio';
import { TwilioVerifyService } from './twilio/twilio-verify.service';

@Module({
  imports: [
    TwilioModule.forRoot({
      accountSid: process.env.TWILIO_ACCOUNT_SID,
      authToken: process.env.TWILIO_AUTH_TOKEN,
    }),
  ],
  providers: [NotificationsService, TwilioVerifyService],
  controllers: [NotificationsController],
})
export class NotificationsModule {}

Expected Behavior

i am still new to nestjs but i think, it should work with feature module. It works with root module(Appmodule.ts)

Error

When i import it to app.module, it works
but when i import it to Notifications.module, it does not work.

   TwilioModule.forRoot({
      accountSid: process.env.TWILIO_ACCOUNT_SID,
      authToken: process.env.TWILIO_AUTH_TOKEN,
    }),

I get below error when i import it to NotificationsModule.ts

/app/node_modules/twilio/lib/rest/Twilio.js:138
    throw new Error('username is required');
          ^
Error: username is required
    at new Twilio (/Users/nileshp/clients/blanclabs/medx/medx-be-notifications/app/node_modules/twilio/lib/rest/Twilio.js:138:11)
    at Object.initializer [as default] (/Users/nileshp/clients/blanclabs/medx/medx-be-notifications/app/node_modules/twilio/lib/index.js:10:10)
    at createTwilioClient (/Users/nileshp/clients/blanclabs/medx/medx-be-notifications/app/node_modules/nestjs-twilio/dist/common/twilio.utils.js:29:34)
    at createTwilioProviders (/Users/nileshp/clients/blanclabs/medx/medx-be-notifications/app/node_modules/nestjs-twilio/dist/providers/twilio.provider.js:8:51)
    at Function.forRoot (/Users/nileshp/clients/blanclabs/medx/medx-be-notifications/app/node_modules/nestjs-twilio/dist/twilio-core.module.js:25:64)
    at Function.forRoot (/Users/nileshp/clients/blanclabs/medx/medx-be-notifications/app/node_modules/nestjs-twilio/dist/twilio.module.js:17:61)
    at Object.<anonymous> (/Users/nileshp/clients/blanclabs/medx/medx-be-notifications/app/src/notifications/notifications.module.ts:9:18)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)

Your Environment

  • OS: macOS
  • nestjs-twilio: v2.2.1

Package JSON Bug

Bug Report

Describe the Bug

A clear and concise description of what the bug is.

package.engines.npm does not specify an appropriate NPM version. I get an NPM warning everytime I install my packages because of this and it is annoying.

How to Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Follow readme and install nest-twilio.
  2. See warning.
  3. Run npm i
  4. See warning again...

Expected Behavior

Tell me what should happen.

Installing the package with the specified version of npm should not display a warning. Installing a package with a version outside of the specified version of npm should produce a meaningful warning.

Error

Error resulted by the potential bug.

Your Environment

  • OS: [e.g. macOS, Windows]
  • Version of nestjs-twilio: [e.g. v1.0.0]

Error reproducing steps

Please explain how did your error ocurr, you can also leave gists, repos or any kind of codebase.

General use of package.

Additional Information

Any other information about the problem here.

N/A

Property 'forRoot' does not exist on type 'typeof TwilioModule'

Bug Report

Describe the Bug

Getting Property 'forRoot' does not exist on type 'typeof TwilioModule'
A clear and concise description of what the bug is.

How to Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:
just add the twilioModule.forRoot....

Expected Behavior

forRoot will not give an ts error
Screen Shot 2022-12-01 at 21 07 17

Tell me what should happen.

Error

Error resulted by the potential bug.

Your Environment

mac, vscode

  • Version of nestjs-twilio: 3.2.0

Error reproducing steps

Please explain how did your error ocurr, you can also leave gists, repos or any kind of codebase.

Additional Information

Any other information about the problem here.

Can I register multiple Twillio accounts in one app?

Help wanted

Can I register multiple Twillio accounts in one app? What should I do?

Case

This is my case.

  • I have a platform to serve different customers.
  • I need to send different information to different customers on the platform. However, each customer wants to use their own Twilio account for independent billing.
  • How should I deal with such a situation?
  • How to dynamically load the Twilio account and ID under the user account from the database and send information according to different user requests when the app is running?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.