Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
twilio-functions-utils
Advanced tools
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.
The useInjection method takes two parameters. The first to apply as a handler and the last is an object of configuration 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
}
);
When using Token Validator, the Request body must contain a valid Token from Twilio.
// Event
{
Token: "Twilio-Token-Here"
}
The responses coming from the function destined to the handler must be returned as an instance of Response.
Response receives a string and a number (status code):
return new Response('Your pretty answer.', 200);
There are two failure response models, BadRequest and NotFound. Its use follows the same model.
const notFound = new NotFoundError('Your error message here.');
const badRequest = new BadRequestError('Your error message here.');
There is a proper 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)
npm install twilio-functions-utils
IMPORTANT TO USE CONVENTIONAL FUNCTIONS ➜
function yourFunctionName() {
// ...
}
With arrow functions it doesn't work as expected as 'this' cannot be injected correctly.
.
// File: assets/create.private.js
/**
* @param { object } event
* @this { {
* client: import('twilio').Twilio,
* env: {
* TWILIO_WORKFLOW_SID: string,
* DOMAIN_NAME: string
* } } }
* @returns { Promise<unknown> }
*/
exports.create = async function (event) {
// Here you can acess Twilio Client as client and Context as env (so you can get env vars).
const { client, env } = this
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 { object } event
* @this { {
* request: object,
* cookies: object,
* env: {
* TWILIO_WORKFLOW_SID: string,
* DOMAIN_NAME: string
* },
* providers: {
* 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, env } = this
// Then just call the providers you provided to handler by using useInjection.
const providerResult = await this.providers.create(event)
// Just put it on a Response object and you are good to go!
return new Response(providerResult, 201);
}
exports.handler = useInjection(createAction, {
providers: {
create,
},
validateToken: true, // When using Token Validator, the Request body must contain a valid Token from Twilio.
});
useMock
The Twilio Serverless structure make it hard for testing sometimes. So this provides a method that works perfectly with useInjection ready functions. The useMock
act like useInjection but mocking some required fragments as getAssets
and getFunctions
.
Exports your function:
async function functionToBeTested(event) {
const something = await this.providers.myCustomProvider(event)
return Response(something)
}
exports.handler = useInjection(functionToBeTested, {
providers: {
myCustomProvider,
},
});
module.exports = { functionToBeTested }; // <--
You always need to import the twilio.mock for Response Twilio Global object on your testing files begining. (Required)
require('twilio-functions-utils/lib/twilio.mock');
Use Twilio Functions Utils useMock
to do the hard job and just write your tests with the generated function.
/* global describe, it, expect */
require('twilio-functions-utils/lib/twilio.mock');
const { useMock, Response } = require('twilio-functions-utils');
const { functionToBeTested } = require('../../functions/functionToBeTested'); // <-- Import here!
// Create the test function from the function to be tested
const fn = useMock(functionToBeTested, {
providers: {
myCustomProvider: async (sid) => ({ sid }), // Mock the providers implementation.
},
});
describe('Function functionToBeTested', () => {
it('if {"someValue": true}', async () => {
const request = { TaskSid: '1234567', TaskAttributes: '{"someValue": true}' };
const res = await fn(request);
expect(res).toBeInstanceOf(Response);
expect(res.body).not.toEqual(request);
expect(res.body).toEqual({ sid: '1234567' });
});
});
FAQs
Twilio Functions utils library
The npm package twilio-functions-utils receives a total of 37 weekly downloads. As such, twilio-functions-utils popularity was classified as not popular.
We found that twilio-functions-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.