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

loopback-connector-twilio-ts

v1.0.0

Published

A basic loopback (v4) connector for twilio

Downloads

10

Readme

Twilio Connector for Loopback 4

This (twilio) connector/integration was built because there was none that worked with Loopback 4 that didn't need tweaking. I do want to give credit to the loopback-connector-twilio package for inspiring me to build my own. While I'm sure loopback-connector-twilio will work for earlier versions of loopback (<4) but for some reason I couldn't get it to work with lb4.

Supported message types:

  • sms
  • call

How to use it

Create a new datasource file: src/datasources/twilio.datasource.ts:

import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';

// import the TwilioConnector class and use it as the connector in the config
import { TwilioConnector } from 'loopback-connector-twilio-ts';

const config = {
  name: 'twilio',
  connector: TwilioConnector, // class goes here
  accountSid: process.env.TWILIO_SID ?? '', // environment
  authToken: process.env.TWILIO_AUTH_TOKEN ?? '',
};

@lifeCycleObserver('datasource')
export class TwilioDataSource extends juggler.DataSource
  implements LifeCycleObserver {
  static dataSourceName = 'twilio';
  static readonly defaultConfig = config;

  constructor(
    @inject('datasources.config.twilio', {optional: true})
    dsConfig: object = config,
  ) {
    super('twilio', dsConfig);
  }
}

Add it to the index file: src/datasources/index.ts

...
export * from './twilio.datasource';

Add a model that represents the message that's going to be sent: src/models/twilio.model.ts

import {Model, model, property} from '@loopback/repository';

@model()
export class TwilioMessage extends Model {
  @property({
    type: 'string',
    id: true,
    generated: false,
    required: true,
  })
  type: string;

  @property({
    type: 'string',
    required: true,
  })
  to: string;

  @property({
    type: 'string',
    required: true,
  })
  from: string;

  @property({
    type: 'string',
    required: true,
  })
  body: string;

  @property({
    type: 'string',
    required: true,
  })
  url: string;

  @property({
    type: 'string',
  })
  subject?: string;

  @property({
    type: 'string',
  })
  text?: string;

  @property({
    type: 'string',
  })
  html?: string;

  constructor(data?: Partial<TwilioMessage>) {
    super(data);
  }
}

export interface TwilioMessageRelations {
  // describe navigational properties here
}

export type TwilioMessageWithRelations = TwilioMessage & TwilioMessageRelations;

Add model to the index file: src/models/index.ts

...
export * from './twilio.model';

Create a twilio service src/services/twilio.service.ts

import {GenericService, getService} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {TwilioDataSource} from '../datasources';
import {TwilioMessage} from '../models';

export interface Twilio extends GenericService {
  send(data: TwilioMessage): Promise<unknown>;
}

export class TwilioProvider implements Provider<Twilio> {
  constructor(
    @inject('datasources.twilio')
    protected dataSource: TwilioDataSource = new TwilioDataSource(),
  ) {}

  value(): Promise<Twilio> {
    return getService(this.dataSource);
  }
}

Add service to index file: src/services/index.ts

...
export * from './twilio.service';

Inject the service into a controller method that might need the twilio service

export class SomeController {
  ... constructor and other methods

  @post('/send-message')
  async send(
    @inject('services.Twilio') twilioProvider: Twilio, // injected service
  ): Promise<{token: string}> {

    twilioProvider.send(new TwilioMessage({
      type: 'sms',
      to: 'some valid phone #',
      from: 'phone # purchased through twilio',
      body: 'Sending with twilio & LoopBack is Fun',
    })).then((a) => {
      console.log('a', a);
    }).catch((e) => {
      console.error('e', e);
    });

  }

  ... other methods

}

One could also inject the service into the class controller constructor like you would any other service/repository