Various changes to get DefinitelyTyped passing

This commit is contained in:
Matthew O'Riordan
2017-02-09 03:42:59 +00:00
parent d5d9afb13d
commit 2f30af5cff
3 changed files with 105 additions and 95 deletions

View File

@@ -1,8 +1,8 @@
import * as Ably from 'ably';
/// <reference path="index.d.ts"/>
const ApiKey = 'appId.keyId:secret';
const client = Ably.Realtime;
const restClient = Ably.Rest;
const client = new Realtime(ApiKey);
const restClient = new Rest(ApiKey);
// Connection
// Successful connection:
@@ -70,8 +70,8 @@ channel.history({ start: Date.now()-10000, end: Date.now(), limit: 100, directio
// Presence on a channel
// Getting presence:
channel.presence.get(function(err, presenceSet) {
presenceSet; // array of PresenceMessages
channel.presence.get(function(presenceSet) {
presenceSet; // array of PresenceMessages
});
// Note that presence#get on a realtime channel does not return a PaginatedResult, as the library maintains a local copy of the presence set.
@@ -86,7 +86,7 @@ channel.presence.update('new status', function(err) {
// my presence data is updated
});
channel.presence.leave(function(err) {
channel.presence.leave(null, function(err) {
// I've left the presence set
});
@@ -114,7 +114,7 @@ channel.history({ start: Date.now()-10000, end: Date.now(), limit: 100, directio
// Generate a random 256-bit key for demonstration purposes (in
// practice you need to create one and distribute it to clients yourselves)
Ably.Realtime.Crypto.generateRandomKey(function(err, key) {
Realtime.Crypto.generateRandomKey(function(err, key) {
var channel = client.channels.get('channelName', { cipher: { key: key } });
channel.subscribe(function(message) {
@@ -123,7 +123,7 @@ Ably.Realtime.Crypto.generateRandomKey(function(err, key) {
});
channel.publish('name is not encrypted', 'sensitive data is encrypted');
})
});
// You can also change the key on an existing channel using setOptions (which takes a callback which is called after the new encryption settings have taken effect):
@@ -133,15 +133,15 @@ channel.setOptions({cipher: {key: '<KEY>'}}, function() {
// Using the REST API
var channel = restClient.channels.get('test');
var restChannel = restClient.channels.get('test');
// Publishing to a channel
// Publish a single message with name and data
channel.publish('greeting', 'Hello World!');
restChannel.publish('greeting', 'Hello World!');
// Optionally, you can use a callback to be notified of success or failure
channel.publish('greeting', 'Hello World!', function(err) {
restChannel.publish('greeting', 'Hello World!', function(err) {
if(err) {
console.log('publish failed with error ' + err);
} else {
@@ -150,11 +150,11 @@ channel.publish('greeting', 'Hello World!', function(err) {
})
// Publish several messages at once
channel.publish([{name: 'greeting', data: 'Hello World!'}], function() {});
restChannel.publish([{name: 'greeting', data: 'Hello World!'}], function() {});
// Querying the History
channel.history(function(err, messagesPage) {
restChannel.history(function(err, messagesPage) {
messagesPage; // PaginatedResult
messagesPage.items; // array of Message
messagesPage.items[0].data; // payload for first message
@@ -165,11 +165,11 @@ channel.history(function(err, messagesPage) {
});
// Can optionally take an options param, see https://www.ably.io/documentation/rest-api/#message-history
channel.history({ start: Date.now()-10000, end: Date.now(), limit: 100, direction: 'forwards' }, function(err, messagesPage) {});
restChannel.history({ start: Date.now()-10000, end: Date.now(), limit: 100, direction: 'forwards' }, function(err, messagesPage) {});
// Presence on a channel
channel.presence.get(function(err, presencePage) { // PaginatedResult
restChannel.presence.get(function(err, presencePage) { // PaginatedResult
presencePage.items; // array of PresenceMessage
presencePage.items[0].data; // payload for first message
presencePage.items.length; // number of messages in the current page of members
@@ -180,7 +180,7 @@ channel.presence.get(function(err, presencePage) { // PaginatedResult
// Querying the Presence History
channel.presence.history(function(err, messagesPage) { // PaginatedResult
restChannel.presence.history(function(err, messagesPage) { // PaginatedResult
messagesPage.items; // array of PresenceMessage
messagesPage.items[0].data; // payload for first message
messagesPage.items.length; // number of messages in the current page of history
@@ -190,7 +190,7 @@ channel.presence.history(function(err, messagesPage) { // PaginatedResult
});
// Can optionally take an options param, see https://www.ably.io/documentation/rest-api/#message-history
channel.history({ start: Date.now()-10000, end: Date.now(), limit: 100, direction: 'forwards' }, function(err, messagesPage) {});
restChannel.history({ start: Date.now()-10000, end: Date.now(), limit: 100, direction: 'forwards' }, function(err, messagesPage) {});
// Generate Token and Token Request
@@ -203,7 +203,7 @@ client.auth.requestToken(function(err, tokenDetails) {
// see https://www.ably.io/documentation/rest/authentication/#token-details for its properties
// Now we have the token, we can send it to someone who can instantiate a client with it:
var clientUsingToken = new Ably.Realtime(tokenDetails.token);
var clientUsingToken = new Realtime(tokenDetails.token);
});
// requestToken can take two optional params
@@ -225,10 +225,10 @@ client.auth.createTokenRequest({}, {}, function(err, tokenRequest) { });
// Fetching your application's stats
client.stats(function(err, statsPage) { // statsPage as PaginatedResult
client.stats({ limit: 50 }, function(err, statsPage) { // statsPage as PaginatedResult
statsPage.items; // array of Stats
statsPage.items[0].data; // payload for first message
statsPage.items.length; // number of messages in the current page of history
statsPage.items[0].inbound.rest.messages.count; // total messages published over REST
statsPage.items.length; // number of stats in the current page of history
statsPage.hasNext(); // true if there are further pages
statsPage.isLast(); // true if this page is the last page
statsPage.next(function(nextPage) {}); // retrieves the next page as PaginatedResult
@@ -236,4 +236,4 @@ client.stats(function(err, statsPage) { // statsPage as PaginatedResult
// Fetching the Ably service time
client.time(function(err, time) {}); // time is in ms since epoch
client.time({}, function(err, time) {}); // time is in ms since epoch

155
ably/index.d.ts vendored
View File

@@ -4,61 +4,61 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace ChannelState {
export type INITIALIZED = 'initialized';
export type ATTACHING = 'attaching';
export type ATTACHED = "attached";
export type DETACHING = "detaching";
export type DETACHED = "detached";
export type SUSPENDED = "suspended";
export type FAILED = "failed";
type INITIALIZED = 'initialized';
type ATTACHING = 'attaching';
type ATTACHED = "attached";
type DETACHING = "detaching";
type DETACHED = "detached";
type SUSPENDED = "suspended";
type FAILED = "failed";
}
type ChannelState = ChannelState.FAILED | ChannelState.INITIALIZED | ChannelState.SUSPENDED | ChannelState.ATTACHED | ChannelState.ATTACHING | ChannelState.DETACHED | ChannelState.DETACHING;
declare namespace ConnectionState {
export type INITIALIZED = "initialized";
export type CONNECTING = "connecting";
export type CONNECTED = "connected";
export type DISCONNECTED = "disconnected";
export type SUSPENDED = "suspended";
export type CLOSING = "closing";
export type CLOSED = "closed";
export type FAILED = "failed";
type INITIALIZED = "initialized";
type CONNECTING = "connecting";
type CONNECTED = "connected";
type DISCONNECTED = "disconnected";
type SUSPENDED = "suspended";
type CLOSING = "closing";
type CLOSED = "closed";
type FAILED = "failed";
}
type ConnectionState = ConnectionState.INITIALIZED | ConnectionState.CONNECTED | ConnectionState.CONNECTING | ConnectionState.DISCONNECTED | ConnectionState.SUSPENDED | ConnectionState.CLOSED | ConnectionState.CLOSING | ConnectionState.FAILED;
declare namespace ConnectionEvent {
export type INITIALIZED = "initialized";
export type CONNECTING = "connecting";
export type CONNECTED = "connected";
export type DISCONNECTED = "disconnected";
export type SUSPENDED = "suspended";
export type CLOSING = "closing";
export type CLOSED = "closed";
export type FAILED = "failed";
export type UPDATE = "update";
type INITIALIZED = "initialized";
type CONNECTING = "connecting";
type CONNECTED = "connected";
type DISCONNECTED = "disconnected";
type SUSPENDED = "suspended";
type CLOSING = "closing";
type CLOSED = "closed";
type FAILED = "failed";
type UPDATE = "update";
}
type ConnectionEvent = ConnectionEvent.INITIALIZED | ConnectionEvent.CONNECTED | ConnectionEvent.CONNECTING | ConnectionEvent.DISCONNECTED | ConnectionEvent.SUSPENDED | ConnectionEvent.CLOSED | ConnectionEvent.CLOSING | ConnectionEvent.FAILED | ConnectionEvent.UPDATE;
declare namespace PresenceAction {
export type ABSENT = "absent";
export type PRESENT = "present";
export type ENTER = "enter";
export type LEAVE = "leave";
export type UPDATE = "update";
type ABSENT = "absent";
type PRESENT = "present";
type ENTER = "enter";
type LEAVE = "leave";
type UPDATE = "update";
}
type PresenceAction = PresenceAction.ABSENT | PresenceAction.PRESENT | PresenceAction.ENTER | PresenceAction.LEAVE | PresenceAction.UPDATE;
declare namespace StatsIntervalGranularity {
export type MINUTE = "minute";
export type HOUR = "hour";
export type DAY = "day";
export type MONTH = "month";
type MINUTE = "minute";
type HOUR = "hour";
type DAY = "day";
type MONTH = "month";
}
type StatsIntervalGranularity = StatsIntervalGranularity.MINUTE | StatsIntervalGranularity.HOUR | StatsIntervalGranularity.DAY | StatsIntervalGranularity.MONTH;
declare namespace HTTPMethods {
export type POST = "POST";
export type GET = "GET";
type POST = "POST";
type GET = "GET";
}
type HTTPMethods = HTTPMethods.GET | HTTPMethods.POST;
@@ -262,7 +262,7 @@ declare interface LogInfo {
/**
* A function to handle each line of log output. If handler is not specified, console.log is used.
**/
handler?: (...args) => void;
handler?: (...args: Array<string>) => void;
}
declare interface ChannelEvent {
@@ -284,14 +284,16 @@ declare interface ConnectionStateChange {
}
// Common Listeners
type PaginatedResultCallback<T> = (error: ErrorInfo, results: PaginatedResult<T> ) => void;
type paginatedResultCallback<T> = (error: ErrorInfo, results: PaginatedResult<T> ) => void;
type standardCallback = (error: ErrorInfo, results: any) => void;
type messageCallback<T> = (message: T) => void;
type errorCallback = (error: ErrorInfo) => void;
type channelEventCallback = (channelEvent: ChannelEvent, changeStateChange: ChannelStateChange) => void;
type connectionEventCallback = (connectionEvent: ConnectionEvent, connectionStateChange: ConnectionStateChange) => void;
type timeCallback = (error: ErrorInfo, time: number) => void;
type realtimePresenceGetCallback = (error: ErrorInfo, messages: Array<PresenceMessage>) => void;
type tokenDetailsCallback = (error: ErrorInfo, Results: TokenDetails) => void
type tokenRequestCallback = (error: ErrorInfo, Results: TokenRequest) => void
// Internal Classes
declare class EventEmitter<T> {
@@ -301,58 +303,59 @@ declare class EventEmitter<T> {
}
// Classes
export declare class Auth {
declare class Auth {
clientId: string;
authorize: (tokenParams?: TokenParams, authOptions?: AuthOptions, callback?: (error: ErrorInfo, Results: TokenDetails) => void) => void;
createTokenRequest: (tokenParams?: TokenParams, authOptions?: AuthOptions, callback?: (error: ErrorInfo, Results: TokenRequest) => void) => void;
requestToken: (TokenParams?: TokenParams, authOptions?: AuthOptions, callback?: (error: ErrorInfo, Results: TokenDetails) => void) => void;
authorize: (tokenParams?: TokenParams | tokenDetailsCallback, authOptions?: AuthOptions | tokenDetailsCallback, callback?: tokenDetailsCallback) => void;
createTokenRequest: (tokenParams?: TokenParams | tokenRequestCallback, authOptions?: AuthOptions | tokenRequestCallback, callback?: tokenRequestCallback) => void;
requestToken: (TokenParams?: TokenParams | tokenDetailsCallback, authOptions?: AuthOptions | tokenDetailsCallback, callback?: tokenDetailsCallback) => void;
}
export declare class Presence {
get: (params: RestPresenceParams, callback: PaginatedResultCallback<PresenceMessage>) => void;
history: (params: RestPresenceHistoryParams, callback: PaginatedResultCallback<PresenceMessage>) => void;
declare class Presence {
get: (params: RestPresenceParams | paginatedResultCallback<PresenceMessage>, callback?: paginatedResultCallback<PresenceMessage>) => void;
history: (params: RestPresenceHistoryParams | paginatedResultCallback<PresenceMessage>, callback?: paginatedResultCallback<PresenceMessage>) => void;
}
export declare class RealtimePresence {
declare class RealtimePresence {
syncComplete: () => boolean;
get: (Params: RealtimePresenceParams, callback?: (error: ErrorInfo, messages: Array<PresenceMessage>) => void) => void;
history: (ParamsOrCallback: RealtimePresenceHistoryParams | PaginatedResultCallback<PresenceMessage>, callback?: PaginatedResultCallback<PresenceMessage>) => void;
get: (Params: realtimePresenceGetCallback | RealtimePresenceParams, callback?: realtimePresenceGetCallback) => void;
history: (ParamsOrCallback: RealtimePresenceHistoryParams | paginatedResultCallback<PresenceMessage>, callback?: paginatedResultCallback<PresenceMessage>) => void;
subscribe: (presenceOrCallback: PresenceAction | messageCallback<PresenceMessage>, listener?: messageCallback<PresenceMessage>) => void;
unsubscribe: (presence?: PresenceAction, listener?: messageCallback<PresenceMessage>) => void;
enter: (data: any, callback?: errorCallback) => void;
update: (data: any, callback?: errorCallback) => void;
leave: (data: any, callback?: errorCallback) => void;
enterClient: (clientId: string, data: any, callback?: errorCallback) => void;
updateClient: (clientId: string, data: any, callback?: errorCallback) => void;
leaveClient: (clientId: string, data: any, callback?: errorCallback) => void;
enter: (data?: errorCallback | any, callback?: errorCallback) => void;
update: (data?: errorCallback | any, callback?: errorCallback) => void;
leave: (data?: errorCallback | any, callback?: errorCallback) => void;
enterClient: (clientId: string, data?: errorCallback | any, callback?: errorCallback) => void;
updateClient: (clientId: string, data?: errorCallback | any, callback?: errorCallback) => void;
leaveClient: (clientId: string, data?: errorCallback | any, callback?: errorCallback) => void;
}
export declare class Channel {
declare class Channel {
name: string;
presence: Presence;
history: (paramsOrCallback?: RestPresenceHistoryParams | PaginatedResultCallback<Message>, callback?: PaginatedResultCallback<Message>) => void;
history: (paramsOrCallback?: RestPresenceHistoryParams | paginatedResultCallback<Message>, callback?: paginatedResultCallback<Message>) => void;
publish: (messagesOrName: any, messagedataOrCallback?: errorCallback | any, callback?: errorCallback) => void;
}
export declare class RealtimeChannel extends EventEmitter<channelEventCallback> {
declare class RealtimeChannel extends EventEmitter<channelEventCallback> {
name: string;
errorReason: ErrorInfo;
state: ChannelState;
presence: RealtimePresence;
attach: (callback?: standardCallback) => void;
detach:(callback?: standardCallback) => void;
history: (paramsOrCallback?: RealtimePresenceHistoryParams | PaginatedResultCallback<Message>, callback?: PaginatedResultCallback<Message>) => void;
history: (paramsOrCallback?: RealtimePresenceHistoryParams | paginatedResultCallback<Message>, callback?: paginatedResultCallback<Message>) => void;
subscribe: (eventOrCallback: messageCallback<Message> | string, listener?: messageCallback<Message>) => void;
unsubscribe: (eventOrCallback?: messageCallback<Message> | string, listener?: messageCallback<Message>) => void;
publish: (messagesOrName: any, messageDataOrCallback?: errorCallback | any, callback?: errorCallback) => void;
setOptions: (options: Object, callback?: errorCallback) => void;
}
export declare class Channels<T> {
declare class Channels<T> {
get: (name: string, channelOptions?: ChannelOptions) => T;
release: (name: string) => void;
}
export declare class Message {
declare class Message {
constructor();
fromEncoded: (JsonObject: string, channelOptions: ChannelOptions) => Message;
fromEncodedArray: (JsonArray: string, channelOptions: ChannelOptions) => Array<Message>;
@@ -366,7 +369,7 @@ export declare class Message {
timestamp: number;
}
export declare class PresenceMessage {
declare class PresenceMessage {
fromEncoded: (JsonObject: any, channelOptions?: ChannelOptions) => PresenceMessage;
fromEncodedArray: (JsonArray: Array<any>, channelOptions?: ChannelOptions) => Array<PresenceMessage>;
action: PresenceAction;
@@ -378,29 +381,35 @@ export declare class PresenceMessage {
timestamp: number;
}
export declare class Rest {
declare interface Crypto {
generateRandomKey: (callback: (error: ErrorInfo, key: string) => void) => void;
}
declare class Rest {
constructor(options: ClientOptions | string);
static Crypto: Crypto;
auth: Auth;
channels: Channels<Channel>;
request: (method: string, path: string, params?: any, body?: Array<any> | any, headers?: any, callback?: (error: ErrorInfo, response: HttpPaginatedResponse) => void) => void;
stats: (paramsOrCallback?: PaginatedResultCallback<Stats> | any, callback?: PaginatedResultCallback<Stats>) => void;
time: (paramsOrCallback?: timeCallback | any, callback?: timeCallback) => void;
stats: (paramsOrCallback?: paginatedResultCallback<Stats> | any, callback?: paginatedResultCallback<Stats>) => void;
time: (paramsOrCallback?: timeCallback | Object, callback?: timeCallback) => void;
}
export declare class Realtime {
declare class Realtime {
constructor(options: ClientOptions | string);
static Crypto: Crypto;
auth: Auth;
channels: Channels<RealtimeChannel>;
clientId: string;
connection: Connection;
request: (method: string, path: string, params?: any, body?: Array<any> | any, headers?: any, callback?: (error: ErrorInfo, response: HttpPaginatedResponse) => void) => void;
stats: (paramsOrCallback?: PaginatedResultCallback<Stats> | any, callback?: PaginatedResultCallback<Stats>) => void;
stats: (paramsOrCallback?: paginatedResultCallback<Stats> | any, callback?: paginatedResultCallback<Stats>) => void;
close: () => void;
connect: () => void;
time: (paramsOrCallback?: timeCallback | any, callback?: timeCallback) => void;
}
export declare class Connection extends EventEmitter<connectionEventCallback> {
declare class Connection extends EventEmitter<connectionEventCallback> {
errorReason: ErrorInfo;
id: string;
key: string;
@@ -412,7 +421,7 @@ export declare class Connection extends EventEmitter<connectionEventCallback> {
ping: (callback?: (error: ErrorInfo, responseTime: number ) => void ) => void;
}
export declare class Stats {
declare class Stats {
all: StatsMessageTypes;
apiRequests: StatsRequestCount;
channels: StatsResourceCount;
@@ -424,16 +433,16 @@ export declare class Stats {
tokenRequests: StatsRequestCount;
}
export declare class PaginatedResult<T> {
declare class PaginatedResult<T> {
items: Array<T>;
first: (results: PaginatedResultCallback<T>) => void;
next: (results: PaginatedResultCallback<T>) => void;
current: (results: PaginatedResultCallback<T>) => void;
first: (results: paginatedResultCallback<T>) => void;
next: (results: paginatedResultCallback<T>) => void;
current: (results: paginatedResultCallback<T>) => void;
hasNext: () => boolean;
isLast: () => boolean;
}
export declare class HttpPaginatedResponse extends PaginatedResult<any> {
declare class HttpPaginatedResponse extends PaginatedResult<any> {
items: Array<string>;
statusCode: number;
success: boolean;

1
ably/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "../tslint.json" }