Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

twilio-functions-utils

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twilio-functions-utils

Twilio Functions utils library

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
46
decreased by-69.93%
Maintainers
1
Weekly downloads
 
Created
Source

Twilio Functions Utils

About

This lib was created with the aim of simplifying the use of serverless Twilio, reducing the need to apply frequent try-catches and improving context management, making it no longer necessary to return the callback() method in all functions.

How it works

useInjection

The useInjection method takes two parameters. The first to apply as a handler and the last is an object of configuration options.

[useInjection] Options

Can contain providers that will be defined, which act as use cases to perform internal actions in the handler function through the "this" method.

You can pass validateToken equal true too, to force Token validation using Twilio Flex Token Validator

useInjection(yourFunction,
  {
    providers: [create, remove],
    validateToken: true
  }
);
Twilio Flex Token Validator

When using Token Validator, the Request body must contain a valid Token from Twilio.

// Event
{
  Token: "Twilio-Token-Here"
}

Response

The responses coming from the function destined to the handler must be returned as an instance of Response.

Response recebe uma string e um number (status code):

return new Response('Your pretty answer.', 200);

There are two failure response models, BadRequest and NotFound. The use follows the same model.

const notFound = new NotFoundError('Your error message here.');
const badRequest = new BadRequestError('Your error message here.');

TwiMLResponse

There is an own response template to use with the TwiML format:

const twimlVoice = new Twilio.twiml
  .VoiceResponse();

const enqueueVoice = twimlVoice
  .enqueue({
    action,
    workflowSid,
  })
  .task('{}');

return new TwiMLResponse(twimlVoice, 201)

Install

npm install twilio-functions-utils

Usage

// File: assets/create.private.js

exports.create = async function () {
  return new Promise((resolve, reject) => {
    const random = Math.random();

    if (random >= 0.5) {
      return resolve({ sucess: 'Resolved' });
    }
  
    return reject(new Error('Unresolved'));
  });
};
// File: functions/create.js

const { useInjection, Response } = require('twilio-functions-utils');
const { create } = require(Runtime.getAssets()['/create.js'].path)

/**
 * @param { Record<string, unknown> } event
 * @this { {
 * request: Record<string, unknown>,
 * cookies: Record<string, string>,
 * client: import('twilio').Twilio,
 * props: {
 *      TWILIO_WORKFLOW_SID: string,
 *      TWILIO_WORKFLOW_SID: string,
 *      DOMAIN_NAME: string
 * },
 * useCase: {
 *      create: create,
 * } } }
 * @returns { Promise<unknown> }
 */
async function createAction(event) {
  // You can perform all your "controller" level actions, as you have access to the request headers and cookies.
  const { cookies, request } = this

  // Then just call the useCase you provided to handler by using useInjection.
  const useCaseResult = await this.useCase.create(event)

  // Just put it on a Response object and you are good to go!
  return new Response(useCaseResult, 201);
}

exports.handler = useInjection(createAction, {
  providers: [
    create,
  ],
  validateToken: true, // When using Token Validator, the Request body must contain a valid Token from Twilio.
});

Author

  • Iago Calazans - 🛠 Senior Node.js Engineer at Stone

Keywords

FAQs

Package last updated on 10 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc