From 0a51536fcbf53c7aec3d29631cf4b6698e2eec01 Mon Sep 17 00:00:00 2001 From: Nicolas Penin Date: Wed, 26 Jul 2017 16:06:15 +0200 Subject: [PATCH] added json-rpc-ws type definition (#17881) * added json-rpc-ws type definition * added json-rpc-ws type definition * removed contributors from package.json * removed script section as requested by Travis CI * removed deprecasted information * removed description * removed license * removed main * removed name * removed typings * removed typescript version * removed version * emptied package.json * Update tsconfig.json * Update tsconfig.json * Update tsconfig.json * Update tsconfig.json * Update package.json * updated dependencies * removed package.json updated reference * changes made according to @andy-ms suggestions * first fixes for tslint * tslint update * applied changes requested by @andy-ms * used capitals restored function names as agreed * updated lint rule change * updated header to use a newer typescript version --- types/json-rpc-ws/index.d.ts | 118 +++++++++++++++++++++++++ types/json-rpc-ws/json-rpc-ws-tests.ts | 42 +++++++++ types/json-rpc-ws/tsconfig.json | 22 +++++ types/json-rpc-ws/tslint.json | 3 + 4 files changed, 185 insertions(+) create mode 100644 types/json-rpc-ws/index.d.ts create mode 100644 types/json-rpc-ws/json-rpc-ws-tests.ts create mode 100644 types/json-rpc-ws/tsconfig.json create mode 100644 types/json-rpc-ws/tslint.json diff --git a/types/json-rpc-ws/index.d.ts b/types/json-rpc-ws/index.d.ts new file mode 100644 index 0000000000..61d72afcf4 --- /dev/null +++ b/types/json-rpc-ws/index.d.ts @@ -0,0 +1,118 @@ +// Type definitions for json-rpc-ws 4.0 +// Project: https://www.npmjs.com/package/json-rpc-ws +// Definitions by: Nicolas Penin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +import * as ws from 'ws'; + +export function createServer(): Server; +export function createClient(): Client; + +export class Server extends Base { + constructor(); + + /** + * Start the server + */ + start(options?: ws.IServerOptions, callback?: () => void): void; + server: ws.Server; + /** + * Stop the server + */ + stop(): void; +} + +export class Client extends Base { + constructor(WebSocket: ws, browser?: boolean); + + /** + * Connect to a json-rpc-ws server + */ + connect(url: string, connected: () => void): void; + + /** + * Test whether we have a connection or not + */ + isConnected(): boolean; + + /** + * Return the current connection (there can be only one) + */ + getConnection(): TConnection; + + /** + * Close the current connection + */ + disconnect(callback: () => void): void; +} +export class Base { + /** + * Add a handler function for a given method + */ + expose(eventName: string, handler: Handler): void; + /** + * Send a method request through a specific connection + */ + send(eventName: string, params?: ParamType, callback?: ReplyCallback): void; + send(eventName: string, params: ParamType): void; + /** + * Connected event handler + */ + connected(socket: ws): void; + /** + * Disconnected event handler + */ + disconnected(connection: TConnection): void; + /** + * Test if a handler exists for a given method + */ + hasHandler(method: string): boolean; + + /** + * Get handler for a given method + */ + getHandler(method: string): Handler; + /** + * Get a connection by id + */ + getConnection(id: string): Connection; + /** + * Shut down all existing connections + * + * @public + */ + hangup(): void; +} + +export interface Connection { + id: string; + socket: ws; + parent: Base; + responseHandlers: { [id: string]: ReplyCallback }; + sendRaw(payload: Payload): void; + processPayload(payload: Payload): void; + sendResult(id: string, error?: any, result?: any): void; + sendMethod(method: string, params: ParamType, callback?: ReplyCallback): void; + sendError(error: any, id?: string, data?: any): void; + close(error?: any): void; + hangup(callback?: () => void): void; + message(data: any): void; +} + +export interface Payload { + jsonrpc?: '2.0'; + id: string; + method: string; + params?: ParamType; + result?: any; + error?: any; +} + +export type Handler = (this: TConnection, params: ParamType, reply: ReplyCallback) => void; +export type ReplyCallback = (error: any, params?: ParamType) => void; + +/** + * Returns a valid jsonrpc2.0 error reply + */ +export function Errors(type: string, id: string | number | null, data: any): any | null; diff --git a/types/json-rpc-ws/json-rpc-ws-tests.ts b/types/json-rpc-ws/json-rpc-ws-tests.ts new file mode 100644 index 0000000000..efcf51cfaf --- /dev/null +++ b/types/json-rpc-ws/json-rpc-ws-tests.ts @@ -0,0 +1,42 @@ +import * as JsonRpcWs from 'json-rpc-ws'; + +let server = JsonRpcWs.createServer(); + +server.expose('mirror', function mirror(params, reply) { + console.log('mirror handler', params); + reply(null, params); +}); + +server.start({ port: 8080 }, function started() { + console.log('Server started on port 8080'); +}); + +let client = JsonRpcWs.createClient(); + +client.connect('ws://localhost:8080', function connected() { + client.send('mirror', ['a param', 'another param'], function mirrorReply(error, reply) { + console.log('mirror reply', reply); + }); +}); + +interface CustomConnection extends JsonRpcWs.Connection { + rooms?: string[]; +} + +let serverWithCustomConnection = JsonRpcWs.createServer(); + +serverWithCustomConnection.expose('join', function(params: { room: string }) { + this.rooms = this.rooms || []; + this.rooms.push(params.room); + console.log(this.id + ' joined ' + params.room); +}); + +serverWithCustomConnection.start({ port: 8080 }, () => { + console.log('Server started on port 8080'); +}); + +let clientForServerWithCustomConnection = JsonRpcWs.createClient(); + +client.connect('ws://localhost:8080', () => { + client.send('join', { room: 'my room' }); +}); diff --git a/types/json-rpc-ws/tsconfig.json b/types/json-rpc-ws/tsconfig.json new file mode 100644 index 0000000000..9707d8e10f --- /dev/null +++ b/types/json-rpc-ws/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "json-rpc-ws-tests.ts" + ] +} \ No newline at end of file diff --git a/types/json-rpc-ws/tslint.json b/types/json-rpc-ws/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/json-rpc-ws/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file