question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with Typescript

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
etcommented, 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
2reactions
dkundelcommented, 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 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · microsoft/TypeScript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - Issues · microsoft/TypeScript.
Read more >
7 really good reasons not to use TypeScript - everyday.codes
Everyone loves TypeScript. It “solves” many problems JS has, it is a “superset” of JS, it will make your code error-prone and pleasant...
Read more >
The Trouble with TypeScript
TypeScript is Noisy. As in it has a higher noise to signal ratio. Less of the code you are looking at is functional....
Read more >
Common TypeScript module problems and how to solve ...
Without configuring the TypeScript compiler as discussed earlier, TypeScript will adopt the Node.js run-time resolution strategy by default in ...
Read more >
Problems with TypeScript
TypeScript's major weakness is that it doesn't want to break away from Javascript. That strict dedication to being a superset means the type ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found