UNPKG

6.21 kBTypeScriptView Raw
1import net = require('net');
2import tls = require('tls');
3import https = require('https');
4import http = require('http');
5import events = require('events');
6
7export as namespace jayson;
8
9interface Utils {
10}
11
12declare type RequestParamsLike = Array<any> | object;
13
14interface JSONRPCError {
15 code: number;
16 message: string;
17 data?: object;
18}
19
20declare type JSONRPCErrorLike = Error | JSONRPCError;
21
22interface JSONRPCVersionOneRequest {
23 method: string;
24 params: Array<any>;
25 id: JSONRPCIDLike;
26}
27
28interface JSONRPCVersionTwoRequest {
29 jsonrpc: number;
30 method: string;
31 params: RequestParamsLike;
32 id?: JSONRPCIDLike | null;
33}
34
35declare type JSONRPCIDLike = number | string;
36
37declare type JSONRPCRequest = JSONRPCVersionOneRequest | JSONRPCVersionTwoRequest;
38
39declare type JSONRPCRequestLike = JSONRPCRequest | string;
40
41declare type JSONRPCResultLike = any;
42
43interface JSONRPCCallbackTypePlain {
44 (err: JSONRPCErrorLike, result?: JSONRPCResultLike): void
45}
46
47interface JSONRPCCallbackTypeSugared {
48 (err: Error, error?: JSONRPCErrorLike, result?: JSONRPCResultLike): void
49}
50
51type JSONRPCCallbackType = JSONRPCCallbackTypePlain | JSONRPCCallbackTypeSugared;
52
53interface JSONRPCCallbackTypeBatchPlain {
54 (err: JSONRPCErrorLike, results?: Array<JSONRPCResultLike>): void
55}
56
57interface JSONRPCCallbackTypeBatchSugared {
58 (err: Error, errors?: Array<JSONRPCErrorLike>, results?: Array<JSONRPCResultLike>): void
59}
60
61type JSONRPCCallbackTypeBatch = JSONRPCCallbackTypeBatchPlain | JSONRPCCallbackTypeBatchSugared;
62
63interface MethodHandlerType {
64 (args: RequestParamsLike, callback: JSONRPCCallbackType): void;
65 (...args: any[]): void; // callback still expected to be last
66}
67
68declare type MethodOptionsParamsLike = Array<any> | Object | object;
69
70interface MethodOptions {
71 handler?: MethodHandlerType;
72 collect?: boolean;
73 params?: MethodOptionsParamsLike;
74}
75
76declare class Method {
77 constructor(handler?: MethodHandlerType, options?: MethodOptions);
78 constructor(options: MethodOptions);
79
80 getHandler(): MethodHandlerType;
81 setHandler(handler: MethodHandlerType): void;
82 execute(server: Server, requestParams: RequestParamsLike, callback: JSONRPCCallbackType): any | Promise<any>;
83}
84
85declare type MethodLike = Function | Method | Client
86
87declare type ServerRouterFunction = (method: string, params: RequestParamsLike) => MethodLike;
88
89interface ServerOptions {
90 collect?: boolean;
91 params?: MethodOptionsParamsLike;
92 version?: number;
93 reviver?: JSONParseReviver;
94 replacer?: JSONStringifyReplacer;
95 encoding?: string;
96 router?: ServerRouterFunction;
97 methodConstructor?: Function;
98}
99
100declare class Server {
101 constructor(methods?: {[methodName: string]: MethodLike}, options?: object);
102
103 static errors: {[errorName: string]: number};
104 static errorMessages: {[errorMessage: string]: string};
105 static interfaces: {[interfaces: string]: Function};
106
107 http(options?: HttpServerOptions): HttpServer;
108 https(options?: HttpsServerOptions): HttpsServer;
109 tcp(options?: TcpServerOptions): TcpServer;
110 tls(options?: TlsServerOptions): TlsServer;
111 middleware(options?: MiddlewareServerOptions): Function;
112
113 method(name: string, definition: MethodLike): void;
114 methods(methods: {[methodName: string]: MethodLike}): void;
115 hasMethod(name: string): boolean;
116 removeMethod(name: string): void;
117 getMethod(name: string): MethodLike;
118 error(code?: number, message?: string, data?: object): JSONRPCError;
119 call(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, originalCallback?: JSONRPCCallbackType): void;
120}
121
122interface MiddlewareServerOptions extends ServerOptions {
123}
124
125interface HttpServerOptions extends ServerOptions {
126}
127
128declare class HttpServer extends http.Server {
129 constructor(server: Server, options?: HttpServerOptions);
130}
131
132interface HttpsServerOptions extends ServerOptions, https.ServerOptions {
133}
134
135declare class HttpsServer extends https.Server {
136 constructor(server: Server, options?: HttpsServerOptions);
137}
138
139interface TcpServerOptions extends ServerOptions {
140}
141
142declare class TcpServer extends net.Server {
143 constructor(server: Server, options?: TcpServerOptions);
144}
145
146interface TlsServerOptions extends tls.TlsOptions {
147}
148
149declare class TlsServer extends tls.Server {
150 constructor(server: Server, options?: TlsServerOptions);
151}
152
153declare type JSONParseReviver = (key: string, value: any) => any;
154declare type JSONStringifyReplacer = (key: string, value: any) => any;
155
156declare type IDGenerator = () => string;
157
158interface ClientOptions {
159 version?: number;
160 reviver?: JSONParseReviver;
161 replacer?: JSONStringifyReplacer;
162 generator?: IDGenerator;
163}
164
165interface HttpClientOptions extends ClientOptions, http.RequestOptions {
166}
167
168declare class HttpClient extends Client {
169 constructor(options?: HttpClientOptions);
170}
171
172interface TlsClientOptions extends ClientOptions, tls.ConnectionOptions {
173}
174
175declare class TlsClient extends Client {
176 constructor(options?: TlsClientOptions);
177}
178
179interface TcpClientOptions extends ClientOptions, net.TcpSocketConnectOpts {
180}
181
182declare class TcpClient extends Client {
183 constructor(options?: TcpClientOptions);
184}
185
186interface HttpsClientOptions extends ClientOptions, https.RequestOptions {
187}
188
189declare class HttpsClient extends Client {
190 constructor(options?: HttpsClientOptions);
191}
192
193type ClientRequestShouldCall = JSONRPCCallbackType | false;
194
195declare class Client extends events.EventEmitter {
196 constructor(server: Server, options?: ClientOptions);
197 constructor(options: ClientOptions);
198
199 static http(options?: HttpClientOptions): HttpClient;
200 static https(options?: HttpsClientOptions): HttpsClient;
201 static tcp(options?: TcpClientOptions): TcpClient;
202 static tls(options?: TlsClientOptions): TlsClient;
203
204 request(method: string, params: RequestParamsLike, id?: string, callback?: JSONRPCCallbackType): JSONRPCRequest;
205 request(method: string, params: RequestParamsLike, callback?: JSONRPCCallbackType): JSONRPCRequest;
206 // request(method: string, params: RequestParamsLike, id: string, callback: ClientRequestShouldCall): JSONRPCRequest;
207 // request(method: Array<JSONRPCRequestLike>): Promise<JSONRPCResultLike>;
208 request(method: Array<JSONRPCRequestLike>, callback: JSONRPCCallbackTypeBatch): Array<JSONRPCRequest>;
209}