From 8ef8a160c9a647434a7be4d22ea4946d5c8e52a6 Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 7 Nov 2016 08:34:54 -0500 Subject: [PATCH] Twilio tweaks (#12109) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Cleanup Imports These should be uniform, and should all use the require() syntax, as they’re referring to common js modules. * Enable client.calls.list() * Split up RestMethod signature By splitting up the union into an overload, we get much more useful autocomplete info. * Remove BaseRequestCallback BaseRequestCallback was only used in RestMethod, and it’s incorrect: the user’s callback is always called with 3 arguments. I think this was an attempt to say that the user’s callback doesn’t need to specify a third argument, but a two-argument callback will already conform to RequestCallback anyway. As the updated tests show, this change gives us better type inference too. * Use new Node HTTP Interface This MiddlewareFunction type isn’t assignable to an express middleware otherwise, as the latest express-static-core type declarations removes Http.ServerRequest from the type definition of an express RequestHandler --- twilio/index.d.ts | 18 +++++++++--------- twilio/twilio-tests.ts | 13 +++++++++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/twilio/index.d.ts b/twilio/index.d.ts index 022ac98440..b5b653f64a 100644 --- a/twilio/index.d.ts +++ b/twilio/index.d.ts @@ -6,11 +6,9 @@ /// /// /// - -import * as Express from 'express'; -import * as Http from 'http'; - -import Q = require('q'); +import Express = require("express"); +import Http = require("http"); +import Q = require("q"); declare interface twilio { (sid?: string, tkn?: string, options?: twilio.ClientOptions): twilio.RestClient; @@ -52,9 +50,11 @@ declare namespace twilio { } export interface RequestCallback { (err: any, data: any, response: Http.ClientResponse): void; } - export interface BaseRequestCallback { (err: any, data: any): void; } - export interface RestMethod { (args: any | BaseRequestCallback, callback?: RequestCallback): Q.Promise; } + export interface RestMethod { + (callback: RequestCallback): Q.Promise; + (args: any, callback?: RequestCallback): Q.Promise; + } /// Resource stock interfaces export interface BaseMappedResource { @@ -448,7 +448,7 @@ declare namespace twilio { } // For interop with node middleware chains - export interface MiddlewareFunction { (request: Http.ServerRequest, response: Http.ServerResponse, next: Express.NextFunction): void; } + export interface MiddlewareFunction { (request: Http.IncomingMessage, response: Http.ServerResponse, next: Express.NextFunction): void; } export function webhook(authToken: string, options?: WebhookOptions): MiddlewareFunction; export function webhook(options?: WebhookOptions): MiddlewareFunction; @@ -581,7 +581,7 @@ declare namespace twilio { post: RestMethod; create: RestMethod; } - export interface CallResource extends CreatableMappedResource { + export interface CallResource extends CreatableMappedResource, ListableResource { feedbackSummary: CallFeedbackSummaryResource; } diff --git a/twilio/twilio-tests.ts b/twilio/twilio-tests.ts index 54823816eb..44ade2e246 100644 --- a/twilio/twilio-tests.ts +++ b/twilio/twilio-tests.ts @@ -18,11 +18,16 @@ client.calls.get(function(err: any, response: any) { }); }); +// Using the alias described above +client.calls.list(function(err: any, response: any) { + response.calls.forEach(function(call: any) {}); +}); + //Get a list of calls made by this account, from this phone number // GET /2010-04-01/Accounts/ACCOUNT_SID/Calls?From=+16513334455 client.calls.get({ from:'+16513334455' -}, function(err: any, response: any) { +}, function(err, response) { response.calls.forEach(function(call: any) { console.log('Received call from: ' + call.from); console.log('This call\'s unique ID is: ' + call.sid); @@ -31,14 +36,14 @@ client.calls.get({ //Get data for a specific call // GET /2010-04-01/Accounts/ACCOUNT_SID/Calls/abc123... -client.calls('abc123...').get(function(err: any, call: any) { +client.calls('abc123...').get(function(err, call) { console.log('This call\'s unique ID is: ' + call.sid); console.log('This call was created at: ' + call.dateCreated); }); //Get data for a specific call, for a specific account // GET /2010-04-01/Accounts/AC.../Calls/abc123... -client.accounts('AC...').calls('abc123...').get(function(err: any, response: any) { +client.accounts('AC...').calls('abc123...').get(function(err, response) { response.calls.forEach(function(call: any) { console.log('Received call from: ' + call.from); console.log('This call\'s unique ID is: ' + call.sid); @@ -59,7 +64,7 @@ client.sms.messages.post({ // Delete a TwiML application // DELETE /2010-04-01/Accounts/ACCOUNT_SID/Applications/APP... -client.applications('APP...').delete(function(err: any, response: any, nodeResponse: any) { +client.applications('APP...').delete(function(err, response, nodeResponse) { //DELETE requests do not return data - if there was no error, it worked. err ? console.log('There was an error') : console.log('it worked!'); });