1 | import net = require('net');
|
2 | import tls = require('tls');
|
3 | import https = require('https');
|
4 | import http = require('http');
|
5 | import events = require('events');
|
6 |
|
7 | export as namespace jayson;
|
8 |
|
9 | interface Utils {
|
10 | }
|
11 |
|
12 | declare type RequestParamsLike = Array<any> | object;
|
13 |
|
14 | interface JSONRPCError {
|
15 | code: number;
|
16 | message: string;
|
17 | data?: object;
|
18 | }
|
19 |
|
20 | declare type JSONRPCErrorLike = Error | JSONRPCError;
|
21 |
|
22 | interface JSONRPCVersionOneRequest {
|
23 | method: string;
|
24 | params: Array<any>;
|
25 | id: JSONRPCIDLike;
|
26 | }
|
27 |
|
28 | interface JSONRPCVersionTwoRequest {
|
29 | jsonrpc: number;
|
30 | method: string;
|
31 | params: RequestParamsLike;
|
32 | id?: JSONRPCIDLike | null;
|
33 | }
|
34 |
|
35 | declare type JSONRPCIDLike = number | string;
|
36 |
|
37 | declare type JSONRPCRequest = JSONRPCVersionOneRequest | JSONRPCVersionTwoRequest;
|
38 |
|
39 | declare type JSONRPCRequestLike = JSONRPCRequest | string;
|
40 |
|
41 | declare type JSONRPCResultLike = any;
|
42 |
|
43 | interface JSONRPCCallbackTypePlain {
|
44 | (err: JSONRPCErrorLike, result?: JSONRPCResultLike): void
|
45 | }
|
46 |
|
47 | interface JSONRPCCallbackTypeSugared {
|
48 | (err: Error, error?: JSONRPCErrorLike, result?: JSONRPCResultLike): void
|
49 | }
|
50 |
|
51 | type JSONRPCCallbackType = JSONRPCCallbackTypePlain | JSONRPCCallbackTypeSugared;
|
52 |
|
53 | interface JSONRPCCallbackTypeBatchPlain {
|
54 | (err: JSONRPCErrorLike, results?: Array<JSONRPCResultLike>): void
|
55 | }
|
56 |
|
57 | interface JSONRPCCallbackTypeBatchSugared {
|
58 | (err: Error, errors?: Array<JSONRPCErrorLike>, results?: Array<JSONRPCResultLike>): void
|
59 | }
|
60 |
|
61 | type JSONRPCCallbackTypeBatch = JSONRPCCallbackTypeBatchPlain | JSONRPCCallbackTypeBatchSugared;
|
62 |
|
63 | interface MethodHandlerType {
|
64 | (args: RequestParamsLike, callback: JSONRPCCallbackType): void;
|
65 | (...args: any[]): void;
|
66 | }
|
67 |
|
68 | declare type MethodOptionsParamsLike = Array<any> | Object | object;
|
69 |
|
70 | interface MethodOptions {
|
71 | handler?: MethodHandlerType;
|
72 | useContext?: boolean;
|
73 | params?: MethodOptionsParamsLike;
|
74 | }
|
75 |
|
76 | declare 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 |
|
85 | declare type MethodLike = Function | Method | Client
|
86 |
|
87 | declare type ServerRouterFunction = (method: string, params: RequestParamsLike) => MethodLike;
|
88 |
|
89 | interface ServerOptions {
|
90 | useContext?: boolean;
|
91 | params?: MethodOptionsParamsLike;
|
92 | version?: number;
|
93 | reviver?: JSONParseReviver;
|
94 | replacer?: JSONStringifyReplacer;
|
95 | encoding?: string;
|
96 | router?: ServerRouterFunction;
|
97 | methodConstructor?: Function;
|
98 | }
|
99 |
|
100 | declare 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 |
|
122 | interface MiddlewareServerOptions extends ServerOptions {
|
123 | }
|
124 |
|
125 | interface HttpServerOptions extends ServerOptions {
|
126 | }
|
127 |
|
128 | declare class HttpServer extends http.Server {
|
129 | constructor(server: Server, options?: HttpServerOptions);
|
130 | }
|
131 |
|
132 | interface HttpsServerOptions extends ServerOptions, https.ServerOptions {
|
133 | }
|
134 |
|
135 | declare class HttpsServer extends https.Server {
|
136 | constructor(server: Server, options?: HttpsServerOptions);
|
137 | }
|
138 |
|
139 | interface TcpServerOptions extends ServerOptions {
|
140 | }
|
141 |
|
142 | declare class TcpServer extends net.Server {
|
143 | constructor(server: Server, options?: TcpServerOptions);
|
144 | }
|
145 |
|
146 | interface TlsServerOptions extends tls.TlsOptions {
|
147 | }
|
148 |
|
149 | declare class TlsServer extends tls.Server {
|
150 | constructor(server: Server, options?: TlsServerOptions);
|
151 | }
|
152 |
|
153 | declare type JSONParseReviver = (key: string, value: any) => any;
|
154 | declare type JSONStringifyReplacer = (key: string, value: any) => any;
|
155 |
|
156 | declare type IDGenerator = () => string;
|
157 |
|
158 | interface ClientOptions {
|
159 | version?: number;
|
160 | reviver?: JSONParseReviver;
|
161 | replacer?: JSONStringifyReplacer;
|
162 | generator?: IDGenerator;
|
163 | }
|
164 |
|
165 | interface HttpClientOptions extends ClientOptions, http.RequestOptions {
|
166 | }
|
167 |
|
168 | declare class HttpClient extends Client {
|
169 | constructor(options?: HttpClientOptions);
|
170 | }
|
171 |
|
172 | interface TlsClientOptions extends ClientOptions, tls.ConnectionOptions {
|
173 | }
|
174 |
|
175 | declare class TlsClient extends Client {
|
176 | constructor(options?: TlsClientOptions);
|
177 | }
|
178 |
|
179 | interface TcpClientOptions extends ClientOptions, net.TcpSocketConnectOpts {
|
180 | }
|
181 |
|
182 | declare class TcpClient extends Client {
|
183 | constructor(options?: TcpClientOptions);
|
184 | }
|
185 |
|
186 | interface HttpsClientOptions extends ClientOptions, https.RequestOptions {
|
187 | }
|
188 |
|
189 | declare class HttpsClient extends Client {
|
190 | constructor(options?: HttpsClientOptions);
|
191 | }
|
192 |
|
193 | type ClientRequestShouldCall = JSONRPCCallbackType | false;
|
194 |
|
195 | declare 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 | }
|