@@ -20,7 +20,7 @@ npm install @klerick/nestjs-json-rpc-sdk
20
20
## Example
21
21
22
22
Once the installation process is complete, we can import the ** RpcFactory** .
23
- For example, we have RPC serve which have service which implement this interface:
23
+ For example, we have RPC server which have service which implement this interface:
24
24
25
25
``` typescript
26
26
export type InputType = {
@@ -34,9 +34,9 @@ export type OutputType = {
34
34
};
35
35
36
36
export interface RpcService {
37
- someMethode (firstArg : number ): Promise <number >;
38
- someOtherMethode (firstArg : number , secondArgument : number ): Promise <string >;
39
- methodeWithObjectParams (a : InputType ): Promise <OutputType >;
37
+ someMethod (firstArg : number ): Promise <number >;
38
+ someOtherMethod (firstArg : number , secondArgument : number ): Promise <string >;
39
+ methodWithObjectParams (a : InputType ): Promise <OutputType >;
40
40
}
41
41
```
42
42
@@ -53,10 +53,10 @@ const { rpc, rpcBatch } = RpcFactory(
53
53
false
54
54
);
55
55
56
- rpc .RpcService .someMethode (1 ).sibcribe (r => console .log (r ))
56
+ rpc .RpcService .someMethod (1 ).sibcribe (r => console .log (r ))
57
57
58
- const call1 = rpcForBatch .RpcService .someMethode (1 );
59
- const call2 = rpcForBatch .RpcService .methodeWithObjectParams ({
58
+ const call1 = rpcForBatch .RpcService .someMethod (1 );
59
+ const call2 = rpcForBatch .RpcService .methodWithObjectParams ({
60
60
a: 1 ,
61
61
b: 2 ,
62
62
});
@@ -66,7 +66,7 @@ rpcBatch(call1, call2).sibcribe(([result1, result2]) => console.log(result1, res
66
66
```
67
67
That's all:)
68
68
69
- You can use typescript type checking:
69
+ You can use typescript for type checking:
70
70
``` typescript
71
71
import {
72
72
RpcFactory ,
@@ -87,7 +87,11 @@ const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
87
87
false
88
88
);
89
89
// TS2345: Argument of type string is not assignable to parameter of type number
90
- const call = rpcForBatch .RpcService .someMethode (' inccorectParam' );
90
+ const call = rpcForBatch .RpcService .someMethod (' inccorectParam' );
91
+ // TS2339: Property IncorrectService does not exist on type MapperRpc
92
+ const call2 = rpcForBatch .IncorrectService .someMethod (1 );
93
+ // TS2339: Property incorrectMethod does not exist on type RpcService
94
+ const call3 = rpcForBatch .RpcService .incorrectMethod (1 );
91
95
92
96
```
93
97
@@ -111,6 +115,22 @@ const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
111
115
false
112
116
);
113
117
```
118
+ Or you can implement your personal factory.
119
+
120
+ You should implement ** HttpAgentFactory** type
121
+
122
+ ``` typescript
123
+
124
+ type Transport <T extends LoopFunc > = (
125
+ body : PayloadRpc <T >
126
+ ) => Observable <RpcResult <T >>;
127
+
128
+ type HttpAgentFactory <T extends LoopFunc > = (
129
+ url : string
130
+ ) => Transport <T >;
131
+ ```
132
+
133
+
114
134
115
135
if you want to use ** Promise** instead of ** Observer**
116
136
@@ -129,12 +149,12 @@ const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>(
129
149
transport: TransportType .HTTP ,
130
150
httpAgentFactory: axiosTransportFactory (axios ),
131
151
},
132
- false
152
+ true // need true for use promise as result
133
153
);
134
- const result = await rpcForBatch .RpcService .someMethode (1 )
154
+ const result = await rpcForBatch .RpcService .someMethod (1 )
135
155
136
- const call1 = rpcForBatch .RpcService .someMethode (1 );
137
- const call2 = rpcForBatch .RpcService .methodeWithObjectParams ({
156
+ const call1 = rpcForBatch .RpcService .someMethod (1 );
157
+ const call2 = rpcForBatch .RpcService .methodWithObjectParams ({
138
158
a: 1 ,
139
159
b: 2 ,
140
160
});
@@ -144,20 +164,21 @@ const [result1, result2] = await rpcBatch(call1, call2);
144
164
145
165
For use ** WebSocket**
146
166
``` typescript
147
- import axios from ' axios' ;
148
167
import {
149
168
RpcFactory ,
150
169
} from ' @klerick/nestjs-json-rpc-sdk' ;
151
- import { WebSocket } from ' ws' ;
170
+ import { WebSocket as ws } from ' ws' ;
152
171
import { webSocket } from ' rxjs/webSocket' ;
172
+
153
173
const someUrl = ' ws://localhost:4200/rpc'
154
174
const destroySubject = new Subject <boolean >();
155
175
const nativeSocketInstance = webSocket <any >(destroySubject );
156
- const { rpc, rpcBatch, rpcForBatch } = RpcFactory <MapperRpc >(
176
+
177
+ const { rpc, rpcBatch } = RpcFactory <MapperRpc >(
157
178
{
158
179
transport: TransportType .WS ,
159
180
useWsNativeSocket: true , // - Will be use native WebSocket
160
- // nativeSocketImplementation: WebSocket , - if you use NodeJS you can use other implementation
181
+ // nativeSocketImplementation: ws , - if you use NodeJS you can use other implementation
161
182
rpcHost: ` http://localhost:4200 ` ,
162
183
rpcPath: ` /rpc ` ,
163
184
destroySubject , // - If you need close connection you need call destroySubject.next(true),
@@ -168,7 +189,6 @@ const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>(
168
189
```
169
190
You can use ** socket.io**
170
191
``` typescript
171
- import axios from ' axios' ;
172
192
import {
173
193
RpcFactory ,
174
194
} from ' @klerick/nestjs-json-rpc-sdk' ;
@@ -178,18 +198,18 @@ import { io } from 'socket.io-client';
178
198
const someUrl = ' ws://localhost:4200'
179
199
const destroySubject = new Subject <boolean >();
180
200
const ioSocketInstance = io (someUrl , { path: ' /rpc' })
181
- const { rpc, rpcBatch, rpcForBatch } = RpcFactory <MapperRpc >(
201
+ const { rpc, rpcBatch } = RpcFactory <MapperRpc >(
182
202
{
183
203
transport: TransportType .WS ,
184
- useWsNativeSocket: false , // - Will be use native WebSocket
204
+ useWsNativeSocket: false , // - Will be use socket.io
185
205
destroySubject , // - If you need close connection you need call destroySubject.next(true),
186
206
ioSocketInstance
187
207
},
188
208
false
189
209
);
190
210
```
191
211
192
- You can use module for Angular:
212
+ You can use Angular module :
193
213
194
214
``` typescript
195
215
@@ -199,6 +219,7 @@ import {
199
219
TransportType ,
200
220
} from ' @klerick/nestjs-json-rpc-sdk/json-rpc-sdk.module'
201
221
import { Subject } from ' rxjs' ;
222
+ import { io } from ' socket.io-client' ;
202
223
import {
203
224
JSON_RPC ,
204
225
RPC_BATCH ,
0 commit comments