I'm working from a CORS Node.js example found in Twilio's documentation, converting it TypeScirpt as a requirement for a project I'm working on:

exports.handler = (context, event, callback) => {
  // Access the NodeJS Helper Library by calling context.getTwilioClient()
  const client = context.getTwilioClient();

  // Create a custom Twilio Response
  const response = new Twilio.Response();
  // Set the CORS headers to allow Flex to make an error-free HTTP request
  // to this Function
  response.appendHeader('Access-Control-Allow-Origin', '*');
  response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
  response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');

  // Use the NodeJS Helper Library to make an API call and gather
  // statistics for the Flex Plugin.
  // Note that the workspace SID is passed from the event parameter
  // of the incoming request.
  client.taskrouter.v1
    .workspaces(event.WorkspaceSid)
    .workers()
    .cumulativeStatistics()
    .fetch()
    .then((data) => {
      response.appendHeader('Content-Type', 'application/json');
      response.setBody(data);
      // Return a success response using the callback function
      return callback(null, response);
    })
    .catch((err) => {
      response.appendHeader('Content-Type', 'plain/text');
      response.setBody(err.message);
      response.setStatusCode(500);
      // If there's an error, send an error response.
      // Keep using the response object for CORS purposes.
      return callback(null, response);
    });
};

Here's my conversion to TypeScript so far:

import { Twilio } from 'twilio';

import {
  Context,
  ServerlessCallback,
  ServerlessFunctionSignature,
  ServerlessEventObject
} from '@twilio-labs/serverless-runtime-types/types';
import { EnvVars } from '../envVars';

export interface EventRequest extends ServerlessEventObject {
  WorkspaceSid: string;
}

export const handler: ServerlessFunctionSignature<EnvVars, EventRequest> = async (
  context: Context<EnvVars>,
  event: EventRequest,
  callback: ServerlessCallback
) => {
  // Access the NodeJS Helper Library by calling context.getTwilioClient()
  const client = context.getTwilioClient();

  // Create a custom Twilio Response
  const response = new Twilio.Response(); //  Error: Property 'Response' does not exist on type 'typeof Twilio'.

  // Set the CORS headers to allow Flex to make an error-free HTTP request
  // to this Function
  response.appendHeader('Access-Control-Allow-Origin', '*');
  response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
  response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');

  // Use the NodeJS Helper Library to make an API call and gather
  // statistics for the Flex Plugin.
  // Note that the workspace SID is passed from the event parameter
  // of the incoming request.
  client.taskrouter.v1
    .workspaces(event.WorkspaceSid)
    .workers()
    .cumulativeStatistics()
    .fetch()
    .then((data: any) => {
      response.appendHeader('Content-Type', 'application/json');
      response.setBody(data);
      // Return a success response using the callback function
      return callback(null, response);
    })
    .catch((err: Error) => {
      response.appendHeader('Content-Type', 'plain/text');
      response.setBody(err.message);
      response.setStatusCode(500);
      // If there's an error, send an error response.
      // Keep using the response object for CORS purposes.
      return callback(null, response);
    });
};

Why am I getting this TS error for new Twilio.Response()?:

Property 'Response' does not exist on type 'typeof Twilio'.

Do I need to reference a different twilio module or submodule for this to build or is this deprecated with an improved response creation method?

1

There are 1 answers

1
IObert On BEST ANSWER

This code was written to run on Twilio Functions, which have different global variables. Hence, Twilio doesn't need to be imported in the snippet above. I'm afraid there is no way you can "translate" this code from "Functions JS" to "selfhosted TS" without doing some edits on your own.

But there are certainly ways to model a similar behavior. Instead of Twilio.response, you would need to create a response object of the server that you use. This could be either express, Nextjs or whatever you are using.