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

Issue with Typescript #374

Closed
naishe opened this issue Aug 19, 2018 · 9 comments
Closed

Issue with Typescript #374

naishe opened this issue Aug 19, 2018 · 9 comments

Comments

@naishe
Copy link

naishe commented Aug 19, 2018

I have been following this documentation(https://github.com/TwilioDevEd/api-snippets/blob/master/rest/access-tokens/video-example/video-example.3.x.js) to generate a auth token.

Using "twilio": "^3.19.1" with "@types/twilio": "0.0.9", seems like the VideoGrant is unavailable and the Typescript and AccessToken.toJwt() requires a String parameter which is not necessary.

I ended up doing the following, but it would be nice to fix these.

Version: ^3.19.1

Code Snippet

const VideoGrant = require('twilio').AccessToken.VideoGrant;
//...
res.json({ jwt: token.toJwt('HS256') });

Thanks

@nicobytes
Copy link

The same case with

import * as Twilio  from 'twilio';
const twiml = new Twilio.TwimlResponse();

error Twilio.TwimlResponse is not a constructor

@jspri
Copy link

jspri commented Aug 24, 2018

In the newest version of the library the types come bundled so you don't need @types/twilio. The types want you to do const client = twilio(accountSid, authToken); without the new.

This is a bit confusing because the docs want you to use the new. It seems to work either way.

@dkundel
Copy link
Member

dkundel commented Aug 29, 2018

Hey folks,

Yes we silently released TS bindings with the latest release and definitely require feedback with this :)

@Crazometer your comment is correct. I made that decision because of the way that the Twilio lib is written I had to decide between twilio being a function or a class. The recommended way to use it right now is (as per TypeScript documentation for libs written in such a way)

import twilio = require('twilio');
const client = twilio();

The first two examples are both from the old library. Since version 3.X TwiML is nested under twiml and specific to the type like VoiceResponse, MessagingResponse and FaxResponse:

import twilio = require('twilio');
const myTwiml = new twilio.twiml.VoiceResponse();
myTwiml.say({ voice: 'alice' }, 'Ahoy');

console.log(myTwiml.toString());

Note There is a known issue where right now you have to pass options to the respective TwiML methods. I hope it gets fixed soon. The code snippet above is also depending on a PR being merged that fixes a few of these issues.

For the AccessTokens, they are now under jwt nested since version 3.X and I open a PR to make the algorithm optional. Then this should work:

// create AccessToken
const accessToken = new twilio.jwt.AccessToken(
  process.env.TWILIO_ACCOUNT_SID!,
  process.env.TWILIO_API_KEY!,
  process.env.TWILIO_API_SECRET!,
  {
    ttl: 360,
  }
);

// create Grant
const grant = new twilio.jwt.AccessToken.VideoGrant();
grant.toPayload();

const AccessToken = twilio.jwt.AccessToken;
const voiceGrant = new AccessToken.VoiceGrant({
  incomingAllow: true,
  outgoingApplicationSid: 'someSid',
});
voiceGrant.toPayload();

accessToken.addGrant(voiceGrant);
accessToken.addGrant(grant);

const token = accessToken.toJwt();
console.log(token);

I hope this helps and your feedback is definitely wanted and helpful :)

@naishe
Copy link
Author

naishe commented Aug 29, 2018

Oh, that's pretty cool thanks. I will remove @types and check it again. Thanks for quick response. Closing it.

@et
Copy link

et commented Sep 5, 2018

@dkundel - is there a way to define the type of a response object without instantiating it?

e.g.

let myTwiml: twilio.twiml.VoiceResponse;
myTwiml = new twilio.twiml.VoiceResponse();

The error message on the let line is: Namespace 'twilio' has no exported member 'twiml'.

@dkundel
Copy link
Member

dkundel commented Sep 10, 2018

If you are using VS Code you should be able to use the AutoFix option to import the actual VoiceResponse type. If you don't you should be able to do this:

import * as VoiceResponse from './lib/twiml/VoiceResponse';

Your code doesn't work because of some odd internals that I had to oddly fix with TypeScript.

Cheers,
Dominik

@et
Copy link

et commented Sep 13, 2018

@dkundel - Thank you for your response. Maybe I'm confused but why would there be a lib prefix if I'm importing this library into my project?

@dkundel
Copy link
Member

dkundel commented Sep 17, 2018

Sorry my bad 😄 I was in that project. It would be 'twilio/lib/twiml/VoiceResponse' so the lib part is correct but the . should be twilio.

Cheers,
Dominik

@et
Copy link

et commented Sep 21, 2018

Awesome.

For those of you who want to define the type for the client too, this is what I did:

import * as twilio from 'twilio';
import * as TwilioClient from 'twilio/lib/rest/Twilio';
const client: TwilioClient = twilio(accountSid, authToken);

and now you'll have nice autocompletion:

screen shot 2018-09-20 at 10 43 17 pm

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

5 participants