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

Tests for typescript #372

Merged
merged 42 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d5a4534
Add base types
dkundel Mar 8, 2018
39968c0
Add basic interfaces and types
dkundel Mar 8, 2018
aaac0b5
Remove types.d.ts. Not needed
dkundel Mar 8, 2018
69cce5b
Remove unnecessary testing code
dkundel Mar 8, 2018
e257ec7
Add HTTP request/response types
dkundel Mar 8, 2018
34c619d
Add webhook typings
dkundel Mar 8, 2018
f824338
Add JWT generation typings
dkundel Mar 8, 2018
514421a
Add typings entry file
dkundel Mar 8, 2018
4e7fda8
Add example file and test command. Not in use yet
dkundel Mar 8, 2018
2b59af7
Remove typings from JS style checking
dkundel Mar 8, 2018
23344ac
Add generic base class for toJSON. Requires TS 2.8
dkundel May 14, 2018
b620792
Merge branch 'master' into DX-383_tests
twilio-ci Aug 1, 2018
b464bb6
regenerate javascript files; add generated typescript declarations
twilio-ci Aug 1, 2018
c48b2ee
actually add typescript declarations
twilio-ci Aug 1, 2018
79bf14e
update package-lock; include typescript tests in test target
twilio-ci Aug 1, 2018
aaefe0f
remove obsolete generated files
twilio-ci Aug 1, 2018
7b13bda
nested options params
twilio-ci Aug 2, 2018
022bba5
sort property docstrings for options interfaces
twilio-ci Aug 2, 2018
c977a6a
Merge branch 'master' into DX-383_tests
twilio-ci Aug 2, 2018
9ca9133
add test for namespaced attribute
twilio-ci Aug 2, 2018
82bdc7c
fix declarations for namespaced attributes
twilio-ci Aug 2, 2018
d1c49d3
no camel casing for namespaced attributes
twilio-ci Aug 2, 2018
bb0ad79
do not camel case attributes with dashes
twilio-ci Aug 2, 2018
fd438b6
enums in resources
twilio-ci Aug 2, 2018
036ac23
fix enum types in resource instance properties
twilio-ci Aug 2, 2018
4f64902
fix context dependent types
twilio-ci Aug 2, 2018
c5db55b
update MessageListInstanceCreateOptions type in example.ts
twilio-ci Aug 2, 2018
e1cf37e
add some missing types to example script
twilio-ci Aug 2, 2018
7b58ab6
fix callback type for resource create methods
twilio-ci Aug 2, 2018
094b01a
fix compile errors in example.ts
twilio-ci Aug 2, 2018
a183513
version dependents are list instances, not lists
twilio-ci Aug 3, 2018
9df3e1a
Merge branch 'master' into DX-383_tests
twilio-ci Aug 3, 2018
76fd7ff
regenerate with new api-defs
twilio-ci Aug 3, 2018
a2c64ae
import list instance *and* list class into version declaration
twilio-ci Aug 3, 2018
05a3af0
fix missing imports for v2010
twilio-ci Aug 3, 2018
06773b1
remove example with callback as first param
twilio-ci Aug 3, 2018
0b01729
fix types of context properties for resources
twilio-ci Aug 3, 2018
d225df9
fix some compile errors in example script; regenerate
twilio-ci Aug 3, 2018
944a04b
use 'object' for empty attributes params
twilio-ci Aug 3, 2018
0788c79
alphabetize / dedupe TS classes
twilio-ci Aug 3, 2018
a904d12
clean up unneeded imports and type ascriptions
twilio-ci Aug 6, 2018
a994b51
Merge branch 'master' into DX-383_tests
twilio-ci Aug 23, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"excludeFiles": [".git", "node_modules", "lib/**/*.d.ts", "index.d.ts"],
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true
},
Expand Down
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
spec/integration/**
lib/**/*.d.ts
index.d.ts
132 changes: 132 additions & 0 deletions examples/typescript/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import * as _ from 'lodash';
import twilio = require('../../');
import {MessageListInstanceCreateOptions} from '../../lib/rest/api/v2010/account/message';

const accountSid: string = process.env.TWILIO_ACCOUNT_SID || '';
const token: string = process.env.TWILIO_AUTH_TOKEN || '';

const client = twilio(accountSid, token);

let i: number = 0;
client.calls.each({
pageSize: 7,
callback: (call, done) => {
console.log(call.sid);
i++;
if (i === 10) {
done();
}
},
done: (err: Error) => {
console.log('je suis fini');
console.log(err);
}
});

client.calls.each({}, call => {
console.log(call.sid);
});

const from = process.env.FROM_NUMBER || '';
const to = process.env.TO_NUMBER || '';

const msgData: MessageListInstanceCreateOptions = {
from,
to,
body: 'create using callback'
};

// Send message using callback
client.messages.create(msgData, (err, result) => {
console.log('Created message using callback');
console.log(result.sid);
});

// Send message using promise
const promise = client.messages.create({
from: from,
to: to,
body: 'create using promises'
});
promise.then(message => {
console.log('Created message using promises');
console.log(message.sid);
});

// Create sip trunk
client.trunking.v1.trunks.create(
{
friendlyName: 'sip trunking'
},
(err, result) => {
console.log('Created trunk with friendly name');
console.log(result.sid);
console.log(result.friendlyName);
}
);

const promiseTrunk = client.trunking.v1.trunks.create({
friendlyName: 'promise trunking'
});
promiseTrunk.then(trunk => {
console.log('Created trunk with friendly name and promises');
console.log(trunk.sid);
console.log(trunk.friendlyName);
});

const trunkSid = 'TK7e37e59861c14bb80dde245cfaad5522';

// Fetch trunk sid using callback
client.trunking.v1.trunks(trunkSid).fetch((err, result) => {
console.log('Fetch trunk using callback');
console.log(result.sid);
});

// Fetch trunk using promise
const promiseTrunk2 = client.trunking.v1.trunks(trunkSid).fetch();
promiseTrunk2.then(trunk => {
console.log('Fetch trunk using promise');
console.log(trunk.sid);
});

// Update trunk using callback
client.trunking.v1.trunks(trunkSid).update(
{
friendlyName: 'callback trunk'
},
(err, result) => {
console.log('Updated using callbacks');
console.log(result.sid);
console.log(result.friendlyName);
}
);

// Update trunk using promise
const promiseTrunk3 = client.trunking.v1.trunks(trunkSid).update({
friendlyName: 'promise trunk'
});
promiseTrunk3.then(trunk => {
console.log('Updated trunk with friendly name and promises');
console.log(trunk.sid);
console.log(trunk.friendlyName);
});

// List messages using callbacks
client.messages.list({}, (err, messages) => {
console.log('Listing messages using callbacks');
_.each(messages, message => {
console.log(message.sid);
});
});

// List messages using promises
const promiseMessage = client.messages.list();
promiseMessage.then(messages => {
console.log('Listing messages using promises');
_.each(messages, function(message) {
console.log(message.sid);
});
});

const twiml = new twilio.twiml.VoiceResponse();
twiml.dial({}, '+12345678901');
43 changes: 43 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as VoiceResponse from './lib/twiml/VoiceResponse';
import * as MessagingResponse from './lib/twiml/MessagingResponse';
import * as FaxResponse from './lib/twiml/FaxResponse';
import * as webhookTools from './lib/webhooks/webhooks';
import * as util from './lib/jwt/taskrouter/util';

import TwilioClient = require('./lib/rest/Twilio');
import AccessToken = require('./lib/jwt/AccessToken');
import ClientCapability = require('./lib/jwt/ClientCapability');
import TaskRouterCapability = require('./lib/jwt/taskrouter/TaskRouterCapability');

interface TwimlConstructor<T> {
new (): T;
}

declare function twilio(
accountSid?: string,
authToken?: string,
opts?: TwilioClient.TwilioClientOptions
): TwilioClient;

declare namespace twilio {
export interface TwimlInterface {
VoiceResponse: typeof VoiceResponse;
FaxResponse: typeof FaxResponse;
MessagingResponse: typeof MessagingResponse;
}
export interface JwtInterface {
AccessToken: typeof AccessToken;
ClientCapability: typeof ClientCapability;
taskrouter: {
TaskRouterCapability: typeof TaskRouterCapability;
util: typeof util;
};
}
export const jwt: JwtInterface;
export const twiml: TwimlInterface;
export const validateRequest: typeof webhookTools.validateRequest;
export const validateExpressRequest: typeof webhookTools.validateExpressRequest;
export const webhook: typeof webhookTools.webhook;
}

export = twilio;
23 changes: 23 additions & 0 deletions lib/base/Domain.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import TwilioClient = require('../rest/Twilio');

declare class Domain {
constructor(twilio: TwilioClient, baseUrl: string);

/**
* Turn a uri into an absolute url
*
* @param uri uri to transform
* @return absolute url
*/
absoluteUrl(uri: string): string;

/**
* Make request to this domain
*
* @param opts request options
* @return request promise
*/
request(opts?: TwilioClient.RequestOptions): Promise<any>;
}

export = Domain;
107 changes: 107 additions & 0 deletions lib/base/Page.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import Version = require('./Version');
import Response = require('../http/response');

interface Solution {

}

/**
* Base page object to maintain request state.
*/
declare class Page<TVersion extends Version, TPayload extends Page.TwilioResponsePayload, TResource, TInstance> {
/**
* Base page object to maintain request state.
*
* @param version - A twilio version instance
* @param response - The http response
* @param solution - path solution
*/
constructor(version: TVersion, response: Response<string>, solution: Solution);

/**
* Get the url of the previous page of records
*
* @return url of the previous page
*/
getPreviousPageUrl(): string;
/**w
* Get the url of the next page of records
*
* @return url of the next page
*/
getNextPageUrl(): string;
/**
* Load a list of records
*
* @param resources json payload of records
* @return list of resources
*/
loadInstance(resources: TResource[]): TInstance[];
/**
* Fetch the next page of records
*
* @return promise that resolves to next page of results
*/
nextPage(): Promise<Page<TVersion, TPayload, TResource, TInstance>>;
/**
* Fetch the previous page of records
*
* @return promise that resolves to previous page of results
*/
previousPage(): Promise<Page<TVersion, TPayload, TResource, TInstance>>;
/**
* Parse json response from API
* @throws If non 200 status code is returned
*
* @param response API response
* @return json parsed response
*/
processResponse(response: Response<string>): TPayload;
/**
* Load a page of records
* @throws {Error} If records cannot be deserialized
*
* @param payload json payload
* @return the page of records
*/
loadPage(payload: TPayload): TResource[];

/**
* @constant META_KEYS
* @description meta keys returned in a list request
*/
static META_KEYS: [
'end',
'first_page_uri',
'last_page_uri',
'next_page_uri',
'num_pages',
'page',
'page_size',
'previous_page_uri',
'start',
'total',
'uri'
];
}

declare namespace Page {
export interface TwilioResponsePayload {
// DEPRECTATED: end: any;
first_page_uri: string;
// DEPRECTATED: last_page_uri: string;
next_page_uri: string;
// DEPRECTATED: num_pages: number;
page: number;
page_size: number;
previous_page_uri: string;
// DEPRECTATED: start: number;
// DEPRECTATED: total: number;
uri: string;
meta?: {
key?: string;
}
}
}

export = Page;
62 changes: 62 additions & 0 deletions lib/base/RequestClient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { HttpMethod } from '../interfaces';
import Response = require('../http/response');

declare class RequestClient {
constructor();
/**
* Make an HTTP request
* @param opts The request options
*/
request<TData>(opts: RequestClient.RequestOptions<TData>): Promise<Response<TData>>;
}

declare namespace RequestClient {
export interface RequestOptions<TData = any, TParams = object> {
/**
* The HTTP method
*/
method: HttpMethod;
/**
* The request URI
*/
uri: string;
/**
* The username used for auth
*/
username?: string;
/**
* The password used for auth
*/
password?: string;
/**
* The request headers
*/
headers?: Headers;
/**
* The object of params added as query string to the request
*/
params?: TParams;
/**
* The form data that should be submitted
*/
data?: TData;
/**
* The request timeout in milliseconds
*/
timeout?: number;
/**
* Should the client follow redirects
*/
allowRedirects?: boolean;
/**
* Set to true to use the forever-agent
*/
forever?: boolean;
}

export interface Headers {
[header: string]: string;
}
}

export = RequestClient;
9 changes: 9 additions & 0 deletions lib/base/RestException.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare class RestException extends Error {
status: number;
message: string;
code: number;
moreInfo: string;
detail: string;
}

export = RestException;
Loading