UNPKG

8.7 kBTypeScriptView Raw
1import net = require('net');
2import tls = require('tls');
3import https = require('https');
4import http = require('http');
5import events = require('events');
6import Stream = require('stream');
7import * as connect from 'connect';
8
9export interface UtilsJSONParseOptions {
10 reviver?: Function;
11}
12
13export interface UtilsJSONStringifyOptions {
14 replacer?: Function;
15}
16
17export declare class Utils {
18
19 static response(error: JSONRPCError | undefined | null, result: JSONRPCResultLike | undefined | null, id: JSONRPCIDLike | null | undefined, version?: number): JSONRPCVersionTwoRequest;
20 static response(error: JSONRPCError | undefined | null, result: JSONRPCResultLike | undefined | null, id: JSONRPCIDLike | null | undefined, version:2): JSONRPCVersionTwoRequest;
21 static response(error: JSONRPCError | undefined | null, result: JSONRPCResultLike | undefined | null, id: JSONRPCIDLike | null | undefined, version:1): JSONRPCVersionOneRequest;
22
23 static generateId(): string;
24
25 static merge(...objs: object[]): object;
26
27 static parseStream(stream:Stream, options:UtilsJSONParseOptions, onRequest: (err?:Error, data?:any) => void): void;
28
29 static parseBody(stream:Stream, options:UtilsJSONParseOptions, callback: (err?:Error, obj?:any) => void): void;
30
31 static getHttpListener(self:http.Server, server:Server): Function;
32
33 static isContentType(request:http.IncomingMessage, typ:string): boolean;
34
35 static isMethod(request:http.IncomingMessage, method:string): boolean;
36
37 static walk(obj:object, key:string, fn: (key:string, value:any) => any): object;
38
39 static JSON: UtilsJSON;
40
41 static Request: UtilsRequest;
42
43 static Response: UtilsResponse;
44
45}
46
47type UtilsJSON = {
48 parse(str:string, options:UtilsJSONParseOptions | null | undefined, callback: (err?:Error, obj?:object) => void):void;
49 stringify(obj:object, options:UtilsJSONStringifyOptions | null | undefined, callback: (err?:Error, str?:string) => void):void;
50}
51
52type UtilsRequest = {
53 isBatch(request:any): boolean;
54 isNotification(request:any): boolean;
55 isValidVersionTwoRequest(request:any): boolean;
56 isValidVersionOneRequest(request:any): boolean;
57 isValidRequest(request:any, version?:number): boolean;
58}
59
60type UtilsResponse = {
61 isValidError(error:any, version?:number): boolean;
62 isValidResponse(response:any, version?:number): boolean;
63}
64
65export type RequestParamsLike = Array<any> | object;
66
67export interface JSONRPCError {
68 code: number;
69 message: string;
70 data?: object;
71}
72
73export type JSONRPCErrorLike = Error | JSONRPCError;
74
75export interface JSONRPCVersionOneRequest {
76 method: string;
77 params: Array<any>;
78 id: JSONRPCIDLike;
79}
80
81export interface JSONRPCVersionTwoRequest {
82 jsonrpc: number;
83 method: string;
84 params: RequestParamsLike;
85 id?: JSONRPCIDLike | null;
86}
87
88export type JSONRPCIDLike = number | string;
89
90export type JSONRPCRequest = JSONRPCVersionOneRequest | JSONRPCVersionTwoRequest;
91
92export type JSONRPCRequestLike = JSONRPCRequest | string;
93
94export type JSONRPCResultLike = any;
95
96export interface JSONRPCCallbackTypePlain {
97 (err?: JSONRPCErrorLike | null, result?: JSONRPCResultLike): void
98}
99
100export interface JSONRPCCallbackTypeSugared {
101 (err?: Error | null, error?: JSONRPCErrorLike, result?: JSONRPCResultLike): void
102}
103
104type JSONRPCCallbackType = JSONRPCCallbackTypePlain | JSONRPCCallbackTypeSugared;
105
106export interface JSONRPCCallbackTypeBatchPlain {
107 (err: JSONRPCErrorLike, results?: Array<JSONRPCResultLike>): void
108}
109
110export interface JSONRPCCallbackTypeBatchSugared {
111 (err: Error, errors?: Array<JSONRPCErrorLike>, results?: Array<JSONRPCResultLike>): void
112}
113
114type JSONRPCCallbackTypeBatch = JSONRPCCallbackTypeBatchPlain | JSONRPCCallbackTypeBatchSugared;
115
116export interface MethodHandler {
117 (this:Server, args:RequestParamsLike, callback:JSONRPCCallbackTypePlain): void;
118}
119
120export interface MethodHandlerContext {
121 (this:Server, args:RequestParamsLike, context:object, callback:JSONRPCCallbackTypePlain): void;
122}
123
124export type MethodHandlerType = MethodHandlerContext | MethodHandler;
125export type MethodOptionsParamsLike = Array<any> | Object | object;
126
127export interface MethodOptions {
128 handler?: MethodHandlerType;
129 useContext?: boolean;
130 params?: MethodOptionsParamsLike;
131}
132
133export declare class Method {
134 constructor(options: MethodOptions);
135 constructor(handler?: MethodHandlerType, options?: MethodOptions);
136
137 getHandler(): MethodHandlerType;
138 setHandler(handler: MethodHandlerType): void;
139 execute(server: Server, requestParams: RequestParamsLike, callback: JSONRPCCallbackType): any | Promise<any>;
140}
141
142export type MethodLike = Function | Method | Client
143
144export type ServerRouterFunction = (this: Server, method: string, params: RequestParamsLike) => MethodLike;
145
146export interface ServerOptions {
147 useContext?: boolean;
148 params?: MethodOptionsParamsLike;
149 version?: number;
150 reviver?: JSONParseReviver;
151 replacer?: JSONStringifyReplacer;
152 encoding?: string;
153 router?: ServerRouterFunction;
154 methodConstructor?: Function;
155}
156
157export interface MethodMap { [methodName:string]: Method }
158
159export declare class Server extends events.EventEmitter {
160 constructor(methods?: {[methodName: string]: MethodLike}, options?: ServerOptions);
161
162 static errors: {[errorName: string]: number};
163 static errorMessages: {[errorMessage: string]: string};
164 static interfaces: {[interfaces: string]: Function};
165
166 public _methods: MethodMap;
167 public options: ServerOptions;
168 public errorMessages: {[errorMessage: string]: string};
169
170 http(options?: HttpServerOptions): HttpServer;
171 https(options?: HttpsServerOptions): HttpsServer;
172 tcp(options?: TcpServerOptions): TcpServer;
173 tls(options?: TlsServerOptions): TlsServer;
174 middleware(options?: MiddlewareServerOptions): connect.HandleFunction;
175
176 method(name: string, definition: MethodLike): void;
177 methods(methods: {[methodName: string]: MethodLike}): void;
178 hasMethod(name: string): boolean;
179 removeMethod(name: string): void;
180 getMethod(name: string): MethodLike;
181 error(code?: number, message?: string, data?: object): JSONRPCError;
182 call(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, originalCallback?: JSONRPCCallbackType): void;
183}
184
185export interface MiddlewareServerOptions extends ServerOptions {
186 end?: boolean;
187}
188
189export interface HttpServerOptions extends ServerOptions {
190}
191
192declare class HttpServer extends http.Server {
193 constructor(server: Server, options?: HttpServerOptions);
194}
195
196export interface HttpsServerOptions extends ServerOptions, https.ServerOptions {
197}
198
199declare class HttpsServer extends https.Server {
200 constructor(server: Server, options?: HttpsServerOptions);
201}
202
203export interface TcpServerOptions extends ServerOptions {
204}
205
206declare class TcpServer extends net.Server {
207 constructor(server: Server, options?: TcpServerOptions);
208}
209
210export interface TlsServerOptions extends tls.TlsOptions {
211}
212
213declare class TlsServer extends tls.Server {
214 constructor(server: Server, options?: TlsServerOptions);
215}
216
217type JSONParseReviver = (key: string, value: any) => any;
218type JSONStringifyReplacer = (key: string, value: any) => any;
219
220type IDGenerator = () => string;
221
222export interface ClientOptions {
223 version?: number;
224 reviver?: JSONParseReviver;
225 replacer?: JSONStringifyReplacer;
226 generator?: IDGenerator;
227}
228
229export interface HttpClientOptions extends ClientOptions, http.RequestOptions {
230}
231
232declare class HttpClient extends Client {
233 constructor(options?: HttpClientOptions);
234}
235
236export interface TlsClientOptions extends ClientOptions, tls.ConnectionOptions {
237}
238
239declare class TlsClient extends Client {
240 constructor(options?: TlsClientOptions);
241}
242
243export interface TcpClientOptions extends ClientOptions, net.TcpSocketConnectOpts {
244}
245
246declare class TcpClient extends Client {
247 constructor(options?: TcpClientOptions);
248}
249
250export interface HttpsClientOptions extends ClientOptions, https.RequestOptions {
251}
252
253declare class HttpsClient extends Client {
254 constructor(options?: HttpsClientOptions);
255}
256
257type ClientRequestShouldCall = JSONRPCCallbackType | false;
258
259export declare class Client extends events.EventEmitter {
260 constructor(server: Server, options?: ClientOptions);
261 constructor(options: ClientOptions);
262
263 static http(options?: HttpClientOptions): HttpClient;
264 static https(options?: HttpsClientOptions): HttpsClient;
265 static tcp(options?: TcpClientOptions): TcpClient;
266 static tls(options?: TlsClientOptions): TlsClient;
267
268 request(method: string, params: RequestParamsLike, id?: string | null, callback?: JSONRPCCallbackType): JSONRPCRequest;
269 request(method: string, params: RequestParamsLike, callback?: JSONRPCCallbackType): JSONRPCRequest;
270 request(method: Array<JSONRPCRequestLike>, callback: JSONRPCCallbackTypeBatch): Array<JSONRPCRequest>;
271}