From 10f421bb830163ddb6c1d9c6adfb5abb7a93f5db Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Mon, 8 May 2017 12:35:40 +0100 Subject: [PATCH] Fix importing nes/client --- .../hapi-auth-basic/hapi-auth-basic-tests.ts | 4 +- types/nes/client.d.ts | 57 +++++++++++ types/nes/index.d.ts | 95 +++++-------------- types/nes/test/broadcast-client.ts | 13 +++ types/nes/test/client-require.ts | 9 +- types/nes/test/nes-tests.ts | 4 +- types/nes/test/route-authentication-client.ts | 13 +++ types/nes/test/route-invocation-client.ts | 13 +++ types/nes/test/subscription-filter-client.ts | 19 ++++ types/nes/test/subscriptions-client.ts | 16 ++++ types/nes/tsconfig.json | 1 + 11 files changed, 168 insertions(+), 76 deletions(-) create mode 100644 types/nes/client.d.ts diff --git a/types/hapi-auth-basic/hapi-auth-basic-tests.ts b/types/hapi-auth-basic/hapi-auth-basic-tests.ts index 13dd159377..7f78fe9f65 100644 --- a/types/hapi-auth-basic/hapi-auth-basic-tests.ts +++ b/types/hapi-auth-basic/hapi-auth-basic-tests.ts @@ -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' } diff --git a/types/nes/client.d.ts b/types/nes/client.d.ts new file mode 100644 index 0000000000..d41f5a608a --- /dev/null +++ b/types/nes/client.d.ts @@ -0,0 +1,57 @@ +// Type definitions for nes 6.4.2 +// Project: https://github.com/hapijs/nes +// Definitions by: Ivo Stratev +// 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; +} diff --git a/types/nes/index.d.ts b/types/nes/index.d.ts index 95c3dd0835..fa33e53ba4 100644 --- a/types/nes/index.d.ts +++ b/types/nes/index.d.ts @@ -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; diff --git a/types/nes/test/broadcast-client.ts b/types/nes/test/broadcast-client.ts index 1cf8797365..4c94b8c575 100644 --- a/types/nes/test/broadcast-client.ts +++ b/types/nes/test/broadcast-client.ts @@ -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!' + }; +}); diff --git a/types/nes/test/client-require.ts b/types/nes/test/client-require.ts index 90bce07d33..f5a07ea1d3 100644 --- a/types/nes/test/client-require.ts +++ b/types/nes/test/client-require.ts @@ -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); diff --git a/types/nes/test/nes-tests.ts b/types/nes/test/nes-tests.ts index 9dd12ae99a..36e8af65bb 100644 --- a/types/nes/test/nes-tests.ts +++ b/types/nes/test/nes-tests.ts @@ -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'); diff --git a/types/nes/test/route-authentication-client.ts b/types/nes/test/route-authentication-client.ts index b3fd33fe95..fe57eb8d56 100644 --- a/types/nes/test/route-authentication-client.ts +++ b/types/nes/test/route-authentication-client.ts @@ -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' + }); +}); diff --git a/types/nes/test/route-invocation-client.ts b/types/nes/test/route-invocation-client.ts index e25e64e349..7b144e3a9c 100644 --- a/types/nes/test/route-invocation-client.ts +++ b/types/nes/test/route-invocation-client.ts @@ -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!' + }); +}); diff --git a/types/nes/test/subscription-filter-client.ts b/types/nes/test/subscription-filter-client.ts index bd5c9c88f5..3f8d3ffb8b 100644 --- a/types/nes/test/subscription-filter-client.ts +++ b/types/nes/test/subscription-filter-client.ts @@ -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) { }); +}); diff --git a/types/nes/test/subscriptions-client.ts b/types/nes/test/subscriptions-client.ts index 1e33eac40f..a9ac55fc6d 100644 --- a/types/nes/test/subscriptions-client.ts +++ b/types/nes/test/subscriptions-client.ts @@ -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) { }); +}); diff --git a/types/nes/tsconfig.json b/types/nes/tsconfig.json index a2cd55a5db..cb28129b71 100644 --- a/types/nes/tsconfig.json +++ b/types/nes/tsconfig.json @@ -17,6 +17,7 @@ }, "files": [ "index.d.ts", + "client.d.ts", "test/client-require.ts", "test/nes-tests.ts", "test/broadcast-client.ts",