-
Notifications
You must be signed in to change notification settings - Fork 7
/
info.ts
99 lines (93 loc) · 2.12 KB
/
info.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
/** Service info method **/
export class ServiceMethod {
/** @ignore */
name: string;
/** @ignore */
description: string;
/** @ignore */
requiredParams: Array<string>;
/** @ignore */
optionalParams: Array<string>;
/**
* @param {string} name - method name
* @param {string} [description] - method description
*/
constructor(name: string, description?: string) {
this.name = name;
this.description = description || "";
this.requiredParams = [];
this.optionalParams = [];
}
/**
* Set a required parameter (chained)
*
* @param {string} name - param name
*
* @returns {ServiceMethod}
*/
required(name: string): ServiceMethod {
this.requiredParams.push(name);
return this;
}
/**
* Set an optional parameter (chained)
*
* @param {string} name - param name
*
* @returns {ServiceMethod}
*/
optional(name: string): ServiceMethod {
this.optionalParams.push(name);
return this;
}
}
/** Service info **/
export class ServiceInfo {
author: string;
/** @ignore */
description: string;
/** @ignore */
version: string;
/** @ignore */
methods: object;
/**
* @param {string} params.author - service author
* @param {string} params.description - service description (long name)
* @param {version} params.version - service version
*/
constructor({
author,
description,
version
}: {
author: string;
description?: string;
version: string;
}) {
this.author = author || "";
this.description = description || "";
this.version = version || "";
this.methods = {};
}
/**
* Add a RPC method (chained)
*
* @param {ServiceMethod} method - method to add
*
* @returns {ServiceInfo}
*/
addMethod(method: ServiceMethod): ServiceInfo {
const params = {};
for (const p of method.requiredParams) {
(params as any)[p] = { required: true };
}
for (const p of method.optionalParams) {
(params as any)[p] = { required: false };
}
(this.methods as any)[method.name] = {
description: method.description,
params: params
};
return this;
}
}