Fix importing nes/client

This commit is contained in:
Alexander James Phillips
2017-05-08 12:35:40 +01:00
parent 91fe980714
commit 10f421bb83
11 changed files with 168 additions and 76 deletions
@@ -1,3 +1,5 @@
// from https://github.com/hapijs/hapi-auth-basic#hapi-auth-basic
import Bcrypt = require('bcrypt');
import Basic = require('hapi-auth-basic');
import * as Hapi from 'hapi';
@@ -14,7 +16,7 @@ interface User {
const users: {[index: string]: User} = {
john: {
username: 'john',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
name: 'John Doe',
id: '2133d32a'
}
+57
View File
@@ -0,0 +1,57 @@
// Type definitions for nes 6.4.2
// Project: https://github.com/hapijs/nes
// Definitions by: Ivo Stratev <https://github.com/NoHomey>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module 'nes/client' {
class Client {
constructor(url: string, options?: Client.ClientOptions);
onError: (err: any) => void;
onConnect: () => void;
onDisconnect: () => void;
onUpdate: (message: any) => void;
connect(options: Client.ClientConnectOptions, callback: (err?: any) => void): void;
connect(callback: (err?: any) => void): void;
disconnect(): void;
id: any;
request(options: string | Client.ClientRequestOptions, callback: (err: any, payload: any, statusCode?: number, headers?: Object) => void): void;
message(message: any, callback: (err: any, message: any) => void): void;
subscribe(path: string, handler: Client.Handler, callback: (err?: any) => void): void;
unsubscribe(path: string, handler: Client.Handler, callback: (err?: any) => void): void;
subscriptions(): string[];
overrideReconnectionAuth(auth: any): void;
}
namespace Client {
interface Handler {
(message: any, flags: Client.ClientSubscribeFlags): void;
}
interface ClientOptions {
ws?: any;
timeout?: number | boolean;
}
interface ClientConnectOptions {
auth?: any;
delay?: number;
maxDelay?: number;
retries?: number;
timeout?: number;
}
interface ClientRequestOptions {
path: string;
method?: string;
headers?: Object;
payload?: any;
}
interface ClientSubscribeFlags {
revoked?: boolean;
}
}
export = Client;
}
+23 -72
View File
@@ -4,6 +4,7 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as Hapi from 'hapi';
import NesClient = require('nes/client');
declare module 'hapi' {
interface Server {
@@ -12,6 +13,10 @@ declare module 'hapi' {
publish(path: string, message: any, options?: nes.ServerPublishOptions): void;
eachSocket(each: (socket: SocketClass) => void, options?: nes.ServerEachSocketOptions): void;
}
interface Request {
socket: SocketClass;
}
}
declare class SocketClass {
@@ -24,32 +29,7 @@ declare class SocketClass {
revoke(path: string, message: any, callback?: (err?: any) => void): void;
}
declare class RequestClass extends Hapi.Request {
socket: SocketClass;
}
declare class ClientClass {
onError: (err: any) => void;
onConnect: () => void;
onDisconnect: () => void;
onUpdate: (message: any) => void;
connect(options: nes.ClientConnectOptions, callback: (err?: any) => void): void;
connect(callback: (err?: any) => void): void;
disconnect(): void;
id: any;
request(options: string | nes.ClientRequestOptions, callback: (err: any, payload: any, statusCode?: number, headers?: Object) => void): void;
message(message: any, callback: (err: any, message: any) => void): void;
subscribe(path: string, handler: nes.Handler, callback: (err?: any) => void): void;
unsubscribe(path: string, handler: nes.Handler, callback: (err?: any) => void): void;
subscriptions(): string[];
overrideReconnectionAuth(auth: any): void;
}
declare module nes {
interface Handler {
(message: any, flags: nes.ClientSubscribeFlags): void;
}
interface SocketAuthObject {
isAuthenticated: boolean;
credentials: any;
@@ -98,44 +78,29 @@ declare module nes {
user?: any;
}
interface ClientOptions {
ws?: any;
timeout?: number | boolean;
}
interface Socket extends SocketClass {}
interface ClientConnectOptions {
auth?: any;
delay?: number;
maxDelay?: number;
retries?: number;
timeout?: number;
}
interface Server extends Hapi.Server {}
interface ClientRequestOptions {
path: string;
method?: string;
headers?: Object;
payload?: any;
}
interface Request extends Hapi.Request {}
interface ClientSubscribeFlags {
revoked?: boolean;
}
interface Client extends NesClient {}
interface Socket extends SocketClass {
}
interface Handler extends NesClient.Handler {}
interface Server extends Hapi.Server {
}
interface ClientOptions extends NesClient.ClientOptions {}
interface Request extends RequestClass {
}
interface ClientConnectOptions extends NesClient.ClientConnectOptions {}
interface Client extends ClientClass {
}
interface ClientRequestOptions extends NesClient.ClientRequestOptions {}
interface ClientSubscribeFlags extends NesClient.ClientSubscribeFlags {}
}
interface NesResources {
// TODO there must be a drier cleaner way of doing this that allows for the
// export to have be of type Hapi.PluginFunction whilst also exposing the
// type and class of Client, Request, etc.
interface NesClassExports {
Socket: {
new(): SocketClass;
};
@@ -145,30 +110,16 @@ interface NesResources {
};
Request: {
new(): RequestClass;
new(): Hapi.Request;
};
Client: {
new(url: string, options?: nes.ClientOptions): ClientClass;
new(url: string, options?: NesClient.ClientOptions): NesClient;
};
}
// TODO fix this. See test/client-require.ts test case.
declare module 'nes/client' {
var nesClient: NesResources;
interface NesAllExports extends NesClassExports, Hapi.PluginFunction<{}> {}
export = nesClient;
// export {
// ClientClass,
// // ClientConnectOptions,
// // ClientRequestOptions,
// // ClientSubscribeFlags
// };
}
interface NesExports extends NesResources, Hapi.PluginFunction<{}> {}
declare var nes: NesExports;
declare var nes: NesAllExports;
export = nes;
+13
View File
@@ -10,3 +10,16 @@ client.connect(function (err) {
// update -> 'welcome!'
};
});
// Added in addition to nes doc example code
import NesClient = require('nes/client');
var client = new NesClient('ws://localhost');
client.connect(function (err) {
client.onUpdate = function (update) {
// update -> 'welcome!'
};
});
+7 -2
View File
@@ -3,5 +3,10 @@
// that is not needed for the browser. The browser will only need the nes client.
// If you are using CommonJS you can load the client with require('nes/client').
// TODO fix this
// import nes = require('nes/client');
import Client = require('nes/client');
var options: Client.ClientConnectOptions = {
delay: 3
}
var client: Client = new Client('ws://localhost', options);
+3 -1
View File
@@ -34,7 +34,9 @@ server.register(Nes, (regErr: any) => {
}
});
let wsClient: Nes.Client = new Nes.Client('ws://localhost:8080');
let options: Nes.ClientConnectOptions = {delay: 3};
let wsClient: Nes.Client = new Nes.Client('ws://localhost:8080', options);
wsClient.connect((err: any) => {
if(err) {
console.log('start err');
@@ -10,3 +10,16 @@ client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } }
// payload -> 'Hello John Doe'
});
});
// Added in addition to nes doc example code
import NesClient = require('nes/client');
var client = new NesClient('ws://localhost');
client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } } }, function (err) {
client.request('hello', function (err, payload) { // Can also request '/h'
// payload -> 'Hello John Doe'
});
});
+13
View File
@@ -10,3 +10,16 @@ client.connect(function (err) {
// payload -> 'world!'
});
});
// Added in addition to nes doc example code
import NesClient = require('nes/client');
var client = new NesClient('ws://localhost');
client.connect(function (err) {
client.request('hello', function (err, payload) { // Can also request '/h'
// payload -> 'world!'
});
});
@@ -16,3 +16,22 @@ client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } }
client.subscribe('/items', handler, function (err) { });
});
// Added in addition to nes doc example code
import NesClient = require('nes/client');
var client = new NesClient('ws://localhost');
// Authenticate as 'john'
client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } } }, function (err) {
var handler: NesClient.Handler = function (err, update) {
// First publish is not received (filtered due to updater key)
// update -> { id: 6, status: 'initial', updater: 'steve' }
};
client.subscribe('/items', handler, function (err) { });
});
+16
View File
@@ -13,3 +13,19 @@ client.connect(function (err) {
client.subscribe('/item/5', handler, function (err) { });
});
// Added in addition to nes doc example code
import NesClient = require('nes/client');
var client = new NesClient('ws://localhost');
client.connect(function (err) {
var handler: NesClient.Handler = function (update, flags) {
// update -> { id: 5, status: 'complete' }
// Second publish is not received (doesn't match)
};
client.subscribe('/item/5', handler, function (err) { });
});
+1
View File
@@ -17,6 +17,7 @@
},
"files": [
"index.d.ts",
"client.d.ts",
"test/client-require.ts",
"test/nes-tests.ts",
"test/broadcast-client.ts",