Skip to content

Commit

Permalink
add some type defs for websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
tedeh committed May 23, 2021
1 parent 9cedfed commit 33ee22c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
21 changes: 21 additions & 0 deletions index.d.ts
Expand Up @@ -4,6 +4,7 @@ import https = require('https');
import http = require('http');
import events = require('events');
import Stream = require('stream');
import WebSocket = require('isomorphic-ws');
import * as connect from 'connect';

export interface UtilsJSONParseOptions {
Expand Down Expand Up @@ -221,6 +222,7 @@ export declare class Server extends events.EventEmitter {
https(options?: HttpsServerOptions): HttpsServer;
tcp(options?: TcpServerOptions): TcpServer;
tls(options?: TlsServerOptions): TlsServer;
websocket(options?: WebsocketServerOptions): WebsocketServer;
middleware(options?: MiddlewareServerOptions): connect.HandleFunction;

method(name: string, definition: MethodLike): void;
Expand Down Expand Up @@ -265,6 +267,14 @@ declare class TlsServer extends tls.Server {
constructor(server: Server, options?: TlsServerOptions);
}

export interface WebsocketServerOptions extends ServerOptions, WebSocket.ServerOptions {
wss?: WebSocket.Server;
}

declare class WebsocketServer {
constructor(server: Server, options?: WebsocketServerOptions);
}

type JSONParseReviver = (key: string, value: any) => any;
type JSONStringifyReplacer = (key: string, value: any) => any;

Expand Down Expand Up @@ -306,6 +316,16 @@ declare class HttpsClient extends Client {
constructor(options?: HttpsClientOptions);
}

export interface WebsocketClientOptions extends ClientOptions {
url?: string;
ws?: WebSocket;
timeout?: number;
}

declare class WebsocketClient extends Client {
constructor(options?: WebsocketClientOptions);
}

export declare class Client extends events.EventEmitter {
constructor(server: Server, options?: ClientOptions);
constructor(options: ClientOptions);
Expand All @@ -314,6 +334,7 @@ export declare class Client extends events.EventEmitter {
static https(options?: HttpsClientOptions): HttpsClient;
static tcp(options?: TcpClientOptions): TcpClient;
static tls(options?: TlsClientOptions): TlsClient;
static websocket(options?: WebsocketClientOptions): WebsocketClient;

request(method: string, params: RequestParamsLike, id?: string | null, callback?: JSONRPCCallbackType): JSONRPCRequest;
request(method: string, params: RequestParamsLike, callback?: JSONRPCCallbackType): JSONRPCRequest;
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -53,6 +53,7 @@
"@types/express-serve-static-core": "^4.17.9",
"@types/lodash": "^4.14.159",
"@types/node": "^12.12.54",
"@types/ws": "^7.4.4",
"commander": "^2.20.3",
"delay": "^5.0.0",
"es6-promisify": "^5.0.0",
Expand Down
21 changes: 21 additions & 0 deletions promise/index.d.ts
Expand Up @@ -4,6 +4,7 @@ import https = require('https');
import http = require('http');
import events = require('events');
import Stream = require('stream');
import WebSocket = require('isomorphic-ws');
import * as connect from 'connect';

export interface UtilsJSONParseOptions {
Expand Down Expand Up @@ -222,6 +223,7 @@ export declare class Server extends events.EventEmitter {
https(options?: HttpsServerOptions): HttpsServer;
tcp(options?: TcpServerOptions): TcpServer;
tls(options?: TlsServerOptions): TlsServer;
websocket(options?: WebsocketServerOptions): WebsocketServer;
middleware(options?: MiddlewareServerOptions): connect.HandleFunction;

method(name: string, definition: MethodLike): void;
Expand Down Expand Up @@ -266,6 +268,14 @@ declare class TlsServer extends tls.Server {
constructor(server: Server, options?: TlsServerOptions);
}

export interface WebsocketServerOptions extends ServerOptions, WebSocket.ServerOptions {
wss?: WebSocket.Server;
}

declare class WebsocketServer {
constructor(server: Server, options?: WebsocketServerOptions);
}

type JSONParseReviver = (key: string, value: any) => any;
type JSONStringifyReplacer = (key: string, value: any) => any;

Expand Down Expand Up @@ -307,6 +317,16 @@ declare class HttpsClient extends Client {
constructor(options?: HttpsClientOptions);
}

export interface WebsocketClientOptions extends ClientOptions {
url?: string;
ws?: WebSocket;
timeout?: number;
}

declare class WebsocketClient extends Client {
constructor(options?: WebsocketClientOptions);
}

export declare class Client extends events.EventEmitter {
constructor(server: Server, options?: ClientOptions);
constructor(options: ClientOptions);
Expand All @@ -315,6 +335,7 @@ export declare class Client extends events.EventEmitter {
static https(options?: HttpsClientOptions): HttpsClient;
static tcp(options?: TcpClientOptions): TcpClient;
static tls(options?: TlsClientOptions): TlsClient;
static websocket(options?: WebsocketClientOptions): WebsocketClient;

request(method:string, params:RequestParamsLike, id:JSONRPCIDLike | undefined, shouldCall:false): JSONRPCRequest;
request(method:string, params:RequestParamsLike, id?:JSONRPCIDLike): Promise<JSONRPCResultLike>;
Expand Down
29 changes: 29 additions & 0 deletions typescript/test.ts
Expand Up @@ -4,6 +4,7 @@ import jaysonBrowserClient from './../lib/client/browser';
import jaysonPromiseBrowserClient from './../promise/lib/client/browser';
import { reduce, isArray } from 'lodash';
import { Express } from 'express-serve-static-core';
import WebSocket from 'isomorphic-ws';

/**
* This file contains tests for the typescript type definitions.
Expand Down Expand Up @@ -804,3 +805,31 @@ export function test_server_and_method_call () {
}
});
}

export function test_websocket () {
const wss = new WebSocket.Server({port: 12345});
const server = new jayson.Server();
const websocketServer = server.websocket({wss});

const ws = new WebSocket('ws://localhost:12345');
const websocketClient = jayson.Client.websocket({
url: 'ws://localhost:12345',
ws,
timeout: 5000,
});
}

export async function test_websocket_Promise () {
const wss = new WebSocket.Server({port: 12345});
const server = new jaysonPromise.Server();
const websocketServer = server.websocket({wss});

const ws = new WebSocket('ws://localhost:12345');
const websocketClient = jaysonPromise.Client.websocket({
url: 'ws://localhost:12345',
ws,
timeout: 5000,
});

const result = await websocketClient.request('add', [1,2,3]);
}

0 comments on commit 33ee22c

Please sign in to comment.