serverless-toolkit icon indicating copy to clipboard operation
serverless-toolkit copied to clipboard

Merge runtime-handler and serverless-runtime-types

Open dkundel opened this issue 4 years ago • 0 comments

Right now the two packages are fragmented but since serverless-runtime-types describes implementations that are dependent on which version of runtime-handler you are using we should probably merge the two.

The main problem is that @twilio/runtime-handler is not meant to be required and in fact throws an error when done in production.

TypeScript offers the ability to do type imports only like:

import type {ServerlessFunctionSignature} from '@twilio/runtime-handler`;

This would technically compile away when converted to JavaScript which would be fine but a customer might actually omit the type from the import statement breaking their code.

One option would be to create another submodule similar to @twilio/runtime-handler/dev and soon @twilio/runtime-handler/test we could have @twilio/runtime-hanlder/types that could be used. Similar to the current @twilio-labs/serverless-runtime-types this would not actually contain any code but instead would only be used for types and to define global types.

The usage for TS would be:

// Imports global types. Since it won't contain any logic this is actually fine.
import '@twilio/runtime-handler/types';

// Fetches specific types
import types {
  Context,
  ServerlessCallback,
  ServerlessEventObject,
  ServerlessFunctionSignature,
} from '@twilio/runtime-handler/types';

export const handler: ServerlessFunctionSignature = function(
  context: Context,
  event: ServerlessEventObject,
  callback: ServerlessCallback
) {
  const twiml = new Twilio.twiml.VoiceResponse();
  twiml.say('Hello World!');
  callback(null, twiml);
};

For JavaScript it would look the following:

/// <reference path="../../node_modules/@twilio/runtime-handler/types/index.d.ts"/>

/**
 * @param {import('@twilio/runtime-handler/types').Context} context
 * @param {import('@twilio/runtime-handler/types').ServerlessEventObject<{}, {}, { token: string }>} event
 * @param {import('@twilio/runtime-handler/types').ServerlessCallback} callback
 */
exports.handler = function (context, event, callback) {
  let twiml = new Twilio.twiml.MessagingResponse();
  console.log(event.cookies.token);
  twiml.message('Hello World');
  callback(null, twiml);
};

Related to #297

dkundel avatar Jul 02 '21 19:07 dkundel