From 1df8038e7effe4b78619f9077e36a895a9c050d5 Mon Sep 17 00:00:00 2001 From: Ivo Stratev Date: Wed, 20 Jul 2016 09:26:54 +0300 Subject: [PATCH] Adding a new type definition for nes (#10129) * adding nes type definitions * Update nes.d.ts * Payload is also optional when making request * adding client interfaces types to Client from 'nes' to 'nes/client' * until TypeScript 2.0 all types are nullable ... --- nes/nes-tests.ts | 65 ++++++++++++++++++++++++ nes/nes.d.ts | 130 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 nes/nes-tests.ts create mode 100644 nes/nes.d.ts diff --git a/nes/nes-tests.ts b/nes/nes-tests.ts new file mode 100644 index 0000000000..d56b42a8c4 --- /dev/null +++ b/nes/nes-tests.ts @@ -0,0 +1,65 @@ +/// + +import Hapi = require('hapi'); +import Nes = require('nes'); + +let server: Hapi.Server = new Hapi.Server(); +server.connection({port: 8080}); + +server.register(Nes, (regErr: any) => { + if(regErr) { + console.log('register err'); + console.log(regErr); + } else { + let wsServer: Nes.Server = server as Nes.Server; + wsServer.subscription('/item/{id}'); + wsServer.route( { + method: 'GET', + path: '/test', + config: { + handler: (request: Hapi.Request, reply: Hapi.IReply) => { + reply({test: 'passes'}); + } + } + }); + wsServer.start((err: any) => { + if(err) { + console.log('start err'); + console.log(err); + } else { + setTimeout(() => { + wsServer.publish('/item/5', { id: 5, status: 'complete' }); + wsServer.publish('/item/6', { id: 6, status: 'initial' }); + }, 100); + } + }); + } +}); + +let wsClient: Nes.Client = new Nes.Client('ws://localhost:8080'); +wsClient.connect((err: any) => { + if(err) { + console.log('start err'); + console.log(err); + } else { + wsClient.subscribe('/item/5', (update) => { + wsClient.request('/test', (reqErr: any, payload: any, statusCode: number) => { + if(reqErr) { + console.log('request err'); + console.log(reqErr); + } else { + console.log(update); + console.log(payload); + if(payload.test === 'passes') { + process.exit(0); + } + } + }) + }, (subErr) => { + if(subErr) { + console.log('subscribe err'); + console.log(subErr); + } + }); + } +}); diff --git a/nes/nes.d.ts b/nes/nes.d.ts new file mode 100644 index 0000000000..c86627db47 --- /dev/null +++ b/nes/nes.d.ts @@ -0,0 +1,130 @@ +// Type definitions for nes 6.2.1 +// Project: https://github.com/hapijs/nes +// Definitions by: Ivo Stratev +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'nes' { + import Hapi = require('hapi'); + + export interface SocketAuthObject { + isAuthenticated: boolean; + credentials: any; + artifacts: any; + } + + export class Socket { + id: any; + app: Object; + auth: SocketAuthObject; + disconect(callback?: () => void): void; + send(message: any, callback?: (err?: any) => void): void; + publish(path: string, message: any, callback?: (err?: any) => void): void; + revoke(path: string, message: any, callback?: (err?: any) => void): void; + } + + export interface ServerBroadcastOptions { + user: any + } + + export interface ServerSubscriptionOptionsFilterOptions { + socket: Socket; + credentials?: any; + params?: any; + } + + export interface ServerSubscriptionOptionsAuthOptions { + mode?: 'required' | 'optional'; + scope?: string | string[]; + entity?: 'user' | 'app' | 'any'; + index?: boolean; + } + + export type ServerOnSubscribeWithParams = (socket: Socket, path: string, params: any, next: (err?: any) => void) => void; + export type ServerOnSubscribeWithoutParams = (socket: Socket, path: string, next: (err?: any) => void) => void; + export type ServerOnSubscribe = ServerOnSubscribeWithParams | ServerOnSubscribeWithoutParams; + + export type ServerOnUnSubscribeWithParams = (socket: Socket, path: string, params: any, next: () => void) => void; + export type ServerOnUnSubscribeWithoutParams = (socket: Socket, path: string, next: () => void) => void; + export type ServerOnUnSubscribe = ServerOnUnSubscribeWithParams | ServerOnUnSubscribeWithoutParams; + + export interface ServerSubscriptionOptions { + filter?: (path: string, message: any, options: ServerSubscriptionOptionsFilterOptions, next: (isMatch: boolean, override: any) => void) => void; + auth?: boolean | ServerSubscriptionOptionsAuthOptions; + onSubscribe?: ServerOnSubscribe; + onUnsubscribe?: ServerOnUnSubscribe; + } + + export interface ServerPublishOptions { + internal?: any; + user?: any; + } + + export interface ServerEachSokcetOptions { + subscription?: string; + user?: any; + } + + export class Server extends Hapi.Server { + broadcast(message: any, options?: ServerBroadcastOptions): void; + subscription(path: string, options?: ServerSubscriptionOptions): void; + publish(path: string, message: any, options?: ServerPublishOptions): void; + eachSocket(each: (socket: Socket) => void, options?: ServerEachSokcetOptions): void; + } + + export class Request extends Hapi.Request { + socket: Socket; + } + + export interface ClientOptions { + ws?: any; + timeout?: number | boolean; + } + + export interface ClientConnnectOptions { + auth?: any; + delay?: number; + maxDelay?: number; + retries?: number; + timeout?: number; + } + + export interface ClientRequestOptions { + path: string; + method?: string; + headers?: Object; + payload?: any; + } + + export interface ClientSubscribeFlags { + revoked?: boolean; + } + + export class Client { + constructor(url: string, options?: ClientOptions); + onError: (err: any) => void; + onConnect: () => void; + onDisconnect: () => void; + onUpdate: (message: any) => void; + connect(options: ClientConnnectOptions, callback: (err?: any) => void): void; + connect(callback: (err?: any) => void): void; + disconnect(): void; + id: any; + request(options: string | 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: (message: any, flags: ClientSubscribeFlags) => void, callback: (err?: any) => void): void; + unsubscribe(path: string, handler: (message: any, flags: ClientSubscribeFlags) => void, callback: (err?: any) => void): void; + subscriptions(): string[]; + overrideReconnectionAuth(auth: any): void; + } +} + +declare module 'nes/client' { + export { + Client, + ClientConnnectOptions, + ClientRequestOptions, + ClientSubscribeFlags + } from 'nes'; +}