Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge runtime-handler and serverless-runtime-types #298

Open
dkundel opened this issue Jul 2, 2021 · 0 comments
Open

Merge runtime-handler and serverless-runtime-types #298

dkundel opened this issue Jul 2, 2021 · 0 comments

Comments

@dkundel
Copy link
Member

dkundel commented Jul 2, 2021

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

@makserik makserik added this to the Improvements/Nice to haves milestone Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants