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