Skip to content

Commit fcbc6b0

Browse files
committed
docs(nestjs-json-rpc,nestjs-json-rpc-sdk): Add readme
1 parent bd08c75 commit fcbc6b0

File tree

6 files changed

+92
-26
lines changed

6 files changed

+92
-26
lines changed

libs/json-rpc/nestjs-json-rpc-sdk/README.md

+42-21
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ npm install @klerick/nestjs-json-rpc-sdk
2020
## Example
2121

2222
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:
2424

2525
```typescript
2626
export type InputType = {
@@ -34,9 +34,9 @@ export type OutputType = {
3434
};
3535

3636
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>;
4040
}
4141
```
4242

@@ -53,10 +53,10 @@ const { rpc, rpcBatch } = RpcFactory(
5353
false
5454
);
5555

56-
rpc.RpcService.someMethode(1).sibcribe(r => console.log(r))
56+
rpc.RpcService.someMethod(1).sibcribe(r => console.log(r))
5757

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({
6060
a: 1,
6161
b: 2,
6262
});
@@ -66,7 +66,7 @@ rpcBatch(call1, call2).sibcribe(([result1, result2]) => console.log(result1, res
6666
```
6767
That's all:)
6868

69-
You can use typescript type checking:
69+
You can use typescript for type checking:
7070
```typescript
7171
import {
7272
RpcFactory,
@@ -87,7 +87,11 @@ const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
8787
false
8888
);
8989
//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);
9195

9296
```
9397

@@ -111,6 +115,22 @@ const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
111115
false
112116
);
113117
```
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+
114134

115135
if you want to use **Promise** instead of **Observer**
116136

@@ -129,12 +149,12 @@ const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>(
129149
transport: TransportType.HTTP,
130150
httpAgentFactory: axiosTransportFactory(axios),
131151
},
132-
false
152+
true // need true for use promise as result
133153
);
134-
const result = await rpcForBatch.RpcService.someMethode(1)
154+
const result = await rpcForBatch.RpcService.someMethod(1)
135155

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({
138158
a: 1,
139159
b: 2,
140160
});
@@ -144,20 +164,21 @@ const [result1, result2] = await rpcBatch(call1, call2);
144164

145165
For use **WebSocket**
146166
```typescript
147-
import axios from 'axios';
148167
import {
149168
RpcFactory,
150169
} from '@klerick/nestjs-json-rpc-sdk';
151-
import { WebSocket } from 'ws';
170+
import { WebSocket as ws } from 'ws';
152171
import { webSocket } from 'rxjs/webSocket';
172+
153173
const someUrl = 'ws://localhost:4200/rpc'
154174
const destroySubject = new Subject<boolean>();
155175
const nativeSocketInstance = webSocket<any>(destroySubject);
156-
const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>(
176+
177+
const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
157178
{
158179
transport: TransportType.WS,
159180
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
161182
rpcHost: `http://localhost:4200`,
162183
rpcPath: `/rpc`,
163184
destroySubject, // - If you need close connection you need call destroySubject.next(true),
@@ -168,7 +189,6 @@ const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>(
168189
```
169190
You can use **socket.io**
170191
```typescript
171-
import axios from 'axios';
172192
import {
173193
RpcFactory,
174194
} from '@klerick/nestjs-json-rpc-sdk';
@@ -178,18 +198,18 @@ import { io } from 'socket.io-client';
178198
const someUrl = 'ws://localhost:4200'
179199
const destroySubject = new Subject<boolean>();
180200
const ioSocketInstance = io(someUrl, { path: '/rpc' })
181-
const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>(
201+
const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
182202
{
183203
transport: TransportType.WS,
184-
useWsNativeSocket: false, // - Will be use native WebSocket
204+
useWsNativeSocket: false, // - Will be use socket.io
185205
destroySubject, // - If you need close connection you need call destroySubject.next(true),
186206
ioSocketInstance
187207
},
188208
false
189209
);
190210
```
191211

192-
You can use module for Angular:
212+
You can use Angular module:
193213

194214
```typescript
195215

@@ -199,6 +219,7 @@ import {
199219
TransportType,
200220
} from '@klerick/nestjs-json-rpc-sdk/json-rpc-sdk.module'
201221
import { Subject } from 'rxjs';
222+
import { io } from 'socket.io-client';
202223
import {
203224
JSON_RPC,
204225
RPC_BATCH,

libs/json-rpc/nestjs-json-rpc-sdk/src/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@ export {
44
ResultRpcFactoryPromise,
55
ResultRpcFactory,
66
} from './lib/factory';
7-
export { RpcConfig, TransportType, ErrorCodeType, RpcError } from './lib/types';
7+
export {
8+
RpcConfig,
9+
TransportType,
10+
ErrorCodeType,
11+
RpcError,
12+
LoopFunc,
13+
HttpAgentFactory,
14+
Transport,
15+
PayloadRpc,
16+
RpcResult,
17+
} from './lib/types';

libs/json-rpc/nestjs-json-rpc-sdk/src/lib/types/rpc.ts

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export type PayloadRpc<T extends LoopFunc> = {
1313
id: number;
1414
};
1515

16-
export type IdRequest = () => number;
17-
1816
export type RpcResultObject<T extends LoopFunc> = {
1917
jsonrpc: JsonRpcVersion;
2018
result: ReturnGenericType<T>;

libs/json-rpc/nestjs-json-rpc/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ export class AppModule {
5959
```
6060
`wsConfig` - is GatewayMetadata from `@nestjs/websockets/interfaces`;
6161

62+
***!!!!***: - NestJs by default using **socket.io** adapter, if you want to use native WebSocket, you should use **WsAdapter**
63+
```typescript
64+
import { WsAdapter } from '@nestjs/platform-ws';
65+
import { NestFactory } from '@nestjs/core';
66+
import { AppModule } from './app/app.module';
67+
68+
async function bootstrap() {
69+
const app = await NestFactory.create(AppModule);
70+
app.useWebSocketAdapter(new WsAdapter(app));
71+
app.init()
72+
await app.listen(3000);
73+
}
74+
```
75+
6276
To allow service to your RPC server, you should create class and add to providers the root **AppModule**.
6377

6478
```typescript
@@ -103,6 +117,7 @@ export class AppModule {
103117
}
104118
```
105119
`@RpcHandler` - decorator which mark class as RPC service
120+
106121
`@RpcParamsPipe` - decorator for validate input data,
107122

108123

@@ -112,7 +127,7 @@ After it, you can call you RPC service:
112127
POST /rpc
113128
```
114129

115-
- **body** - Create new User and add link to address
130+
- **body** - for http request
116131

117132
```json
118133
{"jsonrpc": "2.0", "method": "RpcService.methodeWithObjectParams", "params": {"a": 23}, "id": 1}

libs/json-rpc/nestjs-json-rpc/project.json

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@
2727
"codeCoverage": true,
2828
"coverageReporters": ["json-summary"]
2929
}
30+
},
31+
"upload-badge": {
32+
"executor": "nx:run-commands",
33+
"dependsOn": [
34+
{
35+
"target": "test"
36+
}
37+
],
38+
"options": {
39+
"commands": ["node tools/scripts/upload-badge.mjs nestjs-json-rpc"],
40+
"cwd": "./",
41+
"parallel": false,
42+
"outputPath": "{workspaceRoot}/libs/json-rpc/nestjs-json-rpc"
43+
}
44+
},
45+
"nx-release-publish": {
46+
"options": {
47+
"packageRoot": "dist/libs/json-rpc/nestjs-json-rpc"
48+
}
3049
}
3150
},
3251
"tags": []

libs/json-rpc/nestjs-json-rpc/src/lib/modules/ws-socket-transport/filter/rpc-ws-error-exception.filter.spec.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { WS_EVENT_NAME } from '../constants';
1010

1111
describe('rpc-ws-error-exception.filter', () => {
1212
describe('WebSocket', () => {
13-
const WebSocketInst = new WebSocket('http://0.0.0.0', {});
13+
const WebSocketInst = new WebSocket(
14+
'wss://demo.piesocket.com/v3/channel_123',
15+
{}
16+
);
1417
let argumentsHost: ArgumentsHost;
1518
let getClient: () => WebSocket;
1619

0 commit comments

Comments
 (0)