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
This commit is contained in:
Nicolas Penin
2017-07-26 16:06:15 +02:00
committed by Andy
parent be9d76b7ce
commit 0a51536fcb
4 changed files with 185 additions and 0 deletions

118
types/json-rpc-ws/index.d.ts vendored Normal file
View File

@@ -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 <https://github.com/npenin>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
import * as ws from 'ws';
export function createServer<TConnection extends Connection>(): Server<TConnection>;
export function createClient<TConnection extends Connection>(): Client<TConnection>;
export class Server<TConnection extends Connection> extends Base<TConnection> {
constructor();
/**
* Start the server
*/
start(options?: ws.IServerOptions, callback?: () => void): void;
server: ws.Server;
/**
* Stop the server
*/
stop(): void;
}
export class Client<TConnection extends Connection> extends Base<TConnection> {
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<TConnection extends Connection> {
/**
* Add a handler function for a given method
*/
expose<ParamType, ParamCallbackType>(eventName: string, handler: Handler<TConnection, ParamType, ParamCallbackType>): void;
/**
* Send a method request through a specific connection
*/
send<ParamType, ParamCallbackType>(eventName: string, params?: ParamType, callback?: ReplyCallback<ParamCallbackType>): void;
send<ParamType>(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<ParamType, ParamCallbackType>(method: string): Handler<TConnection, ParamType, ParamCallbackType>;
/**
* 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<any>;
responseHandlers: { [id: string]: ReplyCallback<any> };
sendRaw<ParamType>(payload: Payload<ParamType>): void;
processPayload<ParamType>(payload: Payload<ParamType>): void;
sendResult(id: string, error?: any, result?: any): void;
sendMethod<ParamType, ParamCallbackType>(method: string, params: ParamType, callback?: ReplyCallback<ParamCallbackType>): void;
sendError(error: any, id?: string, data?: any): void;
close(error?: any): void;
hangup(callback?: () => void): void;
message(data: any): void;
}
export interface Payload<ParamType> {
jsonrpc?: '2.0';
id: string;
method: string;
params?: ParamType;
result?: any;
error?: any;
}
export type Handler<TConnection extends Connection, ParamType, ParamCallbackType> = (this: TConnection, params: ParamType, reply: ReplyCallback<ParamCallbackType>) => void;
export type ReplyCallback<ParamType> = (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;

View File

@@ -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<CustomConnection>();
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' });
});

View File

@@ -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"
]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}